-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): makeResetStyles emits rules into separate buckets (#405)
- Loading branch information
1 parent
bd37c4f
commit c33da85
Showing
27 changed files
with
274 additions
and
40 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@griffel-core-9d917d14-e85c-4ca7-beb7-164dcb0d6085.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "fix: makeResetStyles emits at rules into a separate bucket", | ||
"packageName": "@griffel/core", | ||
"email": "olfedias@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@griffel-react-a850ebd1-fc07-46c7-b8c7-3c97f8cfe864.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "chore: add support for different buckets in makeResetStyles", | ||
"packageName": "@griffel/react", | ||
"email": "olfedias@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@griffel-webpack-extraction-plugin-2982acb5-12ca-4655-8e1c-f048c923919f.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "chore: add support for different buckets in makeResetStyles", | ||
"packageName": "@griffel/webpack-extraction-plugin", | ||
"email": "olfedias@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/babel-preset/__fixtures__/reset-styles-at-rules/code.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { makeResetStyles } from '@griffel/react'; | ||
|
||
export const useStyles = makeResetStyles({ | ||
color: 'red', | ||
'@supports (display: flex)': { color: 'pink' }, | ||
'@media (min-width: 100px)': { color: 'blue' }, | ||
}); |
5 changes: 5 additions & 0 deletions
5
packages/babel-preset/__fixtures__/reset-styles-at-rules/output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { __resetStyles } from '@griffel/react'; | ||
export const useStyles = __resetStyles('rjrhw4c', null, { | ||
r: ['.rjrhw4c{color:red;}'], | ||
s: ['@supports (display: flex){.rjrhw4c{color:pink;}}', '@media (min-width: 100px){.rjrhw4c{color:blue;}}'], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { compileResetCSSRules } from './compileResetCSSRules'; | ||
|
||
describe('compileCSSRules', () => { | ||
it('compiles CSS rules', () => { | ||
const cssRules = ` | ||
.foo { | ||
color: red; | ||
@media (min-width: 768px) { color: blue } | ||
} | ||
`; | ||
|
||
expect(compileResetCSSRules(cssRules)).toMatchInlineSnapshot(` | ||
Array [ | ||
Array [ | ||
".foo{color:red;}", | ||
], | ||
Array [ | ||
"@media (min-width: 768px){.foo{color:blue;}}", | ||
], | ||
] | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { compile, middleware, serialize, stringify } from 'stylis'; | ||
|
||
import { globalPlugin } from './stylis/globalPlugin'; | ||
import { isAtRuleElement } from './stylis/isAtRuleElement'; | ||
import { prefixerPlugin } from './stylis/prefixerPlugin'; | ||
import { rulesheetPlugin } from './stylis/rulesheetPlugin'; | ||
|
||
export function compileResetCSSRules(cssRules: string): [string[], string[]] { | ||
const rules: string[] = []; | ||
const atRules: string[] = []; | ||
|
||
serialize( | ||
compile(cssRules), | ||
middleware([ | ||
globalPlugin, | ||
prefixerPlugin, | ||
stringify, | ||
|
||
// 💡 we are using `.insertRule()` API for DOM operations, which does not support | ||
// insertion of multiple CSS rules in a single call. `rulesheet` plugin extracts | ||
// individual rules to be used with this API | ||
rulesheetPlugin((element, rule) => { | ||
if (isAtRuleElement(element)) { | ||
atRules.push(rule); | ||
return; | ||
} | ||
|
||
rules.push(rule); | ||
}), | ||
]), | ||
); | ||
|
||
return [rules, atRules]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { compile } from 'stylis'; | ||
import { isAtRuleElement } from './isAtRuleElement'; | ||
|
||
describe('isAtRuleElement', () => { | ||
it.each([ | ||
['@container { div { color: red } }', true], | ||
['@layer foo { div { color: red } }', true], | ||
['@media (min-width: 100px) { div { color: red } }', true], | ||
['@supports (display: flex) { div { color: red } }', true], | ||
['div { color: red }', false], | ||
['div:hover { color: red }', false], | ||
])('handles "%s"', (css, expected) => { | ||
expect(isAtRuleElement(compile(css)[0])).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { LAYER, MEDIA, SUPPORTS } from 'stylis'; | ||
import type { Element } from 'stylis'; | ||
|
||
export function isAtRuleElement(element: Element): boolean { | ||
switch (element.type) { | ||
case '@container': | ||
case MEDIA: | ||
case SUPPORTS: | ||
case LAYER: | ||
return true; | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { compile, middleware, serialize, stringify, RULESET } from 'stylis'; | ||
|
||
import { rulesheetPlugin } from './rulesheetPlugin'; | ||
import type { RulesheetPluginCallback } from './rulesheetPlugin'; | ||
|
||
function compileRule(rule: string, callback: RulesheetPluginCallback) { | ||
return serialize(compile(rule), middleware([stringify, rulesheetPlugin(callback)])); | ||
} | ||
|
||
describe('rulesheetPlugin', () => { | ||
it('handles basic selectors', () => { | ||
const callback = jest.fn(); | ||
const css = ` | ||
.foo { color: red } | ||
.bar { color: blue } | ||
`; | ||
|
||
expect(compileRule(css, callback)).toMatchInlineSnapshot(`".foo{color:red;}.bar{color:blue;}"`); | ||
|
||
expect(callback).toHaveBeenCalledTimes(2); | ||
expect(callback).toHaveBeenCalledWith( | ||
expect.objectContaining({ type: RULESET, return: '.foo{color:red;}' }), | ||
'.foo{color:red;}', | ||
); | ||
expect(callback).toHaveBeenCalledWith( | ||
expect.objectContaining({ type: RULESET, return: '.bar{color:blue;}' }), | ||
'.bar{color:blue;}', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { Element, Middleware } from 'stylis'; | ||
|
||
export type RulesheetPluginCallback = (type: Element, rule: string) => void; | ||
|
||
/** | ||
* The same plugin as in stylis, but this version also has "element" argument. | ||
*/ | ||
export function rulesheetPlugin(callback: RulesheetPluginCallback): Middleware { | ||
return function (element) { | ||
if (!element.root) { | ||
if (element.return) { | ||
callback(element, element.return); | ||
} | ||
} | ||
}; | ||
} |
Oops, something went wrong.