-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support reset styles * chore: remove log * chore: set slot name * chore: improve regex * chore: remove log * chore: address direction in merge * chore: use real class in example * chore: rename param * chore: improve naming * chore: one loop * chore: remove loop * chore: multiple rules * chore: handle hover * chore: mergeDebugSequence UT * chore: replace array.from usage * chore: improve getResetDebugSequence * Change files --------- Co-authored-by: Charles Assunção <[email protected]>
- Loading branch information
Showing
11 changed files
with
269 additions
and
17 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@griffel-core-2b7b9120-d305-4875-ad0f-637328cf327b.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: support reset styles", | ||
"packageName": "@griffel/core", | ||
"email": "email not defined", | ||
"dependentChangeType": "patch" | ||
} |
7 changes: 7 additions & 0 deletions
7
change/@griffel-devtools-4eac2fde-2f32-4af5-88e7-a2d4982ef325.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: support reset styles", | ||
"packageName": "@griffel/devtools", | ||
"email": "email not defined", | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { RESET_HASH_PREFIX } from '../constants'; | ||
import { makeResetStyles } from '../makeResetStyles'; | ||
import type { MakeStylesOptions } from '../makeStyles'; | ||
import { mergeClasses } from '../mergeClasses'; | ||
import { createDOMRenderer } from '../renderer/createDOMRenderer'; | ||
import { getResetDebugSequence } from './getResetDebugSequence'; | ||
|
||
jest.mock('./isDevToolsEnabled', () => ({ | ||
isDevToolsEnabled: true, | ||
})); | ||
|
||
const options: MakeStylesOptions = { | ||
dir: 'ltr', | ||
renderer: createDOMRenderer(document), | ||
}; | ||
|
||
const findResetHash = (classNames: string) => | ||
classNames.split(' ').find(className => className.startsWith(RESET_HASH_PREFIX)); | ||
|
||
describe('getResetDebugSequence', () => { | ||
it('returns correct debug tree for reset styles', () => { | ||
const resetClassName = makeResetStyles({ | ||
margin: '0', | ||
padding: '0', | ||
})(options); | ||
const className = mergeClasses('ui-button', resetClassName); | ||
const classNameForLookup = findResetHash(className)!; | ||
const result = getResetDebugSequence(classNameForLookup)!; | ||
expect(result).toEqual({ | ||
children: [], | ||
debugClassNames: [{ className: classNameForLookup }], | ||
direction: options.dir, | ||
rules: expect.any(Object), | ||
sequenceHash: classNameForLookup, | ||
slot: 'makeResetStyles()', | ||
}); | ||
expect(result.rules).toMatchInlineSnapshot(` | ||
Object { | ||
"r1l95nvm": ".r1l95nvm{margin:0;padding:0;}", | ||
} | ||
`); | ||
}); | ||
it('handles reset styles with nested selectors', () => { | ||
const resetClassName = makeResetStyles({ | ||
margin: '0', | ||
padding: '0', | ||
':hover': { color: 'blue' }, | ||
'& .foo': { color: 'red' }, | ||
})(options); | ||
const className = mergeClasses('ui-button', resetClassName); | ||
const classNameForLookup = findResetHash(className)!; | ||
const result = getResetDebugSequence(classNameForLookup)!; | ||
expect(result).toEqual({ | ||
children: [], | ||
debugClassNames: [{ className: classNameForLookup }], | ||
direction: options.dir, | ||
rules: expect.any(Object), | ||
sequenceHash: classNameForLookup, | ||
slot: 'makeResetStyles()', | ||
}); | ||
expect(result.rules).toMatchInlineSnapshot(` | ||
Object { | ||
"rf14qlw": ".rf14qlw{margin:0;padding:0;}.rf14qlw:hover{color:blue;}.rf14qlw .foo{color:red;}", | ||
} | ||
`); | ||
}); | ||
}); |
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,32 @@ | ||
import { DEBUG_RESET_CLASSES } from '../constants'; | ||
import type { SequenceHash } from '../types'; | ||
import { debugData } from './store'; | ||
import type { DebugSequence } from './types'; | ||
|
||
export function getResetDebugSequence(debugSequenceHash: SequenceHash) { | ||
const resetClass = DEBUG_RESET_CLASSES[debugSequenceHash]; | ||
if (resetClass === undefined) { | ||
return undefined; | ||
} | ||
|
||
const debugClassNames = [{ className: debugSequenceHash }]; | ||
|
||
const node: DebugSequence = { | ||
sequenceHash: debugSequenceHash, | ||
direction: 'ltr', | ||
children: [], | ||
debugClassNames, | ||
}; | ||
|
||
node.rules = {}; | ||
node.slot = 'makeResetStyles()'; | ||
|
||
const [{ className }] = node.debugClassNames; | ||
const cssRules = debugData.getCSSRules().filter(cssRule => { | ||
return cssRule.includes(`.${className}`); | ||
}); | ||
|
||
node.rules![className] = cssRules.join(''); | ||
|
||
return node; | ||
} |
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,83 @@ | ||
import { DEFINITION_LOOKUP_TABLE, RESET_HASH_PREFIX, SEQUENCE_PREFIX } from '../constants'; | ||
import { makeResetStyles } from '../makeResetStyles'; | ||
import { makeStyles } from '../makeStyles'; | ||
import type { MakeStylesOptions } from '../makeStyles'; | ||
import { createDOMRenderer } from '../renderer/createDOMRenderer'; | ||
import { mergeDebugSequence } from './mergeDebugSequence'; | ||
import { getDebugClassNames } from './utils'; | ||
|
||
jest.mock('./isDevToolsEnabled', () => ({ | ||
isDevToolsEnabled: true, | ||
})); | ||
|
||
const options: MakeStylesOptions = { | ||
dir: 'ltr', | ||
renderer: createDOMRenderer(document), | ||
}; | ||
|
||
const findSequenceHash = (classNames: string) => | ||
classNames.split(' ').find(className => className.startsWith(SEQUENCE_PREFIX)); | ||
|
||
const findResetHash = (classNames: string) => | ||
classNames.split(' ').find(className => className.startsWith(RESET_HASH_PREFIX)); | ||
|
||
describe('mergeDebugSequence', () => { | ||
it('returns undefined when both inputs are undefined', () => { | ||
expect(mergeDebugSequence(undefined, undefined)).toBeUndefined(); | ||
}); | ||
|
||
it('returns atomic debug tree when reset classes is undefined', () => { | ||
const classes = makeStyles({ | ||
block: { display: 'block' }, | ||
})(options); | ||
|
||
const atomicSequence = findSequenceHash(classes.block); | ||
const result = mergeDebugSequence(atomicSequence, undefined); | ||
|
||
expect(result?.sequenceHash).toBe(atomicSequence); | ||
expect(result?.children).toHaveLength(0); | ||
}); | ||
|
||
it('returns reset debug tree when atomic classes is undefined', () => { | ||
const classes = makeResetStyles({ margin: 0 })(options); | ||
|
||
const resetSequence = findResetHash(classes); | ||
const result = mergeDebugSequence(undefined, resetSequence); | ||
|
||
expect(result?.sequenceHash).toBe(resetSequence); | ||
expect(result?.children).toHaveLength(0); | ||
}); | ||
|
||
it('correctly merges atomic and reset debug trees', () => { | ||
const atomicClasses = makeStyles({ | ||
block: { display: 'block' }, | ||
})(options); | ||
|
||
const resetClasses = makeResetStyles({ | ||
margin: 0, | ||
padding: 0, | ||
})(options); | ||
|
||
const atomicSequence = findSequenceHash(atomicClasses.block); | ||
const resetSequence = findResetHash(resetClasses); | ||
|
||
const result = mergeDebugSequence(atomicSequence, resetSequence); | ||
|
||
const debugClassname = getDebugClassNames(DEFINITION_LOOKUP_TABLE[atomicSequence!]); | ||
expect(result).toMatchObject({ | ||
sequenceHash: expect.stringContaining(atomicSequence! + resetSequence!), | ||
direction: 'ltr', | ||
children: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
sequenceHash: atomicSequence, | ||
debugClassNames: expect.any(Array), | ||
}), | ||
expect.objectContaining({ | ||
sequenceHash: resetSequence, | ||
debugClassNames: expect.any(Array), | ||
}), | ||
]), | ||
debugClassNames: expect.arrayContaining([...debugClassname, { className: resetSequence }]), | ||
}); | ||
}); | ||
}); |
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,32 @@ | ||
import { getAtomicDebugSequenceTree } from './getAtomicDebugSequenceTree'; | ||
import { getResetDebugSequence } from './getResetDebugSequence'; | ||
import type { DebugSequence } from './types'; | ||
|
||
export function mergeDebugSequence( | ||
atomicClases: string | undefined, | ||
resetClassName: string | undefined, | ||
): DebugSequence | undefined { | ||
const debugResultRootAtomic = atomicClases ? getAtomicDebugSequenceTree(atomicClases) : undefined; | ||
const debugResultRootReset = resetClassName ? getResetDebugSequence(resetClassName) : undefined; | ||
|
||
if (!debugResultRootAtomic && !debugResultRootReset) { | ||
return undefined; | ||
} | ||
|
||
if (!debugResultRootAtomic) { | ||
return debugResultRootReset; | ||
} | ||
|
||
if (!debugResultRootReset) { | ||
return debugResultRootAtomic; | ||
} | ||
|
||
const debugResultRoot: DebugSequence = { | ||
sequenceHash: debugResultRootAtomic.sequenceHash + debugResultRootReset.sequenceHash, | ||
direction: debugResultRootAtomic.direction, | ||
children: [debugResultRootAtomic, debugResultRootReset], | ||
debugClassNames: [...debugResultRootAtomic.debugClassNames, ...debugResultRootReset.debugClassNames], | ||
}; | ||
|
||
return debugResultRoot; | ||
} |
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