// // Copyright 2026 DXOS.org // // @import-as-namespace import { type Color, ColorPicker as ColorPickerPrimitive, parseColor, useColorPickerContext as useArkColorPickerContext, } from '@ark-ui/react/color-picker'; import { Portal } from '@ark-ui/react/portal'; import React, { type KeyboardEvent, type PropsWithChildren, type ReactNode, type RefObject, forwardRef, useEffect, useLayoutEffect, useMemo, useRef, useState, } from 'react'; import { createContext } from '@dxos/react-hooks'; import { mx } from '@dxos/ui-theme'; import { type ChromaticPalette, type NeutralPalette, type ThemedClassName, hues as allHues } from '@dxos/ui-types'; import { recipes } from '../../recipes.ts'; import { Button } from '../Button/Button.tsx'; import { Input } from '../Input/Input.tsx'; import * as Tabs from '../Tabs/Tabs.tsx'; import * as Toolbar from '../Toolbar/Toolbar.tsx'; // // Root // /** Shown in the picker when the value is unset or is not a color the picker can parse. */ const FALLBACK = '#808080'; const toColor = (value: string | undefined): Color => { try { return parseColor(value ?? FALLBACK); } catch { return parseColor(FALLBACK); } }; type ColorPickerContextValue = { /** Any CSS color string, as typed; undefined when unset. */ value?: string; onValueChange?: (value: string | undefined) => void; /** Called when a pick in the popover ends (a drag released), so a caller can save once rather than per move. */ onValueCommit?: (value: string | undefined) => void; disabled?: boolean; }; const COLOR_PICKER_NAME = 'ColorPicker'; const [ColorPickerProvider, useColorPickerContext] = createContext(COLOR_PICKER_NAME); type ColorPickerRootProps = PropsWithChildren; /** * A color value edited as text (any CSS color) or picked from the trigger's popover. The picker's machine lives here * (in a box with no layout of its own) so the trigger can be a bare button in an Input's end adornment. */ const ColorPickerRoot = ({ children, ...context }: ColorPickerRootProps) => { const { value, onValueChange, onValueCommit, disabled } = context; const color = useMemo(() => toColor(value), [value]); return ( onValueChange?.(next.toString('hex'))} onValueChangeEnd={({ value: next }) => onValueCommit?.(next.toString('hex'))} positioning={{ placement: 'bottom-end' }} className='contents' > {children} ); }; ColorPickerRoot.displayName = 'ColorPicker.Root'; // // Input // type ColorPickerInputProps = ThemedClassName<{ 'placeholder'?: string; /** The input's end adornment, typically `ColorPicker.Trigger`. */ 'end'?: ReactNode; 'data-testid'?: string; }>; /** The color as text, so a value the swatch cannot show (a named or `oklch` color) can still be typed. */ const ColorPickerInput = forwardRef( ({ classNames, placeholder, end, ...props }, forwardedRef) => { const { value, onValueChange, disabled } = useColorPickerContext(COLOR_PICKER_NAME); return ( onValueChange?.(event.target.value.trim() || undefined)} end={end} classNames={classNames} /> ); }, ); ColorPickerInput.displayName = 'ColorPicker.Input'; // // Trigger // /** A swatch row: a theme hue or the neutral ramp. */ type SwatchHue = ChromaticPalette | NeutralPalette; /** The theme's tones the swatches offer by default, light to dark. */ const DEFAULT_TONES = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950]; /** The rows the swatches offer by default: neutral, then every theme hue. */ const DEFAULT_HUES: SwatchHue[] = ['neutral', ...allHues]; /** A popover view: the saturation/brightness area with a hue slider, or the theme swatch grid. */ type ColorPickerView = 'wheel' | 'swatches'; const DEFAULT_VIEWS: ColorPickerView[] = ['wheel', 'swatches']; type ColorPickerTriggerProps = ThemedClassName<{ label?: string; /** Names the button at the end of the tab row that clears the value back to unset. */ resetLabel?: string; /** Rows of the swatch grid; neutral, then every theme hue, by default. */ hues?: SwatchHue[]; /** Columns of the swatch grid: theme tones (e.g. `500`), light to dark. */ tones?: number[]; /** The views the popover offers, as tabs; with one, it shows that view alone (e.g. swatches only). Both by default. */ views?: ColorPickerView[]; }>; /** * A swatch of the current color that opens two tabs: a wheel (a saturation/brightness area and a hue slider, which * write hex) and a grid of the theme's hues by tone. */ const ColorPickerTrigger = ({ classNames, label = 'Pick color', resetLabel = 'Reset', hues, tones, views = DEFAULT_VIEWS, }: ColorPickerTriggerProps) => { const { value, onValueChange, onValueCommit } = useColorPickerContext(COLOR_PICKER_NAME); return ( <> {/* A direct child of the adornment, so it sizes, hovers and sits flush at the end like every Input end button. */}