-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): Auto apply generic appearance keys via style() (#6041)
- Loading branch information
Showing
15 changed files
with
336 additions
and
182 deletions.
There are no files selected for viewing
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
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,49 @@ | ||
import { createMemo, createSignal, onMount } from 'solid-js'; | ||
import { AppearanceKey, appearanceKeys, Elements, useAppearance } from '../context'; | ||
import { cn } from './utils'; | ||
|
||
export const useStyle = () => { | ||
const appearance = useAppearance(); | ||
const [isServer, setIsServer] = createSignal(true); | ||
|
||
onMount(() => { | ||
setIsServer(false); | ||
}); | ||
|
||
const styleFuncMemo = createMemo(() => (appearanceKey: AppearanceKey, className?: string) => { | ||
const appearanceClassname = | ||
typeof appearance.elements?.[appearanceKey] === 'string' | ||
? (appearance.elements?.[appearanceKey] as string) || '' | ||
: ''; | ||
|
||
const appearanceKeyParts = appearanceKey.split('__'); | ||
const finalAppearanceKeys: (keyof Elements)[] = []; | ||
for (let i = 0; i < appearanceKeyParts.length; i++) { | ||
const accumulated = appearanceKeyParts.slice(i).join('__'); | ||
if (appearanceKeys.includes(accumulated as keyof Elements)) { | ||
finalAppearanceKeys.push(accumulated as keyof Elements); | ||
} | ||
} | ||
//Have keys with least amount of `__` first, i.e. have `bar foo__bar` in the DOM. | ||
finalAppearanceKeys.sort((a, b) => { | ||
const countA = (a.match(/__/g) || []).length; | ||
const countB = (b.match(/__/g) || []).length; | ||
|
||
return countA - countB; | ||
}); | ||
|
||
const cssInJsClasses = | ||
!!finalAppearanceKeys.length && !isServer() | ||
? finalAppearanceKeys.map((appKey) => appearance.appearanceKeyToCssInJsClass[appKey]) | ||
: []; | ||
|
||
return cn( | ||
...finalAppearanceKeys, | ||
className, // default styles | ||
appearanceClassname, // overrides via appearance prop classes | ||
...cssInJsClasses | ||
); | ||
}); | ||
|
||
return styleFuncMemo(); | ||
}; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.