-
Notifications
You must be signed in to change notification settings - Fork 318
Add support for @starting-style
rules
#1566
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@vanilla-extract/css': minor | ||
--- | ||
|
||
Add support for `@starting-style` rules | ||
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -60,3 +60,11 @@ export const styleVariantsCompositionInSelector = styleVariants({ | |||
globalStyle(`body ${styleVariantsCompositionInSelector.variant}`, { | ||||
fontSize: '24px', | ||||
}); | ||||
|
||||
// Style with starting-style | ||||
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.
Suggested change
|
||||
export const styleWithStartingStyle = style({ | ||||
backgroundColor: 'black', | ||||
'@starting-style': { | ||||
backgroundColor: 'white', | ||||
}, | ||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,6 +188,7 @@ class Stylesheet { | |
this.transformMedia(root, root.rule['@media']); | ||
this.transformSupports(root, root.rule['@supports']); | ||
this.transformContainer(root, root.rule['@container']); | ||
this.transformStartingStyle(root, root.rule['@starting-style']); | ||
|
||
this.transformSimplePseudos(root, root.rule); | ||
this.transformSelectors(root, root.rule); | ||
|
@@ -408,6 +409,11 @@ class Stylesheet { | |
selectorRule['@container'], | ||
conditions, | ||
); | ||
this.transformStartingStyle( | ||
root, | ||
selectorRule!['@starting-style'], | ||
conditions, | ||
); | ||
Comment on lines
+412
to
+416
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. This transformation inside |
||
}); | ||
} | ||
|
||
|
@@ -445,6 +451,11 @@ class Stylesheet { | |
this.transformLayer(root, mediaRule!['@layer'], conditions); | ||
this.transformSupports(root, mediaRule!['@supports'], conditions); | ||
this.transformContainer(root, mediaRule!['@container'], conditions); | ||
this.transformStartingStyle( | ||
root, | ||
mediaRule!['@starting-style'], | ||
conditions, | ||
); | ||
} | ||
} | ||
} | ||
|
@@ -481,6 +492,11 @@ class Stylesheet { | |
this.transformLayer(root, containerRule!['@layer'], conditions); | ||
this.transformSupports(root, containerRule!['@supports'], conditions); | ||
this.transformMedia(root, containerRule!['@media'], conditions); | ||
this.transformStartingStyle( | ||
root, | ||
containerRule!['@starting-style'], | ||
conditions, | ||
); | ||
}); | ||
} | ||
} | ||
|
@@ -516,6 +532,11 @@ class Stylesheet { | |
this.transformMedia(root, layerRule!['@media'], conditions); | ||
this.transformSupports(root, layerRule!['@supports'], conditions); | ||
this.transformContainer(root, layerRule!['@container'], conditions); | ||
this.transformStartingStyle( | ||
root, | ||
layerRule!['@starting-style'], | ||
conditions, | ||
); | ||
}); | ||
} | ||
} | ||
|
@@ -550,6 +571,11 @@ class Stylesheet { | |
this.transformLayer(root, supportsRule!['@layer'], conditions); | ||
this.transformMedia(root, supportsRule!['@media'], conditions); | ||
this.transformContainer(root, supportsRule!['@container'], conditions); | ||
this.transformStartingStyle( | ||
root, | ||
supportsRule!['@starting-style'], | ||
conditions, | ||
); | ||
Comment on lines
+574
to
+578
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. This transformation inside |
||
}); | ||
} | ||
} | ||
|
@@ -589,6 +615,42 @@ class Stylesheet { | |
} | ||
} | ||
|
||
transformStartingStyle( | ||
root: CSSStyleBlock | CSSSelectorBlock, | ||
rules: WithQueries<StyleWithSelectors>['@starting-style'], | ||
parentConditions: Array<string> = [], | ||
) { | ||
if (rules) { | ||
// Check if there are any nested at-rule keys inside this block. | ||
// The presence of any key starting with '@' indicates nested queries, | ||
// which are not allowed for @starting-style. | ||
Comment on lines
+624
to
+626
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. Just flagging that this is actually valid CSS. Actually, looking at the CSS output of your implementation, it generates nested CSS ( |
||
const nestedAtRuleKey = Object.keys(rules).find((key) => | ||
key.startsWith('@'), | ||
); | ||
if (nestedAtRuleKey) { | ||
throw new Error( | ||
`Nested at-rules (e.g. "${nestedAtRuleKey}") are not allowed inside @starting-style.`, | ||
); | ||
} | ||
|
||
const conditions = [...parentConditions, '@starting-style']; | ||
|
||
this.addConditionalRule( | ||
{ | ||
selector: root.selector, | ||
rule: omit(rules, specialKeys), | ||
}, | ||
conditions, | ||
); | ||
|
||
// Process any simple pseudos or selectors associated with this style. | ||
if (root.type === 'local') { | ||
this.transformSimplePseudos(root, rules, conditions); | ||
this.transformSelectors(root, rules, conditions); | ||
} | ||
Comment on lines
+647
to
+650
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. This logic should have a unit test too. |
||
} | ||
} | ||
|
||
toCss() { | ||
const css: Array<string> = []; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,12 +52,16 @@ export type MediaQueries<StyleType> = Query<'@media', StyleType>; | |
export type FeatureQueries<StyleType> = Query<'@supports', StyleType>; | ||
export type ContainerQueries<StyleType> = Query<'@container', StyleType>; | ||
export type Layers<StyleType> = Query<'@layer', StyleType>; | ||
export type StartingStyleQueries<StyleType> = { | ||
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 the 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. Sounds like a good suggestion! |
||
'@starting-style'?: Omit<StyleType, '@starting-style'>; | ||
}; | ||
|
||
interface AllQueries<StyleType> | ||
extends MediaQueries<StyleType & AllQueries<StyleType>>, | ||
FeatureQueries<StyleType & AllQueries<StyleType>>, | ||
ContainerQueries<StyleType & AllQueries<StyleType>>, | ||
Layers<StyleType & AllQueries<StyleType>> {} | ||
Layers<StyleType & AllQueries<StyleType>>, | ||
StartingStyleQueries<StyleType> {} | ||
|
||
export type WithQueries<StyleType> = StyleType & AllQueries<StyleType>; | ||
|
||
|
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.