@barqjs/aria 0.2.0-next.0 → 0.2.0-next.1

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 (78) hide show
  1. package/README.md +24 -13
  2. package/package.json +127 -37
  3. package/src/autocomplete.tsx +581 -0
  4. package/src/breadcrumbs.tsx +21 -20
  5. package/src/button.tsx +151 -25
  6. package/src/calendar.tsx +1019 -92
  7. package/src/checkbox.tsx +214 -52
  8. package/src/collections.ts +88 -5
  9. package/src/color.ts +1580 -23
  10. package/src/colorpicker.tsx +698 -153
  11. package/src/colorswatchpicker.tsx +240 -0
  12. package/src/combobox.tsx +635 -113
  13. package/src/command.tsx +78 -59
  14. package/src/date.ts +31 -9
  15. package/src/datefield.tsx +1451 -336
  16. package/src/datepicker.tsx +465 -114
  17. package/src/dialog.tsx +302 -115
  18. package/src/disclosure.tsx +81 -29
  19. package/src/dnd/clipboard.ts +125 -0
  20. package/src/dnd/collection.ts +1265 -0
  21. package/src/dnd/data.ts +409 -0
  22. package/src/dnd/drag-drop.ts +801 -0
  23. package/src/dnd/index.ts +133 -0
  24. package/src/dnd/manager.ts +639 -0
  25. package/src/dnd/strings.ts +1279 -0
  26. package/src/dnd/targets.ts +726 -0
  27. package/src/dnd/types.ts +212 -0
  28. package/src/dom.ts +3 -6
  29. package/src/drag-and-drop.tsx +669 -0
  30. package/src/dropzone.tsx +208 -0
  31. package/src/file-trigger.tsx +103 -0
  32. package/src/form.tsx +18 -18
  33. package/src/gridlist.tsx +912 -121
  34. package/src/group.tsx +146 -0
  35. package/src/index.ts +402 -0
  36. package/src/interactions/focus-events.ts +13 -5
  37. package/src/interactions/keyboard.ts +195 -2
  38. package/src/interactions/modality.ts +18 -6
  39. package/src/interactions/open-link.ts +46 -13
  40. package/src/interactions/press.ts +35 -18
  41. package/src/label.ts +18 -6
  42. package/src/link.tsx +146 -50
  43. package/src/listbox.tsx +615 -89
  44. package/src/live.ts +4 -0
  45. package/src/menu.tsx +182 -89
  46. package/src/menubar.tsx +21 -17
  47. package/src/navigation-tree.tsx +357 -0
  48. package/src/numberfield.tsx +164 -60
  49. package/src/overlay-arrow.tsx +157 -0
  50. package/src/overlays.ts +364 -62
  51. package/src/platform.ts +2 -1
  52. package/src/preview-trigger.tsx +582 -0
  53. package/src/progress-bar-context.ts +15 -0
  54. package/src/questionnaire.tsx +109 -104
  55. package/src/radio.tsx +101 -27
  56. package/src/select.tsx +225 -145
  57. package/src/selection-indicator.tsx +74 -0
  58. package/src/selection.ts +73 -16
  59. package/src/shared-element-transition.tsx +316 -0
  60. package/src/sheet.tsx +1461 -0
  61. package/src/slider.tsx +139 -44
  62. package/src/switch.tsx +47 -21
  63. package/src/table.tsx +0 -0
  64. package/src/tabs.tsx +198 -89
  65. package/src/tag.tsx +217 -32
  66. package/src/textarea.tsx +186 -0
  67. package/src/textfield.tsx +103 -37
  68. package/src/toast-components.tsx +589 -0
  69. package/src/toggle-button-group.tsx +175 -0
  70. package/src/toggle-input.tsx +38 -0
  71. package/src/toggle.ts +68 -5
  72. package/src/tokenfield.tsx +1673 -0
  73. package/src/toolbar.tsx +15 -10
  74. package/src/tooltip.tsx +46 -29
  75. package/src/tree.tsx +2075 -0
  76. package/src/utils.ts +30 -1
  77. package/src/validation.ts +79 -22
  78. package/src/virtualizer.tsx +186 -23
