@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/slider.tsx CHANGED
@@ -21,6 +21,7 @@
21
21
  */
22
22
 
23
23
  import {
24
+ cells,
24
25
  type Accessor,
25
26
  type Child,
26
27
  For,
@@ -29,7 +30,6 @@ import {
29
30
  createEffect,
30
31
  getContext,
31
32
  getOwner,
32
- type Incoming,
33
33
  install,
34
34
  createSignal,
35
35
  onSettled,
@@ -679,19 +679,22 @@ export function sliderThumb(options: SliderThumbOptions, state: SliderState): Sl
679
679
  return flipped() ? 1 - percent : percent;
680
680
  };
681
681
 
682
- const interactions: DOMProps = isDisabled()
683
- ? {}
684
- : mergeProps(moveProps, {
685
- onMouseDown: (event: MouseEvent) => {
686
- if (event.button !== 0 || event.altKey || event.ctrlKey || event.metaKey) return;
687
- onDown();
688
- },
689
- onPointerDown: (event: PointerEvent) => {
690
- if (event.button !== 0 || event.altKey || event.ctrlKey || event.metaKey) return;
691
- onDown(event.pointerId);
692
- },
693
- onTouchStart: (event: TouchEvent) => onDown(event.changedTouches[0]?.identifier),
694
- });
682
+ // Bound once and asked on every event, so a slider enabled after it was
683
+ // built answers a drag on its thumb.
684
+ const interactions = whileEnabled(
685
+ mergeProps(moveProps, {
686
+ onMouseDown: (event: MouseEvent) => {
687
+ if (event.button !== 0 || event.altKey || event.ctrlKey || event.metaKey) return;
688
+ onDown();
689
+ },
690
+ onPointerDown: (event: PointerEvent) => {
691
+ if (event.button !== 0 || event.altKey || event.ctrlKey || event.metaKey) return;
692
+ onDown(event.pointerId);
693
+ },
694
+ onTouchStart: (event: TouchEvent) => onDown(event.changedTouches[0]?.identifier),
695
+ }),
696
+ isDisabled,
697
+ );
695
698
 
696
699
  return {
697
700
  isDragging: () => state.isThumbDragging(index),
@@ -740,6 +743,20 @@ export function sliderThumb(options: SliderThumbOptions, state: SliderState): Sl
740
743
  };
741
744
  }
742
745
 
746
+ /** Every handler in `props`, doing nothing while `isDisabled` says so. */
747
+ function whileEnabled(props: DOMProps, isDisabled: Accessor<boolean>): DOMProps {
748
+ const guarded: DOMProps = {};
749
+ for (const [key, value] of Object.entries(props)) {
750
+ guarded[key] =
751
+ /^on[A-Z]/.test(key) && typeof value === "function"
752
+ ? (event: Event) => {
753
+ if (!isDisabled()) (value as (event: Event) => void)(event);
754
+ }
755
+ : value;
756
+ }
757
+ return guarded;
758
+ }
759
+
743
760
  // ---------------------------------------------------------------------------
744
761
  // Components
745
762
  // ---------------------------------------------------------------------------
@@ -805,20 +822,20 @@ export interface SliderComponentProps extends StyleProps {
805
822
  * and a thumb per value. Compose `<SliderOutput>`, `<SliderTrack>` and
806
823
  * `<SliderThumb>` yourself for anything else.
807
824
  */
808
- export function Slider(props: Incoming<SliderComponentProps>) {
825
+ export function Slider(props: SliderComponentProps) {
809
826
  const trackRef = makeRef<HTMLDivElement>();
810
- const options = fromProps(props);
827
+ const options = fromProps(cells(props));
811
828
 
812
829
  const state = sliderState({
813
830
  ...(options as SliderStateOptions),
814
- onChange: (value) => props.onChange?.()?.(value),
815
- onChangeEnd: (value) => props.onChangeEnd?.()?.(value),
831
+ onChange: (value) => props.onChange?.(value),
832
+ onChangeEnd: (value) => props.onChangeEnd?.(value),
816
833
  });
817
834
 
818
835
  // The caller's `id` is the first thumb's, because the thumb is what a
819
836
  // `<label for>` elsewhere on the page can name and focus: on the group it
820
837
  // pointed at a `<div>`, which a label reaches neither way.
821
- const firstThumbId = (): string | undefined => props.id?.();
838
+ const firstThumbId = (): string | undefined => props.id;
822
839
  const { id: _, ...groupOptions } = options as unknown as SliderOptions & { id?: unknown };
823
840
  const { labelProps, groupProps, trackProps, outputProps, baseId } = slider(
824
841
  { ...groupOptions, trackRef, firstThumbId },
@@ -826,8 +843,8 @@ export function Slider(props: Incoming<SliderComponentProps>) {
826
843
  );
827
844
  const ownName = (): boolean =>
828
845
  props.label !== undefined ||
829
- props["aria-label"]?.() !== undefined ||
830
- props["aria-labelledby"]?.() !== undefined;
846
+ props["aria-label"] !== undefined ||
847
+ props["aria-labelledby"] !== undefined;
831
848
 
832
849
  // Named only by a `<label for>` on its first thumb, the group takes its name
833
850
  // from the same label. Asked once the slider is in the document, where the
@@ -874,27 +891,27 @@ export function Slider(props: Incoming<SliderComponentProps>) {
874
891
  : access(groupProps.id as MaybeAccessor<string | undefined>)),
875
892
  firstThumbId,
876
893
  outputProps,
877
- name: () => props.name?.(),
878
- form: () => props.form?.(),
894
+ name: () => props.name,
895
+ form: () => props.form,
879
896
  }));
880
897
  }
