@destyler/color-picker 0.2.6 → 0.2.9

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.
package/dist/index.d.mts CHANGED
@@ -56,79 +56,86 @@ interface PublicContext extends CommonProperties, DirectionProperty, InteractOut
56
56
  /**
57
57
  * The ids of the elements in the color picker. Useful for composition.
58
58
  */
59
- 'ids'?: ElementIds | undefined;
59
+ ids?: ElementIds | undefined;
60
60
  /**
61
- * The current color value
61
+ * The controlled color value
62
62
  * @default #000000
63
63
  */
64
- 'value': Color;
64
+ value?: Color | undefined;
65
+ /**
66
+ * The initial color value when it is not controlled.
67
+ * Prefer this over seeding with `value` for uncontrolled usage.
68
+ * @default #000000
69
+ */
70
+ defaultValue?: Color | undefined;
65
71
  /**
66
72
  * Whether the color picker is disabled
67
73
  */
68
- 'disabled'?: boolean | undefined;
74
+ disabled?: boolean | undefined;
69
75
  /**
70
76
  * Whether the color picker is read-only
71
77
  */
72
- 'readOnly'?: boolean | undefined;
78
+ readOnly?: boolean | undefined;
73
79
  /**
74
80
  * Whether the color picker is required
75
81
  */
76
- 'required'?: boolean | undefined;
82
+ required?: boolean | undefined;
77
83
  /**
78
84
  * Whether the color picker is invalid
79
85
  */
80
- 'invalid'?: boolean | undefined;
86
+ invalid?: boolean | undefined;
81
87
  /**
82
88
  * Handler that is called when the value changes, as the user drags.
83
89
  */
84
- 'onValueChange'?: ((details: ValueChangeDetails) => void) | undefined;
90
+ onValueChange?: ((details: ValueChangeDetails) => void) | undefined;
85
91
  /**
86
92
  * Handler that is called when the user stops dragging.
87
93
  */
88
- 'onValueChangeEnd'?: ((details: ValueChangeDetails) => void) | undefined;
94
+ onValueChangeEnd?: ((details: ValueChangeDetails) => void) | undefined;
89
95
  /**
90
96
  * Handler that is called when the user opens or closes the color picker.
91
97
  */
92
- 'onOpenChange'?: ((details: OpenChangeDetails) => void) | undefined;
98
+ onOpenChange?: ((details: OpenChangeDetails) => void) | undefined;
93
99
  /**
94
100
  * The name for the form input
95
101
  */
96
- 'name'?: string | undefined;
102
+ name?: string | undefined;
97
103
  /**
98
104
  * The positioning options for the color picker
99
105
  */
100
- 'positioning': PositioningOptions;
106
+ positioning: PositioningOptions;
101
107
  /**
102
108
  * The initial focus element when the color picker is opened.
103
109
  */
104
- 'initialFocusEl'?: (() => HTMLElement | null) | undefined;
110
+ initialFocusEl?: (() => HTMLElement | null) | undefined;
105
111
  /**
106
112
  * Whether the color picker is open
107
113
  */
108
- 'open'?: boolean | undefined;
114
+ open?: boolean | undefined;
109
115
  /**
110
- * Whether the color picker open state is controlled by the user
116
+ * The initial open state of the color picker when it is not controlled.
117
+ * Prefer this over seeding with `open` for uncontrolled usage.
111
118
  */
112
- 'open.controlled'?: boolean | undefined;
119
+ defaultOpen?: boolean | undefined;
113
120
  /**
114
121
  * The color format to use
115
122
  * @default "rgba"
116
123
  */
117
- 'format': ColorFormat;
124
+ format: ColorFormat;
118
125
  /**
119
126
  * Function called when the color format changes
120
127
  */
121
- 'onFormatChange'?: ((details: FormatChangeDetails) => void) | undefined;
128
+ onFormatChange?: ((details: FormatChangeDetails) => void) | undefined;
122
129
  /**
123
130
  * Whether to close the color picker when a swatch is selected
124
131
  * @default false
125
132
  */
126
- 'closeOnSelect'?: boolean | undefined;
133
+ closeOnSelect?: boolean | undefined;
127
134
  /**
128
135
  * Whether to auto focus the color picker when it is opened
129
136
  * @default true
130
137
  */
131
- 'openAutoFocus'?: boolean | undefined;
138
+ openAutoFocus?: boolean | undefined;
132
139
  }
