-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add custom hex color configuration toggle #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,4 +39,5 @@ yarn-error.log* | |
|
|
||
| # typescript | ||
| *.tsbuildinfo | ||
| next-env.d.ts | ||
| next-env.d.ts | ||
| .env*.local | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| /// <reference types="next" /> | ||
| /// <reference types="next/image-types/global" /> | ||
| /// <reference path="./.next/types/routes.d.ts" /> | ||
|
|
||
| // NOTE: This file should not be edited | ||
| // see https://nextjs.org/docs/pages/api-reference/config/typescript for more information. |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import React, { ChangeEvent, FC, useCallback } from 'react'; | ||
| import { InputToggle } from '@uniformdev/design-system'; | ||
| import { SetLocationValueDispatch } from '@uniformdev/mesh-sdk-react'; | ||
|
|
||
| type CustomHexConfigToggleType = { | ||
| allowCustomHex?: MeshType.MeshDesignExtensionsParametersConfig['allowCustomHex']; | ||
| setCustomHexConfig: SetLocationValueDispatch< | ||
| Pick<MeshType.MeshDesignExtensionsParametersConfig, 'allowCustomHex'> | undefined, | ||
| Pick<MeshType.MeshDesignExtensionsParametersConfig, 'allowCustomHex'> | ||
| >; | ||
| }; | ||
|
|
||
| const CustomHexConfigToggle: FC<CustomHexConfigToggleType> = ({ allowCustomHex, setCustomHexConfig }) => { | ||
| const handleToggle = useCallback( | ||
| (e: ChangeEvent<HTMLInputElement>) => { | ||
| const { checked } = e.target; | ||
| setCustomHexConfig(previousValue => { | ||
| if (checked) { | ||
| return { newValue: { ...previousValue, allowCustomHex: checked } }; | ||
| } else { | ||
| const { allowCustomHex: _, ...restValues } = previousValue || {}; | ||
| return { newValue: restValues }; | ||
| } | ||
| }); | ||
| }, | ||
| [setCustomHexConfig] | ||
| ); | ||
|
|
||
| return ( | ||
| <InputToggle | ||
| label="Allow custom hex value" | ||
| caption="Allow users to specify a custom hex color value as an override." | ||
| name="allowCustomHex" | ||
| type="checkbox" | ||
| checked={Boolean(allowCustomHex)} | ||
| onChange={handleToggle} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default CustomHexConfigToggle; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| import React, { FC, useCallback, useMemo, ChangeEvent } from 'react'; | ||
| import React, { FC, useCallback, useMemo, useState, ChangeEvent } from 'react'; | ||
| import classNames from 'classnames'; | ||
| import { Callout } from '@uniformdev/design-system'; | ||
| import { Button, Callout } from '@uniformdev/design-system'; | ||
| import { SetLocationValueDispatch } from '@uniformdev/mesh-sdk-react'; | ||
| import WithStylesVariables from '@/components/WithStylesVariables'; | ||
| import { ALLOW_COLOR_GROUP } from '@/constants'; | ||
| import { getGroupFromKey } from '@/utils'; | ||
| import Sketch from '@uiw/react-color-sketch'; | ||
|
|
||
| const CUSTOM_HEX_PREFIX = 'custom:'; | ||
|
|
||
| type ColorPaletteParamProps = { | ||
| value?: string; | ||
|
|
@@ -13,6 +16,7 @@ type ColorPaletteParamProps = { | |
| colors: NonNullable<Type.KVStorage['colors']>; | ||
| selectedGroup?: string; | ||
| allowColors?: string[]; | ||
| allowCustomHex?: boolean; | ||
| }; | ||
|
|
||
| const ColorPaletteParam: FC<ColorPaletteParamProps> = ({ | ||
|
|
@@ -22,7 +26,12 @@ const ColorPaletteParam: FC<ColorPaletteParamProps> = ({ | |
| colors = [], | ||
| selectedGroup, | ||
| allowColors, | ||
| allowCustomHex, | ||
| }) => { | ||
| const isCustomHexValue = value?.startsWith(CUSTOM_HEX_PREFIX) ?? false; | ||
| const customHexColor = isCustomHexValue && value ? value.slice(CUSTOM_HEX_PREFIX.length) : undefined; | ||
| const [showPicker, setShowPicker] = useState(false); | ||
|
|
||
| const availableItems = useMemo(() => { | ||
| if (allowColors?.length) { | ||
| return colors.filter(({ colorKey }) => allowColors.includes(colorKey)); | ||
|
|
@@ -35,12 +44,27 @@ const ColorPaletteParam: FC<ColorPaletteParamProps> = ({ | |
| const handleSelection = useCallback( | ||
| (event: ChangeEvent<HTMLInputElement>) => { | ||
| const selected = event.currentTarget.value; | ||
| setShowPicker(false); | ||
| setValue(prev => ({ newValue: prev === selected ? null : selected })); | ||
| }, | ||
| [setValue] | ||
| ); | ||
|
|
||
| const handleClear = useCallback(() => setValue(() => ({ newValue: null })), [setValue]); | ||
| const handlePickerChange = useCallback( | ||
| (color: { hex: string }) => { | ||
| setValue(() => ({ newValue: `${CUSTOM_HEX_PREFIX}${color.hex}` })); | ||
| }, | ||
| [setValue] | ||
| ); | ||
|
|
||
| const handleCustomSquareClick = useCallback(() => { | ||
| setShowPicker(prev => !prev); | ||
| }, []); | ||
|
|
||
| const handleClear = useCallback(() => { | ||
| setShowPicker(false); | ||
| setValue(() => ({ newValue: null })); | ||
| }, [setValue]); | ||
|
|
||
| if (!colors.length) { | ||
| return ( | ||
|
|
@@ -97,9 +121,33 @@ const ColorPaletteParam: FC<ColorPaletteParamProps> = ({ | |
| </label> | ||
| ); | ||
| })} | ||
| {allowCustomHex && ( | ||
| <button | ||
| type="button" | ||
| onClick={handleCustomSquareClick} | ||
| className={classNames( | ||
| 'relative size-8 rounded-sm border cursor-pointer', | ||
| 'hover:outline hover:outline-2 hover:outline-accent-dark-hover', | ||
| 'flex items-center justify-center', | ||
| isCustomHexValue ? 'outline outline-2 outline-accent-dark border-white' : 'border-dashed border-gray-400' | ||
| )} | ||
| style={customHexColor ? { backgroundColor: customHexColor } : undefined} | ||
| title="Custom hex color" | ||
| > | ||
| {!customHexColor && <span className="text-sm font-bold text-gray-400">?</span>} | ||
| </button> | ||
|
alexshyba marked this conversation as resolved.
|
||
| )} | ||
| </div> | ||
| {allowCustomHex && showPicker && ( | ||
| <div className="mt-2 flex flex-col items-start gap-1.5"> | ||
| <Sketch color={customHexColor || '#000000'} onChange={handlePickerChange} /> | ||
| <Button buttonType="secondary" size="sm" onClick={() => setShowPicker(false)}> | ||
| Accept | ||
| </Button> | ||
|
Comment on lines
+143
to
+146
|
||
| </div> | ||
| )} | ||
| <div className="mt-2 flex items-center justify-between"> | ||
| <span className="h-6 truncate">{value}</span> | ||
| <span className="h-6 truncate">{isCustomHexValue ? customHexColor : value}</span> | ||
| <button | ||
| // eslint-disable-next-line tailwindcss/no-custom-classname | ||
| className="text-action-destructive-default disabled:text-gray-400" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handlePickerChangepersists custom colors as acustom:#RRGGBBstring. However, the property-editor JSON schema for color palette values currently usesz.enum(availableColorKeys), which will reject anycustom:values. Please update the schema/handler to allow acustom:-prefixed hex value whenallowCustomHexis enabled (e.g., a union of the enum and a hex-regex string), otherwise this new feature will break validation/AI edit output for color palette params.