import * as React from 'react'; import clsx from 'clsx'; import Color from 'color'; import * as convert from 'color-convert'; /** * Derived HTML element of the {@link Swatch} component. */ export type SwatchDerivedElement = HTMLInputElement; type ColorValue = ConstructorParameters[0]; type ColorMode = keyof typeof convert; /** * Props of the {@link Swatch} component. */ export interface SwatchProps extends Omit, 'color'> { color: NonNullable; mode?: ColorMode; } export const useSwatchControls = () => { const id = React.useId(); const copyColor: React.ReactEventHandler = React.useCallback(async (e) => { const { value } = e.currentTarget; await window.navigator.clipboard.writeText(value); }, []); return React.useMemo(() => ({ id, copyColor, }), [id, copyColor]); }; /** * Component for displaying a color. */ export const Swatch = React.forwardRef(({ // todo unify color and mode into one "value" attribute color, mode = 'rgb', className, style, ...etcProps }, forwardedRef) => { const { id, copyColor } = useSwatchControls(); const colorInternal = React.useMemo(() => new Color(color, mode), [color, mode]); const colorValue = colorInternal.hex(); return ( ); }); Swatch.displayName = 'Swatch'; Swatch.defaultProps = { mode: 'rgb', };