-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add @container
query support to the createTextStyle
function
#147
base: master
Are you sure you want to change the base?
Changes from all commits
b2f6ce5
e1d791d
5444d21
bf8953e
423fabe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -9,34 +9,59 @@ import { precomputeValues } from '@capsizecss/core'; | |||||||||||||||||||||||||||||||||||||||||||||||||
import { ComputedValues, CreateStyleObjectParameters } from './types'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
import { capsizeStyle, capsizeVars } from './capsize.css'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
interface MediaQueries { | ||||||||||||||||||||||||||||||||||||||||||||||||||
'@media': Record<string, CreateStyleObjectParameters>; | ||||||||||||||||||||||||||||||||||||||||||||||||||
interface ConditionalQueries { | ||||||||||||||||||||||||||||||||||||||||||||||||||
'@media'?: Record<string, CreateStyleObjectParameters>; | ||||||||||||||||||||||||||||||||||||||||||||||||||
'@container'?: Record<string, CreateStyleObjectParameters>; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const generateQueryVars = ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
queryType, | ||||||||||||||||||||||||||||||||||||||||||||||||||
queries, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}: { | ||||||||||||||||||||||||||||||||||||||||||||||||||
queryType: '@media' | '@container'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thoughts on extracting this from the type?
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
queries: Record<string, any>; | ||||||||||||||||||||||||||||||||||||||||||||||||||
}) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const queryVars: StyleRule[typeof queryType] = {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Object.entries(queries).forEach(([query, value]) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const builtValues = | ||||||||||||||||||||||||||||||||||||||||||||||||||
'capHeightTrim' in value | ||||||||||||||||||||||||||||||||||||||||||||||||||
? (value as ComputedValues) | ||||||||||||||||||||||||||||||||||||||||||||||||||
: precomputeValues(value); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
queryVars[query] = { vars: assignVars(capsizeVars, builtValues) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return queryVars; | ||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+24
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can i suggest renaming
Suggested change
That would go for the function too, (I noticed this is likely extending something that was my fault, in |
||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const createVanillaStyle = ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
values, | ||||||||||||||||||||||||||||||||||||||||||||||||||
mediaQueries, | ||||||||||||||||||||||||||||||||||||||||||||||||||
conditionalQueries, | ||||||||||||||||||||||||||||||||||||||||||||||||||
debugId, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}: { | ||||||||||||||||||||||||||||||||||||||||||||||||||
values: ComputedValues; | ||||||||||||||||||||||||||||||||||||||||||||||||||
mediaQueries?: MediaQueries; | ||||||||||||||||||||||||||||||||||||||||||||||||||
conditionalQueries?: ConditionalQueries; | ||||||||||||||||||||||||||||||||||||||||||||||||||
debugId?: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||
}) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const vars: StyleRule = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
vars: assignVars(capsizeVars, values), | ||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
if (typeof mediaQueries !== 'undefined') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const mqs: StyleRule['@media'] = {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||
Object.entries(mediaQueries['@media']).forEach(([mq, val]) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const builtValues = | ||||||||||||||||||||||||||||||||||||||||||||||||||
'capHeightTrim' in val | ||||||||||||||||||||||||||||||||||||||||||||||||||
? (val as ComputedValues) | ||||||||||||||||||||||||||||||||||||||||||||||||||
: precomputeValues(val); | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (typeof conditionalQueries !== 'undefined') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (conditionalQueries['@media']) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
vars['@media'] = generateQueryVars({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
queryType: '@media', | ||||||||||||||||||||||||||||||||||||||||||||||||||
queries: conditionalQueries['@media'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
mqs[mq] = { vars: assignVars(capsizeVars, builtValues) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
vars['@media'] = mqs; | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (conditionalQueries['@container']) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
vars['@container'] = generateQueryVars({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
queryType: '@container', | ||||||||||||||||||||||||||||||||||||||||||||||||||
queries: conditionalQueries['@container'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return composeStyles(capsizeStyle, style(vars, debugId)); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -48,26 +73,27 @@ function createTextStyle( | |||||||||||||||||||||||||||||||||||||||||||||||||
): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||
function createTextStyle( | ||||||||||||||||||||||||||||||||||||||||||||||||||
args: CreateStyleObjectParameters, | ||||||||||||||||||||||||||||||||||||||||||||||||||
mediaQueries?: MediaQueries, | ||||||||||||||||||||||||||||||||||||||||||||||||||
conditionalQueries?: ConditionalQueries, | ||||||||||||||||||||||||||||||||||||||||||||||||||
debugId?: string, | ||||||||||||||||||||||||||||||||||||||||||||||||||
): string; | ||||||||||||||||||||||||||||||||||||||||||||||||||
function createTextStyle(...args: any[]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const hasMediaQueries = | ||||||||||||||||||||||||||||||||||||||||||||||||||
const hasConditionalQueries = | ||||||||||||||||||||||||||||||||||||||||||||||||||
typeof args[1] !== 'undefined' && typeof args[1] !== 'string'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
const debugId = hasMediaQueries ? args[2] : args[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
const mediaQueries = hasMediaQueries ? args[1] : undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const debugId = hasConditionalQueries ? args[2] : args[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
const conditionalQueries = hasConditionalQueries ? args[1] : undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
if ('capHeightTrim' in args[0]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return createVanillaStyle({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
values: args[0], | ||||||||||||||||||||||||||||||||||||||||||||||||||
mediaQueries, | ||||||||||||||||||||||||||||||||||||||||||||||||||
conditionalQueries, | ||||||||||||||||||||||||||||||||||||||||||||||||||
debugId, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return createVanillaStyle({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
values: precomputeValues(args[0]), | ||||||||||||||||||||||||||||||||||||||||||||||||||
mediaQueries, | ||||||||||||||||||||||||||||||||||||||||||||||||||
conditionalQueries, | ||||||||||||||||||||||||||||||||||||||||||||||||||
debugId, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,4 +1,8 @@ | ||||||
import { style, createGlobalTheme } from '@vanilla-extract/css'; | ||||||
import { | ||||||
style, | ||||||
createGlobalTheme, | ||||||
createContainer, | ||||||
} from '@vanilla-extract/css'; | ||||||
import { createTextStyle, precomputeValues } from '../src'; | ||||||
|
||||||
const fontMetrics = { | ||||||
|
@@ -58,6 +62,25 @@ export const responsiveText = createTextStyle( | |||||
'responsiveText', | ||||||
); | ||||||
|
||||||
const containerName = createContainer(); | ||||||
|
||||||
export const container = style({ | ||||||
containerName, | ||||||
containerType: 'inline-size', | ||||||
outline: 'solid 1px blue', | ||||||
}); | ||||||
|
||||||
export const containerText = createTextStyle( | ||||||
textValues.mobile, | ||||||
{ | ||||||
'@container': { | ||||||
[`${containerName} (min-width: 400px)`]: textValues.tablet, | ||||||
[`${containerName} (min-width: 600px)`]: textValues.desktop, | ||||||
}, | ||||||
}, | ||||||
'responsiveText', | ||||||
); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's align the
Suggested change
|
||||||
|
||||||
export const responsiveThemedText = createTextStyle( | ||||||
vars.typography.heading.mobile, | ||||||
{ | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,7 +15,7 @@ export const ThemedText = ({ text, background }: Props) => | |||||
background ? ` style="background: ${background}"` : '' | ||||||
}>${text}</div>`; | ||||||
|
||||||
export const ResponsiveText = ({ text, background }: Props) => | ||||||
export const MediaResponsiveText = ({ text, background }: Props) => | ||||||
`<div class="${styles.fontFamily} ${styles.responsiveText}"${ | ||||||
background ? ` style="background: ${background}"` : '' | ||||||
}>${text}</div>`; | ||||||
|
@@ -24,3 +24,17 @@ export const ResponsiveThemedText = ({ text, background }: Props) => | |||||
`<div class="${styles.fontFamily} ${styles.responsiveThemedText}"${ | ||||||
background ? ` style="background: ${background}"` : '' | ||||||
}>${text}</div>`; | ||||||
|
||||||
export const ContainerResponsiveText = ({ text, background }: Props) => | ||||||
[350, 550, 700] | ||||||
.map( | ||||||
(width) => ` | ||||||
<div style="width: ${width}px;" class="${styles.container}"> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These should probably use a
Suggested change
At the moment they seem to just grow and never conditionally adapt. |
||||||
<div class="${styles.fontFamily} ${styles.containerText}"${ | ||||||
background ? ` style="background: ${background}"` : '' | ||||||
}> | ||||||
${text} | ||||||
</div> | ||||||
</div>`, | ||||||
) | ||||||
.join('\n'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small typo, but you're probably under selling it here too, i'd add an example!
Would you mind re-wording the intro to this section too?