@zeyolabs/ui 0.0.0-stage → 2026.11.5

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.
Files changed (138) hide show
  1. package/README.md +43 -2
  2. package/back-button/BackButton.svelte +26 -0
  3. package/badge/Badge.svelte +73 -0
  4. package/badge/PromoBadge.svelte +319 -0
  5. package/badge/color.ts +10 -0
  6. package/bar/Bar.svelte +63 -0
  7. package/base.css +47 -0
  8. package/boundary/Boundary.svelte +59 -0
  9. package/boundary/BoundaryFailed.svelte +40 -0
  10. package/boundary/PendingTracker.svelte +14 -0
  11. package/boundary/pending-context.ts +15 -0
  12. package/boundary/pending-flag.svelte.ts +33 -0
  13. package/boundary/report-boundary-error.ts +13 -0
  14. package/card/Card.svelte +239 -0
  15. package/card/CardBody.svelte +41 -0
  16. package/card/CardFooter.svelte +36 -0
  17. package/card/CardHeader.svelte +36 -0
  18. package/copy/CopyArea.svelte +37 -0
  19. package/copy/CopyButton.svelte +59 -0
  20. package/copy/copy-state.svelte.ts +23 -0
  21. package/copy/types.ts +1 -0
  22. package/dialog/ConfirmDialog.svelte +61 -0
  23. package/dialog/Dialog.svelte +31 -0
  24. package/dialog/DialogBody.svelte +88 -0
  25. package/dialog/DialogCloseButton.svelte +26 -0
  26. package/dialog/DialogFooter.svelte +23 -0
  27. package/dialog/DialogTrigger.svelte +15 -0
  28. package/distribution-bar/DistributionBar.svelte +37 -0
  29. package/drawer/Drawer.svelte +31 -0
  30. package/drawer/DrawerBody.svelte +61 -0
  31. package/empty-state/EmptyState.svelte +74 -0
  32. package/error/ErrorPage.svelte +54 -0
  33. package/eyebrow/Eyebrow.svelte +10 -0
  34. package/faq/FAQList.svelte +89 -0
  35. package/filter/FilterChipGroup.svelte +78 -0
  36. package/filter/FilterOptionList.svelte +188 -0
  37. package/filter/FilterPopover.svelte +122 -0
  38. package/filter/filter-draft.svelte.ts +25 -0
  39. package/form/Button.svelte +73 -0
  40. package/form/Field.svelte +111 -0
  41. package/form/FieldLabel.svelte +19 -0
  42. package/form/Form.svelte +133 -0
  43. package/form/PhoneField.svelte +214 -0
  44. package/form/UnsavedBar.svelte +22 -0
  45. package/form/field-view.svelte.ts +213 -0
  46. package/form/fields/CheckboxField.svelte +85 -0
  47. package/form/fields/Combobox.svelte +327 -0
  48. package/form/fields/ComboboxAction.svelte +16 -0
  49. package/form/fields/DateField.svelte +31 -0
  50. package/form/fields/DateRangeField.svelte +188 -0
  51. package/form/fields/DateRangePresetField.svelte +119 -0
  52. package/form/fields/EmailField.svelte +63 -0
  53. package/form/fields/FileField.svelte +145 -0
  54. package/form/fields/HexColorField.svelte +83 -0
  55. package/form/fields/IntervalField.svelte +100 -0
  56. package/form/fields/MessageField.svelte +99 -0
  57. package/form/fields/NumberField.svelte +64 -0
  58. package/form/fields/Option.svelte +48 -0
  59. package/form/fields/OptionRegistrar.svelte +15 -0
  60. package/form/fields/PasswordField.svelte +44 -0
  61. package/form/fields/RadioField.svelte +60 -0
  62. package/form/fields/RichTextField.svelte +221 -0
  63. package/form/fields/SelectField.svelte +216 -0
  64. package/form/fields/SliderField.svelte +182 -0
  65. package/form/fields/SwitchField.svelte +102 -0
  66. package/form/fields/TextField.svelte +72 -0
  67. package/form/fields/TimeField.svelte +31 -0
  68. package/form/fields/index.ts +19 -0
  69. package/form/fields/select-field-context.ts +21 -0
  70. package/form/index.ts +21 -0
  71. package/form/primitives/Checkbox.svelte +103 -0
  72. package/form/primitives/DateInput.svelte +51 -0
  73. package/form/primitives/IconInputWrapper.svelte +40 -0
  74. package/form/primitives/Input.svelte +47 -0
  75. package/form/primitives/SearchBar.svelte +37 -0
  76. package/form/primitives/Select.svelte +139 -0
  77. package/form/primitives/TextArea.svelte +51 -0
  78. package/form/primitives/index.ts +6 -0
  79. package/gallery/ImageGallery.svelte +133 -0
  80. package/gallery/flickity-fullscreen.d.ts +2 -0
  81. package/grouped-grid/GroupedGrid.svelte +143 -0
  82. package/grouped-grid/types.ts +25 -0
  83. package/layout/Cluster.svelte +92 -0
  84. package/layout/Container.svelte +9 -0
  85. package/layout/Grid.svelte +131 -0
  86. package/layout/Section.svelte +146 -0
  87. package/layout/Shell.svelte +32 -0
  88. package/layout/SplitShell.svelte +47 -0
  89. package/layout/Stack.svelte +78 -0
  90. package/list-row/ListRow.svelte +77 -0
  91. package/menu/MenuButton.svelte +101 -0
  92. package/mini-stat/MiniStat.svelte +13 -0
  93. package/package.json +169 -5
  94. package/page-hero/PageHero.svelte +35 -0
  95. package/pagination/Pagination.svelte +44 -0
  96. package/pagination/finder-pagination.svelte.ts +55 -0
  97. package/pagination/finder-search.svelte.ts +292 -0
  98. package/percentage-ring/PercentageRing.svelte +76 -0
  99. package/percentage-ring/types.ts +1 -0
  100. package/popover/InfoTip.svelte +36 -0
  101. package/popover/Popover.svelte +157 -0
  102. package/query-state/QueryState.svelte +42 -0
  103. package/schema.ts +20 -0
  104. package/skeleton/Skeleton.svelte +86 -0
  105. package/skeleton/SkeletonButton.svelte +27 -0
  106. package/skeleton/SkeletonCard.svelte +49 -0
  107. package/skeleton/SkeletonCircle.svelte +25 -0
  108. package/skeleton/SkeletonField.svelte +19 -0
  109. package/skeleton/SkeletonRow.svelte +47 -0
  110. package/skeleton/SkeletonText.svelte +46 -0
  111. package/skeleton/TableSkeleton.svelte +60 -0
  112. package/spacing.css +32 -0
  113. package/stat/NextPayoutCountdown.svelte +52 -0
  114. package/stat/Stat.svelte +103 -0
  115. package/stat/ValueBox.svelte +38 -0
  116. package/status-row/StatusRow.svelte +176 -0
  117. package/status-row/types.ts +5 -0
  118. package/table/SelectAllCell.svelte +34 -0
  119. package/table/SelectAllTableCell.svelte +25 -0
  120. package/table/SelectRowCell.svelte +31 -0
  121. package/table/SelectRowTableCell.svelte +25 -0
  122. package/table/SelectionBar.svelte +68 -0
  123. package/table/SelectionOverlay.svelte +83 -0
  124. package/table/Table.svelte +64 -0
  125. package/table/TableCell.svelte +185 -0
  126. package/table/TableRow.svelte +86 -0
  127. package/table/index.ts +9 -0
  128. package/table/selection.svelte.ts +79 -0
  129. package/tabs/TabBlock.svelte +56 -0
  130. package/tabs/TabButton.svelte +33 -0
  131. package/template.css +1393 -0
  132. package/text/Span.svelte +83 -0
  133. package/text/Text.svelte +94 -0
  134. package/text/Title.svelte +75 -0
  135. package/text/color.ts +41 -0
  136. package/theme/theme.svelte.ts +33 -0
  137. package/tokens.css +62 -0
  138. package/typography.css +164 -0
