@torpor/ui 1.3.6 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/dist/Accordion/AccordionItem.torp +1 -1
  2. package/dist/Alert/Alert.torp +41 -0
  3. package/dist/Alert/index.d.ts +37 -0
  4. package/dist/Alert/index.js +1 -0
  5. package/dist/Charts/ChartScatter.torp +167 -0
  6. package/dist/Charts/ScatterChart.torp +49 -178
  7. package/dist/Charts/index.d.ts +38 -78
  8. package/dist/Charts/index.js +2 -4
  9. package/dist/ColorPicker/ColorPalette.torp +107 -29
  10. package/dist/ColorPicker/ColorPicker.torp +121 -10
  11. package/dist/ColorPicker/ColorPickerAlpha.torp +58 -0
  12. package/dist/ColorPicker/ColorPickerArea.torp +175 -0
  13. package/dist/ColorPicker/ColorPickerEyedropper.torp +77 -0
  14. package/dist/ColorPicker/ColorPickerHue.torp +59 -0
  15. package/dist/ColorPicker/ColorPickerInput.torp +7 -18
  16. package/dist/ColorPicker/ColorPickerTypes.ts +11 -1
  17. package/dist/ColorPicker/index.d.ts +139 -14
  18. package/dist/ColorPicker/index.js +5 -1
  19. package/dist/ComboBox/ComboBox.torp +40 -5
  20. package/dist/ComboBox/index.d.ts +5 -2
  21. package/dist/ComboBox/index.js +1 -2
  22. package/dist/Contextual/Contextual.torp +25 -77
  23. package/dist/Contextual/ContextualContent.torp +10 -51
  24. package/dist/Contextual/ContextualHover.torp +13 -85
  25. package/dist/Contextual/ContextualTrigger.torp +9 -16
  26. package/dist/Contextual/ContextualTypes.ts +6 -12
  27. package/dist/Contextual/index.d.ts +10 -13
  28. package/dist/DataGrid/DataGrid.torp +2 -2
  29. package/dist/DataGrid/index.d.ts +1 -1
  30. package/dist/DatePicker/DatePicker.torp +6 -4
  31. package/dist/DateRangePicker/DateRangePicker.torp +14 -13
  32. package/dist/DateTimePicker/DateTimePicker.torp +264 -0
  33. package/dist/DateTimePicker/index.d.ts +46 -0
  34. package/dist/DateTimePicker/index.js +1 -0
  35. package/dist/Dialog/Dialog.torp +0 -1
  36. package/dist/Dialog/index.d.ts +0 -1
  37. package/dist/Drawer/Drawer.torp +5 -23
  38. package/dist/Drawer/DrawerContent.torp +1 -0
  39. package/dist/Drawer/DrawerOverlay.torp +9 -16
  40. package/dist/Drawer/DrawerTrigger.torp +9 -24
  41. package/dist/Drawer/DrawerTypes.ts +3 -1
  42. package/dist/Form/CheckBox.torp +10 -18
  43. package/dist/Form/File.torp +10 -18
  44. package/dist/Form/FileDrop.torp +16 -22
  45. package/dist/Form/Input.torp +14 -20
  46. package/dist/Form/Radio.torp +71 -0
  47. package/dist/Form/RadioGroup.torp +221 -0
  48. package/dist/Form/RadioGroupTypes.ts +40 -0
  49. package/dist/Form/Select.torp +10 -18
  50. package/dist/Form/{Toggle.torp → Switch.torp} +17 -21
  51. package/dist/Form/TextArea.torp +10 -18
  52. package/dist/Form/ToggleButton.torp +94 -0
  53. package/dist/Form/ToggleGroup.torp +150 -0
  54. package/dist/Form/ToggleGroupTypes.ts +23 -0
  55. package/dist/Form/index.d.ts +173 -15
  56. package/dist/Form/index.js +6 -2
  57. package/dist/ListBox/ListBox.torp +27 -5
  58. package/dist/ListBox/ListBoxItem.torp +1 -1
  59. package/dist/ListBox/index.d.ts +6 -2
  60. package/dist/MaskedInput/MaskedInput.torp +1 -1
  61. package/dist/Menu/MenuPopoutTrigger.torp +1 -1
  62. package/dist/MenuBar/MenuBar.torp +5 -3
  63. package/dist/MenuBar/MenuBarItemTrigger.torp +2 -2
  64. package/dist/Meter/Meter.torp +126 -0
  65. package/dist/Meter/MeterIndicator.torp +41 -0
  66. package/dist/Meter/MeterTypes.ts +8 -0
  67. package/dist/Meter/index.d.ts +79 -0
  68. package/dist/Meter/index.js +3 -0
  69. package/dist/Modal/Modal.torp +4 -22
  70. package/dist/Modal/ModalOverlay.torp +9 -16
  71. package/dist/NavMenu/NavMenu.torp +5 -3
  72. package/dist/NumberInput/NumberInput.torp +1 -1
  73. package/dist/PinInput/PinInput.torp +220 -0
  74. package/dist/PinInput/index.d.ts +48 -0
  75. package/dist/PinInput/index.js +1 -0
  76. package/dist/Popover/Popover.torp +18 -24
  77. package/dist/Popover/PopoverContent.torp +8 -0
  78. package/dist/Popover/PopoverHover.torp +30 -3
  79. package/dist/Popover/PopoverTypes.ts +2 -0
  80. package/dist/Popover/index.d.ts +8 -1
  81. package/dist/Rating/Rating.torp +1 -1
  82. package/dist/SegmentedControl/SegmentedControl.torp +3 -3
  83. package/dist/SegmentedControl/SegmentedItem.torp +1 -1
  84. package/dist/SelectBox/SelectBox.torp +40 -5
  85. package/dist/SelectBox/index.d.ts +5 -2
  86. package/dist/SelectBox/index.js +1 -2
  87. package/dist/Slider/Slider.torp +1 -1
  88. package/dist/SortableList/SortableList.torp +132 -0
  89. package/dist/SortableList/SortableListItem.torp +117 -0
  90. package/dist/SortableList/SortableListTypes.ts +16 -0
  91. package/dist/SortableList/index.d.ts +81 -0
  92. package/dist/SortableList/index.js +3 -0
  93. package/dist/TabGroup/TabContent.torp +1 -1
  94. package/dist/TabGroup/TabGroup.torp +2 -2
  95. package/dist/TabGroup/TabHeader.torp +1 -1
  96. package/dist/TagInput/TagInput.torp +1 -1
  97. package/dist/TimePicker/TimePicker.torp +1 -1
  98. package/dist/ToolBar/ToolBar.torp +8 -4
  99. package/dist/ToolBar/ToolBarPopoutTrigger.torp +1 -1
  100. package/dist/Tooltip/Tooltip.torp +104 -0
  101. package/dist/Tooltip/TooltipContent.torp +81 -0
  102. package/dist/Tooltip/TooltipTrigger.torp +79 -0
  103. package/dist/Tooltip/TooltipTypes.ts +28 -0
  104. package/dist/Tooltip/index.d.ts +104 -0
  105. package/dist/Tooltip/index.js +4 -0
  106. package/dist/Tree/Tree.torp +1 -1
  107. package/dist/Tree/TreeItem.torp +1 -1
  108. package/dist/VirtualList/VirtualList.torp +149 -0
  109. package/dist/VirtualList/index.d.ts +46 -0
  110. package/dist/VirtualList/index.js +1 -0
  111. package/dist/dnd/index.d.ts +2 -0
  112. package/dist/dnd/index.js +212 -0
  113. package/dist/dnd/index.js.map +1 -0
  114. package/dist/loader/index.d.ts +2 -0
  115. package/dist/{loader-CBGbVpkN.js → loader/index.js} +2 -2
  116. package/dist/loader/index.js.map +1 -0
  117. package/dist/utils/PopoutClickTrigger.torp +1 -0
  118. package/dist/utils/PopoutOverlay.torp +39 -0
  119. package/dist/utils/PopoutTypes.ts +4 -0
  120. package/dist/utils/SubmenuPopoutTrigger.torp +5 -5
  121. package/dist/utils/colorUtils.ts +260 -75
  122. package/dist/utils/formField.ts +4 -3
  123. package/dist/utils/handlePopoutButton.ts +43 -0
  124. package/package.json +41 -5
  125. package/dist/Charts/GridLines.torp +0 -65
  126. package/dist/Charts/XAxis.torp +0 -69
  127. package/dist/Charts/YAxis.torp +0 -49
  128. package/dist/loader-CBGbVpkN.js.map +0 -1
