@sanity/color-input 4.0.0 → 4.0.1-canary.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var jsxRuntime = require("react/jsx-runtime"), icons = require("@sanity/icons"), ui = require("@sanity/ui"), react = require("react"), sanity = require("sanity"), useEffectEvent = require("use-effect-event"), index = require("./index.js");
3
+ const DEFAULT_COLOR = {
4
+ hex: "#24a3e3",
5
+ hsl: { h: 200, s: 0.7732, l: 0.5156, a: 1 },
6
+ hsv: { h: 200, s: 0.8414, v: 0.8901, a: 1 },
7
+ rgb: { r: 46, g: 163, b: 227, a: 1 },
8
+ source: "hex"
9
+ };
10
+ function ColorInput(props) {
11
+ const { onChange, readOnly } = props, value = props.value, type = props.schemaType, focusRef = react.useRef(null), [color, setColor] = react.useState(value);
12
+ react.useEffect(() => react.startTransition(() => setColor(value)), [value]);
13
+ const [emitColor, setEmitColor] = react.useState(void 0), debouncedColor = react.useDeferredValue(emitColor), handleChange = useEffectEvent.useEffectEvent((nextColor) => {
14
+ const fieldPatches = type.fields.filter((field) => field.name in nextColor).map((field) => {
15
+ const nextFieldValue = nextColor[field.name], isObject = field.type.jsonType === "object";
16
+ return sanity.set(
17
+ isObject ? Object.assign({ _type: field.type.name }, nextFieldValue) : nextFieldValue,
18
+ [field.name]
19
+ );
20
+ });
21
+ onChange([
22
+ sanity.setIfMissing({ _type: type.name }),
23
+ sanity.set(type.name, ["_type"]),
24
+ sanity.set(nextColor.rgb?.a, ["alpha"]),
25
+ ...fieldPatches
26
+ ]);
27
+ });
28
+ react.useEffect(() => {
29
+ if (!debouncedColor) return;
30
+ const raf = requestAnimationFrame(() => handleChange(debouncedColor));
31
+ return () => cancelAnimationFrame(raf);
32
+ }, [debouncedColor, handleChange]);
33
+ const handleCreateColor = react.useCallback(() => {
34
+ setColor(DEFAULT_COLOR), setEmitColor(DEFAULT_COLOR);
35
+ }, []), handleColorChange = react.useCallback((nextColor) => {
36
+ setColor(nextColor), setEmitColor(nextColor);
37
+ }, []), handleUnset = react.useCallback(() => {
38
+ setColor(void 0), onChange(sanity.unset());
39
+ }, [onChange]);
40
+ return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: value && value.hex ? /* @__PURE__ */ jsxRuntime.jsx(
41
+ index.ColorPicker,
42
+ {
43
+ color,
44
+ onChange: handleColorChange,
45
+ readOnly: readOnly || typeof type.readOnly == "boolean" && type.readOnly,
46
+ disableAlpha: !!type.options?.disableAlpha,
47
+ colorList: type.options?.colorList,
48
+ onUnset: handleUnset
49
+ }
50
+ ) : /* @__PURE__ */ jsxRuntime.jsx(
51
+ ui.Button,
52
+ {
53
+ icon: icons.AddIcon,
54
+ mode: "ghost",
55
+ text: "Create color",
56
+ ref: focusRef,
57
+ disabled: !!readOnly,
58
+ onClick: handleCreateColor
59
+ }
60
+ ) });
61
+ }
62
+ exports.default = ColorInput;
63
+ //# sourceMappingURL=ColorInput.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ColorInput.js","sources":["../../src/ColorInput.tsx"],"sourcesContent":["import {AddIcon} from '@sanity/icons'\nimport {Button} from '@sanity/ui'\nimport {startTransition, useCallback, useDeferredValue, useEffect, useRef, useState} from 'react'\nimport type {Color, HSLColor, HSVColor, RGBColor} from 'react-color'\nimport {\n type ObjectInputProps,\n type ObjectOptions,\n type ObjectSchemaType,\n set,\n setIfMissing,\n unset,\n} from 'sanity'\nimport {useEffectEvent} from 'use-effect-event'\n\nimport {ColorPicker} from './ColorPicker'\n\nexport interface ColorValue {\n hex: string\n hsl: HSLColor\n hsv: HSVColor\n rgb: RGBColor\n}\n\nconst DEFAULT_COLOR: ColorValue & {source: string} = {\n hex: '#24a3e3',\n hsl: {h: 200, s: 0.7732, l: 0.5156, a: 1},\n hsv: {h: 200, s: 0.8414, v: 0.8901, a: 1},\n rgb: {r: 46, g: 163, b: 227, a: 1},\n source: 'hex',\n}\n\nexport interface ColorOptions extends Omit<ObjectOptions, 'columns'> {\n disableAlpha?: boolean\n colorList?: Array<Color>\n}\n\nexport type ColorSchemaType = Omit<ObjectSchemaType, 'options'> & {\n options?: ColorOptions\n}\nexport type ColorInputProps = ObjectInputProps<ColorValue, ColorSchemaType>\n\nexport default function ColorInput(props: ObjectInputProps) {\n const {onChange, readOnly} = props\n const value = props.value as ColorValue | undefined\n const type = props.schemaType as ColorSchemaType\n const focusRef = useRef<HTMLButtonElement>(null)\n\n // use local state so we can have instant ui updates while debouncing patch emits\n const [color, setColor] = useState(value)\n // Marking the `setColor` in a transition allows React to interrupt render should the user start dragging the input before React is finished rendering\n useEffect(() => startTransition(() => setColor(value)), [value])\n\n // The color picker emits onChange events continuously while the user is sliding the\n // hue/saturation/alpha selectors. This debounces the event to avoid excessive patches\n // and massively improve render performance and avoid jank\n const [emitColor, setEmitColor] = useState<typeof value>(undefined)\n const debouncedColor = useDeferredValue(emitColor)\n const handleChange = useEffectEvent((nextColor: ColorValue) => {\n const fieldPatches = type.fields\n .filter((field) => field.name in nextColor)\n .map((field) => {\n const nextFieldValue = nextColor[field.name as keyof ColorValue]\n const isObject = field.type.jsonType === 'object'\n return set(\n isObject ? Object.assign({_type: field.type.name}, nextFieldValue) : nextFieldValue,\n [field.name],\n )\n })\n\n onChange([\n setIfMissing({_type: type.name}),\n set(type.name, ['_type']),\n set(nextColor.rgb?.a, ['alpha']),\n ...fieldPatches,\n ])\n })\n useEffect(() => {\n if (!debouncedColor) return undefined\n const raf = requestAnimationFrame(() => handleChange(debouncedColor))\n return () => cancelAnimationFrame(raf)\n }, [debouncedColor, handleChange])\n\n const handleCreateColor = useCallback(() => {\n setColor(DEFAULT_COLOR)\n setEmitColor(DEFAULT_COLOR)\n }, [])\n\n const handleColorChange = useCallback((nextColor: ColorValue) => {\n setColor(nextColor)\n setEmitColor(nextColor)\n }, [])\n\n const handleUnset = useCallback(() => {\n setColor(undefined)\n onChange(unset())\n }, [onChange])\n\n return (\n <>\n {value && value.hex ? (\n <ColorPicker\n color={color}\n onChange={handleColorChange}\n readOnly={readOnly || (typeof type.readOnly === 'boolean' && type.readOnly)}\n disableAlpha={!!type.options?.disableAlpha}\n colorList={type.options?.colorList}\n onUnset={handleUnset}\n />\n ) : (\n <Button\n icon={AddIcon}\n mode=\"ghost\"\n text=\"Create color\"\n ref={focusRef}\n disabled={Boolean(readOnly)}\n onClick={handleCreateColor}\n />\n )}\n </>\n )\n}\n"],"names":["useRef","useState","useEffect","startTransition","useDeferredValue","useEffectEvent","set","setIfMissing","useCallback","unset","jsx","Fragment","ColorPicker","Button","AddIcon"],"mappings":";;AAuBA,MAAM,gBAA+C;AAAA,EACnD,KAAK;AAAA,EACL,KAAK,EAAC,GAAG,KAAK,GAAG,QAAQ,GAAG,QAAQ,GAAG,EAAC;AAAA,EACxC,KAAK,EAAC,GAAG,KAAK,GAAG,QAAQ,GAAG,QAAQ,GAAG,EAAC;AAAA,EACxC,KAAK,EAAC,GAAG,IAAI,GAAG,KAAK,GAAG,KAAK,GAAG,EAAC;AAAA,EACjC,QAAQ;AACV;AAYA,SAAwB,WAAW,OAAyB;AACpD,QAAA,EAAC,UAAU,aAAY,OACvB,QAAQ,MAAM,OACd,OAAO,MAAM,YACb,WAAWA,MAAAA,OAA0B,IAAI,GAGzC,CAAC,OAAO,QAAQ,IAAIC,eAAS,KAAK;AAE9BC,kBAAA,MAAMC,sBAAgB,MAAM,SAAS,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC;AAK/D,QAAM,CAAC,WAAW,YAAY,IAAIF,eAAuB,MAAS,GAC5D,iBAAiBG,MAAAA,iBAAiB,SAAS,GAC3C,eAAeC,eAAA,eAAe,CAAC,cAA0B;AAC7D,UAAM,eAAe,KAAK,OACvB,OAAO,CAAC,UAAU,MAAM,QAAQ,SAAS,EACzC,IAAI,CAAC,UAAU;AACR,YAAA,iBAAiB,UAAU,MAAM,IAAwB,GACzD,WAAW,MAAM,KAAK,aAAa;AAClC,aAAAC,OAA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