Skip to content

Commit 0c22d89

Browse files
authored
chore: rework internals of makeStaticStyles (#398)
1 parent 2ee1ee7 commit 0c22d89

File tree

4 files changed

+59
-55
lines changed

4 files changed

+59
-55
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "chore: rework internals of makeStaticStyles",
4+
"packageName": "@griffel/core",
5+
"email": "olfedias@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

packages/core/src/makeStaticStyles.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ export function makeStaticStyles(styles: GriffelStaticStyles | GriffelStaticStyl
1616
const stylesSet: GriffelStaticStyles[] = Array.isArray(styles) ? styles : [styles];
1717

1818
function useStaticStyles(options: MakeStaticStylesOptions): void {
19-
const cacheKey = options.renderer.id;
20-
if (styleCache[cacheKey]) {
21-
return;
22-
}
19+
const { renderer } = options;
20+
const cacheKey = renderer.id;
2321

24-
for (const styleRules of stylesSet) {
25-
options.renderer.insertCSSRules(resolveStaticStyleRules(styleRules));
22+
if (!styleCache[cacheKey]) {
23+
renderer.insertCSSRules({
24+
// 👇 static rules should be inserted into default bucket
25+
d: resolveStaticStyleRules(stylesSet),
26+
});
27+
styleCache[cacheKey] = true;
2628
}
27-
28-
styleCache[cacheKey] = true;
2929
}
3030

3131
return useStaticStyles;

packages/core/src/runtime/resolveStaticStyleRules.test.ts

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,48 @@ import { resolveStaticStyleRules } from './resolveStaticStyleRules';
33
describe('resolveStaticStyleRules', () => {
44
it('handles font-face', () => {
55
expect(
6-
resolveStaticStyleRules({
7-
'@font-face': {
8-
fontFamily: 'Open Sans',
9-
src: `url("webfont.woff2") format("woff2")`,
6+
resolveStaticStyleRules([
7+
{
8+
'@font-face': {
9+
fontFamily: 'Open Sans',
10+
src: `url("webfont.woff2") format("woff2")`,
11+
},
1012
},
11-
}),
13+
]),
1214
).toMatchInlineSnapshot(`
13-
Object {
14-
"d": Array [
15-
"@font-face{font-family:Open Sans;src:url(\\"webfont.woff2\\") format(\\"woff2\\");}",
16-
],
17-
}
15+
Array [
16+
"@font-face{font-family:Open Sans;src:url(\\"webfont.woff2\\") format(\\"woff2\\");}",
17+
]
1818
`);
1919
});
2020

2121
it('handles static css', () => {
2222
expect(
23-
resolveStaticStyleRules({
24-
body: {
25-
background: 'blue',
23+
resolveStaticStyleRules([
24+
{
25+
body: {
26+
background: 'blue',
27+
},
28+
'.foo': {
29+
background: 'yellow',
30+
marginLeft: '5px',
31+
},
2632
},
27-
'.foo': {
28-
background: 'yellow',
29-
marginLeft: '5px',
30-
},
31-
}),
33+
]),
3234
).toMatchInlineSnapshot(`
33-
Object {
34-
"d": Array [
35-
"body{background:blue;}",
36-
".foo{background:yellow;margin-left:5px;}",
37-
],
38-
}
35+
Array [
36+
"body{background:blue;}",
37+
".foo{background:yellow;margin-left:5px;}",
38+
]
3939
`);
4040
});
4141

4242
it('handles css string', () => {
43-
expect(resolveStaticStyleRules('body {background: red;} div {color: green;}')).toMatchInlineSnapshot(`
44-
Object {
45-
"d": Array [
46-
"body{background:red;}",
47-
"div{color:green;}",
48-
],
49-
}
43+
expect(resolveStaticStyleRules(['body {background: red;} div {color: green;}'])).toMatchInlineSnapshot(`
44+
Array [
45+
"body{background:red;}",
46+
"div{color:green;}",
47+
]
5048
`);
5149
});
5250
});
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
import type { GriffelStaticStyles } from '@griffel/style-types';
2-
import type { CSSRulesByBucket } from '../types';
2+
3+
import type { CSSBucketEntry } from '../types';
34
import { compileCSSRules } from './compileCSSRules';
45
import { compileStaticCSS } from './compileStaticCSS';
56

6-
export function resolveStaticStyleRules(styles: GriffelStaticStyles, result: CSSRulesByBucket = {}): CSSRulesByBucket {
7-
if (typeof styles === 'string') {
8-
const cssRules = compileCSSRules(styles, false);
7+
export function resolveStaticStyleRules(stylesSet: GriffelStaticStyles[]): CSSBucketEntry[] {
8+
return stylesSet.reduce((acc, styles) => {
9+
if (typeof styles === 'string') {
10+
const cssRules = compileCSSRules(styles, false);
11+
12+
for (const rule of cssRules) {
13+
acc.push(rule);
14+
}
915

10-
for (const rule of cssRules) {
11-
addResolvedStyles(rule, result);
16+
return acc;
1217
}
13-
} else {
18+
1419
// eslint-disable-next-line guard-for-in
1520
for (const property in styles) {
1621
const value = styles[property];
1722
const staticCSS = compileStaticCSS(property, value);
1823

19-
addResolvedStyles(staticCSS, result);
24+
acc.push(staticCSS);
2025
}
21-
}
22-
23-
return result;
24-
}
2526

26-
function addResolvedStyles(cssRule: string, result: CSSRulesByBucket = {}): void {
27-
// 👇 static rules should be inserted into default bucket
28-
result.d = result.d || [];
29-
result.d.push(cssRule);
27+
return acc;
28+
}, [] as CSSBucketEntry[]);
3029
}

0 commit comments

Comments
 (0)