@patreon/studio
Version:
Patreon Studio Design System
30 lines • 1.47 kB
JavaScript
import { useCallback } from 'react';
import { useColorSystemContainerState } from '../../components/ColorSystemContainer';
import { useTokenValueCache } from '../../components/TokenValueCache';
import { getCacheKey } from '../../utilities/color-system';
import { getTokenName } from '../../utilities/tokens';
/**
* This hook can be used as an escape hatch when you need
* to get the light and dark value pair of a token.
*/
export function useExtractTokenValuePair() {
const { inputColor, tokenMap } = useColorSystemContainerState();
const tokenValueCache = useTokenValueCache();
// this is casts to the non-partial type because we know that the default token values are always complete
const defaultTokenValues = tokenValueCache.default;
return useCallback((token) => {
const tokenName = getTokenName(token);
if (inputColor === 'default') {
return {
light: defaultTokenValues.light[tokenName],
dark: defaultTokenValues.dark[tokenName],
};
}
const cacheKey = getCacheKey({ inputColor, tokenMap });
return {
light: tokenValueCache[cacheKey].light?.[tokenName] ?? defaultTokenValues.light[tokenName],
dark: tokenValueCache[cacheKey].dark?.[tokenName] ?? defaultTokenValues.dark[tokenName],
};
}, [defaultTokenValues, inputColor, tokenMap, tokenValueCache]);
}
//# sourceMappingURL=useExtractTokenValuePair.js.map