package/src/tag.tsx CHANGED
@@ -19,6 +19,7 @@
19
19
  */
20
20
 
21
21
  import {
22
+ cells,
22
23
  type Accessor,
23
24
  type Child,
24
25
  For,
@@ -38,11 +39,15 @@ import { button } from "./button.tsx";
38
39
  import type { ItemAccessors, Key, ListStateOptions, Node, SelectionMode } from "./collections.ts";
39
40
  import { listState, type ListState } from "./collections.ts";
40
41
  import { focusRing } from "./focus.ts";
41
- import { useLocale } from "./i18n.ts";
42
+ import { StringDictionary, stringFormatter, useLocale } from "./i18n.ts";
43
+ import { description } from "./interactions/description.ts";
42
44
  import { focusWithin } from "./interactions/focus-events.ts";
43
45
  import { hover } from "./interactions/hover.ts";
46
+ import { modality } from "./interactions/modality.ts";
44
47
  import type { ElementRef } from "./interactions/press.ts";
45
48
  import { field, type FieldOptions } from "./label.ts";
49
+ import { SelectionIndicatorProvider } from "./selection-indicator.tsx";
50
+ import { SharedElementTransition } from "./shared-element-transition.tsx";
46
51
  import {
47
52
  ListKeyboardDelegate,
48
53
  selectableItem,
@@ -171,6 +176,9 @@ export interface TagOptions {
171
176
  baseId: MaybeAccessor<string>;
172
177
  isDisabled?: MaybeAccessor<boolean | undefined>;
173
178
  onRemove?: (keys: Set<Key>) => void;
179
+ onAction?: (key: Key) => void;
180
+ /** See `selectableItem`. @default `onAction !== undefined` */
181
+ hasAction?: MaybeAccessor<boolean | undefined>;
174
182
  }
175
183
 
176
184
  export interface TagResult {
@@ -185,8 +193,149 @@ export interface TagResult {
185
193
  isDisabled: Accessor<boolean>;
186
194
  }
187
195
 
196
+ const strings = new StringDictionary<"removeButtonLabel" | "removeDescription">({
197
+ "ar-AE": {
198
+ removeButtonLabel: "إزالة",
199
+ removeDescription: "اضغط على مفتاح DELETE لإزالة علامة.",
200
+ },
201
+ "bg-BG": {
202
+ removeButtonLabel: "Премахване",
203
+ removeDescription: "Натиснете Delete, за да премахнете маркера.",
204
+ },
205
+ "cs-CZ": {
206
+ removeButtonLabel: "Odebrat",
207
+ removeDescription: "Stisknutím klávesy Delete odeberete značku.",
208
+ },
209
+ "da-DK": {
210
+ removeButtonLabel: "Fjern",
211
+ removeDescription: "Tryk på Slet for at fjerne tag.",
212
+ },
213
+ "de-DE": {
214
+ removeButtonLabel: "Entfernen",
215
+ removeDescription: "Auf „Löschen“ drücken, um das Tag zu entfernen.",
216
+ },
217
+ "el-GR": {
218
+ removeButtonLabel: "Κατάργηση",
219
+ removeDescription: "Πατήστε Διαγραφή για να καταργήσετε την ετικέτα.",
220
+ },
221
+ "en-US": {
222
+ removeButtonLabel: "Remove",
223
+ removeDescription: "Press Delete to remove tag.",
224
+ },
225
+ "es-ES": {
226
+ removeButtonLabel: "Quitar",
227
+ removeDescription: "Pulse Eliminar para quitar la etiqueta.",
228
+ },
229
+ "et-EE": {
230
+ removeButtonLabel: "Eemalda",
231
+ removeDescription: "Sildi eemaldamiseks vajutage kustutusklahvi Delete.",
232
+ },
233
+ "fi-FI": {
234
+ removeButtonLabel: "Poista",
235
+ removeDescription: "Poista tunniste painamalla Poista-painiketta.",
236
+ },
237
+ "fr-FR": {
238
+ removeButtonLabel: "Supprimer",
239
+ removeDescription: "Appuyez sur Supprimer pour supprimer l’étiquette.",
240
+ },
241
+ "he-IL": {
242
+ removeButtonLabel: "הסר",
243
+ removeDescription: "לחץ על מחק כדי להסיר תג.",
244
+ },
245
+ "hr-HR": {
246
+ removeButtonLabel: "Ukloni",
247
+ removeDescription: "Pritisnite Delete za uklanjanje oznake.",
248
+ },
249
+ "hu-HU": {
250
+ removeButtonLabel: "Eltávolítás",
251
+ removeDescription: "Nyomja meg a Delete billentyűt a címke eltávolításához.",
252
+ },
253
+ "it-IT": {
254
+ removeButtonLabel: "Rimuovi",
255
+ removeDescription: "Premi Elimina per rimuovere il tag.",
256
+ },
257
+ "ja-JP": {
258
+ removeButtonLabel: "削除",
259
+ removeDescription: "タグを削除するには、Delete キーを押します。",
260
+ },
261
+ "ko-KR": {
262
+ removeButtonLabel: "제거",
263
+ removeDescription: "태그를 제거하려면 Delete 키를 누르십시오.",
264
+ },
265
+ "lt-LT": {
266
+ removeButtonLabel: "Pašalinti",
267
+ removeDescription: "Norėdami pašalinti žymą, paspauskite „Delete“ klavišą.",
268
+ },
269
+ "lv-LV": {
270
+ removeButtonLabel: "Noņemt",
271
+ removeDescription: "Nospiediet Delete [Dzēst], lai noņemtu tagu.",
272
+ },
273
+ "nb-NO": {
274
+ removeButtonLabel: "Fjern",
275
+ removeDescription: "Trykk på Slett for å fjerne taggen.",
276
+ },
277
+ "nl-NL": {
278
+ removeButtonLabel: "Verwijderen",
279
+ removeDescription: "Druk op Verwijderen om de tag te verwijderen.",
280
+ },
281
+ "pl-PL": {
282
+ removeButtonLabel: "Usuń",
283
+ removeDescription: "Naciśnij Usuń, aby usunąć znacznik.",
284
+ },
285
+ "pt-BR": {
286
+ removeButtonLabel: "Remover",
287
+ removeDescription: "Pressione Delete para remover a tag.",
288
+ },
289
+ "pt-PT": {
290
+ removeButtonLabel: "Eliminar",
291
+ removeDescription: "Prima Delete para eliminar a tag.",
292
+ },
293
+ "ro-RO": {
294
+ removeButtonLabel: "Îndepărtaţi",
295
+ removeDescription: "Apăsați pe Delete (Ștergere) pentru a elimina eticheta.",
296
+ },
297
+ "ru-RU": {
298
+ removeButtonLabel: "Удалить",
299
+ removeDescription: "Нажмите DELETE, чтобы удалить тег.",
300
+ },
301
+ "sk-SK": {
302
+ removeButtonLabel: "Odstrániť",
303
+ removeDescription: "Ak chcete odstrániť značku, stlačte kláves Delete.",
304
+ },
305
+ "sl-SI": {
306
+ removeButtonLabel: "Odstrani",
307
+ removeDescription: "Pritisnite Delete, da odstranite oznako.",
308
+ },
309
+ "sr-SP": {
310
+ removeButtonLabel: "Ukloni",
311
+ removeDescription: "Pritisnite Obriši da biste uklonili oznaku.",
312
+ },
313
+ "sv-SE": {
314
+ removeButtonLabel: "Ta bort",
315
+ removeDescription: "Tryck på Radera för att ta bort taggen.",
316
+ },
317
+ "tr-TR": {
318
+ removeButtonLabel: "Kaldır",
319
+ removeDescription: "Etiketi kaldırmak için Sil tuşuna basın.",
320
+ },
321
+ "uk-UA": {
322
+ removeButtonLabel: "Вилучити",
323
+ removeDescription: "Натисніть Delete, щоб вилучити тег.",
324
+ },
325
+ "zh-CN": {
326
+ removeButtonLabel: "删除",
327
+ removeDescription: "按下“删除”以删除标记。",
328
+ },
329
+ "zh-TW": {
330
+ removeButtonLabel: "移除",
331
+ removeDescription: "按 Delete 鍵以移除標記。",
332
+ },
333
+ });
334
+
188
335
  export function tag(options: TagOptions, state: ListState<unknown>): TagResult {
189
336
  const buttonId = id();
337
+ const message = stringFormatter(strings);
338
+ const how = modality();
190
339
  const rowId = (): string => tagIdFor(access(options.baseId), options.key);
191
340
  const node = (): Node<unknown> | null => state.collection().getItem(options.key);
192
341
 
@@ -197,14 +346,19 @@ export function tag(options: TagOptions, state: ListState<unknown>): TagResult {
197
346
  selectionManager: state.selectionManager,
198
347
  isDisabled: options.isDisabled,
199
348
  linkBehavior: "override",
349
+ hasAction: () => access(options.hasAction) ?? options.onAction !== undefined,
350
+ onAction: () => options.onAction?.(options.key),
200
351
  });
201
352
 
202
353
  const allowsRemoving = (): boolean => options.onRemove !== undefined;
203
354
 
204
- const remove = (): void => {
355
+ /**
356
+ * Delete on a tag that is one of SEVERAL selected removes them all: the
357
+ * selection is what the user pointed at. The button is on one tag, and
358
+ * removes that one.
359
+ */
360
+ const removeFromKeyboard = (): void => {
205
361
  if (item.isDisabled()) return;
206
- // Removing one of SEVERAL selected tags removes them all: the selection is
207
- // what the user pointed at, and taking one out of it would be a surprise.
208
362
  const manager = state.selectionManager();
209
363
  if (manager.isSelected(options.key)) {
210
364
  options.onRemove?.(new Set(manager.selectedKeys));
@@ -217,9 +371,17 @@ export function tag(options: TagOptions, state: ListState<unknown>): TagResult {
217
371
  if (!allowsRemoving() || item.isDisabled()) return;
218
372
  if (event.key !== "Delete" && event.key !== "Backspace") return;
219
373
  event.preventDefault();
220
- remove();
374
+ removeFromKeyboard();
221
375
  };
222
376
 
377
+ // Delete is the only way to remove a tag without a pointer, and nothing on
378
+ // screen says so; a pointer user has the button and needs no hint.
379
+ const hint = description(() =>
380
+ allowsRemoving() && (how() === "keyboard" || how() === "virtual")
381
+ ? message().format("removeDescription")
382
+ : undefined,
383
+ );
384
+
223
385
  return {
224
386
  allowsRemoving,
225
387
  isSelected: item.isSelected,
@@ -235,16 +397,19 @@ export function tag(options: TagOptions, state: ListState<unknown>): TagResult {
235
397
  "aria-selected": () =>
236
398
  state.selectionManager().canSelectItem(options.key) ? item.isSelected() : undefined,
237
399
  "aria-disabled": () => item.isDisabled() || undefined,
400
+ "aria-describedby": hint["aria-describedby"],
238
401
  }),
239
402
  removeButtonProps: {
240
403
  id: buttonId,
241
404
  // Named by itself AND by the tag, so it is announced as "Remove,
242
405
  // Barcelona" rather than as one of a row of identical Remove buttons.
243
- "aria-label": "Remove",
406
+ "aria-label": () => message().format("removeButtonLabel"),
244
407
  "aria-labelledby": () => `${buttonId()} ${rowId()}`,
245
408
  isDisabled: item.isDisabled,
246
409
  excludeFromTabOrder: true,
247
- onPress: remove,
410
+ onPress: () => {
411
+ if (!item.isDisabled()) options.onRemove?.(new Set([options.key]));
412
+ },
248
413
  },
249
414
  };
250
415
  }
@@ -257,6 +422,7 @@ interface TagGroupContextValue {
257
422
  state: ListState<unknown>;
258
423
  baseId: Accessor<string>;
259
424
  onRemove: Accessor<((keys: Set<Key>) => void) | undefined>;
425
+ onAction?: Accessor<((key: Key) => void) | undefined>;
260
426
  }
261
427
 
262
428
  const TagGroupContext = createContext<TagGroupContextValue | null>(null);
@@ -279,10 +445,10 @@ export function useTagNode(): Node<unknown> {
279
445
  return node;
280
446
  }
281
447
 
282
- function TagGroupProvider(props: Incoming<{ value: TagGroupContextValue; children?: Child }>) {
448
+ function TagGroupProvider(props: { value: TagGroupContextValue; children?: Child }) {
283
449
  return provide(
284
450
  TagGroupContext,
285
- () => props.value(),
451
+ () => props.value,
286
452
  () => props.children as unknown,
287
453
  );
288
454
  }
@@ -306,6 +472,8 @@ export interface TagGroupComponentProps<T> extends StyleProps, ItemAccessors<T>
306
472
  onSelectionChange?: (keys: "all" | Set<Key>) => void;
307
473
  /** Without it the tags have no remove button and cannot be deleted. */
308
474
  onRemove?: (keys: Set<Key>) => void;
475
+ /** A tag was activated: pressed, or Enter on it. */
476
+ onAction?: (key: Key) => void;
309
477
  }
310
478
 
311
479
  /**
@@ -315,20 +483,20 @@ export interface TagGroupComponentProps<T> extends StyleProps, ItemAccessors<T>
315
483
  * </TagGroup>
316
484
  * ```
317
485
  */
318
- export function TagGroup<T>(props: Incoming<TagGroupComponentProps<T>>) {
486
+ export function TagGroup<T>(props: TagGroupComponentProps<T>) {
319
487
  const domRef = makeRef<HTMLDivElement>();
320
- const options = fromProps(props as unknown as Incoming<Record<string, unknown>>);
488
+ const options = fromProps(cells(props) as unknown as Incoming<Record<string, unknown>>);
321
489
 
322
490
  const state = listState<T>({
323
491
  ...(options as ListStateOptions<T>),
324
- onSelectionChange: (keys) => props.onSelectionChange?.()?.(keys),
492
+ onSelectionChange: (keys) => props.onSelectionChange?.(keys),
325
493
  });
326
494
 
327
495
  const { gridProps, labelProps, descriptionProps, errorMessageProps, baseId } = tagGroup(
328
496
  {
329
497
  ...(options as unknown as TagGroupOptions),
330
498
  ref: domRef,
331
- onRemove: props.onRemove?.() === undefined ? undefined : (keys) => props.onRemove?.()?.(keys),
499
+ onRemove: props.onRemove === undefined ? undefined : (keys) => props.onRemove?.(keys),
332
500
  },
333
501
  state,
334
502
  );
@@ -336,16 +504,22 @@ export function TagGroup<T>(props: Incoming<TagGroupComponentProps<T>>) {
336
504
  const value: TagGroupContextValue = {
337
505
  state: state,
338
506
  baseId,
339
- onRemove: () => props.onRemove?.(),
507
+ onRemove: () => props.onRemove,
508
+ onAction: () => props.onAction,
340
509
  };
341
510
 
511
+ const { focusProps, isFocused, isFocusVisible } = focusRing();
512
+
342
513
  const elementProps = mergeProps(
343
514
  gridProps,
344
- filterDOMProps(fromProps(props), { global: true }),
345
- styleProps(props),
515
+ focusProps,
516
+ filterDOMProps(fromProps(cells(props)), { global: true }),
517
+ styleProps(cells(props)),
346
518
  {
347
519
  "data-empty": () => state.collection().size === 0,
348
- "data-testid": () => props["data-testid"]?.(),
520
+ "data-focused": isFocused,
521
+ "data-focus-visible": isFocusVisible,
522
+ "data-testid": () => props["data-testid"],
349
523
  },
350
524
  );
351
525
 
@@ -354,14 +528,16 @@ export function TagGroup<T>(props: Incoming<TagGroupComponentProps<T>>) {
354
528
  return (
355
529
  <TagGroupProvider value={value}>
356
530
  <span {...labelProps}>{props.label}</span>
357
- <div {...elementProps} ref={mergeRefs(domRef.set, props.ref?.())}>
358
- <For each={() => [...state.collection()]}>
359
- {(node: Node<T>) => {
360
- const rowOwner = getOwner();
361
- if (rowOwner !== null) install(rowOwner, TagNodeContext, () => node as Node<unknown>);
362
- return render(node.value as T);
363
- }}
364
- </For>
531
+ <div {...elementProps} ref={mergeRefs(domRef.set, props.ref)}>
532
+ <SharedElementTransition>
533
+ <For each={() => [...state.collection()]}>
534
+ {(node: Node<T>) => {
535
+ const rowOwner = getOwner();
536
+ if (rowOwner !== null) install(rowOwner, TagNodeContext, () => node as Node<unknown>);
537
+ return render(node.value as T);
538
+ }}
539
+ </For>
540
+ </SharedElementTransition>
365
541
  </div>
366
542
  <span {...descriptionProps}>{props.description}</span>
367
543
  <span {...errorMessageProps}>{props.errorMessage}</span>
@@ -382,7 +558,7 @@ export interface TagComponentProps extends StyleProps {
382
558
  * on it does the same thing. A second stop per tag would double the length of
383
559
  * every filter bar.
384
560
  */
385
- export function Tag(props: Incoming<TagComponentProps>) {
561
+ export function Tag(props: TagComponentProps) {
386
562
  const domRef = makeRef<HTMLDivElement>();
387
563
  const buttonRef = makeRef<HTMLButtonElement>();
388
564
  const group = useTagGroup();
@@ -404,6 +580,8 @@ export function Tag(props: Incoming<TagComponentProps>) {
404
580
  baseId: group.baseId,
405
581
  isDisabled: () => node.props?.isDisabled === true,
406
582
  onRemove: group.onRemove() === undefined ? undefined : (keys) => group.onRemove()?.(keys),
583
+ hasAction: () => group.onAction?.() !== undefined,
584
+ onAction: (key) => group.onAction?.()?.(key),
407
585
  },
408
586
  group.state,
409
587
  );
@@ -416,10 +594,10 @@ export function Tag(props: Incoming<TagComponentProps>) {
416
594
  rowProps,
417
595
  hoverProps,
418
596
  focusProps,
419
- filterDOMProps(fromProps(props), { global: true }),
420
- styleProps(props),
597
+ filterDOMProps(fromProps(cells(props)), { global: true }),
598
+ styleProps(cells(props)),
421
599
  {
422
- "aria-label": () => props["aria-label"]?.(),
600
+ "aria-label": () => props["aria-label"],
423
601
  "data-selected": isSelected,
424
602
  "data-focused": isFocused,
425
603
  "data-focus-visible": isFocusVisible,
@@ -427,7 +605,12 @@ export function Tag(props: Incoming<TagComponentProps>) {
427
605
  "data-hovered": isHovered,
428
606
  "data-disabled": isDisabled,
429
607
  "data-removable": allowsRemoving,
430
- "data-testid": () => props["data-testid"]?.(),
608
+ "data-allows-removing": allowsRemoving,
609
+ "data-selection-mode": () => {
610
+ const mode = group.state.selectionManager().selectionMode;
611
+ return mode === "none" ? undefined : mode;
612
+ },
613
+ "data-testid": () => props["data-testid"],
431
614
  },
432
615
  );
433
616
 
@@ -436,9 +619,11 @@ export function Tag(props: Incoming<TagComponentProps>) {
436
619
  );
437
620
 
438
621
  return (
439
- <div {...elementProps} ref={mergeRefs(domRef.set, props.ref?.())}>
622
+ <div {...elementProps} ref={mergeRefs(domRef.set, props.ref)}>
440
623
  <div {...gridCellProps}>
441
- {props.children}
624
+ <SelectionIndicatorProvider isSelected={isSelected()}>
625
+ {props.children}
626
+ </SelectionIndicatorProvider>
442
627
  {() =>
443
628
  allowsRemoving() ? (
444
629
  <button {...removeProps()} ref={buttonRef.set}>
@@ -0,0 +1,186 @@
1
+ /**
2
+ * A text area on its own: a `<textarea>` with the interaction states a
3
+ * stylesheet needs.
4
+ *
5
+ * A labelled, validated multi-line field is `<TextField isMultiLine>`, which
6
+ * wires the label, description and error message to the control. This is the
7
+ * control alone, for a caller that labels it some other way: a chat composer
8
+ * named by `aria-label`, a cell editor inside a grid.
9
+ */
10
+
11
+ import { cells, type Accessor } from "@barqjs/core";
12
+ import { mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
13
+ import { focusRing } from "./focus.ts";
14
+ import { hover, type HoverEvent } from "./interactions/hover.ts";
15
+ import {
16
+ access,
17
+ controllable,
18
+ type DOMProps,
19
+ filterDOMProps,
20
+ fromProps,
21
+ type MaybeAccessor,
22
+ mergeProps,
23
+ type StyleProps,
24
+ styleProps,
25
+ } from "./utils.ts";
26
+
27
+ export interface TextAreaOptions {
28
+ value?: MaybeAccessor<string | undefined>;
29
+ defaultValue?: MaybeAccessor<string | undefined>;
30
+ isDisabled?: MaybeAccessor<boolean | undefined>;
31
+ isReadOnly?: MaybeAccessor<boolean | undefined>;
32
+ isRequired?: MaybeAccessor<boolean | undefined>;
33
+ isInvalid?: MaybeAccessor<boolean | undefined>;
34
+ autoFocus?: MaybeAccessor<boolean | undefined>;
35
+ onChange?: (value: string) => void;
36
+ onHoverStart?: (event: HoverEvent) => void;
37
+ onHoverEnd?: (event: HoverEvent) => void;
38
+ onHoverChange?: (isHovered: boolean) => void;
39
+ }
40
+
41
+ export interface TextAreaResult {
42
+ textAreaProps: DOMProps;
43
+ value: Accessor<string>;
44
+ isHovered: Accessor<boolean>;
45
+ isFocused: Accessor<boolean>;
46
+ isFocusVisible: Accessor<boolean>;
47
+ }
48
+
49
+ export function textArea(options: TextAreaOptions = {}): TextAreaResult {
50
+ const [value, setValue] = controllable<string>(
51
+ () => access(options.value),
52
+ () => access(options.defaultValue) ?? "",
53
+ options.onChange,
54
+ );
55
+ const isDisabled = (): boolean => access(options.isDisabled) === true;
56
+ const isInvalid = (): boolean => access(options.isInvalid) === true;
57
+
58
+ const { hoverProps, isHovered } = hover({
59
+ isDisabled,
60
+ onHoverStart: options.onHoverStart,
61
+ onHoverEnd: options.onHoverEnd,
62
+ onHoverChange: options.onHoverChange,
63
+ });
64
+ const { focusProps, isFocused, isFocusVisible } = focusRing({
65
+ isTextInput: true,
66
+ autoFocus: options.autoFocus,
67
+ });
68
+
69
+ const onInput = (event: Event): void => {
70
+ const element = event.target as HTMLTextAreaElement;
71
+ setValue(element.value);
72
+ // A controlled owner that declined re-renders nothing on a value that did
73
+ // not move, so the element is put back by hand.
74
+ if (element.value !== value()) element.value = value();
75
+ };
76
+
77
+ return {
78
+ value,
79
+ isHovered,
80
+ isFocused,
81
+ isFocusVisible,
82
+ textAreaProps: mergeProps(hoverProps, focusProps, {
83
+ value,
84
+ disabled: isDisabled,
85
+ readOnly: () => access(options.isReadOnly) === true,
86
+ required: () => access(options.isRequired) === true,
87
+ autofocus: () => access(options.autoFocus) === true || undefined,
88
+ "aria-invalid": () => isInvalid() || undefined,
89
+ onInput,
90
+ "data-hovered": isHovered,
91
+ "data-focused": isFocused,
92
+ "data-focus-visible": isFocusVisible,
93
+ "data-disabled": isDisabled,
94
+ "data-invalid": isInvalid,
95
+ }),
96
+ };
97
+ }
98
+
99
+ const TEXTAREA_ATTRIBUTES = new Set([
100
+ "aria-controls",
101
+ "aria-activedescendant",
102
+ "aria-autocomplete",
103
+ "aria-errormessage",
104
+ "aria-haspopup",
105
+ "onKeyDown",
106
+ "onKeyUp",
107
+ "onBeforeInput",
108
+ "onSelect",
109
+ "onCopy",
110
+ "onCut",
111
+ "onPaste",
112
+ "onCompositionStart",
113
+ "onCompositionEnd",
114
+ "onCompositionUpdate",
115
+ ]);
116
+
117
+ export interface TextAreaComponentProps extends StyleProps {
118
+ value?: string;
119
+ defaultValue?: string;
120
+ isDisabled?: boolean;
121
+ isReadOnly?: boolean;
122
+ isRequired?: boolean;
123
+ isInvalid?: boolean;
124
+ autoFocus?: boolean;
125
+ name?: string;
126
+ form?: string;
127
+ placeholder?: string;
128
+ rows?: number;
129
+ cols?: number;
130
+ wrap?: "hard" | "soft" | "off";
131
+ maxLength?: number;
132
+ minLength?: number;
133
+ autoComplete?: string;
134
+ spellcheck?: boolean | "true" | "false";
135
+ inputMode?: string;
136
+ enterKeyHint?: string;
137
+ "aria-label"?: string;
138
+ "aria-labelledby"?: string;
139
+ "aria-describedby"?: string;
140
+ "aria-details"?: string;
141
+ "aria-controls"?: string;
142
+ "aria-activedescendant"?: string;
143
+ "aria-autocomplete"?: "none" | "inline" | "list" | "both";
144
+ "aria-errormessage"?: string;
145
+ ref?: RefTarget<HTMLTextAreaElement>;
146
+ onChange?: (value: string) => void;
147
+ onKeyDown?: (event: KeyboardEvent) => void;
148
+ onKeyUp?: (event: KeyboardEvent) => void;
149
+ onBeforeInput?: (event: InputEvent) => void;
150
+ onHoverStart?: (event: HoverEvent) => void;
151
+ onHoverEnd?: (event: HoverEvent) => void;
152
+ onHoverChange?: (isHovered: boolean) => void;
153
+ }
154
+
155
+ /**
156
+ * ```tsx
157
+ * <TextArea aria-label="Message" rows={4} onChange={(text) => draft.set(text)} />
158
+ * ```
159
+ */
160
+ export function TextArea(props: TextAreaComponentProps) {
161
+ const options = fromProps(cells(props));
162
+ const { textAreaProps } = textArea(options);
163
+
164
+ const elementProps = mergeProps(
165
+ textAreaProps,
166
+ {
167
+ name: () => props.name,
168
+ form: () => props.form,
169
+ placeholder: () => props.placeholder,
170
+ rows: () => props.rows,
171
+ cols: () => props.cols,
172
+ wrap: () => props.wrap,
173
+ maxlength: () => props.maxLength,
174
+ minlength: () => props.minLength,
175
+ autocomplete: () => props.autoComplete,
176
+ spellcheck: () => props.spellcheck,
177
+ inputmode: () => props.inputMode,
178
+ enterkeyhint: () => props.enterKeyHint,
179
+ },
180
+ filterDOMProps(options, { global: true, labelable: true, propNames: TEXTAREA_ATTRIBUTES }),
181
+ styleProps(cells(props)),
182
+ { "data-testid": () => props["data-testid"] },
183
+ );
184
+
185
+ return <textarea {...elementProps} ref={mergeRefs(props.ref)} />;
186
+ }