|
| 1 | +import { |
| 2 | + Cube, |
| 3 | + Layers, |
| 4 | + MagicWand, |
| 5 | + PencilToLine, |
| 6 | + TargetDart, |
| 7 | + Text as TextIcon, |
| 8 | +} from '@gravity-ui/icons'; |
| 9 | +import {Icon} from '@gravity-ui/uikit'; |
| 10 | +import { |
| 11 | + type ColorOptions, |
| 12 | + type GravityTheme, |
| 13 | + type Theme, |
| 14 | + type UtilityColor, |
| 15 | + createUtilityColorCssVariable, |
| 16 | + isInternalUtilityColorReference, |
| 17 | +} from '@gravity-ui/uikit-themer'; |
| 18 | +import { |
| 19 | + createInternalUtilityColorReference, |
| 20 | + isUtilityColorToken, |
| 21 | + parseInternalUtilityColorReference, |
| 22 | +} from '@gravity-ui/uikit-themer/dist/utils'; |
| 23 | +import {useMemo} from 'react'; |
| 24 | + |
| 25 | +import {DEFAULT_ADVANCED_COLORS} from '../lib/constants'; |
| 26 | +import type {AdvancedColorType} from '../lib/types'; |
| 27 | +import type {BaseColor} from '../ui/PrivateColorSelect/types'; |
| 28 | + |
| 29 | +import {useThemeCreator} from './useThemeCreator'; |
| 30 | + |
| 31 | +export type SemanticColorGroup = { |
| 32 | + icon: React.ReactNode; |
| 33 | + key: string; |
| 34 | + title: string; |
| 35 | + groups: { |
| 36 | + title: string; |
| 37 | + items: (BaseColor & {name?: string; ref?: string})[]; |
| 38 | + }[]; |
| 39 | +}; |
| 40 | + |
| 41 | +const getIconByGroup = (group: Exclude<AdvancedColorType, 'basic-palette'>) => { |
| 42 | + switch (group) { |
| 43 | + case 'brand-summary': |
| 44 | + return <Icon data={TargetDart} />; |
| 45 | + case 'texts': |
| 46 | + return <Icon data={TextIcon} />; |
| 47 | + case 'backgrounds': |
| 48 | + return <Icon data={Layers} />; |
| 49 | + case 'lines': |
| 50 | + return <Icon data={PencilToLine} />; |
| 51 | + case 'effects': |
| 52 | + return <Icon data={MagicWand} />; |
| 53 | + case 'misc': |
| 54 | + return <Icon data={Cube} />; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +const resolveUtilityColor = (state: GravityTheme['utilityColors'], themeVariant: Theme) => { |
| 59 | + const traverse = (colorObject: ColorOptions): string => { |
| 60 | + if (colorObject.ref && isInternalUtilityColorReference(colorObject.ref)) { |
| 61 | + const nextUtilityColorToken = parseInternalUtilityColorReference(colorObject.ref); |
| 62 | + |
| 63 | + if (nextUtilityColorToken) { |
| 64 | + const nextUtilityColor = state[nextUtilityColorToken]; |
| 65 | + |
| 66 | + return traverse(nextUtilityColor[themeVariant]); |
| 67 | + } |
| 68 | + |
| 69 | + return colorObject.value; |
| 70 | + } |
| 71 | + |
| 72 | + return colorObject.value; |
| 73 | + }; |
| 74 | + |
| 75 | + return traverse; |
| 76 | +}; |
| 77 | + |
| 78 | +export const useThemeSemanticColorOption = (themeVariant: Theme): SemanticColorGroup[] => { |
| 79 | + const themeState = useThemeCreator(); |
| 80 | + const {gravityTheme} = themeState; |
| 81 | + |
| 82 | + return useMemo(() => { |
| 83 | + return Object.entries(DEFAULT_ADVANCED_COLORS) |
| 84 | + .filter(([advanceColorGroupName]) => advanceColorGroupName !== 'basic-palette') |
| 85 | + .map(([advanceColorGroupName, advanceColorSubGroups]) => { |
| 86 | + return { |
| 87 | + key: advanceColorGroupName, |
| 88 | + icon: getIconByGroup( |
| 89 | + advanceColorGroupName as Exclude<AdvancedColorType, 'basic-palette'>, |
| 90 | + ), |
| 91 | + title: advanceColorGroupName, |
| 92 | + groups: Object.entries(advanceColorSubGroups).map( |
| 93 | + ([advanceColorSubGroupName, advanceColorSubGroupItems]) => { |
| 94 | + return { |
| 95 | + title: advanceColorSubGroupName, |
| 96 | + items: advanceColorSubGroupItems.map(({colorName}) => { |
| 97 | + const isUtilityColor = isUtilityColorToken(colorName); |
| 98 | + |
| 99 | + const colorObject = isUtilityColor |
| 100 | + ? gravityTheme.utilityColors[colorName as UtilityColor][ |
| 101 | + themeVariant |
| 102 | + ] |
| 103 | + : gravityTheme.baseColors[colorName]?.[themeVariant]; |
| 104 | + |
| 105 | + const resolvedValue = isUtilityColor |
| 106 | + ? resolveUtilityColor( |
| 107 | + gravityTheme.utilityColors, |
| 108 | + themeVariant, |
| 109 | + )( |
| 110 | + gravityTheme.utilityColors[colorName as UtilityColor][ |
| 111 | + themeVariant |
| 112 | + ], |
| 113 | + ) |
| 114 | + : colorObject.value; |
| 115 | + |
| 116 | + return { |
| 117 | + name: colorName, |
| 118 | + title: isUtilityColor |
| 119 | + ? createUtilityColorCssVariable(colorName) |
| 120 | + : colorName, |
| 121 | + color: resolvedValue, |
| 122 | + ref: colorObject.ref, |
| 123 | + token: isUtilityColor |
| 124 | + ? createInternalUtilityColorReference(colorName) |
| 125 | + : colorName, |
| 126 | + }; |
| 127 | + }), |
| 128 | + }; |
| 129 | + }, |
| 130 | + ), |
| 131 | + }; |
| 132 | + }); |
| 133 | + }, [themeState, themeVariant]); |
| 134 | +}; |
0 commit comments