Open History to see the last commit
1import { serialize, stringify } from 'superjson'
2import { clsx as cx } from 'clsx'
3import {
4 Index,
5 Match,
6 Show,
7 Switch,
8 createEffect,
9 createMemo,
10 createSignal,
11 createUniqueId,
12 on,
13 onCleanup,
14 onMount,
15} from 'solid-js'
16import { Key } from '@solid-primitives/keyed'
17import * as goober from 'goober'
18import { tokens } from './theme'
19import {
20 deleteNestedDataByPath,
21 displayValue,
22 isPlainObject,
23 updateNestedDataByPath,
24} from './utils'
25import {
26 Check,
27 CopiedCopier,
28 Copier,
29 ErrorCopier,
30 List,
31 Pencil,
32 Trash,
33} from './icons'
34import { useQueryDevtoolsContext, useTheme } from './contexts'
35import { primitiveText, searchEntries, searchExplorer } from './explorerSearch'
36import { getNativeObjectType } from './nativeObjectType'
37import type { ExplorerSearch } from './explorerSearch'
38import type { Query } from '@tanstack/query-core'
39
40/**
41 * Chunk elements in the array by size.
42 *
43 * When the array cannot be chunked evenly by size, the last chunk will be
44 * filled with the remaining elements.
45 * @param array - The elements to chunk.
46 * @param size - The number of elements per chunk. Below `1`, no chunks are returned.
47 * @returns The chunks.
48 * @example
49 * chunkArray(['a','b', 'c', 'd', 'e'], 2) // returns [['a','b'], ['c', 'd'], ['e']]
50 */
51function chunkArray<T extends { label: string; value: unknown }>(
52 array: Array<T>,
53 size: number,
54): Array<Array<T>> {
55 if (size < 1) return []
56 let i = 0
57 const result: Array<Array<T>> = []
58 while (i < array.length) {
59 result.push(array.slice(i, i + size))
60 i = i + size
61 }
62 return result
63}
64
65const Expander = (props: { expanded: boolean }) => {
66 const theme = useTheme()
67 const css = useQueryDevtoolsContext().shadowDOMTarget
68 ? goober.css.bind({ target: useQueryDevtoolsContext().shadowDOMTarget })
69 : goober.css
70 const styles = createMemo(() => {
71 return theme() === 'dark' ? darkStyles(css) : lightStyles(css)
72 })
73
74 return (
75 <span
76 class={cx(
77 styles().expander,
78 css`
79 transform: rotate(${props.expanded ? 90 : 0}deg);
80 `,
81 props.expanded &&
82 css`
83 & svg {
84 top: -1px;
85 }
86 `,
87 )}
88 >
89 <svg
90 width="16"
91 height="16"
92 viewBox="0 0 16 16"
93 fill="none"
94 xmlns="http://www.w3.org/2000/svg"
95 >
96 <path
97 d="M6 12L10 8L6 4"
98 stroke-width="2"
99 stroke-linecap="round"
100 stroke-linejoin="round"
101 />
102 </svg>
103 </span>
104 )
105}
106
107type CopyState = 'NoCopy' | 'SuccessCopy' | 'ErrorCopy'
108const CopyButton = (props: { value: unknown }) => {
109 const theme = useTheme()
110 const css = useQueryDevtoolsContext().shadowDOMTarget
111 ? goober.css.bind({ target: useQueryDevtoolsContext().shadowDOMTarget })
112 : goober.css
113 const styles = createMemo(() => {
114 return theme() === 'dark' ? darkStyles(css) : lightStyles(css)
115 })
116 const [copyState, setCopyState] = createSignal<CopyState>('NoCopy')
117
118 return (
119 <button
120 class={styles().actionButton}
121 title="Copy object to clipboard"
122 aria-label={`${
123 copyState() === 'NoCopy'
124 ? 'Copy object to clipboard'
125 : copyState() === 'SuccessCopy'
126 ? 'Object copied to clipboard'
127 : 'Error copying object to clipboard'
128 }`}
129 onClick={
130 copyState() === 'NoCopy'
131 ? () => {
132 navigator.clipboard.writeText(stringify(props.value)).then(
133 () => {
134 setCopyState('SuccessCopy')
135 setTimeout(() => {
136 setCopyState('NoCopy')
137 }, 1500)
138 },
139 (err) => {
140 console.error('Failed to copy: ', err)
141 setCopyState('ErrorCopy')
142 setTimeout(() => {
143 setCopyState('NoCopy')
144 }, 1500)
145 },
146 )
147 }
148 : undefined
149 }
150 >
151 <Switch>
152 <Match when={copyState() === 'NoCopy'}>
153 <Copier />
154 </Match>
155 <Match when={copyState() === 'SuccessCopy'}>
156 <CopiedCopier theme={theme()} />
157 </Match>
158 <Match when={copyState() === 'ErrorCopy'}>
159 <ErrorCopier />
160 </Match>
161 </Switch>
162 </button>
163 )
164}
165
166const ClearArrayButton = (props: {
167 dataPath: Array<string>
168 activeQuery: Query
169}) => {
170 const theme = useTheme()
171 const css = useQueryDevtoolsContext().shadowDOMTarget
172 ? goober.css.bind({ target: useQueryDevtoolsContext().shadowDOMTarget })
173 : goober.css
174 const styles = createMemo(() => {
175 return theme() === 'dark' ? darkStyles(css) : lightStyles(css)
176 })
177 const queryClient = useQueryDevtoolsContext().client
178
179 return (
180 <button
181 class={styles().actionButton}
182 title={'Remove all items'}
183 aria-label={'Remove all items'}
184 onClick={() => {
185 const oldData = props.activeQuery.state.data
186 const newData = updateNestedDataByPath(oldData, props.dataPath, [])
187 queryClient.setQueryData(props.activeQuery.queryKey, newData)
188 }}
189 >
190 <List />
191 </button>
192 )
193}
194
195const DeleteItemButton = (props: {
196 dataPath: Array<string>
197 activeQuery: Query
198}) => {
199 const theme = useTheme()
200 const css = useQueryDevtoolsContext().shadowDOMTarget
201 ? goober.css.bind({ target: useQueryDevtoolsContext().shadowDOMTarget })
202 : goober.css
203 const styles = createMemo(() => {
204 return theme() === 'dark' ? darkStyles(css) : lightStyles(css)
205 })
206 const queryClient = useQueryDevtoolsContext().client
207
208 return (
209 <button
210 class={cx(styles().actionButton)}
211 title={'Delete item'}
212 aria-label={'Delete item'}
213 onClick={() => {
214 const oldData = props.activeQuery.state.data
215 const newData = deleteNestedDataByPath(oldData, props.dataPath)
216 queryClient.setQueryData(props.activeQuery.queryKey, newData)
217 }}
218 >
219 <Trash />
220 </button>
221 )
222}
223
224const ToggleValueButton = (props: {
225 dataPath: Array<string>
226 activeQuery: Query
227 value: boolean
228}) => {
229 const theme = useTheme()
230 const css = useQueryDevtoolsContext().shadowDOMTarget
231 ? goober.css.bind({ target: useQueryDevtoolsContext().shadowDOMTarget })
232 : goober.css
233 const styles = createMemo(() => {
234 return theme() === 'dark' ? darkStyles(css) : lightStyles(css)
235 })
236 const queryClient = useQueryDevtoolsContext().client
237
238 return (
239 <button
240 class={cx(
241 styles().actionButton,
242 css`
243 width: ${tokens.size[3.5]};
244 height: ${tokens.size[3.5]};
245 `,
246 )}
247 title={'Toggle value'}
248 aria-label={'Toggle value'}
249 aria-pressed={props.value}
250 onClick={() => {
251 const oldData = props.activeQuery.state.data
252 const newData = updateNestedDataByPath(
253 oldData,
254 props.dataPath,
255 !props.value,
256 )
257 queryClient.setQueryData(props.activeQuery.queryKey, newData)
258 }}
259 >
260 <Check theme={theme()} checked={props.value} />
261 </button>
262 )
263}
264
265type ExplorerProps = {
266 editable?: boolean
267 label: string
268 value: unknown
269 defaultExpanded?: Array<string>
270 dataPath?: Array<string>
271 activeQuery?: Query
272 itemsDeletable?: boolean
273 onEdit?: () => void
274 search?: ExplorerSearch
275 highlight?: Highlight
276}
277
278/**
279 * Adds nested data search without changing the explorer's manual expansion.
280 * @param props - Data explorer options and the selected query or mutation identity.
281 * @returns The search controls and the original data tree.
282 */
283export function SearchableExplorer(
284 props: ExplorerProps & { searchKey?: unknown },
285) {
286 const theme = useTheme()
287 const css = useQueryDevtoolsContext().shadowDOMTarget
288 ? goober.css.bind({ target: useQueryDevtoolsContext().shadowDOMTarget })
289 : goober.css
290 const styles = createMemo(() =>
291 theme() === 'dark' ? darkStyles(css) : lightStyles(css),
292 )
293 const [term, setTerm] = createSignal('')
294 const [debouncedTerm, setDebouncedTerm] = createSignal('')
295 const clear = () => {
296 setTerm('')
297 setDebouncedTerm('')
298 }
299 const searchKey = createMemo(() => props.searchKey)
300 createEffect(on(searchKey, clear))
301 createEffect(() => {
302 const next = term().toLowerCase()
303 if (!next) {
304 setDebouncedTerm('')
305 return
306 }
307 const timeout = setTimeout(() => setDebouncedTerm(next), 150)
308 onCleanup(() => clearTimeout(timeout))
309 })
310 const search = createMemo<ExplorerSearch | undefined>(() => {
311 const activeTerm = debouncedTerm()
312 return activeTerm
313 ? { term: activeTerm, match: searchExplorer(props.value, activeTerm) }
314 : undefined
315 })
316 const highlightName = `tsqd-data-search-${createUniqueId()}`
317 const [nativeHighlight, setNativeHighlight] = createSignal<Highlight>()
318 let root!: HTMLDivElement
319 onMount(() => {
320 const view = root.ownerDocument.defaultView!
321 const registry = view.CSS.highlights
322 createEffect(
323 on(search, () => {
324 const highlight = new view.Highlight()
325 registry.set(highlightName, highlight)
326 setNativeHighlight(highlight)
327 onCleanup(() => {
328 registry.delete(highlightName)
329 highlight.clear()
330 })
331 }),
332 )
333 })
334 const nativeHighlightStyle = createMemo(
335 () => css`
336 & ::highlight(${highlightName}) {
337 color: ${
338 theme() === 'dark' ? tokens.colors.gray[100] : tokens.colors.gray[900]
339 };
340 background-color: ${
341 theme() === 'dark'
342 ? tokens.colors.yellow[900]
343 : tokens.colors.yellow[200]
344 };
345 }
346 `,
347 )
348 return (
349 <div ref={root} class={nativeHighlightStyle()}>
350 <div class={styles().search}>
351 <input
352 type="search"
353 aria-label="Search data"
354 placeholder="Search data"
355 class={styles().searchInput}
356 value={term()}
357 onInput={(event) => setTerm(event.currentTarget.value)}
358 onKeyDown={(event) => {
359 if (event.key === 'Escape') {
360 event.stopPropagation()
361 clear()
362 }
363 }}
364 />
365 <Show when={term()}>
366 <button
367 type="button"
368 class={styles().clearSearch}
369 onClick={clear}
370 aria-label="Clear search"
371 >
372 Clear
373 </button>
374 </Show>
375 </div>
376 <div role="status" aria-live="polite">
377 {search() && !search()!.match.hasMatch ? 'No matches' : ''}
378 </div>
379 <Explorer {...props} search={search()} highlight={nativeHighlight()} />
380 </div>
381 )
382}
383
384const MAX_HIGHLIGHT_RANGES = 1000
385
386/**
387 * Marks literal, case-insensitive matches in a label or primitive value.
388 * @param props - The text, normalized search term, and shared highlight instance.
389 * @returns Text with matching substrings marked.
390 */
391function HighlightedText(props: {
392 text: string
393 term?: string
394 highlight?: Highlight
395}) {
396 const matches = createMemo(() => {
397 const term = props.term
398 if (!term) return []
399 const lower = props.text.toLowerCase()
400 let originalStart = 0
401 let originalEnd = 0
402 let lowercaseEnd = 0
403 const originalPosition = (index: number) => {
404 while (lowercaseEnd <= index) {
405 const character = String.fromCodePoint(
406 props.text.codePointAt(originalEnd)!,
407 )
408 originalStart = originalEnd
409 originalEnd += character.length
410 lowercaseEnd += character.toLowerCase().length
411 }
412 return { start: originalStart, end: originalEnd }
413 }
414 const ranges: Array<{ start: number; end: number }> = []
415 let index = lower.indexOf(term)
416 while (index !== -1 && ranges.length < MAX_HIGHLIGHT_RANGES) {
417 const from = originalPosition(index).start
418 const to = originalPosition(index + term.length - 1).end
419 ranges.push({ start: from, end: to })
420 index = lower.indexOf(term, index + term.length)
421 }
422 return ranges
423 })
424 const text = document.createTextNode('')
425 createEffect(() => {
426 text.data = props.text
427 const highlight = props.highlight
428 if (!highlight) return
429 const ranges: Array<Range> = []
430 for (const match of matches()) {
431 if (highlight.size >= MAX_HIGHLIGHT_RANGES) break
432 const range = text.ownerDocument.createRange()
433 range.setStart(text, match.start)
434 range.setEnd(text, match.end)
435 highlight.add(range)
436 ranges.push(range)
437 }
438 onCleanup(() => {
439 for (const range of ranges) highlight.delete(range)
440 })
441 })
442 return text
443}
444
445/**
446 * Checks whether a value is iterable.
447 * @param x - The value to check.
448 * @returns `true` if `x` has a `Symbol.iterator` method.
449 */
450function isIterable(x: any): x is Iterable<unknown> {
451 return Symbol.iterator in x
452}
453
454/**
455 * Renders an expandable tree view of a value, used to explore a query's data in the devtools. It
456 * can also edit and delete entries in the query's data.
457 * @param props - The value to show and its label, plus the options for expanding, editing, and
458 * deleting entries.
459 * @returns The tree view.
460 */
461export default function Explorer(props: ExplorerProps) {
462 const theme = useTheme()
463 const css = useQueryDevtoolsContext().shadowDOMTarget
464 ? goober.css.bind({ target: useQueryDevtoolsContext().shadowDOMTarget })
465 : goober.css
466 const styles = createMemo(() => {
467 return theme() === 'dark' ? darkStyles(css) : lightStyles(css)
468 })
469 const queryClient = useQueryDevtoolsContext().client
470
471 const [expanded, setExpanded] = createSignal(
472 (props.defaultExpanded || []).includes(props.label),
473 )
474 const hasSearchMatchingChild = () =>
475 !!props.search &&
476 [...props.search.match.children.values()].some((child) => child.hasMatch)
477 const isExpanded = () => expanded() || hasSearchMatchingChild()
478 const toggleExpanded = () => {
479 if (!hasSearchMatchingChild()) setExpanded((old) => !old)
480 }
481 const [expandedPages, setExpandedPages] = createSignal<Array<number>>([])
482 const nativeObjectType = createMemo(() => getNativeObjectType(props.value))
483 const childrenEditable = createMemo(
484 () =>
485 props.editable &&
486 (Array.isArray(props.value) ||
487 nativeObjectType() === 'Map' ||
488 nativeObjectType() === 'Set' ||
489 isPlainObject(props.value)),
490 )
491
492 const subEntries = createMemo(() => {
493 if (props.search) {
494 return props.search.match.kind === 'branch'
495 ? (searchEntries(props.value) ?? [])
496 : []
497 }
498 if (Array.isArray(props.value)) {
499 return props.value.map((d, i) => ({
500 label: i.toString(),
501 key: i.toString(),
502 value: d,
503 }))
504 } else if (nativeObjectType() === 'Map') {
505 return Array.from(
506 Map.prototype.entries.call(props.value),
507 ([key, val]) => ({
508 label: primitiveText(key) ?? '[Object key]',
509 key,
510 value: val,
511 }),
512 )
513 } else if (nativeObjectType() === 'Set') {
514 return Array.from(Set.prototype.values.call(props.value), (val, i) => ({
515 label: i.toString(),
516 key: i.toString(),
517 value: val,
518 }))
519 } else if (
520 props.value !== null &&
521 typeof props.value === 'object' &&
522 isIterable(props.value) &&
523 typeof props.value[Symbol.iterator] === 'function'
524 ) {
525 return Array.from(props.value, (val, i) => ({
526 label: i.toString(),
527 key: i.toString(),
528 value: val,
529 }))
530 } else if (typeof props.value === 'object' && props.value !== null) {
531 return Object.entries(props.value).map(([key, val]) => ({
532 label: key,
533 key,
534 value: val,
535 }))
536 }
537 return []
538 })
539
540 const type = createMemo<string>(() => {
541 if (props.search) {
542 if (Array.isArray(props.value)) return 'array'
543 if (nativeObjectType() === 'Map' || nativeObjectType() === 'Set')
544 return 'Iterable'
545 return props.value === null ? 'object' : typeof props.value
546 }
547 if (Array.isArray(props.value)) {
548 return 'array'
549 } else if (
550 props.value !== null &&
551 typeof props.value === 'object' &&
552 isIterable(props.value) &&
553 typeof props.value[Symbol.iterator] === 'function'
554 ) {
555 return 'Iterable'
556 } else if (typeof props.value === 'object' && props.value !== null) {
557 return 'object'
558 }
559 return typeof props.value
560 })
561
562 const subEntryPages = createMemo(() => chunkArray(subEntries(), 100))
563
564 const pageHasMatch = (index: number) =>
565 !!(
566 props.search &&
567 subEntryPages()[index]?.some(
568 (entry) => props.search?.match.children.get(entry.key)?.hasMatch,
569 )
570 )
571 const matchingPage = createMemo(() =>
572 subEntryPages().findIndex((_, index) => pageHasMatch(index)),
573 )
574 const isPageExpanded = (index: number) =>
575 expandedPages().includes(index) || matchingPage() === index
576 const childSearch = (key: unknown): ExplorerSearch | undefined => {
577 const search = props.search
578 const match = search?.match.children.get(key)
579 return search && match ? { term: search.term, match } : undefined
580 }
581 const label = () => (
582 <HighlightedText
583 text={props.label}
584 term={props.search?.match.label ? props.search.term : undefined}
585 highlight={props.highlight}
586 />
587 )
588 const valueText = () => {
589 if (!props.search) return displayValue(props.value)
590 switch (props.search.match.kind) {
591 case 'circular':
592 return '[Circular]'
593 case 'accessor':
594 return '[Getter]'
595 case 'opaque':
596 return '[Object]'
597 case 'branch':
598 return Array.isArray(props.value)
599 ? '[]'
600 : nativeObjectType() === 'Map'
601 ? 'Map(0)'
602 : nativeObjectType() === 'Set'
603 ? 'Set(0)'
604 : '{}'
605 case 'value':
606 return primitiveText(props.value)
607 }
608 }
609 const renderedValue = () => (
610 <>
611 {props.search && typeof props.value === 'string' ? '"' : ''}
612 <HighlightedText
613 text={valueText() ?? ''}
614 term={props.search?.match.value ? props.search.term : undefined}
615 highlight={props.highlight}
616 />
617 {props.search && typeof props.value === 'string' ? '"' : ''}
618 </>
619 )
620
621 const currentDataPath = props.dataPath ?? []
622
623 const inputId = createUniqueId()
624
625 return (
626 <div class={styles().entry}>
627 <Show when={subEntryPages().length}>
628 <div class={styles().expanderButtonContainer}>
629 <button
630 class={styles().expanderButton}
631 onClick={() => toggleExpanded()}
632 aria-expanded={isExpanded() ? 'true' : 'false'}
633 >
634 <Expander expanded={isExpanded()} /> <span>{label()}</span>{' '}
635 <span class={styles().info}>
636 {String(type()).toLowerCase() === 'iterable' ? '(Iterable) ' : ''}
637 {subEntries().length} {subEntries().length > 1 ? `items` : `item`}
638 </span>
639 </button>
640 <Show when={props.editable}>
641 <div class={styles().actions}>
642 <CopyButton value={props.value} />
643
644 <Show
645 when={props.itemsDeletable && props.activeQuery !== undefined}
646 >
647 <DeleteItemButton
648 activeQuery={props.activeQuery!}
649 dataPath={currentDataPath}
650 />
651 </Show>
652
653 <Show
654 when={type() === 'array' && props.activeQuery !== undefined}
655 >
656 <ClearArrayButton
657 activeQuery={props.activeQuery!}
658 dataPath={currentDataPath}
659 />
660 </Show>
661
662 <Show
663 when={
664 !!props.onEdit &&
665 !props.search &&
666 !serialize(props.value).meta
667 }
668 >
669 <button
670 class={styles().actionButton}
671 title={'Bulk Edit Data'}
672 aria-label={'Bulk Edit Data'}
673 onClick={() => {
674 props.onEdit?.()
675 }}
676 >
677 <Pencil />
678 </button>
679 </Show>
680 </div>
681 </Show>
682 </div>
683 <Show when={isExpanded()}>
684 <Show when={subEntryPages().length === 1}>
685 <div class={styles().subEntry}>
686 <Key each={subEntries()} by={(item) => item.key}>
687 {(entry) => {
688 return (
689 <Explorer
690 defaultExpanded={props.defaultExpanded}
691 label={entry().label}
692 search={childSearch(entry().key)}
693 highlight={props.highlight}
694 value={entry().value}
695 editable={childrenEditable()}
696 dataPath={[...currentDataPath, entry().key]}
697 activeQuery={props.activeQuery}
698 itemsDeletable={
699 type() === 'array' ||
700 type() === 'Iterable' ||
701 type() === 'object'
702 }
703 />
704 )
705 }}
706 </Key>
707 </div>
708 </Show>
709 <Show when={subEntryPages().length > 1}>
710 <div class={styles().subEntry}>
711 <Index each={subEntryPages()}>
712 {(entries, index) => (
713 <div>
714 <div class={styles().entry}>
715 <button
716 onClick={() => {
717 if (matchingPage() === index) return
718 setExpandedPages((old) =>
719 old.includes(index)
720 ? old.filter((d) => d !== index)
721 : [...old, index],
722 )
723 }}
724 class={cx(
725 styles().expanderButton,
726 pageHasMatch(index) && styles().highlight,
727 )}
728 aria-expanded={isPageExpanded(index)}
729 aria-description={
730 pageHasMatch(index)
731 ? 'Contains search matches'
732 : undefined
733 }
734 >
735 <Expander expanded={isPageExpanded(index)} /> [
736 {index * 100}...
737 {index * 100 + 100 - 1}]
738 </button>
739 <Show when={isPageExpanded(index)}>
740 <div class={styles().subEntry}>
741 <Key each={entries()} by={(entry) => entry.key}>
742 {(entry) => (
743 <Explorer
744 defaultExpanded={props.defaultExpanded}
745 label={entry().label}
746 search={childSearch(entry().key)}
747 highlight={props.highlight}
748 value={entry().value}
749 editable={childrenEditable()}
750 dataPath={[...currentDataPath, entry().key]}
751 activeQuery={props.activeQuery}
752 />
753 )}
754 </Key>
755 </div>
756 </Show>
757 </div>
758 </div>
759 )}
760 </Index>
761 </div>
762 </Show>
763 </Show>
764 </Show>
765 <Show when={subEntryPages().length === 0}>
766 <div class={styles().row}>
767 <label for={inputId} class={styles().label}>
768 {label()}:
769 </label>
770 <Show
771 when={
772 props.editable &&
773 props.activeQuery !== undefined &&
774 (type() === 'string' ||
775 type() === 'number' ||
776 type() === 'boolean')
777 }
778 fallback={<span class={styles().value}>{renderedValue()}</span>}
779 >
780 <Show
781 when={
782 props.editable &&
783 props.activeQuery !== undefined &&
784 (type() === 'string' || type() === 'number')
785 }
786 >
787 <input
788 id={inputId}
789 type={type() === 'number' ? 'number' : 'text'}
790 class={cx(
791 styles().value,
792 styles().editableInput,
793 props.search?.match.value && styles().highlight,
794 )}
795 data-search-match={props.search?.match.value || undefined}
796 value={props.value as string | number}
797 onChange={(changeEvent) => {
798 const oldData = props.activeQuery!.state.data
799
800 const newData = updateNestedDataByPath(
801 oldData,
802 currentDataPath,
803 type() === 'number'
804 ? changeEvent.target.valueAsNumber
805 : changeEvent.target.value,
806 )
807
808 queryClient.setQueryData(props.activeQuery!.queryKey, newData)
809 }}
810 />
811 </Show>
812
813 <Show when={type() === 'boolean'}>
814 <span
815 class={cx(
816 styles().value,
817 styles().actions,
818 styles().editableInput,
819 )}
820 >
821 <ToggleValueButton
822 activeQuery={props.activeQuery!}
823 dataPath={currentDataPath}
824 value={props.value as boolean}
825 />
826 {renderedValue()}
827 </span>
828 </Show>
829 </Show>
830
831 <Show
832 when={
833 props.editable &&
834 props.itemsDeletable &&
835 props.activeQuery !== undefined
836 }
837 >
838 <DeleteItemButton
839 activeQuery={props.activeQuery!}
840 dataPath={currentDataPath}
841 />
842 </Show>
843 </div>
844 </Show>
845 </div>
846 )
847}
848
849const stylesFactory = (
850 theme: 'light' | 'dark',
851 css: (typeof goober)['css'],
852) => {
853 const { colors, font, size, border } = tokens
854 const t = (light: string, dark: string) => (theme === 'light' ? light : dark)
855 return {
856 search: css`
857 display: flex;
858 gap: ${size[2]};
859 margin-bottom: ${size[2]};
860 `,
861 searchInput: css`
862 min-width: 0;
863 flex: 1;
864 padding: ${size[1]} ${size[2]};
865 color: inherit;
866 background: ${t(colors.gray[100], colors.darkGray[600])};
867 border: 1px solid ${t(colors.gray[300], colors.darkGray[400])};
868 border-radius: ${border.radius.xs};
869 `,
870 clearSearch: css`
871 color: inherit;
872 background: transparent;
873 border: none;
874 cursor: pointer;
875 `,
876 highlight: css`
877 && {
878 color: ${t(colors.gray[900], colors.gray[100])};
879 background-color: ${t(colors.yellow[200], colors.yellow[900])};
880 }
881 `,
882 entry: css`
883 & * {
884 font-size: ${font.size.xs};
885 font-family:
886 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
887 'Liberation Mono', 'Courier New', monospace;
888 }
889 position: relative;
890 outline: none;
891 word-break: break-word;
892 `,
893 subEntry: css`
894 margin: 0 0 0 0.5em;
895 padding-left: 0.75em;
896 border-left: 2px solid ${t(colors.gray[300], colors.darkGray[400])};
897 /* outline: 1px solid ${colors.teal[400]}; */
898 `,
899 expander: css`
900 & path {
901 stroke: ${colors.gray[400]};
902 }
903 & svg {
904 width: ${size[3]};
905 height: ${size[3]};
906 }
907 display: inline-flex;
908 align-items: center;
909 transition: all 0.1s ease;
910 /* outline: 1px solid ${colors.blue[400]}; */
911 `,
912 expanderButtonContainer: css`
913 display: flex;
914 align-items: center;
915 line-height: ${size[4]};
916 min-height: ${size[4]};
917 gap: ${size[2]};
918 `,
919 expanderButton: css`
920 cursor: pointer;
921 color: inherit;
922 font: inherit;
923 outline: inherit;
924 height: ${size[5]};
925 background: transparent;
926 border: none;
927 padding: 0;
928 display: inline-flex;
929 align-items: center;
930 gap: ${size[1]};
931 position: relative;
932 /* outline: 1px solid ${colors.green[400]}; */
933
934 &:focus-visible {
935 border-radius: ${border.radius.xs};
936 outline: 2px solid ${colors.blue[800]};
937 }
938
939 & svg {
940 position: relative;
941 left: 1px;
942 }
943 `,
944 info: css`
945 color: ${t(colors.gray[500], colors.gray[500])};
946 font-size: ${font.size.xs};
947 margin-left: ${size[1]};
948 /* outline: 1px solid ${colors.yellow[400]}; */
949 `,
950 label: css`
951 color: ${t(colors.gray[700], colors.gray[300])};
952 white-space: nowrap;
953 `,
954 value: css`
955 color: ${t(colors.purple[600], colors.purple[400])};
956 flex-grow: 1;
957 `,
958 actions: css`
959 display: inline-flex;
960 gap: ${size[2]};
961 align-items: center;
962 `,
963 row: css`
964 display: inline-flex;
965 gap: ${size[2]};
966 width: 100%;
967 margin: ${size[0.25]} 0px;
968 line-height: ${size[4.5]};
969 align-items: center;
970 `,
971 editableInput: css`
972 border: none;
973 padding: ${size[0.5]} ${size[1]} ${size[0.5]} ${size[1.5]};
974 flex-grow: 1;
975 border-radius: ${border.radius.xs};
976 background-color: ${t(colors.gray[200], colors.darkGray[500])};
977
978 &:hover {
979 background-color: ${t(colors.gray[300], colors.darkGray[600])};
980 }
981 `,
982 actionButton: css`
983 background-color: transparent;
984 color: ${t(colors.gray[500], colors.gray[500])};
985 border: none;
986 display: inline-flex;
987 padding: 0px;
988 align-items: center;
989 justify-content: center;
990 cursor: pointer;
991 width: ${size[3]};
992 height: ${size[3]};
993 position: relative;
994 z-index: 1;
995
996 &:hover svg {
997 color: ${t(colors.gray[600], colors.gray[400])};
998 }
999
1000 &:focus-visible {
1001 border-radius: ${border.radius.xs};
1002 outline: 2px solid ${colors.blue[800]};
1003 outline-offset: 2px;
1004 }
1005 `,
1006 }
1007}
1008
1009const lightStyles = (css: (typeof goober)['css']) => stylesFactory('light', css)
1010const darkStyles = (css: (typeof goober)['css']) => stylesFactory('dark', css)