Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added color picker component

Damian Legawiec4475bbbe48e4
diff --git a/packages/admin/package.json b/packages/admin/package.json
index 46c6585bd3..4d2c5dc0ae 100644
--- a/packages/admin/package.json
+++ b/packages/admin/package.json
@@ -41,6 +41,7 @@
"lucide-react": "^1.14.0",
"next-themes": "^0.4.6",
"react": "^19.2.6",
+ "react-colorful": "^5.7.0",
"react-day-picker": "^9.14.0",
"react-dom": "^19.2.6",
"react-hook-form": "^7.75.0",
diff --git a/packages/admin/src/components/spree/color-picker.tsx b/packages/admin/src/components/spree/color-picker.tsx
new file mode 100644
index 0000000000..222f4563e5
--- /dev/null
+++ b/packages/admin/src/components/spree/color-picker.tsx
@@ -0,0 +1,196 @@
+import { useEffect, useRef, useState } from 'react'
+import { HexColorPicker } from 'react-colorful'
+import { Input } from '@/components/ui/input'
+import { cn } from '@/lib/utils'
+
+/** Matches the server-side validation in `Spree::OptionValue#color_code`. */
+const HEX_RE = /^#?[0-9A-Fa-f]{6}([0-9A-Fa-f]{2})?$/
+
+function normalizeHex(input: string | null | undefined): string | null {
+ if (!input) return null
+ const trimmed = input.trim()
+ if (!trimmed) return null
+ const withHash = trimmed.startsWith('#') ? trimmed : `#${trimmed}`
+ return HEX_RE.test(withHash) ? withHash.toUpperCase() : null
+}
+
+export interface ColorPickerProps {
+ /** Hex value with `#` prefix (e.g. `#FF0000`). `null` / `undefined` / `''` for empty. */
+ value: string | null | undefined
+ /** Fires with a normalized `#RRGGBB` value, or `null` when cleared. */
+ onChange: (value: string | null) => void
+ /** Disables the trigger and locks the panel closed. */
+ disabled?: boolean
+ /** Forwarded to the text input for label association. */
+ id?: string
+ /** Forwarded to the text input. */
+ 'aria-invalid'?: boolean
+ className?: string
+ placeholder?: string
+ /**
+ * Hides the inline hex input — the swatch button alone becomes the trigger
+ * and the hex input moves into the picker panel. Use in dense surfaces
+ * (table cells, repeated row editors) where the inline input would crowd
+ * the layout. Default `false`.
+ */
+ compact?: boolean
+ /**
+ * Which edge of the swatch the panel anchors to. `start` (default) grows
+ * the panel rightward from the swatch's left edge; `end` grows it leftward
+ * from the swatch's right edge — use when the swatch sits near the right
+ * edge of its container (e.g. the rightmost cells in a table).
+ */
+ panelAlign?: 'start' | 'end'
+}
+
+/**
+ * Hex color picker with a swatch trigger and a free-form text input.
+ *
+ * The picker panel is rendered **inline** (not via a portal) because Base UI's
+ * `<Popover>` Portal mechanism interacts poorly with deeply-nested portal trees
+ * (e.g. when the picker lives inside a `<Sheet>` inside a `<TableRow>` inside a
+ * `useSortable` context — the Popup component silently fails to mount). The
+ * inline approach uses an absolute-positioned panel with click-outside +
+ * Escape-to-close handling.
+ */
+export function ColorPicker({
+ value,
+ onChange,
+ disabled,
+ id,
+ 'aria-invalid': ariaInvalid,
+ className,
+ placeholder = '#000000',
+ compact = false,
+ panelAlign = 'start',
+}: ColorPickerProps) {
+ const normalizedValue = normalizeHex(value)
+ const [open, setOpen] = useState(false)
+ // Local text mirrors `value` but lets the user type freely (including
+ // invalid intermediate states like `#FF`) without bouncing through onChange.
+ const [text, setText] = useState(normalizedValue ?? '')
+ const containerRef = useRef<HTMLDivElement | null>(null)
+
+ // Resync when the form-level value changes (e.g. when the picker writes to
+ // a different row, or when the form resets after a save).
+ useEffect(() => {
+ setText(normalizedValue ?? '')
+ }, [normalizedValue])
+
+ // `pointerdown` (not `click`) fires before the trigger's own click, matching
+ // popover-library behavior so reopening doesn't double-toggle.
+ useEffect(() => {
+ if (!open) return
+ function handle(event: PointerEvent) {
+ const target = event.target as Node | null
+ if (!target || !containerRef.current) return
+ if (!containerRef.current.contains(target)) {
+ setOpen(false)
+ }
+ }
+ function handleKey(event: KeyboardEvent) {
+ if (event.key === 'Escape') setOpen(false)
+ }
+ document.addEventListener('pointerdown', handle)
+ document.addEventListener('keydown', handleKey)
+ return () => {
+ document.removeEventListener('pointerdown', handle)
+ document.removeEventListener('keydown', handleKey)
+ }
+ }, [open])
+
+ function commitText(raw: string) {
+ const normalized = normalizeHex(raw)
+ if (normalized) {
+ onChange(normalized)
+ setText(normalized)
+ } else if (raw.trim() === '') {
+ onChange(null)
+ setText('')
+ }
+ // else: invalid input — keep the text, don't propagate
+ }
+
+ const hexInput = (
+ <Input
+ id={id}
+ type="text"
+ inputMode="text"
+ autoComplete="off"
+ spellCheck={false}
+ value={text}
+ placeholder={placeholder}
+ disabled={disabled}
+ aria-invalid={ariaInvalid}
+ className="font-mono uppercase"
+ onChange={(e) => setText(e.target.value)}
+ onBlur={(e) => commitText(e.target.value)}
+ onKeyDown={(e) => {
+ if (e.key === 'Enter') {
+ e.preventDefault()
+ commitText(e.currentTarget.value)
+ }
+ }}
+ />
+ )
+
+ return (
+ <div ref={containerRef} className={cn('relative flex items-center gap-2', className)}>
+ <button
+ type="button"
+ disabled={disabled}
+ aria-label={normalizedValue ? `Change color (${normalizedValue})` : 'Pick a color'}
+ aria-haspopup="dialog"
+ aria-expanded={open}
+ onClick={() => setOpen((v) => !v)}
+ className={cn(
+ 'inline-flex size-10 shrink-0 cursor-pointer items-center justify-center rounded-md border border-border shadow-xs transition-shadow',
+ 'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring',
+ 'disabled:cursor-not-allowed disabled:opacity-50',
+ !normalizedValue && 'color-picker-empty-swatch',
+ )}
+ style={normalizedValue ? { backgroundColor: normalizedValue } : undefined}
+ />
+ {!compact && hexInput}
+ {open && (
+ <div
+ role="dialog"
+ aria-label="Pick a color"
+ className={cn(
+ 'absolute top-full z-50 mt-2 min-w-min rounded-lg border border-border bg-popover p-3 text-popover-foreground shadow-md',
+ panelAlign === 'end' ? 'right-0' : 'left-0',
+ )}
+ >
+ <HexColorPicker
+ color={normalizedValue ?? '#000000'}
+ onChange={(next) => {
+ const normalized = normalizeHex(next)
+ if (normalized) {
+ setText(normalized)
+ onChange(normalized)
+ }
+ }}
+ />
+ {compact && <div className="mt-3">{hexInput}</div>}
+ <div className="mt-3 flex items-center justify-between gap-2">
+ <span className="font-mono text-xs text-muted-foreground">
+ {normalizedValue ?? 'No color'}
+ </span>
+ {normalizedValue && (
+ <button
+ type="button"
+ onClick={() => {
+ setText('')
+ onChange(null)
+ }}
+ className="text-xs text-muted-foreground underline-offset-2 hover:underline"
+ >
+ Clear
+ </button>
+ )}
+ </div>
+ </div>
+ )}
+ </div>
+ )
+}
diff --git a/packages/admin/src/index.css b/packages/admin/src/index.css
index e9106ad44e..d46dc32587 100644
--- a/packages/admin/src/index.css
+++ b/packages/admin/src/index.css
@@ -270,3 +270,22 @@
@apply text-base;
}
}
+
+@layer components {
+ /* Checkerboard for the color picker's "no color selected" swatch.
+ Two-tone diagonal pattern using `--border` + `--background` so it's
+ visible in both light and dark themes. */
+ .color-picker-empty-swatch {
+ background-color: var(--background);
+ background-image: linear-gradient(45deg, var(--border) 25%, transparent 25%),
+ linear-gradient(-45deg, var(--border) 25%, transparent 25%),
+ linear-gradient(45deg, transparent 75%, var(--border) 75%),
+ linear-gradient(-45deg, transparent 75%, var(--border) 75%);
+ background-size: 10px 10px;
+ background-position:
+ 0 0,
+ 0 5px,
+ 5px -5px,
+ -5px 0;
+ }
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 184a480360..589a33c982 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -96,6 +96,9 @@ importers:
react:
specifier: ^19.2.6
version: 19.2.6
+ react-colorful:
+ specifier: ^5.7.0
react-day-picker:
specifier: ^9.14.0
version: 9.14.0([email protected])
@@ -3362,6 +3365,12 @@ packages:
resolution: {integrity: sha512-K5zQjDllxWkf7Z5xJdV0/B0WTNqx6vxG70zJE4N0kBs4LovmEYWJzQGxC9bS9RAKu3bgM40lrd5zoLJ12MQ5BA==}
engines: {node: '>= 0.10'}
+ resolution: {integrity: sha512-fuesYIemttah97XmsIHmz4OORDHiSFzyc9HMAIrCHJou2jaRQmL8cFJ76K4zQhhj8jzwOBlOi4BaGTjjOZCfTg==}
+ peerDependencies:
+ react: '>=16.8.0'
+ react-dom: '>=16.8.0'
+
resolution: {integrity: sha512-tBaoDWjPwe0M5pGrum4H0SR6Lyk+BO9oHnp9JbKpGKW2mlraNPgP9BMfsg5pWpwrssARmeqk7YBl2oXutZTaHA==}
engines: {node: '>=18'}
@@ -6912,6 +6921,11 @@ snapshots:
iconv-lite: 0.7.2
unpipe: 1.0.0
+ dependencies:
+ react: 19.2.6
+ react-dom: 19.2.6([email protected])
+
dependencies:
'@date-fns/tz': 1.4.1