sanity-plugin-iconify 1.0.1 → 1.0.3

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,206 @@
1
+ import { BaseSchemaDefinition } from 'sanity';
2
+ import { IconifyInfo } from '@iconify/types';
3
+ import { Plugin as Plugin_2 } from 'sanity';
4
+
5
+ export { BaseSchemaDefinition };
6
+
7
+ /**
8
+ * Usage in `sanity.config.ts` (or .js)
9
+ *
10
+ * ```ts
11
+ * import { defineConfig } from 'sanity'
12
+ * import { iconify } from 'sanity-plugin-iconify'
13
+ *
14
+ * export default defineConfig({
15
+ * // ...
16
+ * plugins: [iconify()],
17
+ * })
18
+ * ```
19
+ */
20
+ export declare const iconify: Plugin_2<void | IconifyPluginConfig>;
21
+
22
+ export declare interface IconifyPluginConfig {
23
+ collections?: IconPrefix[];
24
+ showName?: boolean;
25
+ }
26
+
27
+ export declare interface IconifySearchResult {
28
+ icons: string[];
29
+ collections: Record<string, IconifyInfo>;
30
+ }
31
+
32
+ export declare interface IconOptions {
33
+ collections?: IconPrefix[];
34
+ showName?: boolean;
35
+ }
36
+
37
+ export declare type IconPrefix =
38
+ | 'material-symbols'
39
+ | 'ic'
40
+ | 'mdi'
41
+ | 'ph'
42
+ | 'solar'
43
+ | 'tabler'
44
+ | 'ri'
45
+ | 'bi'
46
+ | 'carbon'
47
+ | 'iconamoon'
48
+ | 'ion'
49
+ | 'uil'
50
+ | 'tdesign'
51
+ | 'teenyicons'
52
+ | 'mingcute'
53
+ | 'clarity'
54
+ | 'iconoir'
55
+ | 'majesticons'
56
+ | 'zondicons'
57
+ | 'ant-design'
58
+ | 'bx'
59
+ | 'bxs'
60
+ | 'gg'
61
+ | 'octicon'
62
+ | 'cil'
63
+ | 'lucide'
64
+ | 'basil'
65
+ | 'pixelarticons'
66
+ | 'system-uicons'
67
+ | 'ci'
68
+ | 'akar-icons'
69
+ | 'memory'
70
+ | 'typcn'
71
+ | 'radix-icons'
72
+ | 'ep'
73
+ | 'circum'
74
+ | 'mdi-light'
75
+ | 'fe'
76
+ | 'eos-icons'
77
+ | 'charm'
78
+ | 'prime'
79
+ | 'humbleicons'
80
+ | 'uiw'
81
+ | 'uim'
82
+ | 'uit'
83
+ | 'uis'
84
+ | 'maki'
85
+ | 'gridicons'
86
+ | 'mi'
87
+ | 'quill'
88
+ | 'gala'
89
+ | 'fluent'
90
+ | 'icon-park-outline'
91
+ | 'icon-park-solid'
92
+ | 'icon-park-twotone'
93
+ | 'icon-park'
94
+ | 'vscode-icons'
95
+ | 'jam'
96
+ | 'heroicons'
97
+ | 'codicon'
98
+ | 'pajamas'
99
+ | 'pepicons-pop'
100
+ | 'pepicons-print'
101
+ | 'pepicons-pencil'
102
+ | 'bytesize'
103
+ | 'ei'
104
+ | 'streamline'
105
+ | 'guidance'
106
+ | 'fa6-solid'
107
+ | 'fa6-regular'
108
+ | 'ooui'
109
+ | 'nimbus'
110
+ | 'formkit'
111
+ | 'line-md'
112
+ | 'svg-spinners'
113
+ | 'openmoji'
114
+ | 'twemoji'
115
+ | 'noto'
116
+ | 'fluent-emoji'
117
+ | 'fluent-emoji-flat'
118
+ | 'fluent-emoji-high-contrast'
119
+ | 'noto-v1'
120
+ | 'emojione'
121
+ | 'emojione-monotone'
122
+ | 'emojione-v1'
123
+ | 'fxemoji'
124
+ | 'streamline-emojis'
125
+ | 'bxl'
126
+ | 'logos'
127
+ | 'simple-icons'
128
+ | 'cib'
129
+ | 'fa6-brands'
130
+ | 'nonicons'
131
+ | 'arcticons'
132
+ | 'file-icons'
133
+ | 'devicon'
134
+ | 'devicon-plain'
135
+ | 'skill-icons'
136
+ | 'brandico'
137
+ | 'entypo-social'
138
+ | 'cryptocurrency'
139
+ | 'cryptocurrency-color'
140
+ | 'flag'
141
+ | 'circle-flags'
142
+ | 'flagpack'
143
+ | 'cif'
144
+ | 'gis'
145
+ | 'map'
146
+ | 'geo'
147
+ | 'game-icons'
148
+ | 'fad'
149
+ | 'academicons'
150
+ | 'wi'
151
+ | 'healthicons'
152
+ | 'medical-icon'
153
+ | 'covid'
154
+ | 'la'
155
+ | 'eva'
156
+ | 'dashicons'
157
+ | 'flat-color-icons'
158
+ | 'entypo'
159
+ | 'foundation'
160
+ | 'raphael'
161
+ | 'icons8'
162
+ | 'iwwa'
163
+ | 'heroicons-outline'
164
+ | 'heroicons-solid'
165
+ | 'fa-solid'
166
+ | 'fa-regular'
167
+ | 'fa-brands'
168
+ | 'fa'
169
+ | 'fluent-mdl2'
170
+ | 'fontisto'
171
+ | 'icomoon-free'
172
+ | 'subway'
173
+ | 'oi'
174
+ | 'wpf'
175
+ | 'simple-line-icons'
176
+ | 'et'
177
+ | 'el'
178
+ | 'vaadin'
179
+ | 'grommet-icons'
180
+ | 'whh'
181
+ | 'si-glyph'
182
+ | 'zmdi'
183
+ | 'ls'
184
+ | 'bpmn'
185
+ | 'flat-ui'
186
+ | 'vs'
187
+ | 'topcoat'
188
+ | 'il'
189
+ | 'websymbol'
190
+ | 'fontelico'
191
+ | 'ps'
192
+ | 'feather'
193
+ | 'mono-icons'
194
+ | 'pepicons';
195
+
196
+ export {};
197
+
198
+ declare module 'sanity' {
199
+ interface IconDefinition extends BaseSchemaDefinition {
200
+ type: 'icon';
201
+ options?: IconOptions;
202
+ }
203
+ interface IntrinsicDefinitions {
204
+ icon: IconDefinition;
205
+ }
206
+ }
@@ -0,0 +1,456 @@
1
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
+ import { unset, set, definePlugin } from 'sanity';
3
+ import { Grid, Box, Card, Text, TextInput, Button, useToast, Popover, useTheme, studioTheme, ThemeProvider, Stack, Flex } from '@sanity/ui';
4
+ import { useState, useCallback, forwardRef, useId, useRef, useEffect, useMemo } from 'react';
5
+ import { Combobox } from '@headlessui/react';
6
+ import { match } from 'ts-pattern';
7
+ import { useQueryClient, useQuery, QueryClient, QueryClientProvider as QueryClientProvider$1 } from '@tanstack/react-query';
8
+ import { useDebounce } from 'use-debounce';
9
+ import styled from 'styled-components';
10
+ import { Icon } from '@iconify/react';
11
+ import { TrashIcon } from '@sanity/icons';
12
+ import { stringToIcon } from '@iconify/utils';
13
+ import { sentenceCase } from 'change-case';
14
+ const BASE_API_URL = "https://api.iconify.design";
15
+ function fetchJson(_ref) {
16
+ let {
17
+ url,
18
+ signal
19
+ } = _ref;
20
+ return fetch(url, {
21
+ signal
22
+ }).then(response => {
23
+ if (!response.ok) {
24
+ throw new Error("Network error: status ".concat(response.status));
25
+ }
26
+ return response.json();
27
+ }).then(result => result, error => {
28
+ if (error instanceof Error) {
29
+ throw error;
30
+ } else {
31
+ console.error("Unknown error: ".concat(error));
32
+ throw new Error("Something went wrong");
33
+ }
34
+ });
35
+ }
36
+ function useSearch(_ref2) {
37
+ let {
38
+ collections
39
+ } = _ref2;
40
+ const queryClient = useQueryClient();
41
+ const [term, setTerm] = useState("");
42
+ const [debouncedTerm, setDebouncedTerm] = useDebounce(term, 500);
43
+ const updateTerm = useCallback(function (newTerm) {
44
+ let updateImmediately = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
45
+ setTerm(newTerm);
46
+ if (updateImmediately) {
47
+ setDebouncedTerm(newTerm);
48
+ }
49
+ }, [setDebouncedTerm]);
50
+ const {
51
+ isInitialLoading,
52
+ isError,
53
+ error,
54
+ data,
55
+ isPreviousData
56
+ } = useQuery({
57
+ queryKey: ["search", collections, debouncedTerm],
58
+ queryFn: async _ref3 => {
59
+ let {
60
+ signal
61
+ } = _ref3;
62
+ var _a;
63
+ const url = new URL("/search", BASE_API_URL);
64
+ url.searchParams.append("query", debouncedTerm);
65
+ url.searchParams.append("limit", "60");
66
+ if (collections) {
67
+ url.searchParams.append("prefixes", collections.join(","));
68
+ }
69
+ const result = debouncedTerm ? await fetchJson({
70
+ url,
71
+ signal
72
+ }) : null;
73
+ if (result) {
74
+ Object.entries(result.collections).forEach(_ref4 => {
75
+ let [prefix, info] = _ref4;
76
+ queryClient.setQueryData(["iconSetInfo", prefix], info);
77
+ });
78
+ }
79
+ return (_a = result == null ? void 0 : result.icons) != null ? _a : [];
80
+ },
81
+ enabled: debouncedTerm.length > 0,
82
+ keepPreviousData: debouncedTerm.length > 0,
83
+ staleTime: 5 * 60 * 1e3
84
+ // 5 minutes
85
+ });
86
+
87
+ return {
88
+ term,
89
+ setTerm: updateTerm,
90
+ debouncedTerm,
91
+ isInitialLoading,
92
+ isError,
93
+ error,
94
+ data,
95
+ isPreviousData
96
+ };
97
+ }
98
+ function useIconSetInfo(_ref5) {
99
+ let {
100
+ prefix
101
+ } = _ref5;
102
+ return useQuery({
103
+ queryKey: ["iconSetInfo", prefix],
104
+ queryFn: async _ref6 => {
105
+ let {
106
+ signal
107
+ } = _ref6;
108
+ var _a;
109
+ if (!prefix) return null;
110
+ const url = new URL("/collection", BASE_API_URL);
111
+ url.searchParams.append("prefix", prefix);
112
+ url.searchParams.append("info", "true");
113
+ const result = await fetchJson({
114
+ url,
115
+ signal
116
+ });
117
+ return (_a = result == null ? void 0 : result.info) != null ? _a : null;
118
+ },
119
+ staleTime: Infinity
120
+ });
121
+ }
122
+ var __freeze = Object.freeze;
123
+ var __defProp = Object.defineProperty;
124
+ var __template = (cooked, raw) => __freeze(__defProp(cooked, "raw", {
125
+ value: __freeze(raw || cooked.slice())
126
+ }));
127
+ var _a, _b;
128
+ const ComboboxWrapper = styled(Grid)(_a || (_a = __template(["\n grid-template-columns: 1fr min-content;\n position: relative;\n"])));
129
+ const OptionsWrapper = styled(Box)(_b || (_b = __template(["\n box-sizing: border-box;\n padding: 0.5rem;\n\n & [role='listbox'] {\n display: grid;\n grid-template-columns: repeat(10, minmax(min(100%, 1rem), 1fr));\n gap: 0.5rem;\n list-style: none;\n margin: 0;\n padding: 0;\n }\n\n & [role='option'] {\n display: grid;\n place-items: center;\n\n & button {\n aspect-ratio: 1;\n cursor: pointer;\n width: 100%;\n }\n }\n"])));
130
+ function MessageWrapper(_ref7) {
131
+ let {
132
+ children
133
+ } = _ref7;
134
+ return /* @__PURE__ */jsx(Card, {
135
+ padding: 4,
136
+ children: /* @__PURE__ */jsx(Text, {
137
+ align: "center",
138
+ muted: true,
139
+ children
140
+ })
141
+ });
142
+ }
143
+ const SearchInput = forwardRef((props, ref) => {
144
+ const {
145
+ term,
146
+ selectedIcon,
147
+ onChange
148
+ } = props;
149
+ return /* @__PURE__ */jsx(Combobox.Input, {
150
+ as: TextInput,
151
+ ref,
152
+ inputMode: "search",
153
+ value: term,
154
+ onChange,
155
+ icon: selectedIcon ? /* @__PURE__ */jsx(Icon, {
156
+ icon: selectedIcon
157
+ }) : null,
158
+ placeholder: selectedIcon ? "Search and replace selected icon..." : "Search for an icon..."
159
+ });
160
+ });
161
+ SearchInput.displayName = "SearchInput";
162
+ function SearchResults(props) {
163
+ const {
164
+ state,
165
+ data
166
+ } = props;
167
+ if (state === "initial") {
168
+ return /* @__PURE__ */jsx(MessageWrapper, {
169
+ children: "Search for icons"
170
+ });
171
+ }
172
+ if (state === "loading") {
173
+ return /* @__PURE__ */jsx(MessageWrapper, {
174
+ children: "Searching..."
175
+ });
176
+ }
177
+ if (state === "error") {
178
+ return /* @__PURE__ */jsx(MessageWrapper, {
179
+ children: "Something went wrong..."
180
+ });
181
+ }
182
+ if (state === "empty") {
183
+ return /* @__PURE__ */jsx(MessageWrapper, {
184
+ children: "No icons found"
185
+ });
186
+ }
187
+ return /* @__PURE__ */jsx(Combobox.Options, {
188
+ style: {
189
+ opacity: state === "stale" ? 0.5 : 1
190
+ },
191
+ children: data.map(icon => /* @__PURE__ */jsx(Combobox.Option, {
192
+ value: icon,
193
+ children: _ref8 => {
194
+ let {
195
+ active
196
+ } = _ref8;
197
+ return /* @__PURE__ */jsx(Button, {
198
+ mode: "bleed",
199
+ icon: /* @__PURE__ */jsx(Icon, {
200
+ icon
201
+ }),
202
+ selected: active
203
+ });
204
+ }
205
+ }, icon))
206
+ });
207
+ }
208
+ function UnsetButton(props) {
209
+ const {
210
+ onUnset
211
+ } = props;
212
+ return /* @__PURE__ */jsx(Card, {
213
+ border: true,
214
+ borderLeft: false,
215
+ padding: 1,
216
+ display: "flex",
217
+ children: /* @__PURE__ */jsx(Button, {
218
+ icon: /* @__PURE__ */jsx(TrashIcon, {}),
219
+ onClick: onUnset,
220
+ mode: "bleed",
221
+ fontSize: 1,
222
+ padding: 2
223
+ })
224
+ });
225
+ }
226
+ function IconifyCombobox(props) {
227
+ const {
228
+ selectedIcon,
229
+ onSelect: pushSelection,
230
+ collections
231
+ } = props;
232
+ const id = useId();
233
+ const toast = useToast();
234
+ const inputRef = useRef(null);
235
+ const {
236
+ term,
237
+ setTerm,
238
+ debouncedTerm,
239
+ isInitialLoading,
240
+ isError,
241
+ error,
242
+ data,
243
+ isPreviousData
244
+ } = useSearch({
245
+ collections
246
+ });
247
+ const handleTermChange = useCallback(event => setTerm(event.target.value), [setTerm]);
248
+ const handleSelect = useCallback(icon => {
249
+ pushSelection(icon);
250
+ setTerm("", true);
251
+ }, [pushSelection, setTerm]);
252
+ const handleUnset = useCallback(() => handleSelect(""), [handleSelect]);
253
+ useEffect(() => {
254
+ if (isError) {
255
+ console.error("Iconify input error:", error);
256
+ toast.push({
257
+ id,
258
+ status: "error",
259
+ title: "Iconify input error",
260
+ description: error == null ? void 0 : error.message
261
+ });
262
+ }
263
+ }, [error, id, isError, toast]);
264
+ return /* @__PURE__ */jsxs(ComboboxWrapper, {
265
+ children: [/* @__PURE__ */jsx(Combobox, {
266
+ onChange: handleSelect,
267
+ children: _ref9 => {
268
+ let {
269
+ open
270
+ } = _ref9;
271
+ return /* @__PURE__ */jsxs(Fragment, {
272
+ children: [/* @__PURE__ */jsx(SearchInput, {
273
+ ref: inputRef,
274
+ term,
275
+ selectedIcon,
276
+ onChange: handleTermChange
277
+ }), /* @__PURE__ */jsx(Popover, {
278
+ open,
279
+ placement: "bottom",
280
+ arrow: false,
281
+ matchReferenceWidth: true,
282
+ portal: true,
283
+ constrainSize: true,
284
+ referenceElement: inputRef.current,
285
+ content: /* @__PURE__ */jsx(OptionsWrapper, {
286
+ children: /* @__PURE__ */jsx(SearchResults, {
287
+ state: match(true).returnType().with(isInitialLoading, () => "loading").with(!debouncedTerm, () => "initial").with(isError, () => "error").with(!data || data.length === 0, () => "empty").with(isPreviousData, () => "stale").otherwise(() => "data"),
288
+ data
289
+ })
290
+ })
291
+ })]
292
+ });
293
+ }
294
+ }), selectedIcon ? /* @__PURE__ */jsx(UnsetButton, {
295
+ onUnset: handleUnset
296
+ }) : null]
297
+ });
298
+ }
299
+ const queryClient = new QueryClient();
300
+ function QueryClientProvider(props) {
301
+ return /* @__PURE__ */jsx(QueryClientProvider$1, {
302
+ client: queryClient,
303
+ children: props.children
304
+ });
305
+ }
306
+ function usePrettyIconName(props) {
307
+ var _a, _b;
308
+ const {
309
+ name
310
+ } = props;
311
+ const iconMeta = useMemo(() => {
312
+ var _a2;
313
+ return (_a2 = props.iconMeta) != null ? _a2 : name ? stringToIcon(name) : null;
314
+ }, [name, props.iconMeta]);
315
+ const iconSetInfo = useIconSetInfo({
316
+ prefix: (_a = iconMeta == null ? void 0 : iconMeta.prefix) != null ? _a : null
317
+ });
318
+ return useMemo(() => {
319
+ var _a2, _b2;
320
+ return iconMeta ? {
321
+ name: sentenceCase(iconMeta.name),
322
+ collection: (_b2 = (_a2 = iconSetInfo.data) == null ? void 0 : _a2.name) != null ? _b2 : iconMeta.prefix
323
+ } : null;
324
+ }, [iconMeta, (_b = iconSetInfo.data) == null ? void 0 : _b.name]);
325
+ }
326
+ function IconifyInput(props) {
327
+ var _a, _b, _c, _d, _e;
328
+ const {
329
+ config,
330
+ value,
331
+ onChange: pushChange,
332
+ schemaType
333
+ } = props;
334
+ const selectedIcon = (_a = value == null ? void 0 : value.name) != null ? _a : null;
335
+ const options = schemaType.options;
336
+ const collections = !((_b = options == null ? void 0 : options.collections) == null ? void 0 : _b.length) && options.collections || !((_c = config == null ? void 0 : config.collections) == null ? void 0 : _c.length) && config.collections || null;
337
+ const showName = (_e = (_d = options == null ? void 0 : options.showName) != null ? _d : config == null ? void 0 : config.showName) != null ? _e : false;
338
+ const parentTheme = useTheme();
339
+ const theme = useMemo(() => parentTheme ? {
340
+ ...parentTheme.sanity,
341
+ color: studioTheme.color
342
+ } : studioTheme, [parentTheme]);
343
+ const handleSelect = useCallback(icon => {
344
+ pushChange(icon === "" ? unset() : set(icon, ["name"]));
345
+ }, [pushChange]);
346
+ return /* @__PURE__ */jsx(QueryClientProvider, {
347
+ children: /* @__PURE__ */jsx(ThemeProvider, {
348
+ theme,
349
+ children: /* @__PURE__ */jsxs(Stack, {
350
+ space: 2,
351
+ children: [/* @__PURE__ */jsx(IconifyCombobox, {
352
+ selectedIcon,
353
+ onSelect: handleSelect,
354
+ collections
355
+ }), showName && selectedIcon ? /* @__PURE__ */jsx(IconifyNameDisplay, {
356
+ name: selectedIcon
357
+ }) : null]
358
+ })
359
+ })
360
+ });
361
+ }
362
+ function IconifyNameDisplay(props) {
363
+ var _a;
364
+ const {
365
+ name
366
+ } = props;
367
+ const prettyName = usePrettyIconName({
368
+ name
369
+ });
370
+ return /* @__PURE__ */jsxs(Flex, {
371
+ gap: 1,
372
+ children: [/* @__PURE__ */jsx(Text, {
373
+ size: 1,
374
+ muted: true,
375
+ children: "Selected:"
376
+ }), /* @__PURE__ */jsx(Text, {
377
+ size: 1,
378
+ weight: "semibold",
379
+ children: (_a = prettyName == null ? void 0 : prettyName.name) != null ? _a : name
380
+ }), (prettyName == null ? void 0 : prettyName.collection) && /* @__PURE__ */jsxs(Text, {
381
+ size: 1,
382
+ muted: true,
383
+ style: {
384
+ fontStyle: "italic"
385
+ },
386
+ children: ["by ", prettyName == null ? void 0 : prettyName.collection]
387
+ })]
388
+ });
389
+ }
390
+ function IconifyPreview(props) {
391
+ const {
392
+ title
393
+ } = props;
394
+ const iconName = typeof props.title === "string" ? stringToIcon(props.title) : null;
395
+ if (typeof title === "string" && iconName) {
396
+ return /* @__PURE__ */jsx(QueryClientProvider, {
397
+ children: /* @__PURE__ */jsx(IconifyPreviewInner, {
398
+ ...props,
399
+ iconName: title,
400
+ iconMeta: iconName
401
+ })
402
+ });
403
+ }
404
+ return props.renderDefault(props);
405
+ }
406
+ function IconifyPreviewInner(props) {
407
+ var _a;
408
+ const {
409
+ iconMeta,
410
+ iconName,
411
+ ...previewProps
412
+ } = props;
413
+ const prettyName = usePrettyIconName({
414
+ iconMeta
415
+ });
416
+ return props.renderDefault({
417
+ ...previewProps,
418
+ media: /* @__PURE__ */jsx(Icon, {
419
+ icon: iconName
420
+ }),
421
+ title: (_a = prettyName == null ? void 0 : prettyName.name) != null ? _a : iconName,
422
+ subtitle: prettyName == null ? void 0 : prettyName.collection
423
+ });
424
+ }
425
+ const iconify = definePlugin(function () {
426
+ let config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
427
+ return {
428
+ name: "sanity-plugin-iconify",
429
+ schema: {
430
+ types: [{
431
+ name: "icon",
432
+ title: "Icon",
433
+ type: "object",
434
+ fields: [{
435
+ name: "name",
436
+ title: "Name",
437
+ type: "string"
438
+ }],
439
+ components: {
440
+ input: props => /* @__PURE__ */jsx(IconifyInput, {
441
+ ...props,
442
+ config
443
+ }),
444
+ preview: IconifyPreview,
445
+ // This makes sure the input component is not indented
446
+ field: props => props.renderDefault({
447
+ ...props,
448
+ level: 0
449
+ })
450
+ }
451
+ }]
452
+ }
453
+ };
454
+ });
455
+ export { iconify };
456
+ //# sourceMappingURL=index.esm.js.map