@proyecto-viviana/solid-stately 0.2.4 → 0.3.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 (243) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +30 -283
  3. package/dist/autocomplete/createAutocompleteState.d.ts +3 -2
  4. package/dist/autocomplete/index.d.ts +1 -1
  5. package/dist/calendar/createCalendarState.d.ts +22 -7
  6. package/dist/calendar/createDateFieldState.d.ts +30 -7
  7. package/dist/calendar/createDatePickerState.d.ts +60 -0
  8. package/dist/calendar/createRangeCalendarState.d.ts +27 -8
  9. package/dist/calendar/createTimeFieldState.d.ts +34 -7
  10. package/dist/calendar/index.d.ts +7 -6
  11. package/dist/checkbox/createCheckboxGroupState.d.ts +12 -3
  12. package/dist/checkbox/index.d.ts +1 -1
  13. package/dist/collections/ListCollection.d.ts +1 -1
  14. package/dist/collections/createListState.d.ts +8 -8
  15. package/dist/collections/createMenuState.d.ts +7 -7
  16. package/dist/collections/createSelectionState.d.ts +5 -5
  17. package/dist/collections/index.d.ts +5 -5
  18. package/dist/collections/types.d.ts +23 -12
  19. package/dist/color/Color.d.ts +1 -1
  20. package/dist/color/createColorAreaState.d.ts +4 -2
  21. package/dist/color/createColorFieldState.d.ts +38 -12
  22. package/dist/color/createColorSliderState.d.ts +9 -2
  23. package/dist/color/createColorWheelState.d.ts +13 -2
  24. package/dist/color/getColorChannels.d.ts +20 -0
  25. package/dist/color/index.d.ts +6 -6
  26. package/dist/color/types.d.ts +4 -4
  27. package/dist/combobox/createComboBoxState.d.ts +27 -13
  28. package/dist/combobox/index.d.ts +1 -1
  29. package/dist/data/createAsyncList.d.ts +111 -0
  30. package/dist/data/createListData.d.ts +65 -0
  31. package/dist/data/createTreeData.d.ts +65 -0
  32. package/dist/data/index.d.ts +3 -0
  33. package/dist/datepicker/index.d.ts +8 -0
  34. package/dist/disclosure/createDisclosureState.d.ts +2 -2
  35. package/dist/disclosure/index.d.ts +1 -1
  36. package/dist/dnd/createDragState.d.ts +2 -2
  37. package/dist/dnd/createDraggableCollectionState.d.ts +2 -2
  38. package/dist/dnd/createDropState.d.ts +2 -2
  39. package/dist/dnd/createDroppableCollectionState.d.ts +3 -3
  40. package/dist/dnd/index.d.ts +5 -5
  41. package/dist/dnd/types.d.ts +16 -16
  42. package/dist/form/createFormValidationState.d.ts +3 -3
  43. package/dist/form/index.d.ts +1 -1
  44. package/dist/grid/createGridState.d.ts +2 -2
  45. package/dist/grid/index.d.ts +2 -2
  46. package/dist/grid/types.d.ts +17 -11
  47. package/dist/index.d.ts +31 -25
  48. package/dist/index.js +7990 -6264
  49. package/dist/index.js.map +1 -7
  50. package/dist/menu/index.d.ts +8 -0
  51. package/dist/numberfield/createNumberFieldState.d.ts +2 -2
  52. package/dist/numberfield/index.d.ts +1 -1
  53. package/dist/overlays/createOverlayTriggerState.d.ts +2 -2
  54. package/dist/overlays/index.d.ts +1 -1
  55. package/dist/radio/createRadioGroupState.d.ts +13 -4
  56. package/dist/radio/index.d.ts +1 -1
  57. package/dist/searchfield/createSearchFieldState.d.ts +2 -2
  58. package/dist/searchfield/index.d.ts +2 -2
  59. package/dist/select/createSelectState.d.ts +20 -3
  60. package/dist/select/index.d.ts +1 -1
  61. package/dist/selection/index.d.ts +11 -0
  62. package/dist/slider/createSliderState.d.ts +3 -3
  63. package/dist/slider/index.d.ts +2 -2
  64. package/dist/steplist/createStepListState.d.ts +48 -0
  65. package/dist/steplist/index.d.ts +1 -0
  66. package/dist/table/TableCollection.d.ts +3 -3
  67. package/dist/table/createTableColumnResizeState.d.ts +54 -0
  68. package/dist/table/createTableState.d.ts +2 -2
  69. package/dist/table/index.d.ts +5 -3
  70. package/dist/table/types.d.ts +10 -10
  71. package/dist/tabs/createTabListState.d.ts +5 -5
  72. package/dist/tabs/index.d.ts +1 -1
  73. package/dist/textfield/createTextFieldState.d.ts +2 -2
  74. package/dist/textfield/index.d.ts +1 -1
  75. package/dist/toast/createToastState.d.ts +15 -3
  76. package/dist/toast/index.d.ts +1 -1
  77. package/dist/toggle/createToggleGroupState.d.ts +45 -0
  78. package/dist/toggle/createToggleState.d.ts +2 -2
  79. package/dist/toggle/index.d.ts +2 -1
  80. package/dist/tooltip/createTooltipTriggerState.d.ts +2 -2
  81. package/dist/tooltip/index.d.ts +1 -1
  82. package/dist/tree/TreeCollection.d.ts +5 -4
  83. package/dist/tree/createTreeState.d.ts +2 -2
  84. package/dist/tree/index.d.ts +3 -3
  85. package/dist/tree/types.d.ts +19 -13
  86. package/dist/utils/index.d.ts +1 -1
  87. package/dist/utils/reactivity.d.ts +1 -1
  88. package/package.json +30 -30
  89. package/src/autocomplete/createAutocompleteState.d.ts +19 -17
  90. package/src/autocomplete/createAutocompleteState.ts +25 -32
  91. package/src/autocomplete/index.d.ts +6 -2
  92. package/src/autocomplete/index.ts +1 -1
  93. package/src/calendar/createCalendarState.d.ts +139 -117
  94. package/src/calendar/createCalendarState.ts +221 -74
  95. package/src/calendar/createDateFieldState.d.ts +130 -95
  96. package/src/calendar/createDateFieldState.ts +286 -151
  97. package/src/calendar/createDatePickerState.ts +347 -0
  98. package/src/calendar/createRangeCalendarState.d.ts +154 -129
  99. package/src/calendar/createRangeCalendarState.ts +293 -79
  100. package/src/calendar/createTimeFieldState.d.ts +81 -79
  101. package/src/calendar/createTimeFieldState.ts +187 -108
  102. package/src/calendar/index.d.ts +80 -7
  103. package/src/calendar/index.ts +21 -12
  104. package/src/checkbox/createCheckboxGroupState.d.ts +57 -55
  105. package/src/checkbox/createCheckboxGroupState.ts +57 -30
  106. package/src/checkbox/index.d.ts +6 -2
  107. package/src/checkbox/index.ts +1 -1
  108. package/src/collections/ListCollection.d.ts +25 -22
  109. package/src/collections/ListCollection.ts +152 -146
  110. package/src/collections/createListState.d.ts +73 -58
  111. package/src/collections/createListState.ts +267 -264
  112. package/src/collections/createMenuState.d.ts +36 -31
  113. package/src/collections/createMenuState.ts +112 -106
  114. package/src/collections/createSelectionState.d.ts +78 -65
  115. package/src/collections/createSelectionState.ts +340 -336
  116. package/src/collections/index.d.ts +38 -6
  117. package/src/collections/index.ts +41 -46
  118. package/src/collections/types.d.ts +101 -101
  119. package/src/collections/types.ts +181 -169
  120. package/src/color/Color.d.ts +20 -5
  121. package/src/color/Color.ts +1054 -951
  122. package/src/color/createColorAreaState.d.ts +67 -65
  123. package/src/color/createColorAreaState.ts +301 -293
  124. package/src/color/createColorFieldState.d.ts +46 -44
  125. package/src/color/createColorFieldState.ts +428 -292
  126. package/src/color/createColorSliderState.d.ts +58 -56
  127. package/src/color/createColorSliderState.ts +272 -241
  128. package/src/color/createColorWheelState.d.ts +42 -40
  129. package/src/color/createColorWheelState.ts +248 -211
  130. package/src/color/getColorChannels.ts +36 -0
  131. package/src/color/index.d.ts +36 -7
  132. package/src/color/index.ts +44 -47
  133. package/src/color/types.d.ts +68 -63
  134. package/src/color/types.ts +119 -127
  135. package/src/combobox/createComboBoxState.d.ts +107 -105
  136. package/src/combobox/createComboBoxState.ts +790 -703
  137. package/src/combobox/index.d.ts +10 -2
  138. package/src/combobox/index.ts +13 -13
  139. package/src/data/createAsyncList.ts +403 -0
  140. package/src/data/createListData.ts +298 -0
  141. package/src/data/createTreeData.ts +646 -0
  142. package/src/data/index.ts +25 -0
  143. package/src/datepicker/index.ts +32 -0
  144. package/src/disclosure/createDisclosureState.d.ts +47 -43
  145. package/src/disclosure/createDisclosureState.ts +9 -23
  146. package/src/disclosure/index.d.ts +10 -2
  147. package/src/disclosure/index.ts +1 -1
  148. package/src/dnd/createDragState.d.ts +51 -42
  149. package/src/dnd/createDragState.ts +151 -153
  150. package/src/dnd/createDraggableCollectionState.d.ts +52 -41
  151. package/src/dnd/createDraggableCollectionState.ts +158 -165
  152. package/src/dnd/createDropState.d.ts +64 -45
  153. package/src/dnd/createDropState.ts +202 -212
  154. package/src/dnd/createDroppableCollectionState.d.ts +88 -59
  155. package/src/dnd/createDroppableCollectionState.ts +354 -357
  156. package/src/dnd/index.d.ts +54 -6
  157. package/src/dnd/index.ts +63 -76
  158. package/src/dnd/types.d.ts +167 -156
  159. package/src/dnd/types.ts +316 -317
  160. package/src/form/createFormValidationState.d.ts +47 -45
  161. package/src/form/createFormValidationState.ts +356 -389
  162. package/src/form/index.d.ts +16 -2
  163. package/src/form/index.ts +15 -15
  164. package/src/grid/createGridState.d.ts +7 -4
  165. package/src/grid/createGridState.ts +44 -51
  166. package/src/grid/index.d.ts +3 -3
  167. package/src/grid/index.ts +2 -8
  168. package/src/grid/types.d.ts +136 -129
  169. package/src/grid/types.ts +24 -17
  170. package/src/index.d.ts +310 -26
  171. package/src/index.ts +87 -79
  172. package/src/menu/index.ts +17 -0
  173. package/src/numberfield/createNumberFieldState.d.ts +56 -54
  174. package/src/numberfield/createNumberFieldState.ts +422 -383
  175. package/src/numberfield/index.d.ts +6 -2
  176. package/src/numberfield/index.ts +5 -5
  177. package/src/overlays/createOverlayTriggerState.d.ts +22 -20
  178. package/src/overlays/createOverlayTriggerState.ts +66 -67
  179. package/src/overlays/index.d.ts +6 -2
  180. package/src/overlays/index.ts +5 -5
  181. package/src/radio/createRadioGroupState.d.ts +54 -52
  182. package/src/radio/createRadioGroupState.ts +66 -30
  183. package/src/radio/index.d.ts +7 -2
  184. package/src/radio/index.ts +1 -1
  185. package/src/searchfield/createSearchFieldState.d.ts +16 -14
  186. package/src/searchfield/createSearchFieldState.ts +56 -62
  187. package/src/searchfield/index.d.ts +3 -3
  188. package/src/searchfield/index.ts +2 -5
  189. package/src/select/createSelectState.d.ts +63 -61
  190. package/src/select/createSelectState.ts +290 -181
  191. package/src/select/index.d.ts +2 -2
  192. package/src/select/index.ts +1 -5
  193. package/src/selection/index.ts +27 -0
  194. package/src/slider/createSliderState.d.ts +60 -60
  195. package/src/slider/createSliderState.ts +233 -211
  196. package/src/slider/index.d.ts +3 -3
  197. package/src/slider/index.ts +2 -6
  198. package/src/ssr/index.d.ts +1 -1
  199. package/src/ssr/index.ts +2 -2
  200. package/src/steplist/createStepListState.ts +191 -0
  201. package/src/steplist/index.ts +5 -0
  202. package/src/table/TableCollection.d.ts +42 -40
  203. package/src/table/TableCollection.ts +61 -64
  204. package/src/table/createTableColumnResizeState.ts +242 -0
  205. package/src/table/createTableState.d.ts +7 -4
  206. package/src/table/createTableState.ts +10 -7
  207. package/src/table/index.d.ts +14 -4
  208. package/src/table/index.ts +10 -3
  209. package/src/table/types.d.ts +98 -92
  210. package/src/table/types.ts +17 -13
  211. package/src/tabs/createTabListState.d.ts +59 -57
  212. package/src/tabs/createTabListState.ts +58 -39
  213. package/src/tabs/index.d.ts +8 -2
  214. package/src/tabs/index.ts +1 -1
  215. package/src/textfield/createTextFieldState.d.ts +16 -14
  216. package/src/textfield/createTextFieldState.ts +7 -18
  217. package/src/textfield/index.d.ts +6 -2
  218. package/src/textfield/index.ts +1 -1
  219. package/src/toast/createToastState.d.ts +92 -81
  220. package/src/toast/createToastState.ts +56 -44
  221. package/src/toast/index.d.ts +12 -2
  222. package/src/toast/index.ts +1 -1
  223. package/src/toggle/createToggleGroupState.ts +117 -0
  224. package/src/toggle/createToggleState.d.ts +19 -19
  225. package/src/toggle/createToggleState.ts +2 -16
  226. package/src/toggle/index.d.ts +2 -2
  227. package/src/toggle/index.ts +6 -4
  228. package/src/tooltip/createTooltipTriggerState.d.ts +17 -17
  229. package/src/tooltip/createTooltipTriggerState.ts +184 -183
  230. package/src/tooltip/index.d.ts +7 -2
  231. package/src/tooltip/index.ts +6 -6
  232. package/src/tree/TreeCollection.d.ts +26 -23
  233. package/src/tree/TreeCollection.ts +215 -175
  234. package/src/tree/createTreeState.d.ts +7 -4
  235. package/src/tree/createTreeState.ts +390 -392
  236. package/src/tree/index.d.ts +10 -4
  237. package/src/tree/index.ts +13 -13
  238. package/src/tree/types.d.ts +131 -126
  239. package/src/tree/types.ts +183 -174
  240. package/src/utils/index.d.ts +2 -2
  241. package/src/utils/index.ts +1 -1
  242. package/src/utils/reactivity.d.ts +2 -2
  243. package/src/utils/reactivity.ts +3 -3