@@ -0,0 +1,264 @@
1
+ import type { Bindable, ClassValue, StyleValue } from "@torpor/view";
2
+ import {
3
+ Calendar,
4
+ CalendarDay,
5
+ CalendarDayHeader,
6
+ CalendarGrid,
7
+ CalendarGridHeader,
8
+ CalendarHeader,
9
+ CalendarHeaderTrigger,
10
+ } from "../Calendar/index";
11
+ import {
12
+ type FieldContext,
13
+ FieldContextName,
14
+ type FormContext,
15
+ FormContextName,
16
+ } from "../Form/FormTypes";
17
+ import FormValues from "../Form/FormValues.torp";
18
+ import { TimePicker } from "../TimePicker/index";
19
+ import { formatDate } from "../utils/dateUtils";
20
+ import { addDocumentEvent, removeDocumentEvent } from "../utils/documentEvents";
21
+ import createFormField from "../utils/formField";
22
+ import getId from "../utils/getId";
23
+
24
+ interface DateTimePickerProps {
25
+ /** An ID for the root element */
26
+ id?: string;
27
+ /** Classes for the root element */
28
+ class?: ClassValue;
29
+ /** Styles for the root element */
30
+ style?: StyleValue;
31
+ /** The name of the picker's form data field */
32
+ name?: string;
33
+ /** The selected date and time */
34
+ value?: Bindable<Date | undefined>;
35
+ /** Text to display when the value is empty */
36
+ placeholder?: string;
37
+ /** The format for the displayed date (e.g. "d MMM yyyy"); the time follows */
38
+ dateFormat?: string;
39
+ /** If set to true, the user cannot interact with this picker */
40
+ disabled?: boolean;
41
+ /** An accessible name for the trigger button */
42
+ ariaLabel?: string;
43
+ /** Raised when the value has changed */
44
+ onchange?: (value: Date) => void;
45
+ }
46
+
47
+ function toTimeText(date: Date): string {
48
+ const pad = (n: number) => n.toString().padStart(2, "0");
49
+ return `${pad(date.getHours())}:${pad(date.getMinutes())}`;
50
+ }
51
+
52
+ function toFormText(date: Date): string {
53
+ const pad = (n: number) => n.toString().padStart(2, "0");
54
+ return `${formatDate(date, "yyyy-MM-dd")}T${pad(date.getHours())}:${pad(date.getMinutes())}`;
55
+ }
56
+
57
+ /**
58
+ * An input for selecting a date and a time together, from a calendar and a
59
+ * time field in one popout.
60
+ *
61
+ * ```
62
+ * <DateTimePicker name="starts_at" &value={$state.startsAt} />
63
+ * ```
64
+ *
65
+ * The value is a Date. When the time is unset, it defaults to 09:00, and
66
+ * selecting a day keeps the chosen time.
67
+ */
68
+ export default function DateTimePicker($props: DateTimePickerProps | undefined) {
69
+ let defaultId = getId();
70
+
71
+ let content: HTMLDivElement | undefined;
72
+ let closeOnClick = false;
73
+
74
+ // Wire up the enclosing Form and Field, if any
75
+ const form = createFormField(
76
+ () => $props?.name,
77
+ $context[FormContextName] as FormContext | undefined,
78
+ $context[FieldContextName] as FieldContext | undefined,
79
+ );
80
+
81
+ // The time field inside is part of this picker, not a separate field:
82
+ // hide the enclosing Field's context so it doesn't submit its own value
83
+ $context[FieldContextName] = undefined;
84
+
85
+ function getDateFormat(): string {
86
+ return $props?.dateFormat ?? "yyyy-MM-dd";
87
+ }
88
+ function getDisabled(): boolean {
89
+ return $props?.disabled === true;
90
+ }
91
+
92
+ let $state = $watch({
93
+ value: undefined as Date | undefined,
94
+ visible: false,
95
+ });
96
+
97
+ $bind($state, $props, "value");
98
+
99
+ const triggerId = `${$props?.id ?? defaultId}-trigger`;
100
+ const contentId = `${$props?.id ?? defaultId}-content`;
101
+
102
+ function displayText(): string {
103
+ const value = $state.value;
104
+ if (value && value instanceof Date) {
105
+ return `${formatDate($unwrap(value), getDateFormat())} ${toTimeText($unwrap(value))}`;
106
+ }
107
+ return $props?.placeholder || "-";
108
+ }
109
+
110
+ // The calendar's value, defaulting to today when the picker is empty
111
+ function calendarValue(): Date {
112
+ const value = $state.value;
113
+ return value ? new Date($unwrap(value)) : new Date();
114
+ }
115
+
116
+ function timeValue(): string {
117
+ const value = $state.value;
118
+ return value ? toTimeText($unwrap(value)) : "";
119
+ }
120
+
121
+ function commit(next: Date) {
122
+ $state.value = next;
123
+ $props?.onchange?.(next);
124
+ form.handleInput();
125
+ }
126
+
127
+ // Selecting a day keeps the current time (or 09:00, the calendar's default)
128
+ function handleDate(date: Date) {
129
+ const next = new Date($unwrap(date));
130
+ const current = $state.value;
131
+ if (current) {
132
+ next.setHours(current.getHours(), current.getMinutes(), 0, 0);
133
+ } else {
134
+ next.setHours(9, 0, 0, 0);
135
+ }
136
+ commit(next);
137
+ }
138
+
139
+ function handleTime(time: string | undefined) {
140
+ const [hours, minutes] = (time ?? "").split(":").map(Number);
141
+ if (!Number.isFinite(hours) || !Number.isFinite(minutes)) return;
142
+ const next = calendarValue();
143
+ next.setHours(hours, minutes, 0, 0);
144
+ commit(next);
145
+ }
146
+
147
+ function setVisible(visible: boolean) {
148
+ $state.visible = visible;
149
+ if (visible) {
150
+ closeOnClick = false;
151
+ addDocumentEvent("mousedown", handleDocumentMouseDown);
152
+ addDocumentEvent("click", handleDocumentClick);
153
+ // Wait for the popout to be shown before focusing the calendar
154
+ setTimeout(() => {
155
+ const grid = content?.querySelector(".torp-calendar-grid");
156
+ if (grid instanceof HTMLElement && !getDisabled()) {
157
+ grid.focus();
158
+ }
159
+ });
160
+ } else {
161
+ removeDocumentEvent("mousedown", handleDocumentMouseDown);
162
+ removeDocumentEvent("click", handleDocumentClick);
163
+ }
164
+ }
165
+
166
+ function close() {
167
+ setVisible(false);
168
+ const trigger = document.getElementById(triggerId);
169
+ if (trigger instanceof HTMLElement) {
170
+ trigger.focus();
171
+ }
172
+ }
173
+
174
+ function handleDocumentMouseDown() {
175
+ closeOnClick = true;
176
+ }
177
+
178
+ function handleDocumentClick(e: MouseEvent) {
179
+ if (closeOnClick && content && !content.contains(e.target as HTMLElement)) {
180
+ e.preventDefault();
181
+ setVisible(false);
182
+ }
183
+ }
184
+
185
+ function handleKey(e: KeyboardEvent) {
186
+ if (e.key === "Escape") {
187
+ e.preventDefault();
188
+ close();
189
+ }
190
+ }
191
+
192
+ @render {
193
+ <div
194
+ {$props?.id}
195
+ class={["torp-date-time-picker", $props?.class]}
196
+ {$props?.style}
197
+ data-valid={form.valid ? "valid" : "invalid"}
198
+ >
199
+ <button
200
+ type="button"
201
+ id={triggerId}
202
+ class="torp-date-time-picker-trigger"
203
+ aria-haspopup="dialog"
204
+ aria-expanded={$state.visible ? "true" : "false"}
205
+ aria-controls={$state.visible ? contentId : undefined}
206
+ aria-label={$props?.ariaLabel}
207
+ aria-describedby={form.messageId}
208
+ aria-invalid={form.inField && !form.valid ? "true" : undefined}
209
+ onblur={() => form.handleBlur()}
210
+ onclick={(e: MouseEvent) => {
211
+ e.stopPropagation();
212
+ setVisible(!$state.visible);
213
+ }}
214
+ disabled={getDisabled()}
215
+ >
216
+ {displayText()}
217
+ </button>
218
+
219
+ <div
220
+ id={contentId}
221
+ class={["torp-date-time-picker-content", $state.visible ? undefined : "hidden"]}
222
+ role="dialog"
223
+ aria-label={$props?.ariaLabel ?? "Date and time"}
224
+ aria-hidden={$state.visible ? undefined : "true"}
225
+ onkeydown={handleKey}
226
+ &ref={content}
227
+ >
228
+ <Calendar
229
+ selectable
230
+ value={calendarValue()}
231
+ onchange={handleDate}
232
+ onclose={close}
233
+ >
234
+ <CalendarHeader>
235
+ <CalendarHeaderTrigger type="previous" />
236
+ <span>{$slot.monthName} {$slot.year}</span>
237
+ <CalendarHeaderTrigger type="next" />
238
+ </CalendarHeader>
239
+ <CalendarGridHeader>
240
+ @for (let day of $slot.days) {
241
+ <CalendarDayHeader {day.dayName} {day.shortDayName} />
242
+ }
243
+ </CalendarGridHeader>
244
+ <CalendarGrid>
245
+ <CalendarDay date={$slot.day.date} muted={$slot.day.muted} active={$slot.day.active} />
246
+ </CalendarGrid>
247
+ </Calendar>
248
+
249
+ <TimePicker value={timeValue()} onchange={handleTime} disabled={getDisabled()} />
250
+
251
+ <button type="button" class="torp-date-time-picker-done" onclick={close}>Done</button>
252
+ </div>
253
+ </div>
254
+
255
+ <FormValues name={form.name} value={$state.value ? toFormText($unwrap($state.value)) : undefined} />
256
+ }
257
+
258
+ @style {
259
+ /* Visibility only -- see SelectBoxContent */
260
+ .torp-date-time-picker-content.hidden {
261
+ display: none;
262
+ }
263
+ }
264
+ }
@@ -0,0 +1,46 @@
1
+ import { type SlotRender } from "@torpor/view";
2
+
3
+
4
+ interface DateTimePickerProps {
5
+ /** An ID for the root element */
6
+ id?: string;
7
+ /** Classes for the root element */
8
+ class?: ClassValue;
9
+ /** Styles for the root element */
10
+ style?: StyleValue;
11
+ /** The name of the picker's form data field */
12
+ name?: string;
13
+ /** The selected date and time */
14
+ value?: Bindable<Date | undefined>;
15
+ /** Text to display when the value is empty */
16
+ placeholder?: string;
17
+ /** The format for the displayed date (e.g. "d MMM yyyy"); the time follows */
18
+ dateFormat?: string;
19
+ /** If set to true, the user cannot interact with this picker */
20
+ disabled?: boolean;
21
+ /** An accessible name for the trigger button */
22
+ ariaLabel?: string;
23
+ /** Raised when the value has changed */
24
+ onchange?: (value: Date) => void;
25
+ }
26
+
27
+ /**
28
+ * An input for selecting a date and a time together, from a calendar and a
29
+ * time field in one popout.
30
+ *
31
+ * ```
32
+ * <DateTimePicker name="starts_at" &value={$state.startsAt} />
33
+ * ```
34
+ *
35
+ * The value is a Date. When the time is unset, it defaults to 09:00, and
36
+ * selecting a day keeps the chosen time.
37
+ */
38
+ declare function DateTimePicker(
39
+ $parent: ParentNode,
40
+ $anchor: Node | null,
41
+ $props: DateTimePickerProps | undefined,
42
+ $context?: Record<PropertyKey, any>,
43
+ $slots?: Record<string, SlotRender>,
44
+ ): void;
45
+
46
+ export { DateTimePicker };
@@ -0,0 +1 @@
1
+ export { default as DateTimePicker } from "./DateTimePicker.torp";
@@ -32,7 +32,6 @@ interface DialogProps {
32
32
  *
33
33
  * ```
34
34
  * <Dialog>
35
- * <ModalOverlay /> (optional -- will set `modal` to true)
36
35
  * <DialogHeader /> (optional)
37
36
  * <DialogBody /> (optional)
38
37
  * <DialogFooter> (optional)
@@ -25,7 +25,6 @@ interface DialogProps {
25
25
  *
26
26
  * ```
27
27
  * <Dialog>
28
- * <ModalOverlay /> (optional -- will set `modal` to true)
29
28
  * <DialogHeader /> (optional)
30
29
  * <DialogBody /> (optional)
31
30
  * <DialogFooter> (optional)
@@ -1,5 +1,6 @@
1
1
  import type { Bindable, ClassValue, StyleValue } from "@torpor/view";
2
2
  import $handle from "../utils/$handle";
3
+ import { createPopoutButtonHandler } from "../utils/handlePopoutButton";
3
4
  import { type DrawerContext, DrawerContextName, type DrawerState } from "./DrawerTypes";
4
5
 
5
6
  interface DrawerProps {
@@ -42,7 +43,8 @@ export default function Drawer($props: DrawerProps | undefined) {
42
43
 
43
44
  $bind($state, $props, "visible");
44
45
 
45
- let result: any;
46
+
47
+ const { handleButton, getResult } = createPopoutButtonHandler($state);
46
48
 
47
49
  // Set the context to pass down to items
48
50
  let context: DrawerContext = {
@@ -59,32 +61,12 @@ export default function Drawer($props: DrawerProps | undefined) {
59
61
  if (visible) {
60
62
  $props?.onopen?.();
61
63
  } else {
62
- $props?.onclose?.(result);
64
+ $props?.onclose?.(getResult());
63
65
  }
64
66
  });
65
67
 
66
- function handleButton(type: "confirm" | "cancel" | undefined, value?: any) {
67
- // Get the button's result
68
- switch (type) {
69
- case "confirm": {
70
- result = value ?? true;
71
- break;
72
- }
73
- case "cancel": {
74
- result = value ?? false;
75
- break;
76
- }
77
- default: {
78
- result = value;
79
- break;
80
- }
81
- }
82
-
83
- // Hide the drawer
84
- $state.visible = false;
85
- }
86
68
 
87
- function handleClickOutside(e: MouseEvent) {
69
+ function handleClickOutside(e: MouseEvent | KeyboardEvent) {
88
70
  e.preventDefault();
89
71
  $state.visible = false;
90
72
  }
@@ -34,6 +34,7 @@ export default function DrawerContent($props: DrawerContentProps | undefined) {
34
34
  let $state = context.state;
35
35
 
36
36
  $state.contentId = $props?.id ?? defaultId;
37
+ $state.contentRole = "dialog";
37
38
 
38
39
  $run(() => {
39
40
  if (!$state.visible) {
@@ -1,5 +1,5 @@
1
1
  import type { ClassValue, StyleValue } from "@torpor/view";
2
- import { disableScroll } from "../mount/disableScroll";
2
+ import PopoutOverlay from "../utils/PopoutOverlay.torp";
3
3
  import { type DrawerContext, DrawerContextName } from "./DrawerTypes";
4
4
 
5
5
  interface DrawerOverlayProps {
@@ -21,22 +21,15 @@ export default function DrawerOverlay($props: DrawerOverlayProps | undefined) {
21
21
  if (!context) {
22
22
  throw new Error("DrawerOverlay must be contained within a Drawer");
23
23
  }
24
- let $state = context.state;
25
-
26
- // Any drawer with an overlay is modal
27
- $state.modal = true;
28
24
 
29
25
  @render {
30
- @if ($state.visible) {
31
- <div
32
- {$props?.id}
33
- class={["torp-drawer-overlay", $props?.class]}
34
- {$props?.style}
35
- tabindex="-1"
36
- onmount={(_el) => disableScroll()}
37
- >
38
- <slot />
39
- </div>
40
- }
26
+ <PopoutOverlay
27
+ id={$props?.id}
28
+ class={["torp-drawer-overlay", $props?.class]}
29
+ style={$props?.style}
30
+ {context}
31
+ >
32
+ <slot />
33
+ </PopoutOverlay>
41
34
  }
42
35
  }
@@ -1,5 +1,5 @@
1
1
  import type { ClassValue, StyleValue } from "@torpor/view";
2
- import getId from "../utils/getId";
2
+ import PopoutClickTrigger from "../utils/PopoutClickTrigger.torp";
3
3
  import { type DrawerContext, DrawerContextName } from "./DrawerTypes";
4
4
 
5
5
  interface DrawerTriggerProps {
@@ -15,37 +15,22 @@ interface DrawerTriggerProps {
15
15
  * The button that causes a Drawer to appear or disappear.
16
16
  */
17
17
  export default function DrawerTrigger($props: DrawerTriggerProps | undefined) {
18
- const defaultId = getId();
19
-
20
18
  // Get stuff out of the DrawerContext
21
19
  const context = $context[DrawerContextName] as DrawerContext;
22
20
  if (!context) {
23
21
  throw new Error("DrawerTrigger must be contained within a Drawer");
24
22
  }
25
- let $state = context.state;
26
-
27
- let button: HTMLButtonElement;
28
-
29
- $state.triggerId = $props?.id ?? defaultId;
30
- context.focusTrigger = () => button.focus();
31
-
32
- function handleClick() {
33
- $state.visible = !$state.visible;
34
- }
35
23
 
36
24
  @render {
37
- <button
38
- type="button"
39
- id={$props?.id ?? defaultId}
40
- class={["torp-drawer-trigger", $props?.class]}
41
- {$props?.style}
42
- onclick={handleClick}
43
- aria-expanded={$state.visible ? "true" : "false"}
44
- aria-haspopup="dialog"
45
- aria-controls={$state.contentId}
46
- &ref={button}
25
+ <PopoutClickTrigger
26
+ id={$props?.id}
27
+ class={$props?.class ?? "torp-drawer-trigger"}
28
+ style={$props?.style}
29
+ context={context}
30
+ tag="button"
31
+ event="click"
47
32
  >
48
33
  <slot />
49
- </button>
34
+ </PopoutClickTrigger>
50
35
  }
51
36
  }
@@ -5,7 +5,7 @@ export interface DrawerContext {
5
5
  handleButton: (type: "confirm" | "cancel" | undefined, value?: any) => void;
6
6
  // HACK: We need to be able to let children (i.e. Dialogs) override the click outside functionality
7
7
  // There may be a more elegant way to accomplish this too
8
- handleClickOutside?: (e: MouseEvent) => void;
8
+ handleClickOutside?: (e: MouseEvent | KeyboardEvent) => void;
9
9
  state: DrawerState;
10
10
  // HACK: focusFirstElement is necessary because if we focus the first element e.g. in a Dialog's
11
11
  // onMount, it will scroll the page to the focused element and mess up our careful positioning
@@ -24,4 +24,6 @@ export interface DrawerState {
24
24
  position: "left" | "right" | "top" | "bottom";
25
25
  triggerId?: string;
26
26
  contentId?: string;
27
+ /** The role of the drawer's content, for the trigger's aria-haspopup */
28
+ contentRole?: string;
27
29
  }
@@ -1,4 +1,5 @@
1
1
  import type { ClassValue, StyleValue } from "@torpor/view";
2
+ import createFormField from "../utils/formField";
2
3
  import getId from "../utils/getId";
3
4
  import {
4
5
  type FieldContext,
@@ -32,11 +33,10 @@ interface CheckBoxProps {
32
33
  export default function CheckBox($props: CheckBoxProps | undefined) {
33
34
  let defaultId = getId();
34
35
 
35
- let blurred = false;
36
-
37
36
  // Get stuff out of the FormContext and FieldContext
38
- const formContext = $context[FormContextName] as FormContext;
39
- const fieldContext = $context[FieldContextName] as FieldContext;
37
+ const formContext = $context[FormContextName] as FormContext | undefined;
38
+ const fieldContext = $context[FieldContextName] as FieldContext | undefined;
39
+ const form = createFormField(() => $props?.name, formContext, fieldContext);
40
40
 
41
41
  // Use the FieldContext's state, or create a new one
42
42
  let $state =
@@ -60,22 +60,14 @@ export default function CheckBox($props: CheckBoxProps | undefined) {
60
60
  $state.inputId = $props?.id ?? defaultId;
61
61
 
62
62
  function handleInput(e: Event) {
63
- if (formContext) {
64
- formContext.validate();
65
- }
66
- if (fieldContext && blurred) {
67
- fieldContext.validate();
68
- }
63
+ form.handleInput();
69
64
  if ($props?.oninput) {
70
65
  $props.oninput(e);
71
66
  }
72
67
  }
73
68
 
74
69
  function handleBlur() {
75
- if (fieldContext) {
76
- fieldContext.validate();
77
- }
78
- blurred = true;
70
+ form.handleBlur();
79
71
  }
80
72
 
81
73
  @render {
@@ -83,12 +75,12 @@ export default function CheckBox($props: CheckBoxProps | undefined) {
83
75
  id={$props?.id ?? defaultId}
84
76
  class={["torp-check-box", $props?.class]}
85
77
  type="checkbox"
86
- name={$props?.name ?? fieldContext?.name}
78
+ name={form.name}
87
79
  &checked={$state.value}
88
80
  aria-label={$props?.ariaLabel}
89
- data-valid={$state.valid ? "valid" : "invalid"}
90
- aria-describedby={$state.messageId}
91
- aria-invalid={fieldContext && !$state.valid ? "true" : undefined}
81
+ data-valid={form.valid ? "valid" : "invalid"}
82
+ aria-describedby={form.messageId}
83
+ aria-invalid={form.inField && !form.valid ? "true" : undefined}
92
84
  {$props?.onchange}
93
85
  oninput={handleInput}
94
86
  onblur={handleBlur}
@@ -1,4 +1,5 @@
1
1
  import type { ClassValue, StyleValue } from "@torpor/view";
2
+ import createFormField from "../utils/formField";
2
3
  import getId from "../utils/getId";
3
4
  import {
4
5
  type FieldContext,
@@ -38,11 +39,10 @@ interface FileProps {
38
39
  export default function File($props: FileProps | undefined) {
39
40
  let defaultId = getId();
40
41
 
41
- let blurred = false;
42
-
43
42
  // Get stuff out of the FormContext and FieldContext
44
- const formContext = $context[FormContextName] as FormContext;
45
- const fieldContext = $context[FieldContextName] as FieldContext;
43
+ const formContext = $context[FormContextName] as FormContext | undefined;
44
+ const fieldContext = $context[FieldContextName] as FieldContext | undefined;
45
+ const form = createFormField(() => $props?.name, formContext, fieldContext);
46
46
 
47
47
  // This will change the form's enctype to `multipart/form-data`
48
48
  if (formContext) {
@@ -65,22 +65,14 @@ export default function File($props: FileProps | undefined) {
65
65
  $state.inputId = $props?.id ?? defaultId;
66
66
 
67
67
  function handleInput(e: Event & { currentTarget: EventTarget & HTMLInputElement }) {
68
- if (formContext) {
69
- formContext.validate();
70
- }
71
- if (fieldContext && blurred) {
72
- fieldContext.validate();
73
- }
68
+ form.handleInput();
74
69
  if ($props?.oninput) {
75
70
  $props.oninput(e);
76
71
  }
77
72
  }
78
73
 
79
74
  function handleBlur() {
80
- if (fieldContext) {
81
- fieldContext.validate();
82
- }
83
- blurred = true;
75
+ form.handleBlur();
84
76
  }
85
77
 
86
78
  @render {
@@ -89,15 +81,15 @@ export default function File($props: FileProps | undefined) {
89
81
  class={["torp-file", $props?.class]}
90
82
  {$props?.style}
91
83
  type="file"
92
- name={$props?.name ?? fieldContext?.name}
84
+ name={form.name}
93
85
  {$props?.accept}
94
86
  {$props?.placeholder}
95
87
  {$props?.readonly}
96
88
  {$props?.maxlength}
97
89
  aria-label={$props?.ariaLabel}
98
- data-valid={$state.valid ? "valid" : "invalid"}
99
- aria-describedby={$state.messageId}
100
- aria-invalid={fieldContext && !$state.valid ? "true" : undefined}
90
+ data-valid={form.valid ? "valid" : "invalid"}
91
+ aria-describedby={form.messageId}
92
+ aria-invalid={form.inField && !form.valid ? "true" : undefined}
101
93
  {$props?.onchange}
102
94
  oninput={handleInput}
103
95
  onblur={handleBlur}