@destyler/color-picker 0.2.6 → 0.2.7

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
@@ -1,4 +1,3 @@
1
- import * as _destyler_anatomy0 from "@destyler/anatomy";
2
1
  import { Color, Color as Color$1, ColorAxes, ColorChannel, ColorFormat, ColorType } from "@destyler/color";
3
2
  import { PositioningOptions } from "@destyler/popper";
4
3
  import { FocusOutsideEvent, InteractOutsideEvent, InteractOutsideHandlers, PointerDownOutsideEvent } from "@destyler/dismissable";
@@ -7,7 +6,7 @@ import { AnyEventObject, Machine, XSend, XState } from "@destyler/xstate";
7
6
  import { CommonProperties, DirectionProperty, NormalizeProps, Orientation, PropTypes, RequiredBy } from "@destyler/types";
8
7
 
9
8
  //#region src/anatomy.d.ts
10
- declare const anatomy: _destyler_anatomy0.AnatomyInstance<"root" | "label" | "control" | "trigger" | "positioner" | "content" | "area" | "areaThumb" | "valueText" | "areaBackground" | "channelSlider" | "channelSliderLabel" | "channelSliderTrack" | "channelSliderThumb" | "channelSliderValueText" | "channelInput" | "transparencyGrid" | "swatchGroup" | "swatchTrigger" | "swatchIndicator" | "swatch" | "eyeDropperTrigger" | "formatTrigger" | "formatSelect">;
9
+ declare const anatomy: any;
11
10
  //#endregion
12
11
  //#region src/types.d.ts
13
12
  type ExtendedColorChannel = ColorChannel | 'hex' | 'css';
@@ -56,79 +55,86 @@ interface PublicContext extends CommonProperties, DirectionProperty, InteractOut
56
55
  /**
57
56
  * The ids of the elements in the color picker. Useful for composition.
58
57
  */
59
- 'ids'?: ElementIds | undefined;
58
+ ids?: ElementIds | undefined;
60
59
  /**
61
- * The current color value
60
+ * The controlled color value
62
61
  * @default #000000
63
62
  */
64
- 'value': Color;
63
+ value?: Color | undefined;
64
+ /**
65
+ * The initial color value when it is not controlled.
66
+ * Prefer this over seeding with `value` for uncontrolled usage.
67
+ * @default #000000
68
+ */
69
+ defaultValue?: Color | undefined;
65
70
  /**
66
71
  * Whether the color picker is disabled
67
72
  */
68
- 'disabled'?: boolean | undefined;
73
+ disabled?: boolean | undefined;
69
74
  /**
70
75
  * Whether the color picker is read-only
71
76
  */
72
- 'readOnly'?: boolean | undefined;
77
+ readOnly?: boolean | undefined;
73
78
  /**
74
79
  * Whether the color picker is required
75
80
  */
76
- 'required'?: boolean | undefined;
81
+ required?: boolean | undefined;
77
82
  /**
78
83
  * Whether the color picker is invalid
79
84
  */
80
- 'invalid'?: boolean | undefined;
85
+ invalid?: boolean | undefined;
81
86
  /**
82
87
  * Handler that is called when the value changes, as the user drags.
83
88
  */
84
- 'onValueChange'?: ((details: ValueChangeDetails) => void) | undefined;
89
+ onValueChange?: ((details: ValueChangeDetails) => void) | undefined;
85
90
  /**
86
91
  * Handler that is called when the user stops dragging.
87
92
  */
88
- 'onValueChangeEnd'?: ((details: ValueChangeDetails) => void) | undefined;
93
+ onValueChangeEnd?: ((details: ValueChangeDetails) => void) | undefined;
89
94
  /**
90
95
  * Handler that is called when the user opens or closes the color picker.
91
96
  */
92
- 'onOpenChange'?: ((details: OpenChangeDetails) => void) | undefined;
97
+ onOpenChange?: ((details: OpenChangeDetails) => void) | undefined;
93
98
  /**
94
99
  * The name for the form input
95
100
  */
96
- 'name'?: string | undefined;
101
+ name?: string | undefined;
97
102
  /**
98
103
  * The positioning options for the color picker
99
104
  */
100
- 'positioning': PositioningOptions;
105
+ positioning: PositioningOptions;
101
106
  /**
102
107
  * The initial focus element when the color picker is opened.
103
108
  */
104
- 'initialFocusEl'?: (() => HTMLElement | null) | undefined;
109
+ initialFocusEl?: (() => HTMLElement | null) | undefined;
105
110
  /**
106
111
  * Whether the color picker is open
107
112
  */
108
- 'open'?: boolean | undefined;
113
+ open?: boolean | undefined;
109
114
  /**
110
- * Whether the color picker open state is controlled by the user
115
+ * The initial open state of the color picker when it is not controlled.
116
+ * Prefer this over seeding with `open` for uncontrolled usage.
111
117
  */
112
- 'open.controlled'?: boolean | undefined;
118
+ defaultOpen?: boolean | undefined;
113
119
  /**
114
120
  * The color format to use
115
121
  * @default "rgba"
116
122
  */
117
- 'format': ColorFormat;
123
+ format: ColorFormat;
118
124
  /**
119
125
  * Function called when the color format changes
120
126
  */
121
- 'onFormatChange'?: ((details: FormatChangeDetails) => void) | undefined;
127
+ onFormatChange?: ((details: FormatChangeDetails) => void) | undefined;
122
128
  /**
123
129
  * Whether to close the color picker when a swatch is selected
124
130
  * @default false
125
131
  */
126
- 'closeOnSelect'?: boolean | undefined;
132
+ closeOnSelect?: boolean | undefined;
127
133
  /**
128
134
  * Whether to auto focus the color picker when it is opened
129
135
  * @default true
130
136
  */
131
- 'openAutoFocus'?: boolean | undefined;
137
+ openAutoFocus?: boolean | undefined;
132
138
  }
133
139
  interface PrivateContext {
134
140
  /**
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.7",
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.7",
26
+ "@destyler/color": "0.2.7",
27
+ "@destyler/dom": "0.2.7",
28
+ "@destyler/types": "0.2.7",
29
+ "@destyler/xstate": "0.2.7",
30
+ "@destyler/popper": "0.2.7",
31
+ "@destyler/dismissable": "0.2.7",
32
+ "@destyler/utils": "0.2.7"
33
33
  },
34
34
  "publishConfig": {
35
35
  "access": "public"