|
| 1 | +import { |
| 2 | + Box, |
| 3 | + FieldProps, |
| 4 | + control, |
| 5 | + useBentoConfig, |
| 6 | + getRadiusPropsFromConfig, |
| 7 | + Body, |
| 8 | + Field, |
| 9 | + Popover, |
| 10 | +} from "@buildo/bento-design-system"; |
| 11 | +import { useRef } from "react"; |
| 12 | +import { useSelectState } from "@react-stately/select"; |
| 13 | +import { Section, Item } from "@react-stately/collections"; |
| 14 | +import { useSelect, HiddenSelect } from "@react-aria/select"; |
| 15 | +import { PalettesDropdown } from "./PalettesDropdown"; |
| 16 | +import { ThemeConfig } from "../ConfiguratorStatusContext"; |
| 17 | +import { useButton } from "@react-aria/button"; |
| 18 | +import { getPalette } from "../ColorEditor/Palette"; |
| 19 | +import { ColorConfig } from "../ColorEditor/ColorEditor"; |
| 20 | + |
| 21 | +type Props = FieldProps<string> & { colors: ThemeConfig["colors"] }; |
| 22 | + |
| 23 | +function getPaletteItemsSection(key: string, colorConfig: ColorConfig) { |
| 24 | + const paletteItems = getPalette(colorConfig).map((color, index) => ( |
| 25 | + <Item key={`${key}-${index}`}>{color}</Item> |
| 26 | + )); |
| 27 | + return ( |
| 28 | + <Section |
| 29 | + key={key} |
| 30 | + children={ |
| 31 | + colorConfig.useReferenceAsKeyColor |
| 32 | + ? [...paletteItems, <Item key={`${key}-reference`}>{colorConfig.referenceColor}</Item>] |
| 33 | + : paletteItems |
| 34 | + } |
| 35 | + /> |
| 36 | + ); |
| 37 | +} |
| 38 | + |
| 39 | +function getColorItems(colors: ThemeConfig["colors"]) { |
| 40 | + return [ |
| 41 | + <Section key="General"> |
| 42 | + <Item key="white">white</Item> |
| 43 | + <Item key="black">black</Item> |
| 44 | + </Section>, |
| 45 | + getPaletteItemsSection("BrandPrimary", colors.brand[0]), |
| 46 | + getPaletteItemsSection("Interactive", colors.interactive), |
| 47 | + getPaletteItemsSection("Neutral", colors.neutral), |
| 48 | + getPaletteItemsSection("Informative", colors.semantic.informative), |
| 49 | + getPaletteItemsSection("Positive", colors.semantic.positive), |
| 50 | + getPaletteItemsSection("Warning", colors.semantic.warning), |
| 51 | + getPaletteItemsSection("Negative", colors.semantic.negative), |
| 52 | + getPaletteItemsSection("Grey", colors.dataVisualization.grey), |
| 53 | + getPaletteItemsSection("Red", colors.dataVisualization.red), |
| 54 | + getPaletteItemsSection("Orange", colors.dataVisualization.orange), |
| 55 | + getPaletteItemsSection("Yellow", colors.dataVisualization.yellow), |
| 56 | + getPaletteItemsSection("Green", colors.dataVisualization.green), |
| 57 | + getPaletteItemsSection("Jade", colors.dataVisualization.jade), |
| 58 | + getPaletteItemsSection("Blue", colors.dataVisualization.blue), |
| 59 | + getPaletteItemsSection("Indigo", colors.dataVisualization.indigo), |
| 60 | + getPaletteItemsSection("Violet", colors.dataVisualization.violet), |
| 61 | + getPaletteItemsSection("Pink", colors.dataVisualization.pink), |
| 62 | + ]; |
| 63 | +} |
| 64 | + |
| 65 | +export function ColorPickerField(props: Props) { |
| 66 | + const inputConfig = useBentoConfig().input; |
| 67 | + const dropdownConfig = useBentoConfig().dropdown; |
| 68 | + |
| 69 | + const ref = useRef(null); |
| 70 | + |
| 71 | + const state = useSelectState({ |
| 72 | + ...props, |
| 73 | + isDisabled: props.disabled, |
| 74 | + children: getColorItems(props.colors), |
| 75 | + onSelectionChange: (key) => { |
| 76 | + props.onChange(state.collection.getItem(key)!.textValue); |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + const { labelProps, errorMessageProps, descriptionProps, triggerProps, valueProps, menuProps } = |
| 81 | + useSelect( |
| 82 | + { |
| 83 | + ...props, |
| 84 | + isDisabled: props.disabled, |
| 85 | + }, |
| 86 | + state, |
| 87 | + ref |
| 88 | + ); |
| 89 | + |
| 90 | + const { buttonProps } = useButton( |
| 91 | + { |
| 92 | + ...triggerProps, |
| 93 | + elementType: "div", |
| 94 | + }, |
| 95 | + ref |
| 96 | + ); |
| 97 | + |
| 98 | + return ( |
| 99 | + <Field |
| 100 | + {...props} |
| 101 | + labelProps={labelProps} |
| 102 | + assistiveTextProps={descriptionProps} |
| 103 | + errorMessageProps={errorMessageProps} |
| 104 | + > |
| 105 | + <HiddenSelect |
| 106 | + isDisabled={props.disabled} |
| 107 | + state={state} |
| 108 | + triggerRef={ref} |
| 109 | + label={props.label} |
| 110 | + name={props.name} |
| 111 | + /> |
| 112 | + <Box |
| 113 | + as="button" |
| 114 | + {...buttonProps} |
| 115 | + color={undefined} |
| 116 | + display="flex" |
| 117 | + cursor="pointer" |
| 118 | + outline="none" |
| 119 | + className={control({ validation: "valid", menuIsOpen: state.isOpen, isReadOnly: false })} |
| 120 | + disabled={props.disabled} |
| 121 | + {...getRadiusPropsFromConfig(inputConfig.radius)} |
| 122 | + paddingX={inputConfig.paddingX} |
| 123 | + paddingY={inputConfig.paddingY} |
| 124 | + ref={ref} |
| 125 | + > |
| 126 | + <Box {...valueProps} flexGrow={1} textAlign="left"> |
| 127 | + <Body size={inputConfig.fontSize} color={props.disabled ? "disabled" : "primary"}> |
| 128 | + {props.value} |
| 129 | + </Body> |
| 130 | + </Box> |
| 131 | + <Box paddingLeft={16} aria-hidden="true"> |
| 132 | + {dropdownConfig.openIndicatorIcon({ |
| 133 | + size: dropdownConfig.openIndicatorIconSize, |
| 134 | + color: props.disabled ? "disabled" : "default", |
| 135 | + })} |
| 136 | + </Box> |
| 137 | + </Box> |
| 138 | + {state.isOpen && ( |
| 139 | + <Popover triggerRef={ref} onClose={state.close}> |
| 140 | + <PalettesDropdown |
| 141 | + colors={props.colors} |
| 142 | + value={props.value} |
| 143 | + onChange={props.onChange} |
| 144 | + state={state} |
| 145 | + menuProps={menuProps} |
| 146 | + /> |
| 147 | + </Popover> |
| 148 | + )} |
| 149 | + </Field> |
| 150 | + ); |
| 151 | +} |
0 commit comments