@@ -0,0 +1,188 @@
1
+ <script lang="ts">
2
+ import { Check, Search } from '@lucide/svelte';
3
+ import { tick } from 'svelte';
4
+
5
+ import Input from '../form/primitives/Input.svelte';
6
+
7
+ let {
8
+ emptyText = 'No matches.',
9
+ filterLocally = true,
10
+ multiple = true,
11
+ options,
12
+ query = $bindable(''),
13
+ searchable = false,
14
+ searchPlaceholder = 'Search…',
15
+ selected = $bindable(),
16
+ }: {
17
+ emptyText?: string;
18
+ filterLocally?: boolean;
19
+ multiple?: boolean;
20
+ options: { count?: number | string; group?: string; label: string; value: string }[];
21
+ query?: string;
22
+ searchable?: boolean;
23
+ searchPlaceholder?: string;
24
+ selected: string[];
25
+ } = $props();
26
+
27
+ const uid = $props.id();
28
+ let activeIndex = $state(0);
29
+
30
+ const visible = $derived(
31
+ searchable && filterLocally && query
32
+ ? options.filter((o) => o.label.toLowerCase().includes(query.toLowerCase()))
33
+ : options
34
+ );
35
+ const optionId = (i: number) => `${uid}-opt-${i}`;
36
+
37
+ $effect(() => {
38
+ void visible;
39
+ activeIndex = 0;
40
+ });
41
+
42
+ function toggle(value: string) {
43
+ if (multiple) {
44
+ selected = selected.includes(value)
45
+ ? selected.filter((v) => v !== value)
46
+ : [...selected, value];
47
+ } else {
48
+ selected = selected.includes(value) ? [] : [value];
49
+ }
50
+ }
51
+
52
+ function move(index: number) {
53
+ activeIndex = Math.max(0, Math.min(visible.length - 1, index));
54
+ void tick().then(() =>
55
+ document.getElementById(optionId(activeIndex))?.scrollIntoView({ block: 'nearest' })
56
+ );
57
+ }
58
+
59
+ function onkeydown(e: KeyboardEvent) {
60
+ if (e.key === 'ArrowDown') move(activeIndex + 1);
61
+ else if (e.key === 'ArrowUp') move(activeIndex - 1);
62
+ else if (e.key === 'Home') move(0);
63
+ else if (e.key === 'End') move(visible.length - 1);
64
+ else if (e.key === 'Enter' || (e.key === ' ' && !(e.target instanceof HTMLInputElement))) {
65
+ if (!visible[activeIndex]) return;
66
+ toggle(visible[activeIndex].value);
67
+ } else return;
68
+ e.preventDefault();
69
+ }
70
+ </script>
71
+
72
+ <!-- Key events from the search input and the listbox both bubble to this wrapper. -->
73
+ <div class="wrap" {onkeydown} role="presentation">
74
+ {#if searchable}
75
+ <Input
76
+ aria-activedescendant={visible.length ? optionId(activeIndex) : undefined}
77
+ icon={Search}
78
+ placeholder={searchPlaceholder}
79
+ size="sm"
80
+ bind:value={query}
81
+ />
82
+ {/if}
83
+ <div
84
+ class="list"
85
+ aria-activedescendant={visible.length ? optionId(activeIndex) : undefined}
86
+ aria-multiselectable={multiple}
87
+ role="listbox"
88
+ tabindex="0"
89
+ >
90
+ {#each visible as option, i (option.value)}
91
+ {@const on = selected.includes(option.value)}
92
+ {#if option.group && option.group !== visible[i - 1]?.group}
93
+ <div class="group">{option.group}</div>
94
+ {/if}
95
+ <div
96
+ id={optionId(i)}
97
+ class="row"
98
+ class:active={i === activeIndex}
99
+ class:on
100
+ aria-selected={on}
101
+ onclick={() => {
102
+ activeIndex = i;
103
+ toggle(option.value);
104
+ }}
105
+ onkeydown={null}
106
+ role="option"
107
+ tabindex="-1"
108
+ >
109
+ <span class="box"
110
+ >{#if on}<Check size={10} />{/if}</span
111
+ >
112
+ <span class="text">{option.label}</span>
113
+ {#if option.count !== undefined}<span class="count">{option.count}</span>{/if}
114
+ </div>
115
+ {:else}
116
+ <div class="empty">{emptyText}</div>
117
+ {/each}
118
+ </div>
119
+ </div>
120
+
121
+ <style>
122
+ .wrap {
123
+ display: flex;
124
+ flex-direction: column;
125
+ gap: 8px;
126
+ }
127
+ .list {
128
+ display: flex;
129
+ flex-direction: column;
130
+ outline: none;
131
+ }
132
+ .group {
133
+ padding: 6px 6px 2px;
134
+ font-size: 10px;
135
+ font-weight: 600;
136
+ letter-spacing: 0.06em;
137
+ text-transform: uppercase;
138
+ color: var(--txs);
139
+ }
140
+ .row {
141
+ display: flex;
142
+ align-items: center;
143
+ gap: 8px;
144
+ padding: 5px 6px;
145
+ font-size: 12px;
146
+ color: var(--tx);
147
+ border-radius: var(--radius-xs);
148
+ cursor: pointer;
149
+ }
150
+ .row:hover,
151
+ .row.active {
152
+ background: var(--sf);
153
+ }
154
+ .row.on {
155
+ font-weight: 500;
156
+ }
157
+ .box {
158
+ display: inline-flex;
159
+ align-items: center;
160
+ justify-content: center;
161
+ flex: none;
162
+ width: 14px;
163
+ height: 14px;
164
+ color: var(--bg);
165
+ border: 1px solid var(--bd);
166
+ border-radius: var(--radius-xs);
167
+ }
168
+ .on .box {
169
+ background: var(--tx);
170
+ border-color: var(--tx);
171
+ }
172
+ .text {
173
+ flex: 1;
174
+ min-width: 0;
175
+ overflow: hidden;
176
+ text-overflow: ellipsis;
177
+ white-space: nowrap;
178
+ }
179
+ .count {
180
+ font-size: 11px;
181
+ color: var(--txs);
182
+ }
183
+ .empty {
184
+ padding: 8px 6px;
185
+ font-size: 12px;
186
+ color: var(--txs);
187
+ }
188
+ </style>
@@ -0,0 +1,122 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte';
3
+
4
+ import { LoaderCircle } from '@lucide/svelte';
5
+ import { tick } from 'svelte';
6
+
7
+ import Button from '../form/Button.svelte';
8
+ import Popover from '../popover/Popover.svelte';
9
+
10
+ let {
11
+ applyDisabled = false,
12
+ children,
13
+ clearDisabled = false,
14
+ error,
15
+ loading = false,
16
+ onApply,
17
+ onCancel,
18
+ onClear,
19
+ open = $bindable(false),
20
+ side = 'right',
21
+ title,
22
+ width = 320,
23
+ }: {
24
+ applyDisabled?: boolean;
25
+ children: Snippet;
26
+ clearDisabled?: boolean;
27
+ error?: string;
28
+ loading?: boolean;
29
+ onApply: () => void;
30
+ onCancel?: () => void;
31
+ onClear: () => void;
32
+ open?: boolean;
33
+ side?: 'bottom' | 'right';
34
+ title: string;
35
+ width?: number;
36
+ } = $props();
37
+
38
+ let bodyEl: HTMLDivElement | undefined = $state();
39
+ let restoreTo: Element | null = null;
40
+
41
+ $effect(() => {
42
+ if (!open) {
43
+ if (restoreTo instanceof HTMLElement) restoreTo.focus();
44
+ restoreTo = null;
45
+ return;
46
+ }
47
+ restoreTo = document.activeElement;
48
+ void tick().then(() => {
49
+ const target = bodyEl?.querySelector<HTMLElement>('input, button, [tabindex]');
50
+ (target ?? bodyEl)?.focus();
51
+ });
52
+ });
53
+
54
+ function apply() {
55
+ onApply();
56
+ open = false;
57
+ }
58
+ </script>
59
+
60
+ <Popover ariaLabel={title} maxHeight={480} onClose={() => onCancel?.()} {side} {width} bind:open>
61
+ <div class="header">{title}</div>
62
+ <div bind:this={bodyEl} class="body" tabindex="-1">
63
+ {#if loading}
64
+ <div class="state"><LoaderCircle class="spin" size={16} /></div>
65
+ {:else if error}
66
+ <div class="state error">{error}</div>
67
+ {:else}
68
+ {@render children()}
69
+ {/if}
70
+ </div>
71
+ <div class="footer">
72
+ <Button disabled={clearDisabled} onclick={onClear} size="sm" variant="ghost">Clear</Button>
73
+ <Button disabled={applyDisabled} onclick={apply} size="sm">Apply</Button>
74
+ </div>
75
+ </Popover>
76
+
77
+ <style>
78
+ .header {
79
+ padding: 8px 8px 4px;
80
+ font-size: 13px;
81
+ font-weight: 500;
82
+ color: var(--tx);
83
+ }
84
+ .body {
85
+ display: flex;
86
+ flex-direction: column;
87
+ gap: 12px;
88
+ max-height: 300px;
89
+ overflow-y: auto;
90
+ padding: 4px 8px 8px;
91
+ outline: none;
92
+ }
93
+ /* The body scrolls; its children must keep their full height rather than shrink to fit. */
94
+ .body > :global(*) {
95
+ flex-shrink: 0;
96
+ }
97
+ .state {
98
+ display: flex;
99
+ justify-content: center;
100
+ padding: 16px 0;
101
+ font-size: 12px;
102
+ color: var(--txs);
103
+ }
104
+ .state.error {
105
+ color: var(--err);
106
+ }
107
+ .state :global(.spin) {
108
+ animation: spin 0.8s linear infinite;
109
+ }
110
+ @keyframes spin {
111
+ to {
112
+ transform: rotate(360deg);
113
+ }
114
+ }
115
+ .footer {
116
+ display: flex;
117
+ align-items: center;
118
+ justify-content: space-between;
119
+ padding: 8px 8px 2px;
120
+ border-top: 1px solid var(--bd);
121
+ }
122
+ </style>
@@ -0,0 +1,25 @@
1
+ export function createFilterDraft<T>(getCommitted: () => T[], commit: (values: T[]) => void) {
2
+ let values = $state<T[]>([...getCommitted()]);
3
+
4
+ return {
5
+ apply() {
6
+ commit([...values]);
7
+ },
8
+ clear() {
9
+ values = [];
10
+ },
11
+ get dirty() {
12
+ const committed = getCommitted();
13
+ return values.length !== committed.length || !values.every((v) => committed.includes(v));
14
+ },
15
+ reset() {
16
+ values = [...getCommitted()];
17
+ },
18
+ get values() {
19
+ return values;
20
+ },
21
+ set values(next: T[]) {
22
+ values = next;
23
+ },
24
+ };
25
+ }
@@ -0,0 +1,73 @@
1
+ <script generics="Input extends RemoteFormInput" lang="ts">
2
+ import type { RemoteForm, RemoteFormInput } from '$app/server';
3
+ import type { Snippet } from 'svelte';
4
+ import type { HTMLAnchorAttributes, HTMLButtonAttributes } from 'svelte/elements';
5
+
6
+ import Loader2 from '@lucide/svelte/icons/loader-2';
7
+ import { Button as ButtonPrimitive } from 'bits-ui';
8
+
9
+ type Size = 'sm' | 'xs' | (string & {});
10
+ type Variant = 'default' | 'destructive' | 'ghost' | 'ghost-destructive' | (string & {});
11
+
12
+ interface Props extends Omit<HTMLButtonAttributes, 'form' | 'type'> {
13
+ children?: Snippet;
14
+ class?: string;
15
+ form?: Omit<RemoteForm<Input, unknown>, 'for'> | RemoteForm<Input, unknown>;
16
+ href?: string;
17
+ /** Square, zero-padding footprint for a single icon child. Combines with `size`. */
18
+ icon?: boolean;
19
+ loading?: boolean;
20
+ loadingMessage?: string;
21
+ /** Anchor-only — forwarded only when `href` is set. */
22
+ rel?: HTMLAnchorAttributes['rel'];
23
+ reset?: boolean;
24
+ /** Omit for the default button (`--control-h`, 35px). */
25
+ size?: Size;
26
+ /** Anchor-only — forwarded only when `href` is set. */
27
+ target?: HTMLAnchorAttributes['target'];
28
+ variant?: Variant;
29
+ }
30
+
31
+ let {
32
+ 'aria-label': ariaLabel,
33
+ children,
34
+ class: className = '',
35
+ disabled,
36
+ form,
37
+ href,
38
+ icon = false,
39
+ loading = false,
40
+ loadingMessage,
41
+ rel,
42
+ reset = false,
43
+ size,
44
+ target,
45
+ variant = 'default',
46
+ ...restProps
47
+ }: Props = $props();
48
+
49
+ const isPending = $derived(form ? form.pending > 0 : loading);
50
+ const type = $derived(reset ? 'reset' : form ? 'submit' : 'button');
51
+ const classes = $derived(`btn ${variant} ${size ?? ''} ${icon ? 'icon' : ''} ${className}`);
52
+ </script>
53
+
54
+ {#if href}
55
+ <ButtonPrimitive.Root class={classes} aria-label={ariaLabel} {href} {rel} {target}>
56
+ {@render children?.()}
57
+ </ButtonPrimitive.Root>
58
+ {:else}
59
+ <ButtonPrimitive.Root
60
+ class={classes}
61
+ aria-label={ariaLabel}
62
+ disabled={disabled || isPending}
63
+ {type}
64
+ {...restProps}
65
+ >
66
+ {#if isPending}
67
+ <Loader2 class="animate-spin" size={14} />
68
+ {#if loadingMessage}{loadingMessage}{/if}
69
+ {:else}
70
+ {@render children?.()}
71
+ {/if}
72
+ </ButtonPrimitive.Root>
73
+ {/if}
@@ -0,0 +1,111 @@
1
+ <script generics="Input extends RemoteFormInput, T = string" lang="ts">
2
+ import type { RemoteForm, RemoteFormInput } from '$app/server';
3
+ import type { Component, Snippet } from 'svelte';
4
+ import type { HTMLInputAttributes } from 'svelte/elements';
5
+ import type { ZodType } from 'zod';
6
+
7
+ import { createFormView, createLocalTextView, type FieldView } from './field-view.svelte.js';
8
+ import FieldLabel from './FieldLabel.svelte';
9
+ import UiInput from './primitives/Input.svelte';
10
+
11
+ interface Props {
12
+ /** Native autocapitalize hint — passed through to the underlying <input>. */
13
+ autocapitalize?: HTMLInputAttributes['autocapitalize'];
14
+ /** Native autocomplete hint — passed through to the underlying <input>. */
15
+ autocomplete?: HTMLInputAttributes['autocomplete'];
16
+ children?: Snippet;
17
+ /** Extra class(es) merged onto the underlying <input> (in addition to `form-input`). */
18
+ class?: string;
19
+ /** Passed through to the underlying <input>. */
20
+ disabled?: HTMLInputAttributes['disabled'];
21
+ /** When omitted the field is standalone — controlled via `bind:value`. */
22
+ form?: Omit<RemoteForm<Input, unknown>, 'for'> | RemoteForm<Input, unknown>;
23
+ icon?: Component;
24
+ /** Mobile keyboard hint (e.g. 'numeric', 'decimal') — passed through to the underlying <input>. */
25
+ inputmode?: HTMLInputAttributes['inputmode'];
26
+ name: keyof Input & string;
27
+ /** Standalone only — fired on the native `change` event. */
28
+ onChange?: (value: T) => void;
29
+ /** Standalone only — fired on every keystroke. */
30
+ onInput?: (value: T) => void;
31
+ /** Raw native keydown — passed through to the underlying <input> (e.g. Enter-to-submit). */
32
+ onkeydown?: HTMLInputAttributes['onkeydown'];
33
+ optional?: boolean;
34
+ placeholder?: string;
35
+ /** Standalone only — optional per-field validation. */
36
+ schema?: ZodType;
37
+ /** Native spellcheck hint — passed through to the underlying <input>. */
38
+ spellcheck?: HTMLInputAttributes['spellcheck'];
39
+ type: string;
40
+ /** Standalone only — bound value when no `form` is provided. */
41
+ value?: T;
42
+ }
43
+
44
+ let {
45
+ autocapitalize,
46
+ autocomplete,
47
+ children,
48
+ class: className,
49
+ disabled,
50
+ form,
51
+ icon,
52
+ inputmode,
53
+ name,
54
+ onChange,
55
+ onInput,
56
+ onkeydown,
57
+ optional,
58
+ placeholder,
59
+ schema,
60
+ spellcheck,
61
+ type,
62
+ value = $bindable(),
63
+ }: Props = $props();
64
+
65
+ // `form`, `name` and `type` are fixed for a field instance; reading them once is intended.
66
+ // `as()` is reached only via the `form` branch, so the standalone path never calls it.
67
+ // svelte-ignore state_referenced_locally
68
+ const view: FieldView = form
69
+ ? createFormView(form, name, type)
70
+ : createLocalTextView<T>({
71
+ get: () => value as T,
72
+ name,
73
+ onChange,
74
+ onInput,
75
+ schema,
76
+ write: (v) => (value = v),
77
+ });
78
+
79
+ const required = $derived(!optional);
80
+
81
+ // With a label the asterisk carries the required/optional cue. Without one,
82
+ // required fields just show the placeholder; optional fields get an (Optional)
83
+ // suffix so the cue reads naturally after the placeholder text.
84
+ const displayPlaceholder = $derived(
85
+ children || required ? placeholder : placeholder ? `${placeholder} (Optional)` : '(Optional)'
86
+ );
87
+ </script>
88
+
89
+ <div class="form-field">
90
+ {#if children}
91
+ <FieldLabel for={view.attrs.name} {required}>{@render children()}</FieldLabel>
92
+ {/if}
93
+ <UiInput
94
+ id={view.attrs.name}
95
+ class={className}
96
+ {autocapitalize}
97
+ {autocomplete}
98
+ {disabled}
99
+ {icon}
100
+ {inputmode}
101
+ {onkeydown}
102
+ placeholder={displayPlaceholder}
103
+ {required}
104
+ {spellcheck}
105
+ {type}
106
+ {...view.attrs}
107
+ />
108
+ {#each view.issues() as issue, i (i)}
109
+ <p class="form-error">{issue.message}</p>
110
+ {/each}
111
+ </div>
@@ -0,0 +1,19 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from 'svelte';
3
+
4
+ interface Props {
5
+ children?: Snippet;
6
+ class?: string;
7
+ for: string;
8
+ required?: boolean;
9
+ }
10
+
11
+ let { children, class: className = 'form-label', for: forId, required = true }: Props = $props();
12
+ </script>
13
+
14
+ <label class={className} for={forId}>
15
+ {@render children?.()}
16
+ {#if required}
17
+ <span class="form-required-star" aria-hidden="true">*</span>
18
+ {/if}
19
+ </label>
@@ -0,0 +1,133 @@
1
+ <script
2
+ generics="
3
+ Input extends RemoteFormInput,
4
+ Data
5
+ "
6
+ lang="ts"
7
+ >
8
+ import type { RemoteForm, RemoteFormInput } from '$app/server';
9
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
10
+ import type { FormResult } from '@zeyolabs/kit/utils/form';
11
+ import type { Snippet } from 'svelte';
12
+
13
+ import { toast } from 'svelte-sonner';
14
+
15
+ import { setFormChangeListener } from './field-view.svelte.js';
16
+
17
+ // Accepts both a plain remote form and a `.for(id)` instance (which omits `for`).
18
+ type AnyRemoteForm =
19
+ Omit<RemoteForm<Input, FormResult<Data>>, 'for'> | RemoteForm<Input, FormResult<Data>>;
20
+
21
+ interface Props {
22
+ children?: Snippet;
23
+ class?: string;
24
+ defaults?: Partial<Input>;
25
+ /** Bound `true` on any field change, reset to `false` on a successful submit. */
26
+ dirty?: boolean;
27
+ enctype?: 'application/x-www-form-urlencoded' | 'multipart/form-data';
28
+ form: AnyRemoteForm;
29
+ hidden?: Partial<Input>;
30
+ onError?: (info: { data?: Data; message?: string }) => void;
31
+ onInput?: (event: Event) => void;
32
+ onSuccess?: (data: Data) => void;
33
+ onThrow?: (error: unknown) => void;
34
+ onWarning?: (info: { data?: Data; message?: string }) => void;
35
+ reset?: boolean;
36
+ schema: StandardSchemaV1<Input, unknown>;
37
+ }
38
+
39
+ let {
40
+ children,
41
+ class: className,
42
+ defaults,
43
+ dirty = $bindable(false),
44
+ enctype,
45
+ form: remote,
46
+ hidden,
47
+ onError,
48
+ onInput,
49
+ onSuccess,
50
+ onThrow,
51
+ onWarning,
52
+ reset = true,
53
+ schema,
54
+ }: Props = $props();
55
+ // svelte-ignore state_referenced_locally
56
+ if (defaults !== undefined) {
57
+ remote.fields.set(defaults);
58
+ }
59
+
60
+ // bits-ui fields (Switch/Slider/Select) change value via `view.set()` without a
61
+ // bubbling DOM `input` event, so the `oninput` handler below never sees them.
62
+ // They notify through this listener instead — same re-validate + dirty as typing.
63
+ setFormChangeListener(() => {
64
+ remote.validate();
65
+ dirty = true;
66
+ });
67
+
68
+ // Only render hidden inputs whose value is set — `as('hidden', value)` throws on
69
+ // null/undefined/empty string. Derived so value changes on `hidden` propagate.
70
+ const hiddenEntries = $derived(
71
+ hidden ? Object.entries(hidden).filter(([, value]) => value != null && value !== '') : []
72
+ );
73
+
74
+ // Force enctype after the remote-form spread applies (which sets its own enctype).
75
+ // Defaults to multipart when the form contains file inputs, so consumers can't forget it.
76
+ function applyEnctype(node: HTMLFormElement) {
77
+ const wanted =
78
+ enctype ?? (node.querySelector('input[type="file"]') ? 'multipart/form-data' : undefined);
79
+ if (wanted) node.setAttribute('enctype', wanted);
80
+ }
81
+ </script>
82
+
83
+ <form
84
+ class={className}
85
+ novalidate
86
+ oninput={(event) => {
87
+ remote.validate();
88
+ dirty = true;
89
+ onInput?.(event);
90
+ }}
91
+ onreset={() => {
92
+ dirty = false;
93
+ }}
94
+ use:applyEnctype
95
+ {...remote.preflight(schema).enhance(async (instance) => {
96
+ try {
97
+ const submitted = await instance.submit();
98
+ const result = remote.result;
99
+
100
+ if (submitted && reset && result?.type === 'ok') {
101
+ instance.element.reset();
102
+ }
103
+
104
+ if (result !== undefined) {
105
+ const { data, message, type } = result;
106
+ switch (type) {
107
+ case 'error':
108
+ if (message) toast.error(message);
109
+ onError?.({ data, message });
110
+ break;
111
+ case 'ok':
112
+ if (message) toast.success(message);
113
+ dirty = false;
114
+ onSuccess?.(data);
115
+ break;
116
+ case 'warning':
117
+ if (message) toast.warning(message);
118
+ onWarning?.({ data, message });
119
+ break;
120
+ }
121
+ }
122
+ } catch (e) {
123
+ toast.error('An error occurred while submitting the form.');
124
+ onThrow?.(e);
125
+ throw e;
126
+ }
127
+ })}
128
+ >
129
+ {#each hiddenEntries as [name, value] (name)}
130
+ <input {...remote.fields[name].as('hidden', value)} />
131
+ {/each}
132
+ {@render children?.()}
133
+ </form>