@sanity/color-input 3.0.0 → 3.0.2

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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Sanity.io
3
+ Copyright (c) 2023 Sanity.io
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -36,7 +36,7 @@ export declare interface ColorDefinition
36
36
  options?: ColorOptions
37
37
  }
38
38
 
39
- export declare function ColorInput(props: ColorInputProps): JSX.Element
39
+ export declare function ColorInput(props: ObjectInputProps): JSX.Element
40
40
 
41
41
  export declare const colorInput: Plugin_2<void>
42
42
 
@@ -83,7 +83,7 @@ export declare const rgbaColor: {
83
83
  export {}
84
84
 
85
85
  declare module '@sanity/types' {
86
- interface IntrinsicDefinitions {
87
- color: ColorDefinition;
88
- }
89
- }
86
+ interface IntrinsicDefinitions {
87
+ color: ColorDefinition
88
+ }
89
+ }
package/lib/index.esm.js CHANGED
@@ -1,2 +1,490 @@
1
- var e,t;function n(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);t&&(l=l.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,l)}return n}function l(e){for(var t=1;t<arguments.length;t++){var l=null!=arguments[t]?arguments[t]:{};t%2?n(Object(l),!0).forEach((function(t){r(e,t,l[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(l)):n(Object(l)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(l,t))}))}return e}function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){return t||(t=e.slice(0)),Object.freeze(Object.defineProperties(e,{raw:{value:Object.freeze(t)}}))}import{defineType as a,set as i,setIfMissing as s,unset as c,definePlugin as h}from"sanity";import{jsxs as d,jsx as u,Fragment as p}from"react/jsx-runtime";import{useMemo as b,useCallback as g,useRef as m,useState as f,useEffect as y}from"react";import{debounce as v}from"lodash";import{useTheme as x,Flex as O,Box as C,Card as w,Stack as j,Text as S,Inline as z,Button as A}from"@sanity/ui";import{TrashIcon as L,AddIcon as P}from"@sanity/icons";import{EditableInput as H,Saturation as M,Hue as k,Alpha as R,Checkboard as B}from"react-color/lib/components/common";import{CustomPicker as D}from"react-color";import N from"styled-components";import{isValidHex as T}from"react-color/lib/helpers/color";const G=a({title:"Hue Saturation Lightness",name:"hslaColor",type:"object",fields:[{name:"h",type:"number",title:"Hue"},{name:"s",type:"number",title:"Saturation"},{name:"l",type:"number",title:"Lightness"},{name:"a",type:"number",title:"Alpha"}]}),V=a({title:"Red Green Blue (rgb)",name:"rgbaColor",type:"object",fields:[{name:"r",type:"number",title:"Red"},{name:"g",type:"number",title:"Green"},{name:"b",type:"number",title:"Blue"},{name:"a",type:"number",title:"Alpha"}]}),_=e=>{let{onChange:t,rgb:n,hsl:l,hex:r,disableAlpha:o}=e;var a;const{sanity:i}=x(),s=b((()=>({input:{width:"80%",padding:"4px 10% 3px",border:"none",boxShadow:"inset 0 0 0 1px ".concat(i.color.input.default.enabled.border),color:i.color.input.default.enabled.fg,backgroundColor:i.color.input.default.enabled.bg,fontSize:i.fonts.text.sizes[0].fontSize,textAlign:"center"},label:{display:"block",textAlign:"center",fontSize:i.fonts.label.sizes[0].fontSize,color:i.color.base.fg,paddingTop:"3px",paddingBottom:"4px",textTransform:"capitalize"}})),[i]),c=g((e=>{if("hex"in e&&e.hex&&T(e.hex))t({hex:e.hex,source:"hex"});else if(n&&("r"in e&&e.r||"g"in e&&e.g||"b"in e&&e.b))t({r:Number(e.r)||n.r,g:Number(e.g)||n.g,b:Number(e.b)||n.b,a:n.a,source:"rgb"});else if(l&&"a"in e&&e.a){let n=Number(e.a);n<0?n=0:n>100&&(n=100),n/=100,t({h:l.h,s:l.s,l:l.l,a:n,source:"hsl"})}}),[t,l,n]);return d(O,{children:[u(C,{flex:2,marginRight:1,children:u(H,{style:s,label:"hex",value:null==r?void 0:r.replace("#",""),onChange:c})}),u(C,{flex:1,marginRight:1,children:u(H,{style:s,label:"r",value:null==n?void 0:n.r,onChange:c,dragLabel:!0,dragMax:255})}),u(C,{flex:1,marginRight:1,children:u(H,{style:s,label:"g",value:null==n?void 0:n.g,onChange:c,dragLabel:!0,dragMax:255})}),u(C,{flex:1,marginRight:1,children:u(H,{style:s,label:"b",value:null==n?void 0:n.b,onChange:c,dragLabel:!0,dragMax:255})}),!o&&u(C,{flex:1,children:u(H,{style:s,label:"a",value:Math.round(100*(null!=(a=null==n?void 0:n.a)?a:1)),onChange:c,dragLabel:!0,dragMax:100})})]})},E=N(C)(e||(e=o(["\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n"]))),U=N(O)(t||(t=o(["\n margin-top: 6rem;\n background-color: var(--card-bg-color);\n position: relative;\n width: 100%;\n"]))),W=D((e=>{var t,n,l;const{width:r,color:{rgb:o,hex:a,hsv:i,hsl:s},onChange:c,onUnset:h,disableAlpha:b,readOnly:g}=e;return s&&i?u("div",{style:{width:r},children:u(w,{padding:1,border:!0,radius:1,children:d(j,{space:2,children:[!g&&d(p,{children:[u(w,{overflow:"hidden",style:{position:"relative",height:"5em"},children:u(M,{onChange:c,hsl:s,hsv:i})}),u(w,{shadow:1,radius:3,overflow:"hidden",style:{position:"relative",height:"10px"},children:u(k,{hsl:s,onChange:!g&&c})}),!b&&u(w,{shadow:1,radius:3,overflow:"hidden",style:{position:"relative",height:"10px"},children:u(R,{rgb:o,hsl:s,onChange:c})})]}),d(O,{children:[d(w,{flex:1,radius:2,overflow:"hidden",style:{position:"relative",minWidth:"4em"},children:[u(B,{}),u(E,{style:{backgroundColor:"rgba(".concat(null==o?void 0:o.r,",").concat(null==o?void 0:o.g,",").concat(null==o?void 0:o.b,",").concat(null==o?void 0:o.a,")")}}),g&&u(U,{padding:2,paddingBottom:1,sizing:"border",justify:"space-between",children:d(j,{space:3,marginTop:1,children:[u(S,{size:3,weight:"bold",children:a}),d(z,{space:3,children:[d(S,{size:1,children:[u("strong",{children:"RGB: "}),null==o?void 0:o.r," ",null==o?void 0:o.g," ",null==o?void 0:o.b]}),d(S,{size:1,children:[u("strong",{children:"HSL: "})," ",Math.round(null!=(t=null==s?void 0:s.h)?t:0)," ",Math.round(null!=(n=null==s?void 0:s.s)?n:0),"%"," ",Math.round(null!=(l=null==s?void 0:s.l)?l:0)]})]})]})})]}),!g&&d(O,{align:"flex-start",marginLeft:2,children:[u(C,{style:{width:200},children:u(_,{rgb:o,hsl:s,hex:a,onChange:c,disableAlpha:b})}),u(C,{marginLeft:2,children:u(A,{onClick:h,title:"Delete color",icon:L,tone:"critical"})})]})]})]})})}):null})),q={hex:"#24a3e3",hsl:{h:200,s:.7732,l:.5156,a:1},hsv:{h:200,s:.8414,v:.8901,a:1},rgb:{r:46,g:163,b:227,a:1},source:"hex"};function F(e){var t;const{onChange:n,schemaType:l,readOnly:r,value:o}=e,a=m(null),[h,d]=f(o);y((()=>d(o)),[o]);const x=g((e=>{var t;const r=l.fields.filter((t=>t.name in e)).map((t=>{const n=e[t.name],l="object"===t.type.jsonType;return i(l?Object.assign({_type:t.type.name},n):n,[t.name])}));n([s({_type:l.name}),i(l.name,["_type"]),i(null==(t=e.rgb)?void 0:t.a,["alpha"]),...r])}),[n,l]),O=b((()=>v(x,100)),[x]),C=g((e=>{d(e),O(e)}),[O,d]),w=g((()=>{d(q),x(q)}),[x]),j=g((()=>{d(void 0),n(c())}),[n]);return u(p,{children:o?u(W,{color:h,onChange:C,readOnly:r||"boolean"==typeof l.readOnly&&l.readOnly,disableAlpha:!!(null==(t=l.options)?void 0:t.disableAlpha),onUnset:j}):u(A,{icon:P,mode:"ghost",text:"Create color",ref:a,disabled:Boolean(r),onClick:w})})}const I=function(){let e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:1;return Math.round(100*e)},J=a(l(l({name:"color",type:"object",title:"Color"},{components:{input:F}}),{},{fields:[{title:"Hex",name:"hex",type:"string"},{title:"Alpha",name:"alpha",type:"number"},{title:"Hue Saturation Lightness",name:"hsl",type:"hslaColor"},{title:"Hue Saturation Value",name:"hsv",type:"hsvaColor"},{title:"Red Green Blue (rgb)",name:"rgb",type:"rgbaColor"}],preview:{select:{title:"hex",alpha:"alpha",hex:"hex",hsl:"hsl"},prepare(e){let{title:t,hex:n,hsl:l,alpha:r}=e,o=n||"No color set";return l&&(o="H:".concat(I(l.h)," S:").concat(I(l.s)," L:").concat(I(l.l)," A:").concat(I(r))),{title:t,subtitle:o,media:()=>u("div",{style:{backgroundColor:null!=n?n:"#000",opacity:null!=r?r:1,position:"absolute",height:"100%",width:"100%",top:"0",left:"0"}})}}}})),K=a({title:"Hue Saturation Value",name:"hsvaColor",type:"object",fields:[{name:"h",type:"number",title:"Hue"},{name:"s",type:"number",title:"Saturation"},{name:"v",type:"number",title:"Value"},{name:"a",type:"number",title:"Alpha"}]}),Q=h({name:"@sanity/color-input",schema:{types:[G,K,V,J]}});export{F as ColorInput,J as color,Q as colorInput,G as hslaColor,K as hsvaColor,V as rgbaColor};
1
+ var _templateObject, _templateObject2;
2
+ function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
3
+ import { defineType, set, setIfMissing, unset, definePlugin } from 'sanity';
4
+ import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
5
+ import { useMemo, useCallback, useRef, useState, useEffect } from 'react';
6
+ import { debounce } from 'lodash';
7
+ import { useTheme, Flex, Box, Card, Stack, Text, Inline, Button } from '@sanity/ui';
8
+ import { TrashIcon, AddIcon } from '@sanity/icons';
9
+ import { EditableInput, Saturation, Hue, Alpha, Checkboard } from 'react-color/lib/components/common';
10
+ import { CustomPicker } from 'react-color';
11
+ import styled from 'styled-components';
12
+ import { isValidHex } from 'react-color/lib/helpers/color';
13
+ const hslaColor = defineType({
14
+ title: "Hue Saturation Lightness",
15
+ name: "hslaColor",
16
+ type: "object",
17
+ fields: [{
18
+ name: "h",
19
+ type: "number",
20
+ title: "Hue"
21
+ }, {
22
+ name: "s",
23
+ type: "number",
24
+ title: "Saturation"
25
+ }, {
26
+ name: "l",
27
+ type: "number",
28
+ title: "Lightness"
29
+ }, {
30
+ name: "a",
31
+ type: "number",
32
+ title: "Alpha"
33
+ }]
34
+ });
35
+ const rgbaColor = defineType({
36
+ title: "Red Green Blue (rgb)",
37
+ name: "rgbaColor",
38
+ type: "object",
39
+ fields: [{
40
+ name: "r",
41
+ type: "number",
42
+ title: "Red"
43
+ }, {
44
+ name: "g",
45
+ type: "number",
46
+ title: "Green"
47
+ }, {
48
+ name: "b",
49
+ type: "number",
50
+ title: "Blue"
51
+ }, {
52
+ name: "a",
53
+ type: "number",
54
+ title: "Alpha"
55
+ }]
56
+ });
57
+ const ColorPickerFields = _ref => {
58
+ let {
59
+ onChange,
60
+ rgb,
61
+ hsl,
62
+ hex,
63
+ disableAlpha
64
+ } = _ref;
65
+ var _a;
66
+ const {
67
+ sanity
68
+ } = useTheme();
69
+ const inputStyles = useMemo(() => ({
70
+ input: {
71
+ width: "80%",
72
+ padding: "4px 10% 3px",
73
+ border: "none",
74
+ boxShadow: "inset 0 0 0 1px ".concat(sanity.color.input.default.enabled.border),
75
+ color: sanity.color.input.default.enabled.fg,
76
+ backgroundColor: sanity.color.input.default.enabled.bg,
77
+ fontSize: sanity.fonts.text.sizes[0].fontSize,
78
+ textAlign: "center"
79
+ },
80
+ label: {
81
+ display: "block",
82
+ textAlign: "center",
83
+ fontSize: sanity.fonts.label.sizes[0].fontSize,
84
+ color: sanity.color.base.fg,
85
+ paddingTop: "3px",
86
+ paddingBottom: "4px",
87
+ textTransform: "capitalize"
88
+ }
89
+ }), [sanity]);
90
+ const handleChange = useCallback(data => {
91
+ if ("hex" in data && data.hex && isValidHex(data.hex)) {
92
+ onChange({
93
+ hex: data.hex,
94
+ source: "hex"
95
+ });
96
+ } else if (rgb && ("r" in data && data.r || "g" in data && data.g || "b" in data && data.b)) {
97
+ onChange({
98
+ r: Number(data.r) || rgb.r,
99
+ g: Number(data.g) || rgb.g,
100
+ b: Number(data.b) || rgb.b,
101
+ a: rgb.a,
102
+ source: "rgb"
103
+ });
104
+ } else if (hsl && "a" in data && data.a) {
105
+ let alpha = Number(data.a);
106
+ if (alpha < 0) {
107
+ alpha = 0;
108
+ } else if (alpha > 100) {
109
+ alpha = 100;
110
+ }
111
+ alpha /= 100;
112
+ onChange({
113
+ h: hsl.h,
114
+ s: hsl.s,
115
+ l: hsl.l,
116
+ a: alpha,
117
+ source: "hsl"
118
+ });
119
+ }
120
+ }, [onChange, hsl, rgb]);
121
+ return /* @__PURE__ */jsxs(Flex, {
122
+ children: [/* @__PURE__ */jsx(Box, {
123
+ flex: 2,
124
+ marginRight: 1,
125
+ children: /* @__PURE__ */jsx(EditableInput, {
126
+ style: inputStyles,
127
+ label: "hex",
128
+ value: hex == null ? void 0 : hex.replace("#", ""),
129
+ onChange: handleChange
130
+ })
131
+ }), /* @__PURE__ */jsx(Box, {
132
+ flex: 1,
133
+ marginRight: 1,
134
+ children: /* @__PURE__ */jsx(EditableInput, {
135
+ style: inputStyles,
136
+ label: "r",
137
+ value: rgb == null ? void 0 : rgb.r,
138
+ onChange: handleChange,
139
+ dragLabel: true,
140
+ dragMax: 255
141
+ })
142
+ }), /* @__PURE__ */jsx(Box, {
143
+ flex: 1,
144
+ marginRight: 1,
145
+ children: /* @__PURE__ */jsx(EditableInput, {
146
+ style: inputStyles,
147
+ label: "g",
148
+ value: rgb == null ? void 0 : rgb.g,
149
+ onChange: handleChange,
150
+ dragLabel: true,
151
+ dragMax: 255
152
+ })
153
+ }), /* @__PURE__ */jsx(Box, {
154
+ flex: 1,
155
+ marginRight: 1,
156
+ children: /* @__PURE__ */jsx(EditableInput, {
157
+ style: inputStyles,
158
+ label: "b",
159
+ value: rgb == null ? void 0 : rgb.b,
160
+ onChange: handleChange,
161
+ dragLabel: true,
162
+ dragMax: 255
163
+ })
164
+ }), !disableAlpha && /* @__PURE__ */jsx(Box, {
165
+ flex: 1,
166
+ children: /* @__PURE__ */jsx(EditableInput, {
167
+ style: inputStyles,
168
+ label: "a",
169
+ value: Math.round(((_a = rgb == null ? void 0 : rgb.a) != null ? _a : 1) * 100),
170
+ onChange: handleChange,
171
+ dragLabel: true,
172
+ dragMax: 100
173
+ })
174
+ })]
175
+ });
176
+ };
177
+ const ColorBox = styled(Box)(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n"])));
178
+ const ReadOnlyContainer = styled(Flex)(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n margin-top: 6rem;\n background-color: var(--card-bg-color);\n position: relative;\n width: 100%;\n"])));
179
+ const ColorPickerInner = props => {
180
+ var _a, _b, _c;
181
+ const {
182
+ width,
183
+ color: {
184
+ rgb,
185
+ hex,
186
+ hsv,
187
+ hsl
188
+ },
189
+ onChange,
190
+ onUnset,
191
+ disableAlpha,
192
+ readOnly
193
+ } = props;
194
+ if (!hsl || !hsv) {
195
+ return null;
196
+ }
197
+ return /* @__PURE__ */jsx("div", {
198
+ style: {
199
+ width
200
+ },
201
+ children: /* @__PURE__ */jsx(Card, {
202
+ padding: 1,
203
+ border: true,
204
+ radius: 1,
205
+ children: /* @__PURE__ */jsxs(Stack, {
206
+ space: 2,
207
+ children: [!readOnly && /* @__PURE__ */jsxs(Fragment, {
208
+ children: [/* @__PURE__ */jsx(Card, {
209
+ overflow: "hidden",
210
+ style: {
211
+ position: "relative",
212
+ height: "5em"
213
+ },
214
+ children: /* @__PURE__ */jsx(Saturation, {
215
+ onChange,
216
+ hsl,
217
+ hsv
218
+ })
219
+ }), /* @__PURE__ */jsx(Card, {
220
+ shadow: 1,
221
+ radius: 3,
222
+ overflow: "hidden",
223
+ style: {
224
+ position: "relative",
225
+ height: "10px"
226
+ },
227
+ children: /* @__PURE__ */jsx(Hue, {
228
+ hsl,
229
+ onChange: !readOnly && onChange
230
+ })
231
+ }), !disableAlpha && /* @__PURE__ */jsx(Card, {
232
+ shadow: 1,
233
+ radius: 3,
234
+ overflow: "hidden",
235
+ style: {
236
+ position: "relative",
237
+ height: "10px"
238
+ },
239
+ children: /* @__PURE__ */jsx(Alpha, {
240
+ rgb,
241
+ hsl,
242
+ onChange
243
+ })
244
+ })]
245
+ }), /* @__PURE__ */jsxs(Flex, {
246
+ children: [/* @__PURE__ */jsxs(Card, {
247
+ flex: 1,
248
+ radius: 2,
249
+ overflow: "hidden",
250
+ style: {
251
+ position: "relative",
252
+ minWidth: "4em"
253
+ },
254
+ children: [/* @__PURE__ */jsx(Checkboard, {}), /* @__PURE__ */jsx(ColorBox, {
255
+ style: {
256
+ backgroundColor: "rgba(".concat(rgb == null ? void 0 : rgb.r, ",").concat(rgb == null ? void 0 : rgb.g, ",").concat(rgb == null ? void 0 : rgb.b, ",").concat(rgb == null ? void 0 : rgb.a, ")")
257
+ }
258
+ }), readOnly && /* @__PURE__ */jsx(ReadOnlyContainer, {
259
+ padding: 2,
260
+ paddingBottom: 1,
261
+ sizing: "border",
262
+ justify: "space-between",
263
+ children: /* @__PURE__ */jsxs(Stack, {
264
+ space: 3,
265
+ marginTop: 1,
266
+ children: [/* @__PURE__ */jsx(Text, {
267
+ size: 3,
268
+ weight: "bold",
269
+ children: hex
270
+ }), /* @__PURE__ */jsxs(Inline, {
271
+ space: 3,
272
+ children: [/* @__PURE__ */jsxs(Text, {
273
+ size: 1,
274
+ children: [/* @__PURE__ */jsx("strong", {
275
+ children: "RGB: "
276
+ }), rgb == null ? void 0 : rgb.r, " ", rgb == null ? void 0 : rgb.g, " ", rgb == null ? void 0 : rgb.b]
277
+ }), /* @__PURE__ */jsxs(Text, {
278
+ size: 1,
279
+ children: [/* @__PURE__ */jsx("strong", {
280
+ children: "HSL: "
281
+ }), " ", Math.round((_a = hsl == null ? void 0 : hsl.h) != null ? _a : 0), " ", Math.round((_b = hsl == null ? void 0 : hsl.s) != null ? _b : 0), "%", " ", Math.round((_c = hsl == null ? void 0 : hsl.l) != null ? _c : 0)]
282
+ })]
283
+ })]
284
+ })
285
+ })]
286
+ }), !readOnly && /* @__PURE__ */jsxs(Flex, {
287
+ align: "flex-start",
288
+ marginLeft: 2,
289
+ children: [/* @__PURE__ */jsx(Box, {
290
+ style: {
291
+ width: 200
292
+ },
293
+ children: /* @__PURE__ */jsx(ColorPickerFields, {
294
+ rgb,
295
+ hsl,
296
+ hex,
297
+ onChange,
298
+ disableAlpha
299
+ })
300
+ }), /* @__PURE__ */jsx(Box, {
301
+ marginLeft: 2,
302
+ children: /* @__PURE__ */jsx(Button, {
303
+ onClick: onUnset,
304
+ title: "Delete color",
305
+ icon: TrashIcon,
306
+ tone: "critical"
307
+ })
308
+ })]
309
+ })]
310
+ })]
311
+ })
312
+ })
313
+ });
314
+ };
315
+ const ColorPicker = CustomPicker(ColorPickerInner);
316
+ const DEFAULT_COLOR = {
317
+ hex: "#24a3e3",
318
+ hsl: {
319
+ h: 200,
320
+ s: 0.7732,
321
+ l: 0.5156,
322
+ a: 1
323
+ },
324
+ hsv: {
325
+ h: 200,
326
+ s: 0.8414,
327
+ v: 0.8901,
328
+ a: 1
329
+ },
330
+ rgb: {
331
+ r: 46,
332
+ g: 163,
333
+ b: 227,
334
+ a: 1
335
+ },
336
+ source: "hex"
337
+ };
338
+ function ColorInput(props) {
339
+ var _a;
340
+ const {
341
+ onChange,
342
+ readOnly
343
+ } = props;
344
+ const value = props.value;
345
+ const type = props.schemaType;
346
+ const focusRef = useRef(null);
347
+ const [color, setColor] = useState(value);
348
+ useEffect(() => setColor(value), [value]);
349
+ const emitSetColor = useCallback(nextColor => {
350
+ var _a2;
351
+ const fieldPatches = type.fields.filter(field => field.name in nextColor).map(field => {
352
+ const nextFieldValue = nextColor[field.name];
353
+ const isObject = field.type.jsonType === "object";
354
+ return set(isObject ? Object.assign({
355
+ _type: field.type.name
356
+ }, nextFieldValue) : nextFieldValue, [field.name]);
357
+ });
358
+ onChange([setIfMissing({
359
+ _type: type.name
360
+ }), set(type.name, ["_type"]), set((_a2 = nextColor.rgb) == null ? void 0 : _a2.a, ["alpha"]), ...fieldPatches]);
361
+ }, [onChange, type]);
362
+ const debouncedColorChange = useMemo(() => debounce(emitSetColor, 100), [emitSetColor]);
363
+ const handleColorChange = useCallback(nextColor => {
364
+ setColor(nextColor);
365
+ debouncedColorChange(nextColor);
366
+ }, [debouncedColorChange, setColor]);
367
+ const handleCreateColor = useCallback(() => {
368
+ setColor(DEFAULT_COLOR);
369
+ emitSetColor(DEFAULT_COLOR);
370
+ }, [emitSetColor]);
371
+ const handleUnset = useCallback(() => {
372
+ setColor(void 0);
373
+ onChange(unset());
374
+ }, [onChange]);
375
+ return /* @__PURE__ */jsx(Fragment, {
376
+ children: value && value.hex ? /* @__PURE__ */jsx(ColorPicker, {
377
+ color,
378
+ onChange: handleColorChange,
379
+ readOnly: readOnly || typeof type.readOnly === "boolean" && type.readOnly,
380
+ disableAlpha: !!((_a = type.options) == null ? void 0 : _a.disableAlpha),
381
+ onUnset: handleUnset
382
+ }) : /* @__PURE__ */jsx(Button, {
383
+ icon: AddIcon,
384
+ mode: "ghost",
385
+ text: "Create color",
386
+ ref: focusRef,
387
+ disabled: Boolean(readOnly),
388
+ onClick: handleCreateColor
389
+ })
390
+ });
391
+ }
392
+ const round = function () {
393
+ let val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
394
+ return Math.round(val * 100);
395
+ };
396
+ const colorTypeName = "color";
397
+ const color = defineType({
398
+ name: colorTypeName,
399
+ type: "object",
400
+ title: "Color",
401
+ components: {
402
+ input: ColorInput
403
+ },
404
+ fields: [{
405
+ title: "Hex",
406
+ name: "hex",
407
+ type: "string"
408
+ }, {
409
+ title: "Alpha",
410
+ name: "alpha",
411
+ type: "number"
412
+ }, {
413
+ title: "Hue Saturation Lightness",
414
+ name: "hsl",
415
+ type: "hslaColor"
416
+ }, {
417
+ title: "Hue Saturation Value",
418
+ name: "hsv",
419
+ type: "hsvaColor"
420
+ }, {
421
+ title: "Red Green Blue (rgb)",
422
+ name: "rgb",
423
+ type: "rgbaColor"
424
+ }],
425
+ preview: {
426
+ select: {
427
+ title: "hex",
428
+ alpha: "alpha",
429
+ hex: "hex",
430
+ hsl: "hsl"
431
+ },
432
+ prepare(_ref2) {
433
+ let {
434
+ title,
435
+ hex,
436
+ hsl,
437
+ alpha
438
+ } = _ref2;
439
+ let subtitle = hex || "No color set";
440
+ if (hsl) {
441
+ subtitle = "H:".concat(round(hsl.h), " S:").concat(round(hsl.s), " L:").concat(round(hsl.l), " A:").concat(round(alpha));
442
+ }
443
+ return {
444
+ title,
445
+ subtitle,
446
+ media: () => /* @__PURE__ */jsx("div", {
447
+ style: {
448
+ backgroundColor: hex != null ? hex : "#000",
449
+ opacity: alpha != null ? alpha : 1,
450
+ position: "absolute",
451
+ height: "100%",
452
+ width: "100%",
453
+ top: "0",
454
+ left: "0"
455
+ }
456
+ })
457
+ };
458
+ }
459
+ }
460
+ });
461
+ const hsvaColor = defineType({
462
+ title: "Hue Saturation Value",
463
+ name: "hsvaColor",
464
+ type: "object",
465
+ fields: [{
466
+ name: "h",
467
+ type: "number",
468
+ title: "Hue"
469
+ }, {
470
+ name: "s",
471
+ type: "number",
472
+ title: "Saturation"
473
+ }, {
474
+ name: "v",
475
+ type: "number",
476
+ title: "Value"
477
+ }, {
478
+ name: "a",
479
+ type: "number",
480
+ title: "Alpha"
481
+ }]
482
+ });
483
+ const colorInput = definePlugin({
484
+ name: "@sanity/color-input",
485
+ schema: {
486
+ types: [hslaColor, hsvaColor, rgbaColor, color]
487
+ }
488
+ });
489
+ export { ColorInput, color, colorInput, hslaColor, hsvaColor, rgbaColor };
2
490
  //# sourceMappingURL=index.esm.js.map