@@ -2,66 +2,68 @@
2
2
  * ColorSlider state management.
3
3
  * Based on @react-stately/color useColorSliderState.
4
4
  */
5
- import { type Accessor } from 'solid-js';
6
- import type { Color, ColorChannel } from './types';
5
+ import { type Accessor } from "solid-js";
6
+ import type { Color, ColorChannel } from "./types";
7
7
  export interface ColorSliderStateOptions {
8
- /** The current color value (controlled). */
9
- value?: Color | string;
10
- /** The default color value (uncontrolled). */
11
- defaultValue?: Color | string;
12
- /** Handler called when the color changes. */
13
- onChange?: (color: Color) => void;
14
- /** Handler called when dragging ends. */
15
- onChangeEnd?: (color: Color) => void;
16
- /** The color channel this slider controls. */
17
- channel: ColorChannel;
18
- /** Whether the slider is disabled. */
19
- isDisabled?: boolean;
20
- /** The locale for formatting. */
21
- locale?: string;
8
+ /** The current color value (controlled). */
9
+ value?: Color | string;
10
+ /** The default color value (uncontrolled). */
11
+ defaultValue?: Color | string;
12
+ /** Handler called when the color changes. */
13
+ onChange?: (color: Color) => void;
14
+ /** Handler called when dragging ends. */
15
+ onChangeEnd?: (color: Color) => void;
16
+ /** The color channel this slider controls. */
17
+ channel: ColorChannel;
18
+ /** Whether the slider is disabled. */
19
+ isDisabled?: boolean;
20
+ /** The locale for formatting. */
21
+ locale?: string;
22
22
  }
