Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: insertionFactory API for @griffel/core (#407)
* feat: insertionFactory API * add tests
- Loading branch information
1 parent
9e29013
commit 9f7710e
Showing
11 changed files
with
120 additions
and
57 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@griffel-core-80492b15-83a7-4ca1-8c1b-b5aeb0c3aa58.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": "minor", | ||
"comment": "feat: add API for styles insertion", | ||
"packageName": "@griffel/core", | ||
"email": "olfedias@microsoft.com", | ||
"dependentChangeType": "patch" | ||
} |
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,25 @@ | ||
import { insertionFactory } from './insertionFactory'; | ||
import type { GriffelRenderer } from './types'; | ||
|
||
describe('insertionFactory', () => { | ||
it('should return a function', () => { | ||
expect(insertionFactory()).toBeInstanceOf(Function); | ||
}); | ||
|
||
it('inserts CSS rules only once per renderer', () => { | ||
const rendererA: Partial<GriffelRenderer> = { id: 'a', insertCSSRules: jest.fn() }; | ||
const rendererB: Partial<GriffelRenderer> = { id: 'b', insertCSSRules: jest.fn() }; | ||
|
||
const insertStyles = insertionFactory(); | ||
|
||
insertStyles(rendererA as GriffelRenderer, { d: ['a'] }); | ||
insertStyles(rendererA as GriffelRenderer, { d: ['a'] }); | ||
|
||
expect(rendererA.insertCSSRules).toHaveBeenCalledTimes(1); | ||
|
||
insertStyles(rendererB as GriffelRenderer, { d: ['a'] }); | ||
insertStyles(rendererB as GriffelRenderer, { d: ['a'] }); | ||
|
||
expect(rendererB.insertCSSRules).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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,18 @@ | ||
import type { CSSRulesByBucket, GriffelInsertionFactory, GriffelRenderer } from './types'; | ||
|
||
/** | ||
* Default implementation of insertion factory. Inserts styles only once per renderer and performs | ||
* insertion immediately after styles computation. | ||
* | ||
* @internal | ||
*/ | ||
export const insertionFactory: GriffelInsertionFactory = () => { | ||
const insertionCache: Record<string, boolean> = {}; | ||
|
||
return function insertStyles(renderer: GriffelRenderer, cssRules: CSSRulesByBucket) { | ||
if (insertionCache[renderer.id] === undefined) { | ||
renderer.insertCSSRules(cssRules!); | ||
insertionCache[renderer.id] = true; | ||
} | ||
}; | ||
}; |
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