Color Picker
payload-extra-fields/color-pickerFreeItem
A customizable color picker field for PayloadCMS.
Install it
npx shadcn@latest add https://payload.veiag.dev/r/color-picker.jsonSource
1'use client'2import {3 FieldDescription,4 FieldError,5 FieldLabel,6 TextInput,7 useField,8 useTranslation,9} from '@payloadcms/ui'10import { DefaultCellComponentProps, TextFieldClient, TextFieldClientProps } from 'payload'11import React, { useCallback, useEffect, useRef, useState } from 'react'12import './style.scss'13import { ColorPickerProps } from './types'1415const ColorPicker: React.FC<TextFieldClientProps & ColorPickerProps> = ({16 path,17 field,18 colorPresets,19 debounceDelay = 300,20 showTextInput,21 readOnly,22}) => {23 const { setValue, value, disabled, showError } = useField<string>({ path: path || field.name })24 const [localValue, setLocalValue] = useState(value || '')25 const colorInputRef = useRef<HTMLInputElement>(null)26 const debounceTimerRef = useRef<NodeJS.Timeout | null>(null)2728 const actualReadOnly = readOnly || field.admin?.readOnly || false2930 // Sync external value changes to local state31 useEffect(() => {32 if (value !== localValue) {33 setLocalValue(value || '')34 }35 }, [value])3637 // Debounced value update38 const debouncedSetValue = useCallback(39 (newValue: string) => {40 if (debounceTimerRef.current) {41 clearTimeout(debounceTimerRef.current)42 }4344 debounceTimerRef.current = setTimeout(() => {45 setValue(newValue)46 }, debounceDelay)47 },48 [setValue, debounceDelay],49 )5051 // Cleanup debounce timer on unmount52 useEffect(() => {53 return () => {54 if (debounceTimerRef.current) {55 clearTimeout(debounceTimerRef.current)56 }57 }58 }, [])5960 const handleColorChange = (e: React.ChangeEvent<HTMLInputElement>) => {61 const newColor = e.target.value62 setLocalValue(newColor)63 debouncedSetValue(newColor)64 }6566 const handlePresetClick = (color: string) => {67 if (actualReadOnly) return68 setLocalValue(color)69 setValue(color) // Immediate update for preset clicks70 if (colorInputRef.current) {71 colorInputRef.current.value = color72 }73 }7475 const handleTextInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {76 let newColor = e.target.value7778 // Auto-add # if missing79 if (newColor && !newColor.startsWith('#')) {80 newColor = `#${newColor}`81 }8283 setLocalValue(newColor)84 //Update value with debounce, validation will handle incorrect formats85 debouncedSetValue(newColor)86 }8788 const isValidHex = /^#[0-9A-Fa-f]{6}$/.test(localValue)8990 return (91 <div className="color-picker">92 <FieldLabel93// … truncated
Files it writes
More from payload-extra-fields
All 4 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Custom Array Row Labelarray-row-label | payload-extra-fields | Other | Free | no deps · 3 files | |
| Lucide Icon Pickerlucide-icon-picker | payload-extra-fields | Other | Free | 4 deps · 5 files | |
| Slug Fieldslug | payload-extra-fields | Other | Free | no deps · 6 files |