23
23
  export interface ColorSliderState {
24
- /** The current color value. */
25
- readonly value: Color;
26
- /** Whether the slider is being dragged. */
27
- readonly isDragging: boolean;
28
- /** The color channel being controlled. */
29
- readonly channel: ColorChannel;
30
- /** The step value for the channel. */
31
- readonly step: number;
32
- /** The page step value for the channel. */
33
- readonly pageSize: number;
34
- /** The minimum value for the channel. */
35
- readonly minValue: number;
36
- /** The maximum value for the channel. */
37
- readonly maxValue: number;
38
- /** Whether the slider is disabled. */
39
- readonly isDisabled: boolean;
40
- /** Get the current channel value. */
41
- getThumbValue(): number;
42
- /** Get the minimum percent. */
43
- getThumbMinValue(): number;
44
- /** Get the maximum percent. */
45
- getThumbMaxValue(): number;
46
- /** Get the thumb value as a percentage. */
47
- getThumbPercent(): number;
48
- /** Set the channel value. */
49
- setThumbValue(value: number): void;
50
- /** Set the thumb value from a percentage (0-1). */
51
- setThumbPercent(percent: number): void;
52
- /** Increment the channel value. */
53
- incrementThumb(stepSize?: number): void;
54
- /** Decrement the channel value. */
55
- decrementThumb(stepSize?: number): void;
56
- /** Set the dragging state. */
57
- setDragging(isDragging: boolean): void;
58
- /** Get the display color (with alpha = 1). */
59
- getDisplayColor(): Color;
60
- /** Get the formatted value label. */
61
- getThumbValueLabel(): string;
24
+ /** The current color value. */
25
+ readonly value: Color;
26
+ /** Whether the slider is being dragged. */
27
+ readonly isDragging: boolean;
28
+ /** The color channel being controlled. */
29
+ readonly channel: ColorChannel;
30
+ /** The step value for the channel. */
31
+ readonly step: number;
32
+ /** The page step value for the channel. */
33
+ readonly pageSize: number;
34
+ /** The minimum value for the channel. */
35
+ readonly minValue: number;
36
+ /** The maximum value for the channel. */
37
+ readonly maxValue: number;
38
+ /** Whether the slider is disabled. */
39
+ readonly isDisabled: boolean;
40
+ /** Get the current channel value. */
41
+ getThumbValue(): number;
42
+ /** Get the minimum percent. */
43
+ getThumbMinValue(): number;
44
+ /** Get the maximum percent. */
45
+ getThumbMaxValue(): number;
46
+ /** Get the thumb value as a percentage. */
47
+ getThumbPercent(): number;
48
+ /** Set the channel value. */
49
+ setThumbValue(value: number): void;
50
+ /** Set the thumb value from a percentage (0-1). */
51
+ setThumbPercent(percent: number): void;
52
+ /** Increment the channel value. */
53
+ incrementThumb(stepSize?: number): void;
54
+ /** Decrement the channel value. */
55
+ decrementThumb(stepSize?: number): void;
56
+ /** Set the dragging state. */
57
+ setDragging(isDragging: boolean): void;
58
+ /** Get the display color (with alpha = 1). */
59
+ getDisplayColor(): Color;
60
+ /** Get the formatted value label. */
61
+ getThumbValueLabel(): string;
62
62
  }
