|
| 1 | +import {createEffect, createSignal, onCleanup, onMount, Show} from "solid-js"; |
| 2 | +import iro from "@jaames/iro"; |
| 3 | +import Icon from "../icons/Icon"; |
| 4 | +import PenToSquare from "../icons/svg/PenToSquare"; |
| 5 | +import Palette from "../icons/svg/Palette"; |
| 6 | + |
| 7 | +interface BaseProps { |
| 8 | + width?: number |
| 9 | + placement?: 'right' | 'bottom' |
| 10 | + title?: string |
| 11 | + swatchClass?: string |
| 12 | + iconClass?: string |
| 13 | +} |
| 14 | + |
| 15 | +interface NullableProps extends BaseProps { |
| 16 | + nullable: true |
| 17 | + removeLabel?: string |
| 18 | + color: string | null |
| 19 | + onChange: (color: string | null) => void |
| 20 | +} |
| 21 | + |
| 22 | +interface NonNullableProps extends BaseProps { |
| 23 | + nullable?: false |
| 24 | + color: string |
| 25 | + onChange: (color: string) => void |
| 26 | +} |
| 27 | + |
| 28 | +type Props = NullableProps | NonNullableProps |
| 29 | + |
| 30 | +export default function ColorPicker(props: Props) { |
| 31 | + const width = () => props.width ?? 150 |
| 32 | + const placement = () => props.placement ?? 'right' |
| 33 | + |
| 34 | + const fg = (hexColor: string | null) => { |
| 35 | + if (!hexColor) return 'fill-fg' |
| 36 | + const r = parseInt(hexColor.slice(1, 3), 16) |
| 37 | + const g = parseInt(hexColor.slice(3, 5), 16) |
| 38 | + const b = parseInt(hexColor.slice(5, 7), 16) |
| 39 | + return (r * 0.299 + g * 0.587 + b * 0.114) > 186 ? 'fill-black' : 'fill-white' |
| 40 | + } |
| 41 | + |
| 42 | + let colorPickerRef: HTMLDivElement | null = null |
| 43 | + const [colorPicker, setColorPicker] = createSignal<iro.ColorPicker>() |
| 44 | + |
| 45 | + onMount(() => { |
| 46 | + setColorPicker(iro.ColorPicker(colorPickerRef!, { |
| 47 | + width: width(), |
| 48 | + color: props.color ?? '#000000', |
| 49 | + layout: [ |
| 50 | + {component: iro.ui.Box}, |
| 51 | + {component: iro.ui.Slider, options: {sliderType: 'hue'}}, |
| 52 | + ], |
| 53 | + sliderSize: 12, |
| 54 | + sliderMargin: 8, |
| 55 | + })) |
| 56 | + }) |
| 57 | + |
| 58 | + createEffect(() => { |
| 59 | + const picker = colorPicker() |
| 60 | + const hex = props.color ?? '#000000' |
| 61 | + if (picker && picker.color.hexString.toLowerCase() !== hex.toLowerCase()) |
| 62 | + picker.setColors([hex]) |
| 63 | + }) |
| 64 | + |
| 65 | + const colorPickerHandler = (color: iro.Color) => { |
| 66 | + props.onChange(color.hexString as any) |
| 67 | + } |
| 68 | + createEffect(() => colorPicker()?.on('color:change', colorPickerHandler)) |
| 69 | + onCleanup(() => colorPicker()?.off('color:change', colorPickerHandler)) |
| 70 | + |
| 71 | + let pickerAreaRef: HTMLDivElement | null = null |
| 72 | + const [showColorPicker, setShowColorPicker] = createSignal(false) |
| 73 | + const outsideClickListener = (e: MouseEvent) => { |
| 74 | + if (pickerAreaRef && !(pickerAreaRef as any).contains(e.target as Node)) |
| 75 | + setShowColorPicker(false) |
| 76 | + } |
| 77 | + onMount(() => document.addEventListener('click', outsideClickListener, true)) |
| 78 | + onCleanup(() => document.removeEventListener('click', outsideClickListener, true)) |
| 79 | + |
| 80 | + const [hexInput, setHexInput] = createSignal(props.color ?? '') |
| 81 | + createEffect(() => setHexInput(props.color ?? '')) |
| 82 | + |
| 83 | + const panelPositionClass = () => |
| 84 | + placement() === 'right' ? 'left-full top-0 mx-2' : 'top-full left-0 mt-2' |
| 85 | + |
| 86 | + return ( |
| 87 | + <div ref={pickerAreaRef!} class="flex flex-col items-center self-start relative"> |
| 88 | + <button |
| 89 | + type="button" |
| 90 | + class={`rounded-xl border-2 border-fg/20 group/swatch flex items-center justify-center ${props.swatchClass ?? 'w-14 h-14'}`} |
| 91 | + style={{"background-color": props.color ?? 'transparent'}} |
| 92 | + onClick={() => setShowColorPicker(p => !p)} |
| 93 | + > |
| 94 | + <Icon |
| 95 | + icon={props.color ? PenToSquare : Palette} |
| 96 | + class="w-6 h-6 transition-opacity group-hover/swatch:opacity-100" |
| 97 | + classList={{ |
| 98 | + [fg(props.color)]: true, |
| 99 | + [props.iconClass ?? (props.color == null ? 'opacity-70' : 'opacity-25')]: true, |
| 100 | + }} |
| 101 | + title={props.title} |
| 102 | + /> |
| 103 | + </button> |
| 104 | + |
| 105 | + <Show when={props.nullable && props.color != null && props.removeLabel}> |
| 106 | + <button |
| 107 | + type="button" |
| 108 | + class="text-xs text-fg/50 hover:text-fg/100 transition mt-1" |
| 109 | + onClick={() => (props as NullableProps).onChange(null)} |
| 110 | + > |
| 111 | + {(props as NullableProps).removeLabel} |
| 112 | + </button> |
| 113 | + </Show> |
| 114 | + |
| 115 | + <div |
| 116 | + class={`bg-bg-0/80 backdrop-blur rounded-xl p-4 absolute z-[200] transition-opacity ${panelPositionClass()}`} |
| 117 | + classList={{ |
| 118 | + 'opacity-0 pointer-events-none': !showColorPicker(), |
| 119 | + 'opacity-100': showColorPicker(), |
| 120 | + }} |
| 121 | + > |
| 122 | + <div ref={colorPickerRef!} /> |
| 123 | + <input |
| 124 | + class="mt-2 w-full bg-0 rounded-lg text-sm font-light py-1 px-2 outline-none focus:ring-2 ring-accent" |
| 125 | + value={hexInput()} |
| 126 | + placeholder="#abcdef" |
| 127 | + onInput={(e) => { |
| 128 | + let candidate = e.currentTarget.value |
| 129 | + setHexInput(candidate) |
| 130 | + if (!candidate.startsWith('#')) candidate = '#' + candidate |
| 131 | + if (/^#[0-9A-Fa-f]{6}$/.test(candidate)) |
| 132 | + props.onChange(candidate as any) |
| 133 | + }} |
| 134 | + maxLength={7} |
| 135 | + /> |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + ) |
| 139 | +} |
0 commit comments