133
140
  interface PrivateContext {
134
141
  /**
package/dist/index.mjs CHANGED
@@ -3,7 +3,7 @@ import { getColorAreaGradient, normalizeColor, parseColor } from "@destyler/colo
3
3
  import { createScope, dataAttr, disableTextSelection, dispatchInputValueEvent, getEventKey, getEventPoint, getEventStep, getInitialFocus, getRelativePoint, isLeftClick, isModifierKey, query, queryAll, raf, trackFormControl, trackPointerMove, visuallyHiddenStyle } from "@destyler/dom";
4
4
  import { getPlacement, getPlacementStyles } from "@destyler/popper";
5
5
  import { trackDismissableElement } from "@destyler/dismissable";
6
- import { compact, tryCatch } from "@destyler/utils";
6
+ import { compact, isControlled, isPropUserProvided, resolveControllableOpen, resolveControllableProp, tryCatch, withControllableProvided } from "@destyler/utils";
7
7
  import { createMachine, guards } from "@destyler/xstate";
8
8
 
9
9
  //#region src/anatomy.ts
@@ -912,6 +912,14 @@ const invoke = {
912
912
  const set = {
913
913
  value(ctx, color) {
914
914
  if (!color || ctx.value.isEqual(color)) return;
915
+ if (isControlled(ctx, "value")) {
916
+ const value = color.toFormat(ctx.format);
917
+ ctx.onValueChange?.({
918
+ value,
919
+ valueAsString: value.toString(ctx.format)
920
+ });
921
+ return;
922
+ }
915
923
  ctx.value = color;
916
924
  invoke.change(ctx);
917
925
  },
@@ -922,10 +930,17 @@ const set = {
922
930
  }
923
931
  };
924
932
  function machine(userContext) {
925
- const ctx = compact(userContext);
933
+ const ctx = compact(withControllableProvided(userContext, ["open", "value"]));
934
+ const { initialOpen } = resolveControllableOpen(ctx);
935
+ const { initial: initialValue } = resolveControllableProp({
936
+ value: ctx.value,
937
+ defaultValue: ctx.defaultValue,
938
+ valueProvided: isPropUserProvided(ctx, "value"),
939
+ fallback: parse("#000000")
940
+ });
926
941
  return createMachine({
927
942
  id: "color-picker",
928
- initial: ctx.open ? "open" : "idle",
943
+ initial: initialOpen ? "open" : "idle",
929
944
  context: {
930
945
  dir: "ltr",
931
946
  value: parse("#000000"),
@@ -934,6 +949,7 @@ function machine(userContext) {
934
949
  closeOnSelect: false,
935
950
  openAutoFocus: true,
936
951
  ...ctx,
952
+ value: initialValue,
937
953
  activeId: null,
938
954
  activeChannel: null,
939
955
  activeOrientation: null,
@@ -1166,7 +1182,7 @@ function machine(userContext) {
1166
1182
  }, {
1167
1183
  guards: {
1168
1184
  closeOnSelect: (ctx$1) => !!ctx$1.closeOnSelect,
1169
- isOpenControlled: (ctx$1) => !!ctx$1["open.controlled"],
1185
+ isOpenControlled: (ctx$1) => isControlled(ctx$1, "open"),
1170
1186
  shouldRestoreFocus: (ctx$1) => !!ctx$1.restoreFocus
1171
1187
  },
1172
1188
  activities: {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@destyler/color-picker",
3
3
  "type": "module",
4
- "version": "0.2.6",
4
+ "version": "0.2.9",
5
5
  "description": "a component that allows users to select a color from a color picker.",
6
6
  "author": "Elone Hoo <elonehoo@gmail.com>",
7
7
  "license": "MIT",
@@ -22,14 +22,14 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@destyler/anatomy": "0.2.6",
26
- "@destyler/color": "0.2.6",
27
- "@destyler/dom": "0.2.6",
28
- "@destyler/popper": "0.2.6",
29
- "@destyler/dismissable": "0.2.6",
30
- "@destyler/types": "0.2.6",
31
- "@destyler/utils": "0.2.6",
32
- "@destyler/xstate": "0.2.6"
25
+ "@destyler/anatomy": "0.2.9",
26
+ "@destyler/color": "0.2.9",
27
+ "@destyler/dom": "0.2.9",
28
+ "@destyler/dismissable": "0.2.9",
29
+ "@destyler/utils": "0.2.9",
30
+ "@destyler/types": "0.2.9",
31
+ "@destyler/xstate": "0.2.9",
32
+ "@destyler/popper": "0.2.9"
33
33
  },
34
34
  "publishConfig": {
35
35
  "access": "public"