-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usesxtokens): useSxToken -> useSxTokens (#19)
BREAKING CHANGE: useSxToken is changed to useSxTokens
- Loading branch information
Showing
5 changed files
with
65 additions
and
54 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useContext } from 'react'; | ||
import { generateArray } from '@mj-studio/js-util'; | ||
|
||
import type { ThemedDict } from '../@types/ThemedDict'; | ||
import type { ThemedTypings } from '../@types/ThemedTypings'; | ||
import { printWarning } from '../internal/util/printWarning'; | ||
import { StyledSystemContext } from '../provider/StyledSystemProvider'; | ||
|
||
export type UseSxTokensOptions = { | ||
theme?: ThemedDict; | ||
}; | ||
export const useSxTokens = <T extends keyof ThemedTypings, V extends ThemedTypings[T]>( | ||
tokenType: T, | ||
tokenValues: Array<Exclude<V, null | undefined>>, | ||
{ theme: optionTheme }: UseSxTokensOptions = {}, | ||
): Array<ThemedDict[T][keyof ThemedDict[T]]> => { | ||
const styledSystemContext = useContext(StyledSystemContext); | ||
|
||
const theme = optionTheme ?? styledSystemContext?.theme; | ||
|
||
if (!theme) { | ||
printWarning('theme not found from useSxTokens, undefineds will be returned.'); | ||
|
||
return generateArray(tokenValues.length).map(() => undefined) as any; | ||
} | ||
|
||
const ret: Array<ThemedDict[T][keyof ThemedDict[T]]> = []; | ||
|
||
for (let i = 0; i < tokenValues.length; i++) { | ||
const tokenValue = tokenValues[i]; | ||
if (!(tokenValue in theme[tokenType])) { | ||
printWarning( | ||
`tokenValue ${String(tokenValue)} at index ${i} doesn't exist in tokenType ${tokenType} from useSxTokens, undefined will be returned.`, | ||
); | ||
ret.push(undefined as any); | ||
} else { | ||
ret.push(theme[tokenType][tokenValue]); | ||
} | ||
} | ||
|
||
return ret; | ||
}; |