63
63
  /**
64
64
  * Creates state for a color slider.
65
65
  */
66
- export declare function createColorSliderState(options: Accessor<ColorSliderStateOptions>): ColorSliderState;
67
- //# sourceMappingURL=createColorSliderState.d.ts.map
66
+ export declare function createColorSliderState(
67
+ options: Accessor<ColorSliderStateOptions>,
68
+ ): ColorSliderState;
69
+ //# sourceMappingURL=createColorSliderState.d.ts.map
@@ -1,241 +1,272 @@
1
- /**
2
- * ColorSlider state management.
3
- * Based on @react-stately/color useColorSliderState.
4
- */
5
-
6
- import { createSignal, createMemo, type Accessor } from 'solid-js';
7
- import type { Color, ColorChannel } from './types';
8
- import { normalizeColor } from './Color';
9
-
10
- export interface ColorSliderStateOptions {
11
- /** The current color value (controlled). */
12
- value?: Color | string;
13
- /** The default color value (uncontrolled). */
14
- defaultValue?: Color | string;
15
- /** Handler called when the color changes. */
16
- onChange?: (color: Color) => void;
17
- /** Handler called when dragging ends. */
18
- onChangeEnd?: (color: Color) => void;
19
- /** The color channel this slider controls. */
20
- channel: ColorChannel;
21
- /** Whether the slider is disabled. */
22
- isDisabled?: boolean;
23
- /** The locale for formatting. */
24
- locale?: string;
25
- }
26
-
27
- export interface ColorSliderState {
28
- /** The current color value. */
29
- readonly value: Color;
30
- /** Whether the slider is being dragged. */
31
- readonly isDragging: boolean;
32
- /** The color channel being controlled. */
33
- readonly channel: ColorChannel;
34
- /** The step value for the channel. */
35
- readonly step: number;
36
- /** The page step value for the channel. */
37
- readonly pageSize: number;
38
- /** The minimum value for the channel. */
39
- readonly minValue: number;
40
- /** The maximum value for the channel. */
41
- readonly maxValue: number;
42
- /** Whether the slider is disabled. */
43
- readonly isDisabled: boolean;
44
-
45
- /** Get the current channel value. */
46
- getThumbValue(): number;
47
- /** Get the minimum percent. */
48
- getThumbMinValue(): number;
49
- /** Get the maximum percent. */
50
- getThumbMaxValue(): number;
51
- /** Get the thumb value as a percentage. */
52
- getThumbPercent(): number;
53
- /** Set the channel value. */
54
- setThumbValue(value: number): void;
55
- /** Set the thumb value from a percentage (0-1). */
56
- setThumbPercent(percent: number): void;
57
- /** Increment the channel value. */
58
- incrementThumb(stepSize?: number): void;
59
- /** Decrement the channel value. */
60
- decrementThumb(stepSize?: number): void;
61
- /** Set the dragging state. */
62
- setDragging(isDragging: boolean): void;
63
- /** Get the display color (with alpha = 1). */
64
- getDisplayColor(): Color;
65
- /** Get the formatted value label. */
66
- getThumbValueLabel(): string;
67
- }
68
-
69
- /**
70
- * Creates state for a color slider.
71
- */
72
- export function createColorSliderState(
73
- options: Accessor<ColorSliderStateOptions>
74
- ): ColorSliderState {
75
- const getOptions = () => options();
76
-
77
- // Internal value state
78
- const [internalValue, setInternalValue] = createSignal<Color | null>(null);
79
- const [isDragging, setIsDragging] = createSignal(false);
80
-
81
- // Initialize internal value
82
- const initValue = () => {
83
- const opts = getOptions();
84
- if (opts.defaultValue) {
85
- return normalizeColor(opts.defaultValue);
86
- }
87
- return null;
88
- };
89
-
90
- // Set initial value
91
- if (internalValue() === null) {
92
- const init = initValue();
93
- if (init) {
94
- setInternalValue(init);
95
- }
96
- }
97
-
98
- // Controlled vs uncontrolled value
99
- const value = createMemo(() => {
100
- const opts = getOptions();
101
- if (opts.value !== undefined) {
102
- return normalizeColor(opts.value);
103
- }
104
- return internalValue() ?? normalizeColor('#ff0000');
105
- });
106
-
107
- const channel = createMemo(() => getOptions().channel);
108
- const isDisabled = createMemo(() => getOptions().isDisabled ?? false);
109
- const locale = createMemo(() => getOptions().locale ?? 'en-US');
110
-
111
- // Get channel range
112
- const channelRange = createMemo(() => value().getChannelRange(channel()));
113
-
114
- const step = createMemo(() => channelRange().step);
115
- const pageSize = createMemo(() => channelRange().pageSize);
116
- const minValue = createMemo(() => channelRange().minValue);
117
- const maxValue = createMemo(() => channelRange().maxValue);
118
-
119
- // Update value
120
- const updateValue = (newColor: Color) => {
121
- const opts = getOptions();
122
-
123
- // Controlled mode
124
- if (opts.value !== undefined) {
125
- opts.onChange?.(newColor);
126
- return;
127
- }
128
-
129
- // Uncontrolled mode
130
- setInternalValue(newColor);
131
- opts.onChange?.(newColor);
132
- };
133
-
134
- // Get thumb value
135
- const getThumbValue = () => {
136
- return value().getChannelValue(channel());
137
- };
138
-
139
- // Get min/max values
140
- const getThumbMinValue = () => minValue();
141
- const getThumbMaxValue = () => maxValue();
142
-
143
- // Get thumb percent
144
- const getThumbPercent = () => {
145
- const val = getThumbValue();
146
- const min = minValue();
147
- const max = maxValue();
148
- return (val - min) / (max - min);
149
- };
150
-
151
- // Set thumb value
152
- const setThumbValue = (newValue: number) => {
153
- const clamped = Math.max(minValue(), Math.min(maxValue(), newValue));
154
- const rounded = Math.round(clamped / step()) * step();
155
- const newColor = value().withChannelValue(channel(), rounded);
156
- updateValue(newColor);
157
- };
158
-
159
- // Set thumb from percent
160
- const setThumbPercent = (percent: number) => {
161
- const min = minValue();
162
- const max = maxValue();
163
- const val = min + (max - min) * percent;
164
- setThumbValue(val);
165
- };
166
-
167
- // Increment
168
- const incrementThumb = (stepSize?: number) => {
169
- const s = stepSize ?? step();
170
- setThumbValue(getThumbValue() + s);
171
- };
172
-
173
- // Decrement
174
- const decrementThumb = (stepSize?: number) => {
175
- const s = stepSize ?? step();
176
- setThumbValue(getThumbValue() - s);
177
- };
178
-
179
- // Set dragging
180
- const setDraggingState = (dragging: boolean) => {
181
- const wasDragging = isDragging();
182
- setIsDragging(dragging);
183
-
184
- // Call onChangeEnd when dragging ends
185
- if (wasDragging && !dragging) {
186
- getOptions().onChangeEnd?.(value());
187
- }
188
- };
189
-
190
- // Get display color (alpha = 1)
191
- const getDisplayColor = () => {
192
- const v = value();
193
- return v.withChannelValue('alpha', 1);
194
- };
195
-
196
- // Get formatted value label
197
- const getThumbValueLabel = () => {
198
- const v = value();
199
- const ch = channel();
200
- const loc = locale();
201
- return v.formatChannelValue(ch, loc);
202
- };
203
-
204
- return {
205
- get value() {
206
- return value();
207
- },
208
- get isDragging() {
209
- return isDragging();
210
- },
211
- get channel() {
212
- return channel();
213
- },
214
- get step() {
215
- return step();
216
- },
217
- get pageSize() {
218
- return pageSize();
219
- },
220
- get minValue() {
221
- return minValue();
222
- },
223
- get maxValue() {
224
- return maxValue();
225
- },
226
- get isDisabled() {
227
- return isDisabled();
228
- },
229
- getThumbValue,
230
- getThumbMinValue,
231
- getThumbMaxValue,
232
- getThumbPercent,
233
- setThumbValue,
234
- setThumbPercent,
235
- incrementThumb,
236
- decrementThumb,
237
- setDragging: setDraggingState,
238
- getDisplayColor,
239
- getThumbValueLabel,
240
- };
241
- }
1
+ /**
2
+ * ColorSlider state management.
3
+ * Based on @react-stately/color useColorSliderState.
4
+ */
5
+
6
+ import { createSignal, createMemo, type Accessor } from "solid-js";
7
+ import type { Color, ColorChannel, ColorSpace } from "./types";
8
+ import { normalizeColor } from "./Color";
9
+
10
+ export type ColorSliderOrientation = "horizontal" | "vertical";
11
+
12
+ export interface ColorSliderStateOptions {
13
+ /** The current color value (controlled). */
14
+ value?: Color | string;
15
+ /** The default color value (uncontrolled). */
16
+ defaultValue?: Color | string;
17
+ /** Handler called when the color changes. */
18
+ onChange?: (color: Color) => void;
19
+ /** Handler called when dragging ends. */
20
+ onChangeEnd?: (color: Color) => void;
21
+ /** The color channel this slider controls. */
22
+ channel: ColorChannel;
23
+ /** The color space to use for channel values. */
24
+ colorSpace?: ColorSpace;
25
+ /** The slider orientation. */
26
+ orientation?: ColorSliderOrientation;
27
+ /** Whether the slider is disabled. */
28
+ isDisabled?: boolean;
29
+ /** The locale for formatting. */
30
+ locale?: string;
31
+ }
32
+
33
+ export interface ColorSliderState {
34
+ /** The current color value. */
35
+ readonly value: Color;
36
+ /** Whether the slider is being dragged. */
37
+ readonly isDragging: boolean;
38
+ /** The color channel being controlled. */
39
+ readonly channel: ColorChannel;
40
+ /** The slider orientation. */
41
+ readonly orientation: ColorSliderOrientation;
42
+ /** The step value for the channel. */
43
+ readonly step: number;
44
+ /** The page step value for the channel. */
45
+ readonly pageSize: number;
46
+ /** The minimum value for the channel. */
47
+ readonly minValue: number;
48
+ /** The maximum value for the channel. */
49
+ readonly maxValue: number;
50
+ /** Whether the slider is disabled. */
51
+ readonly isDisabled: boolean;
52
+
53
+ /** Get the current channel value. */
54
+ getThumbValue(): number;
55
+ /** Get the minimum percent. */
56
+ getThumbMinValue(): number;
57
+ /** Get the maximum percent. */
58
+ getThumbMaxValue(): number;
59
+ /** Get the thumb value as a percentage. */
60
+ getThumbPercent(): number;
61
+ /** Set the channel value. */
62
+ setThumbValue(value: number): void;
63
+ /** Set the thumb value from a percentage (0-1). */
64
+ setThumbPercent(percent: number): void;
65
+ /** Increment the channel value. */
66
+ incrementThumb(stepSize?: number): void;
67
+ /** Decrement the channel value. */
68
+ decrementThumb(stepSize?: number): void;
69
+ /** Set the dragging state. */
70
+ setDragging(isDragging: boolean): void;
71
+ /** Get the display color (with alpha = 1). */
72
+ getDisplayColor(): Color;
73
+ /** Get the formatted value label. */
74
+ getThumbValueLabel(): string;
75
+ }
76
+
77
+ /**
78
+ * Creates state for a color slider.
79
+ */
80
+ export function createColorSliderState(
81
+ options: Accessor<ColorSliderStateOptions>,
82
+ ): ColorSliderState {
83
+ const getOptions = () => options();
84
+
85
+ const normalizeValue = (value: Color | string) => {
86
+ const color = normalizeColor(value);
87
+ const colorSpace = getOptions().colorSpace;
88
+ return colorSpace ? color.toFormat(colorSpace) : color;
89
+ };
90
+
91
+ // Internal value state
92
+ const [internalValue, setInternalValue] = createSignal<Color | null>(null);
93
+ const [isDragging, setIsDragging] = createSignal(false);
94
+
95
+ // Initialize internal value
96
+ const initValue = () => {
97
+ const opts = getOptions();
98
+ if (opts.defaultValue) {
99
+ return normalizeValue(opts.defaultValue);
100
+ }
101
+ return null;
102
+ };
103
+
104
+ // Set initial value
105
+ if (internalValue() === null) {
106
+ const init = initValue();
107
+ if (init) {
108
+ setInternalValue(init);
109
+ }
110
+ }
111
+
112
+ // Controlled vs uncontrolled value
113
+ const value = createMemo(() => {
114
+ const opts = getOptions();
115
+ if (opts.value !== undefined) {
116
+ return normalizeValue(opts.value);
117
+ }
118
+ const fallback = internalValue() ?? normalizeValue("#ff0000");
119
+ return opts.colorSpace ? fallback.toFormat(opts.colorSpace) : fallback;
120
+ });
121
+
122
+ const channel = createMemo(() => getOptions().channel);
123
+ const orientation = createMemo<ColorSliderOrientation>(
124
+ () => getOptions().orientation ?? "horizontal",
125
+ );
126
+ const isDisabled = createMemo(() => getOptions().isDisabled ?? false);
127
+ const locale = createMemo(() => getOptions().locale ?? "en-US");
128
+
129
+ // Get channel range
130
+ const channelRange = createMemo(() => value().getChannelRange(channel()));
131
+
132
+ const step = createMemo(() => channelRange().step);
133
+ const pageSize = createMemo(() => channelRange().pageSize);
134
+ const minValue = createMemo(() => channelRange().minValue);
135
+ const maxValue = createMemo(() => channelRange().maxValue);
136
+
137
+ // Update value
138
+ const updateValue = (newColor: Color) => {
139
+ const opts = getOptions();
140
+ const nextColor = opts.colorSpace ? newColor.toFormat(opts.colorSpace) : newColor;
141
+
142
+ // Controlled mode
143
+ if (opts.value !== undefined) {
144
+ opts.onChange?.(nextColor);
145
+ return;
146
+ }
147
+
148
+ // Uncontrolled mode
149
+ setInternalValue(nextColor);
150
+ opts.onChange?.(nextColor);
151
+ };
152
+
153
+ // Get thumb value
154
+ const getThumbValue = () => {
155
+ return value().getChannelValue(channel());
156
+ };
157
+
158
+ // Get min/max values
159
+ const getThumbMinValue = () => minValue();
160
+ const getThumbMaxValue = () => maxValue();
161
+
162
+ // Get thumb percent
163
+ const getThumbPercent = () => {
164
+ const val = getThumbValue();
165
+ const min = minValue();
166
+ const max = maxValue();
167
+ return (val - min) / (max - min);
168
+ };
169
+
170
+ // Set thumb value
171
+ const setThumbValue = (newValue: number) => {
172
+ const min = minValue();
173
+ const max = maxValue();
174
+ const stepValue = step();
175
+ const clamped = Math.max(min, Math.min(max, newValue));
176
+ const precision = `${stepValue}`.split(".")[1]?.length ?? 0;
177
+ const rounded = Number(
178
+ (Math.round((clamped - min) / stepValue) * stepValue + min).toFixed(precision),
179
+ );
180
+ const newColor = value().withChannelValue(channel(), rounded);
181
+ updateValue(newColor);
182
+ };
183
+
184
+ // Set thumb from percent
185
+ const setThumbPercent = (percent: number) => {
186
+ const min = minValue();
187
+ const max = maxValue();
188
+ const val = min + (max - min) * percent;
189
+ setThumbValue(val);
190
+ };
191
+
192
+ // Increment
193
+ const incrementThumb = (stepSize?: number) => {
194
+ const s = stepSize ?? step();
195
+ setThumbValue(getThumbValue() + s);
196
+ };
197
+
198
+ // Decrement
199
+ const decrementThumb = (stepSize?: number) => {
200
+ const s = stepSize ?? step();
201
+ setThumbValue(getThumbValue() - s);
202
+ };
203
+
204
+ // Set dragging
205
+ const setDraggingState = (dragging: boolean) => {
206
+ const wasDragging = isDragging();
207
+ setIsDragging(dragging);
208
+
209
+ // Call onChangeEnd when dragging ends
210
+ if (wasDragging && !dragging) {
211
+ getOptions().onChangeEnd?.(value());
212
+ }
213
+ };
214
+
215
+ // Get display color (alpha = 1)
216
+ const getDisplayColor = () => {
217
+ const v = value();
218
+ if (channel() === "hue") {
219
+ return normalizeColor(`hsl(${v.getChannelValue("hue")}, 100%, 50%)`);
220
+ }
221
+ return v.withChannelValue("alpha", 1);
222
+ };
223
+
224
+ // Get formatted value label
225
+ const getThumbValueLabel = () => {
226
+ const v = value();
227
+ const ch = channel();
228
+ const loc = locale();
229
+ return v.formatChannelValue(ch, loc);
230
+ };
231
+
232
+ return {
233
+ get value() {
234
+ return value();
235
+ },
236
+ get isDragging() {
237
+ return isDragging();
238
+ },
239
+ get channel() {
240
+ return channel();
241
+ },
242
+ get orientation() {
243
+ return orientation();
244
+ },
245
+ get step() {
246
+ return step();
247
+ },
248
+ get pageSize() {
249
+ return pageSize();
250
+ },
251
+ get minValue() {
252
+ return minValue();
253
+ },
254
+ get maxValue() {
255
+ return maxValue();
256
+ },
257
+ get isDisabled() {
258
+ return isDisabled();
259
+ },
260
+ getThumbValue,
261
+ getThumbMinValue,
262
+ getThumbMaxValue,
263
+ getThumbPercent,
264
+ setThumbValue,
265
+ setThumbPercent,
266
+ incrementThumb,
267
+ decrementThumb,
268
+ setDragging: setDraggingState,
269
+ getDisplayColor,
270
+ getThumbValueLabel,
271
+ };
272
+ }