@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
@@ -7,7 +7,13 @@ import {
7
7
  } from "../Form/FormTypes";
8
8
  import FormValues from "../Form/FormValues.torp";
9
9
  import { createFocusGroup, type FocusItemState } from "../utils/focusGroup";
10
- import { defaultPalette, parseHex } from "../utils/colorUtils";
10
+ import {
11
+ type Color,
12
+ colorName,
13
+ colorValue,
14
+ defaultPalette,
15
+ parseHex,
16
+ } from "../utils/colorUtils";
11
17
  import createFormField from "../utils/formField";
12
18
 
13
19
  interface ColorPaletteProps {
@@ -19,8 +25,19 @@ interface ColorPaletteProps {
19
25
  style?: StyleValue;
20
26
  /** The selected color as "#rrggbb"; undefined if nothing is selected */
21
27
  value?: Bindable<string | undefined>;
22
- /** The colors to offer (default: a built-in gray + hue palette) */
23
- colors?: string[];
28
+ /**
29
+ * The colors to offer (default: a built-in gray + hue palette). Each entry
30
+ * is either a hex string or `{ value, name }`; names are used as the
31
+ * swatches' accessible names instead of the raw hex.
32
+ */
33
+ colors?: Color[];
34
+ /** The number of swatches per row, for up/down arrow movement (default 6) */
35
+ columns?: number;
36
+ /**
37
+ * If true, selecting the swatch that is already selected clears the
38
+ * value. Off by default, like a radio group.
39
+ */
40
+ allowClear?: boolean;
24
41
  /** If set to true, the user cannot interact with this palette */
25
42
  disabled?: boolean;
26
43
  /** An accessible name for the palette */
@@ -33,8 +50,11 @@ interface ColorPaletteProps {
33
50
 
34
51
  /**
35
52
  * A grid of color swatches that behaves like a radio group: one swatch can
36
- * be selected, clicking it again deselects, and the arrow keys move through
37
- * the swatches selecting as they go.
53
+ * be selected, and the arrow keys move focus and selection through the
54
+ * swatches. Left/Right move by one swatch, Up/Down by one row.
55
+ *
56
+ * Set `allowClear` to let clicking (or pressing Space on) the selected swatch
57
+ * deselect it, like Rating.
38
58
  *
39
59
  * Use ColorPicker instead when arbitrary hex values should be enterable
40
60
  * alongside the palette.
@@ -44,7 +64,7 @@ export default function ColorPalette($props: ColorPaletteProps | undefined) {
44
64
 
45
65
  // Wire up the enclosing Form and Field, if any
46
66
  const form = createFormField(
47
- $props?.name,
67
+ () => $props?.name,
48
68
  $context[FormContextName] as FormContext | undefined,
49
69
  $context[FieldContextName] as FieldContext | undefined,
50
70
  );
@@ -53,10 +73,14 @@ export default function ColorPalette($props: ColorPaletteProps | undefined) {
53
73
  return $props?.disabled === true;
54
74
  }
55
75
 
56
- function getColors(): string[] {
76
+ function getColors(): Color[] {
57
77
  return $props?.colors ?? defaultPalette;
58
78
  }
59
79
 
80
+ function getColumns(): number {
81
+ return Math.max(1, $props?.columns ?? 6);
82
+ }
83
+
60
84
  let $state = $watch({
61
85
  value: undefined as string | undefined,
62
86
  });
@@ -69,21 +93,42 @@ export default function ColorPalette($props: ColorPaletteProps | undefined) {
69
93
  return parseHex(color) ?? "";
70
94
  }
71
95
 
72
- function isSelected(color: string): boolean {
73
- return $state.value !== undefined && normalized($state.value) === normalized(color);
96
+ function isSelected(color: Color): boolean {
97
+ return $state.value !== undefined && normalized($state.value) === normalized(colorValue(color));
74
98
  }
75
99
 
76
- function select(color: string) {
100
+ function select(color: Color) {
77
101
  if (getDisabled()) return;
78
- const next = normalized(color);
79
- // Selecting the current swatch clears it, like Rating's allowClear
80
- $state.value = isSelected(color) ? undefined : next || undefined;
81
- form.handleInput();
82
- $props?.onchange?.($state.value);
102
+ let next: string | undefined = normalized(colorValue(color)) || undefined;
103
+ // Clearing only happens when explicitly allowed, like a radio group
104
+ if (getAllowClear() && isSelected(color)) {
105
+ next = undefined;
106
+ }
107
+ if (next !== $state.value) {
108
+ $state.value = next;
109
+ form.handleInput();
110
+ $props?.onchange?.(next);
111
+ }
112
+ }
113
+
114
+ function getAllowClear(): boolean {
115
+ return $props?.allowClear === true;
116
+ }
117
+
118
+ // Moves to an absolute index, focusing and selecting it. Clamps at the
119
+ // ends rather than wrapping
120
+ function moveTo(index: number) {
121
+ const colors = getColors();
122
+ if (colors.length === 0) return;
123
+ const target = Math.max(0, Math.min(colors.length - 1, index));
124
+ group.handleItemFocus(target);
125
+ group.itemStates[target]?.setFocused();
126
+ select(colors[target]);
83
127
  }
84
128
 
85
129
  function handleKey(index: number, e: KeyboardEvent) {
86
130
  if (getDisabled()) return;
131
+ const columns = getColumns();
87
132
  switch (e.key) {
88
133
  case "Enter":
89
134
  case " ": {
@@ -91,19 +136,48 @@ export default function ColorPalette($props: ColorPaletteProps | undefined) {
91
136
  select(getColors()[index]);
92
137
  break;
93
138
  }
94
- default: {
95
- const before = group.getFocusIndex();
96
- group.handleGroupKey(index, e);
97
- const after = group.getFocusIndex();
98
- if (after !== before && after >= 0) {
99
- group.handleItemFocus(after);
100
- select(getColors()[after]);
101
- }
139
+ case "ArrowRight": {
140
+ e.preventDefault();
141
+ moveTo(index + 1);
142
+ break;
143
+ }
144
+ case "ArrowLeft": {
145
+ e.preventDefault();
146
+ moveTo(index - 1);
147
+ break;
148
+ }
149
+ case "ArrowDown": {
150
+ e.preventDefault();
151
+ moveTo(index + columns);
152
+ break;
153
+ }
154
+ case "ArrowUp": {
155
+ e.preventDefault();
156
+ moveTo(index - columns);
157
+ break;
158
+ }
159
+ case "Home": {
160
+ e.preventDefault();
161
+ moveTo(0);
162
+ break;
163
+ }
164
+ case "End": {
165
+ e.preventDefault();
166
+ moveTo(getColors().length - 1);
102
167
  break;
103
168
  }
104
169
  }
105
170
  }
106
171
 
172
+ // Before focus enters the group the tab stop is the selected swatch (or
173
+ // the first one when nothing is selected), like other radio groups
174
+ function tabStopIndex(): number {
175
+ const focused = group.getFocusIndex();
176
+ if (focused !== -1) return focused;
177
+ const selected = getColors().findIndex((color) => isSelected(color));
178
+ return selected === -1 ? 0 : selected;
179
+ }
180
+
107
181
  // Only blur when focus leaves the whole palette
108
182
  function handleFocusOut(e: FocusEvent) {
109
183
  if (!root.contains(e.relatedTarget as Node | null)) {
@@ -125,20 +199,24 @@ export default function ColorPalette($props: ColorPaletteProps | undefined) {
125
199
  &ref={root}
126
200
  >
127
201
  @for (let [i, color] of getColors().entries()) {
128
- @key = color
202
+ @key = colorValue(color)
129
203
  <button
130
204
  type="button"
131
205
  class="torp-color-palette-swatch"
132
206
  role="radio"
133
- tabindex={group.isTabStop(i) ? 0 : -1}
207
+ tabindex={tabStopIndex() === i ? 0 : -1}
134
208
  aria-checked={isSelected(color) ? "true" : "false"}
135
- aria-label={normalized(color)}
209
+ aria-label={colorName(color)}
136
210
  aria-disabled={getDisabled() ? "true" : undefined}
137
- data-color={color}
211
+ data-color={colorValue(color)}
138
212
  data-selected={isSelected(color) ? "selected" : undefined}
139
213
  disabled={getDisabled()}
140
- style={`background-color:${color}`}
141
- onclick={() => select(color)}
214
+ style={`background-color:${colorValue(color)}`}
215
+ onfocus={() => group.handleItemFocus(i)}
216
+ onclick={() => {
217
+ group.handleItemFocus(i);
218
+ select(color);
219
+ }}
142
220
  onkeydown={(e: KeyboardEvent) => handleKey(i, e)}
143
221
  onmount={(el: HTMLElement) => {
144
222
  const item: FocusItemState = { setFocused: () => el.focus() };
@@ -6,9 +6,20 @@ import {
6
6
  FormContextName,
7
7
  } from "../Form/FormTypes";
8
8
  import FormValues from "../Form/FormValues.torp";
9
- import { defaultPalette } from "../utils/colorUtils";
9
+ import {
10
+ type Color,
11
+ type Hsv,
12
+ hexToRgba,
13
+ hsvToHex,
14
+ rgbToHsv,
15
+ withAlpha,
16
+ } from "../utils/colorUtils";
10
17
  import createFormField from "../utils/formField";
11
18
  import ColorPalette from "./ColorPalette.torp";
19
+ import ColorPickerAlpha from "./ColorPickerAlpha.torp";
20
+ import ColorPickerArea from "./ColorPickerArea.torp";
21
+ import ColorPickerEyedropper from "./ColorPickerEyedropper.torp";
22
+ import ColorPickerHue from "./ColorPickerHue.torp";
12
23
  import ColorPickerInput from "./ColorPickerInput.torp";
13
24
  import { type ColorPickerContext, ColorPickerContextName } from "./ColorPickerTypes";
14
25
 
@@ -21,13 +32,28 @@ interface ColorPickerProps {
21
32
  style?: StyleValue;
22
33
  /** The selected color as "#rrggbb"; undefined if nothing is selected */
23
34
  value?: Bindable<string | undefined>;
24
- /** The colors offered in the palette */
25
- colors?: string[];
26
35
  /**
27
- * If set to true, a text field for arbitrary hex values ("#ff8800") is
28
- * shown below the palette
36
+ * The colors offered in the palette. Each entry is either a hex string or
37
+ * `{ value, name }`; names are used as the swatches' accessible names.
38
+ */
39
+ colors?: Color[];
40
+ /** The number of swatches per row, for up/down arrow movement (default 6) */
41
+ columns?: number;
42
+ /** If true, selecting the already-selected swatch clears the value */
43
+ allowClear?: boolean;
44
+ /**
45
+ * If set to true, a text field for arbitrary color values ("#ff8800",
46
+ * "rgb(255 136 0)", "hsl(32 100% 50%)") is shown below the palette
29
47
  */
30
48
  showInput?: boolean;
49
+ /** If set to true, a 2D saturation/brightness area is shown above the palette */
50
+ showArea?: boolean;
51
+ /** If set to true, a hue slider is shown above the palette */
52
+ showHue?: boolean;
53
+ /** If set to true, an alpha slider is shown above the palette */
54
+ showAlpha?: boolean;
55
+ /** If set to true, an eyedropper button is shown when the browser supports it */
56
+ showEyedropper?: boolean;
31
57
  /** If set to true, the user cannot interact with this picker */
32
58
  disabled?: boolean;
33
59
  /** An accessible name for the picker */
@@ -39,8 +65,13 @@ interface ColorPickerProps {
39
65
  }
40
66
 
41
67
  /**
42
- * A color input: a ColorPalette with an optional hex text field, sharing a
43
- * single bound value.
68
+ * A color input: a ColorPalette with optional saturation/brightness, hue
69
+ * and alpha controls and a text field, sharing a single bound value.
70
+ * Enable `showArea`, `showHue`, `showAlpha` and `showEyedropper` for the
71
+ * full picker; by default it is just a swatch palette.
72
+ *
73
+ * Values are normalized hex colors, with an alpha channel when the color
74
+ * isn't fully opaque ("#ff8800", "#ff880080").
44
75
  *
45
76
  * ```
46
77
  * <ColorPicker name="accent" showInput &value={$state.accent} />
@@ -58,23 +89,55 @@ interface ColorPickerProps {
58
89
  * With no children, the palette and input are rendered automatically.
59
90
  */
60
91
  export default function ColorPicker($props: ColorPickerProps | undefined) {
92
+ let root: HTMLElement;
93
+
61
94
  // Wire up the enclosing Form and Field, if any
62
95
  const form = createFormField(
63
- $props?.name,
96
+ () => $props?.name,
64
97
  $context[FormContextName] as FormContext | undefined,
65
98
  $context[FieldContextName] as FieldContext | undefined,
66
99
  );
67
100
 
101
+ // The palette and input inside are parts of this picker, not standalone
102
+ // fields: hide the enclosing Field's context so they don't resolve its
103
+ // name and each submit their own copy of the value
104
+ $context[FieldContextName] = undefined;
105
+
68
106
  function getDisabled(): boolean {
69
107
  return $props?.disabled === true;
70
108
  }
71
109
 
72
110
  let $state = $watch({
73
111
  value: undefined as string | undefined,
112
+ h: 0,
113
+ s: 0,
114
+ v: 0,
115
+ a: 1,
74
116
  });
75
117
 
76
118
  $bind($state, $props, "value");
77
119
 
120
+ // Keep the hue/saturation/value/alpha controls in sync with the value.
121
+ // The hue is preserved for grays, which have no meaningful hue of their
122
+ // own, so dragging into a gray doesn't snap the hue slider to red
123
+ $run(() => {
124
+ const value = $state.value;
125
+ if (value === undefined) {
126
+ $state.s = 0;
127
+ $state.v = 0;
128
+ $state.a = 1;
129
+ return;
130
+ }
131
+ const rgba = hexToRgba(value);
132
+ const hsv = rgbToHsv(rgba);
133
+ if (hsv.s > 0 && hsv.v > 0) {
134
+ $state.h = hsv.h;
135
+ }
136
+ $state.s = hsv.s;
137
+ $state.v = hsv.v;
138
+ $state.a = rgba.a;
139
+ });
140
+
78
141
  function commitValue(next: string | undefined) {
79
142
  if (next !== $state.value) {
80
143
  $state.value = next;
@@ -87,21 +150,69 @@ export default function ColorPicker($props: ColorPickerProps | undefined) {
87
150
  commitValue(value);
88
151
  }
89
152
 
153
+ function getHsv(): Hsv {
154
+ return { h: $state.h, s: $state.s, v: $state.v };
155
+ }
156
+
157
+ function commitHsv(hsv: Hsv, alpha?: number) {
158
+ commitValue(hsvToHex(hsv, alpha ?? $state.a));
159
+ }
160
+
161
+ function setAlpha(alpha: number) {
162
+ commitValue(withAlpha($state.value ?? "#000000", alpha));
163
+ }
164
+
165
+ // Only blur when focus leaves the whole picker
166
+ function handleFocusOut(e: FocusEvent) {
167
+ if (!root.contains(e.relatedTarget as Node | null)) {
168
+ form.handleBlur();
169
+ }
170
+ }
171
+
90
172
  // Set the context to pass down to the input
91
173
  let context: ColorPickerContext = {
92
174
  getDisabled,
93
175
  getValue: () => $state.value,
94
176
  getAriaLabel: () => $props?.ariaLabel,
95
177
  commitValue,
178
+ getHsv,
179
+ commitHsv,
180
+ getAlpha: () => $state.a,
181
+ setAlpha,
96
182
  };
97
183
  $context[ColorPickerContextName] = context;
98
184
 
99
185
  @render {
100
- <div class={["torp-color-picker", $props?.class]} {$props?.style}>
186
+ <div
187
+ {$props?.id}
188
+ class={["torp-color-picker", $props?.class]}
189
+ {$props?.style}
190
+ role="group"
191
+ aria-label={$props?.ariaLabel}
192
+ aria-disabled={getDisabled() ? "true" : undefined}
193
+ data-valid={form.valid ? "valid" : "invalid"}
194
+ aria-describedby={form.messageId}
195
+ onfocusout={handleFocusOut}
196
+ &ref={root}
197
+ >
101
198
  <slot>
199
+ @if ($props?.showArea) {
200
+ <ColorPickerArea />
201
+ }
202
+ @if ($props?.showHue) {
203
+ <ColorPickerHue />
204
+ }
205
+ @if ($props?.showAlpha) {
206
+ <ColorPickerAlpha />
207
+ }
208
+ @if ($props?.showEyedropper) {
209
+ <ColorPickerEyedropper />
210
+ }
102
211
  <ColorPalette
103
212
  class="torp-color-picker-palette"
104
- colors={$props?.colors ?? defaultPalette}
213
+ colors={$props?.colors}
214
+ columns={$props?.columns}
215
+ allowClear={$props?.allowClear}
105
216
  value={$state.value}
106
217
  disabled={getDisabled()}
107
218
  ariaLabel={$props?.ariaLabel}
@@ -0,0 +1,58 @@
1
+ import type { ClassValue, StyleValue } from "@torpor/view";
2
+ import { hsvToHex } from "../utils/colorUtils";
3
+ import { type ColorPickerContext, ColorPickerContextName } from "./ColorPickerTypes";
4
+
5
+ interface ColorPickerAlphaProps {
6
+ /** Classes for the slider */
7
+ class?: ClassValue;
8
+ /** Styles for the slider */
9
+ style?: StyleValue;
10
+ /** An accessible name for the slider; falls back to the picker's ariaLabel */
11
+ ariaLabel?: string;
12
+ }
13
+
14
+ const CHECKERBOARD =
15
+ "repeating-conic-gradient(#ccc 0% 25%, #fff 0% 50%) 50% / 0.75rem 0.75rem";
16
+
17
+ /**
18
+ * An alpha (opacity) slider for a ColorPicker.
19
+ */
20
+ export default function ColorPickerAlpha($props: ColorPickerAlphaProps | undefined) {
21
+ // Get stuff out of the ColorPicker context
22
+ const context = $context[ColorPickerContextName] as ColorPickerContext;
23
+ if (!context) {
24
+ throw new Error("ColorPickerAlpha must be contained within a ColorPicker");
25
+ }
26
+
27
+ function label(): string {
28
+ const base = $props?.ariaLabel ?? context.getAriaLabel();
29
+ return base ? `${base} opacity` : "Opacity";
30
+ }
31
+
32
+ function handleInput(e: Event) {
33
+ const alpha = Number((e.currentTarget as HTMLInputElement).value);
34
+ if (!Number.isFinite(alpha)) return;
35
+ context.setAlpha(alpha / 100);
36
+ }
37
+
38
+ function background(): string {
39
+ const opaque = hsvToHex(context.getHsv(), 1);
40
+ return `${CHECKERBOARD}, linear-gradient(to right, transparent, ${opaque})`;
41
+ }
42
+
43
+ @render {
44
+ <input
45
+ type="range"
46
+ class={["torp-color-picker-alpha", $props?.class]}
47
+ style={[$props?.style, `background: ${background()}`]}
48
+ min="0"
49
+ max="100"
50
+ step="1"
51
+ value={Math.round(context.getAlpha() * 100)}
52
+ aria-label={label()}
53
+ aria-disabled={context.getDisabled() ? "true" : undefined}
54
+ disabled={context.getDisabled()}
55
+ oninput={handleInput}
56
+ />
57
+ }
58
+ }
@@ -0,0 +1,175 @@
1
+ import type { ClassValue, StyleValue } from "@torpor/view";
2
+ import { hsvToHex } from "../utils/colorUtils";
3
+ import { type ColorPickerContext, ColorPickerContextName } from "./ColorPickerTypes";
4
+
5
+ interface ColorPickerAreaProps {
6
+ /** Classes for the area */
7
+ class?: ClassValue;
8
+ /** Styles for the area */
9
+ style?: StyleValue;
10
+ }
11
+
12
+ function clamp(value: number, min: number, max: number): number {
13
+ return Math.min(max, Math.max(min, value));
14
+ }
15
+
16
+ /**
17
+ * A two-dimensional saturation/brightness area for a ColorPicker. Dragging
18
+ * or using the arrow keys selects a saturation (horizontal) and brightness
19
+ * (vertical) within the current hue.
20
+ */
21
+ export default function ColorPickerArea($props: ColorPickerAreaProps | undefined) {
22
+ let area: HTMLElement;
23
+ let dragging = false;
24
+
25
+ // Get stuff out of the ColorPicker context
26
+ const context = $context[ColorPickerContextName] as ColorPickerContext;
27
+ if (!context) {
28
+ throw new Error("ColorPickerArea must be contained within a ColorPicker");
29
+ }
30
+
31
+ function getDisabled(): boolean {
32
+ return context.getDisabled();
33
+ }
34
+
35
+ function commit(s: number, v: number) {
36
+ const hsv = context.getHsv();
37
+ context.commitHsv({ h: hsv.h, s: clamp(s, 0, 1), v: clamp(v, 0, 1) });
38
+ }
39
+
40
+ function updateFromPointer(e: PointerEvent) {
41
+ if (!area) return;
42
+ const rect = area.getBoundingClientRect();
43
+ if (rect.width <= 0 || rect.height <= 0) return;
44
+ const s = clamp((e.clientX - rect.left) / rect.width, 0, 1);
45
+ const v = 1 - clamp((e.clientY - rect.top) / rect.height, 0, 1);
46
+ commit(s, v);
47
+ }
48
+
49
+ function handlePointerMove(e: PointerEvent) {
50
+ if (!dragging) return;
51
+ e.preventDefault();
52
+ updateFromPointer(e);
53
+ }
54
+
55
+ function stopDragging() {
56
+ if (!dragging) return;
57
+ dragging = false;
58
+ document.removeEventListener("pointermove", handlePointerMove);
59
+ document.removeEventListener("pointerup", stopDragging);
60
+ document.removeEventListener("pointercancel", stopDragging);
61
+ }
62
+
63
+ function handlePointerDown(e: PointerEvent) {
64
+ if (getDisabled()) return;
65
+ e.preventDefault();
66
+ dragging = true;
67
+ updateFromPointer(e);
68
+ document.addEventListener("pointermove", handlePointerMove);
69
+ document.addEventListener("pointerup", stopDragging);
70
+ document.addEventListener("pointercancel", stopDragging);
71
+ }
72
+
73
+ function handleKey(e: KeyboardEvent) {
74
+ if (getDisabled()) return;
75
+ const hsv = context.getHsv();
76
+ const step = e.shiftKey ? 0.1 : 0.01;
77
+ switch (e.key) {
78
+ case "ArrowLeft": {
79
+ e.preventDefault();
80
+ commit(hsv.s - step, hsv.v);
81
+ break;
82
+ }
83
+ case "ArrowRight": {
84
+ e.preventDefault();
85
+ commit(hsv.s + step, hsv.v);
86
+ break;
87
+ }
88
+ case "ArrowDown": {
89
+ e.preventDefault();
90
+ commit(hsv.s, hsv.v - step);
91
+ break;
92
+ }
93
+ case "ArrowUp": {
94
+ e.preventDefault();
95
+ commit(hsv.s, hsv.v + step);
96
+ break;
97
+ }
98
+ case "Home": {
99
+ e.preventDefault();
100
+ commit(0, 1);
101
+ break;
102
+ }
103
+ case "End": {
104
+ e.preventDefault();
105
+ commit(1, 0);
106
+ break;
107
+ }
108
+ }
109
+ }
110
+
111
+ $run(() => {
112
+ return () => stopDragging();
113
+ });
114
+
115
+ function hueColor(): string {
116
+ return hsvToHex({ h: context.getHsv().h, s: 1, v: 1 });
117
+ }
118
+
119
+ function valueText(): string {
120
+ const hsv = context.getHsv();
121
+ return `Saturation ${Math.round(hsv.s * 100)}%, brightness ${Math.round(hsv.v * 100)}%`;
122
+ }
123
+
124
+ @render {
125
+ <div
126
+ class={["torp-color-picker-area", $props?.class]}
127
+ style={[$props?.style, `--torp-color-picker-hue:${hueColor()}`]}
128
+ role="slider"
129
+ tabindex={getDisabled() ? -1 : 0}
130
+ aria-label="Saturation and brightness"
131
+ aria-valuetext={valueText()}
132
+ aria-disabled={getDisabled() ? "true" : undefined}
133
+ onpointerdown={handlePointerDown}
134
+ onkeydown={handleKey}
135
+ &ref={area}
136
+ >
137
+ <div class="torp-color-picker-area-saturation" />
138
+ <div class="torp-color-picker-area-value" />
139
+ <div
140
+ class="torp-color-picker-area-knob"
141
+ style={`left:${context.getHsv().s * 100}%;top:${(1 - context.getHsv().v) * 100}%`}
142
+ />
143
+ </div>
144
+ }
145
+
146
+ @style {
147
+ .torp-color-picker-area {
148
+ position: relative;
149
+ touch-action: none;
150
+ background-color: var(--torp-color-picker-hue, #f00);
151
+ }
152
+ .torp-color-picker-area-saturation,
153
+ .torp-color-picker-area-value {
154
+ position: absolute;
155
+ inset: 0;
156
+ pointer-events: none;
157
+ }
158
+ .torp-color-picker-area-saturation {
159
+ background: linear-gradient(to right, #fff, transparent);
160
+ }
161
+ .torp-color-picker-area-value {
162
+ background: linear-gradient(to top, #000, transparent);
163
+ }
164
+ .torp-color-picker-area-knob {
165
+ position: absolute;
166
+ width: 0.75rem;
167
+ height: 0.75rem;
168
+ border: 2px solid #fff;
169
+ border-radius: 50%;
170
+ box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);
171
+ transform: translate(-50%, -50%);
172
+ pointer-events: none;
173
+ }
174
+ }
175
+ }