881
898
 
882
899
  const elementProps = mergeProps(
883
900
  groupProps,
884
901
  filterDOMProps(groupOptions, { global: true }),
885
- styleProps(props),
902
+ styleProps(cells(props)),
886
903
  {
887
904
  "aria-labelledby": () =>
888
905
  access(groupProps["aria-labelledby"] as MaybeAccessor<string | undefined>) ?? namedBy(),
889
906
  "data-orientation": state.orientation,
890
907
  "data-inverted": state.isInverted,
891
908
  "data-disabled": state.isDisabled,
892
- "data-testid": () => props["data-testid"]?.(),
909
+ "data-testid": () => props["data-testid"],
893
910
  },
894
911
  );
895
912
 
896
913
  return (
897
- <div {...elementProps} ref={mergeRefs(props.ref?.())}>
914
+ <div {...elementProps} ref={mergeRefs(props.ref)}>
898
915
  <span {...labelProps}>{props.label}</span>
899
916
  {() =>
900
917
  props.children === undefined ? (
@@ -917,15 +934,15 @@ export interface SliderOutputComponentProps extends StyleProps {
917
934
  }
918
935
 
919
936
  /** The current value, as text. */
920
- export function SliderOutput(props: Incoming<SliderOutputComponentProps>) {
937
+ export function SliderOutput(props: SliderOutputComponentProps) {
921
938
  const group = useSlider();
922
939
 
923
940
  const elementProps = mergeProps(
924
941
  group.outputProps,
925
- filterDOMProps(fromProps(props), { global: true }),
926
- styleProps(props),
942
+ filterDOMProps(fromProps(cells(props)), { global: true }),
943
+ styleProps(cells(props)),
927
944
  {
928
- "data-testid": () => props["data-testid"]?.(),
945
+ "data-testid": () => props["data-testid"],
929
946
  },
930
947
  );
931
948
 
@@ -950,23 +967,23 @@ export interface SliderTrackComponentProps extends StyleProps {
950
967
  * A press anywhere on it moves the nearest thumb there, which is what makes a
951
968
  * slider usable with one gesture rather than requiring the thumb to be hit.
952
969
  */
953
- export function SliderTrack(props: Incoming<SliderTrackComponentProps>) {
970
+ export function SliderTrack(props: SliderTrackComponentProps) {
954
971
  const group = useSlider();
955
972
  const render = props.children;
956
973
 
957
974
  const elementProps = mergeProps(
958
- props.trackProps?.() ?? {},
959
- filterDOMProps(fromProps(props), { global: true }),
960
- styleProps(props),
975
+ props.trackProps ?? {},
976
+ filterDOMProps(fromProps(cells(props)), { global: true }),
977
+ styleProps(cells(props)),
961
978
  {
962
979
  "data-orientation": group.state.orientation,
963
980
  "data-disabled": group.state.isDisabled,
964
- "data-testid": () => props["data-testid"]?.(),
981
+ "data-testid": () => props["data-testid"],
965
982
  },
966
983
  );
967
984
 
968
985
  return (
969
- <div {...elementProps} ref={mergeRefs(group.trackRef.set, props.ref?.())}>
986
+ <div {...elementProps} ref={mergeRefs(group.trackRef.set, props.ref)}>
970
987
  <For each={() => group.state.values().map((_, index) => index)}>
971
988
  {(index: number) => render(index)}
972
989
  </For>
@@ -974,6 +991,84 @@ export function SliderTrack(props: Incoming<SliderTrackComponentProps>) {
974
991
  );
975
992
  }
976
993
 
994
+ export interface SliderFillComponentProps extends StyleProps {
995
+ /**
996
+ * Where a single thumb's fill starts, as a value: the middle of a balance
997
+ * control fills outwards from zero. A range fills between its first and last
998
+ * thumbs and ignores it.
999
+ *
1000
+ * @default the minimum
1001
+ */
1002
+ offset?: number;
1003
+ ref?: RefTarget<HTMLDivElement>;
1004
+ }
1005
+
1006
+ /**
1007
+ * The part of the track that is selected, placed inside `<SliderTrack>`.
1008
+ *
1009
+ * It is positioned the way the thumbs are, so it follows right to left, a
1010
+ * vertical slider and `isInverted` without anything set on it. A `style` given
1011
+ * here is laid over that rather than replacing it.
1012
+ */
1013
+ export function SliderFill(props: SliderFillComponentProps) {
1014
+ const group = useSlider();
1015
+ const state = group.state;
1016
+ const locale = useLocale();
1017
+ const { hoverProps, isHovered } = hover({});
1018
+
1019
+ /** Where it starts and how much it covers, as fractions of the track. */
1020
+ const span = (): [number, number] => {
1021
+ const values = state.values();
1022
+ let start: number;
1023
+ if (values.length > 1) {
1024
+ start = state.getThumbPercent(0);
1025
+ } else {
1026
+ const offset = props.offset;
1027
+ const min = state.getThumbMinValue(0);
1028
+ start = state.getValuePercent(
1029
+ offset === undefined ? min : clamp(offset, min, state.getThumbMaxValue(0)),
1030
+ );
1031
+ }
1032
+ const end = values.length === 0 ? 0 : state.getThumbPercent(values.length - 1);
1033
+ const low = Math.min(start, end);
1034
+ const high = Math.max(start, end);
1035
+ const flipped = runsBackwards(state, locale().direction === "rtl");
1036
+ return [flipped ? 1 - high : low, high - low];
1037
+ };
1038
+
1039
+ const elementProps = mergeProps(
1040
+ hoverProps,
1041
+ filterDOMProps(fromProps(cells(props)), { global: true }),
1042
+ styleProps(cells(props)),
1043
+ {
1044
+ style: () => {
1045
+ const [from, size] = span();
1046
+ const placed =
1047
+ state.orientation() === "vertical"
1048
+ ? {
1049
+ position: "absolute",
1050
+ top: `${from * 100}%`,
1051
+ height: `${size * 100}%`,
1052
+ width: "100%",
1053
+ }
1054
+ : {
1055
+ position: "absolute",
1056
+ left: `${from * 100}%`,
1057
+ width: `${size * 100}%`,
1058
+ height: "100%",
1059
+ };
1060
+ return { ...placed, ...props.style };
1061
+ },
1062
+ "data-orientation": state.orientation,
1063
+ "data-disabled": state.isDisabled,
1064
+ "data-hovered": isHovered,
1065
+ "data-testid": () => props["data-testid"],
1066
+ },
1067
+ );
1068
+
1069
+ return <div {...elementProps} ref={mergeRefs(props.ref)} />;
1070
+ }
1071
+
977
1072
  export interface SliderThumbComponentProps extends StyleProps {
978
1073
  children?: Child;
979
1074
  /** @default 0 */
@@ -989,10 +1084,10 @@ export interface SliderThumbComponentProps extends StyleProps {
989
1084
  * The input is what the keyboard, the form and assistive technology all talk
990
1085
  * to; the element around it is only what the user sees.
991
1086
  */
992
- export function SliderThumb(props: Incoming<SliderThumbComponentProps>) {
1087
+ export function SliderThumb(props: SliderThumbComponentProps) {
993
1088
  const group = useSlider();
994
1089
  const inputRef = makeRef<HTMLInputElement>();
995
- const index = props.index?.() ?? 0;
1090
+ const index = props.index ?? 0;
996
1091
 
997
1092
  const { thumbProps, inputProps, isDragging, isFocused, isDisabled } = sliderThumb(
998
1093
  {
@@ -1002,10 +1097,10 @@ export function SliderThumb(props: Incoming<SliderThumbComponentProps>) {
1002
1097
  baseId: group.baseId,
1003
1098
  id: () => (index === 0 ? group.firstThumbId() : undefined),
1004
1099
  groupLabelId: group.groupLabelId,
1005
- isDisabled: () => props.isDisabled?.(),
1100
+ isDisabled: () => props.isDisabled,
1006
1101
  name: group.name,
1007
1102
  form: group.form,
1008
- "aria-label": () => props["aria-label"]?.(),
1103
+ "aria-label": () => props["aria-label"],
1009
1104
  },
1010
1105
  group.state,
1011
1106
  );
@@ -1017,15 +1112,15 @@ export function SliderThumb(props: Incoming<SliderThumbComponentProps>) {
1017
1112
  const elementProps = mergeProps(
1018
1113
  thumbProps,
1019
1114
  hoverProps,
1020
- filterDOMProps(fromProps(props), { global: true }),
1021
- styleProps(props),
1115
+ filterDOMProps(fromProps(cells(props)), { global: true }),
1116
+ styleProps(cells(props)),
1022
1117
  {
1023
1118
  "data-dragging": isDragging,
1024
1119
  "data-focused": isFocused,
1025
1120
  "data-focus-visible": isFocusVisible,
1026
1121
  "data-hovered": isHovered,
1027
1122
  "data-disabled": isDisabled,
1028
- "data-testid": () => props["data-testid"]?.(),
1123
+ "data-testid": () => props["data-testid"],
1029
1124
  },
1030
1125
  );
1031
1126
 
@@ -1034,7 +1129,7 @@ export function SliderThumb(props: Incoming<SliderThumbComponentProps>) {
1034
1129
  });
1035
1130
 
1036
1131
  return (
1037
- <div {...elementProps} ref={mergeRefs(props.ref?.())}>
1132
+ <div {...elementProps} ref={mergeRefs(props.ref)}>
1038
1133
  <input {...hiddenInputProps} ref={inputRef.set} />
1039
1134
  {props.children}
1040
1135
  </div>
package/src/switch.tsx CHANGED
@@ -30,7 +30,7 @@ import {
30
30
  type ToggleOptions,
31
31
  type ToggleResult,
32
32
  } from "./toggle.ts";
33
- import { toggleInput } from "./toggle-input.tsx";
33
+ import { toggleInput, withHelpText } from "./toggle-input.tsx";
34
34
  import type { ValidateFunction, ValidationBehavior } from "./validation.ts";
35
35
  import {
36
36
  callback,
@@ -75,6 +75,13 @@ export interface SwitchComponentProps extends StyleProps {
75
75
  validate?: ValidateFunction<boolean>;
76
76
  /** @default "aria" */
77
77
  validationBehavior?: ValidationBehavior;
78
+ /** Help text, rendered after the switch and announced with it. */
79
+ description?: Child;
80
+ /**
81
+ * What is wrong, rendered after the switch while it is invalid. Without one,
82
+ * whatever validation found is shown.
83
+ */
84
+ errorMessage?: Child;
78
85
  value?: string;
79
86
  name?: string;
80
87
  form?: string;
@@ -131,16 +138,26 @@ export function Switch(props: Incoming<SwitchComponentProps>) {
131
138
  const options = fromProps(props);
132
139
 
133
140
  const state = toggleState(options);
134
- const { labelProps, inputProps, isSelected, isPressed, isDisabled, isReadOnly, isInvalid } =
135
- switchToggle(
136
- {
137
- ...(options as SwitchOptions),
138
- validate: callback(props.validate),
139
- validationBehavior: () => props.validationBehavior?.(),
140
- },
141
- state,
142
- inputRef,
143
- );
141
+ const {
142
+ labelProps,
143
+ inputProps,
144
+ descriptionProps,
145
+ errorMessageProps,
146
+ isSelected,
147
+ isPressed,
148
+ isDisabled,
149
+ isReadOnly,
150
+ isInvalid,
151
+ errorMessage,
152
+ } = switchToggle(
153
+ {
154
+ ...(options as SwitchOptions),
155
+ validate: callback(props.validate),
156
+ validationBehavior: () => props.validationBehavior?.(),
157
+ },
158
+ state,
159
+ inputRef,
160
+ );
144
161
  const { hoverProps, isHovered } = hover({ isDisabled: options.isDisabled });
145
162
  const { focusProps, isFocused, isFocusVisible } = focusRing();
146
163
 
@@ -170,17 +187,26 @@ export function Switch(props: Incoming<SwitchComponentProps>) {
170
187
  "data-disabled": isDisabled,
171
188
  "data-readonly": isReadOnly,
172
189
  "data-invalid": isInvalid,
190
+ "data-required": () => props.isRequired?.() === true,
173
191
  },
174
192
  );
175
193
 
176
- return toggleInput({
177
- elementType: toggleElementType(props.elementType?.(), props.children),
178
- outerProps,
179
- inputProps,
180
- focusProps,
181
- inputRef,
182
- ref: props.ref?.(),
183
- children: props.children,
184
- isDisabled,
185
- });
194
+ return withHelpText(
195
+ toggleInput({
196
+ elementType: toggleElementType(props.elementType?.(), props.children),
197
+ outerProps,
198
+ inputProps,
199
+ focusProps,
200
+ inputRef,
201
+ ref: props.ref?.(),
202
+ children: props.children,
203
+ isDisabled,
204
+ }),
205
+ {
206
+ descriptionProps,
207
+ errorMessageProps,
208
+ description: () => props.description?.(),
209
+ errorMessage: () => (isInvalid() ? errorMessage() : undefined),
210
+ },
211
+ );
186
212
  }
package/src/table.tsx CHANGED
Binary file