@zag-js/color-picker 0.56.1 → 0.57.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.
- package/dist/index.d.mts +14 -5
- package/dist/index.d.ts +14 -5
- package/dist/index.js +64 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +64 -32
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -12
- package/src/color-picker.anatomy.ts +2 -0
- package/src/color-picker.connect.ts +47 -15
- package/src/color-picker.machine.ts +19 -17
- package/src/color-picker.types.ts +14 -3
- package/src/index.ts +1 -0
package/dist/index.mjs
CHANGED
|
@@ -11,8 +11,10 @@ var anatomy = createAnatomy("color-picker", [
|
|
|
11
11
|
"areaThumb",
|
|
12
12
|
"areaBackground",
|
|
13
13
|
"channelSlider",
|
|
14
|
+
"channelSliderLabel",
|
|
14
15
|
"channelSliderTrack",
|
|
15
16
|
"channelSliderThumb",
|
|
17
|
+
"channelSliderValueText",
|
|
16
18
|
"channelInput",
|
|
17
19
|
"transparencyGrid",
|
|
18
20
|
"swatchGroup",
|
|
@@ -256,6 +258,9 @@ function connect(state, send, normalize) {
|
|
|
256
258
|
getChannelValue(channel) {
|
|
257
259
|
return getChannelValue(value, channel);
|
|
258
260
|
},
|
|
261
|
+
getChannelValueText(channel, locale) {
|
|
262
|
+
return value.formatChannelValue(channel, locale);
|
|
263
|
+
},
|
|
259
264
|
setChannelValue(channel, channelValue) {
|
|
260
265
|
const color = value.withChannelValue(channel, channelValue);
|
|
261
266
|
send({ type: "VALUE.SET", value: color, src: "set-channel" });
|
|
@@ -489,7 +494,7 @@ function connect(state, send, normalize) {
|
|
|
489
494
|
});
|
|
490
495
|
},
|
|
491
496
|
getChannelSliderProps(props) {
|
|
492
|
-
const { orientation = "horizontal", channel } = props;
|
|
497
|
+
const { orientation = "horizontal", channel, format } = props;
|
|
493
498
|
return normalize.element({
|
|
494
499
|
...parts.channelSlider.attrs,
|
|
495
500
|
"data-channel": channel,
|
|
@@ -500,7 +505,7 @@ function connect(state, send, normalize) {
|
|
|
500
505
|
if (!isLeftClick(event)) return;
|
|
501
506
|
if (isModifierKey(event)) return;
|
|
502
507
|
const point = getEventPoint(event);
|
|
503
|
-
send({ type: "CHANNEL_SLIDER.POINTER_DOWN", channel, point, id: channel, orientation });
|
|
508
|
+
send({ type: "CHANNEL_SLIDER.POINTER_DOWN", channel, format, point, id: channel, orientation });
|
|
504
509
|
event.preventDefault();
|
|
505
510
|
},
|
|
506
511
|
style: {
|
|
@@ -510,7 +515,8 @@ function connect(state, send, normalize) {
|
|
|
510
515
|
});
|
|
511
516
|
},
|
|
512
517
|
getChannelSliderTrackProps(props) {
|
|
513
|
-
const { orientation = "horizontal", channel } = props;
|
|
518
|
+
const { orientation = "horizontal", channel, format } = props;
|
|
519
|
+
const normalizedValue = format ? value.toFormat(format) : areaValue;
|
|
514
520
|
return normalize.element({
|
|
515
521
|
...parts.channelSliderTrack.attrs,
|
|
516
522
|
id: dom.getChannelSliderId(state.context, channel),
|
|
@@ -524,16 +530,40 @@ function connect(state, send, normalize) {
|
|
|
524
530
|
orientation,
|
|
525
531
|
channel,
|
|
526
532
|
dir: state.context.dir,
|
|
527
|
-
value:
|
|
533
|
+
value: normalizedValue
|
|
528
534
|
})
|
|
529
535
|
}
|
|
530
536
|
});
|
|
531
537
|
},
|
|
538
|
+
getChannelSliderLabelProps(props) {
|
|
539
|
+
const { channel } = props;
|
|
540
|
+
return normalize.element({
|
|
541
|
+
...parts.channelSliderLabel.attrs,
|
|
542
|
+
"data-channel": channel,
|
|
543
|
+
onClick(event) {
|
|
544
|
+
if (!interactive) return;
|
|
545
|
+
event.preventDefault();
|
|
546
|
+
const thumbId = dom.getChannelSliderThumbId(state.context, channel);
|
|
547
|
+
dom.getById(state.context, thumbId)?.focus({ preventScroll: true });
|
|
548
|
+
},
|
|
549
|
+
style: {
|
|
550
|
+
userSelect: "none",
|
|
551
|
+
WebkitUserSelect: "none"
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
},
|
|
555
|
+
getChannelSliderValueTextProps(props) {
|
|
556
|
+
return normalize.element({
|
|
557
|
+
...parts.channelSliderValueText.attrs,
|
|
558
|
+
"data-channel": props.channel
|
|
559
|
+
});
|
|
560
|
+
},
|
|
532
561
|
getChannelSliderThumbProps(props) {
|
|
533
|
-
const { orientation = "horizontal", channel } = props;
|
|
534
|
-
const
|
|
535
|
-
const
|
|
536
|
-
const
|
|
562
|
+
const { orientation = "horizontal", channel, format } = props;
|
|
563
|
+
const normalizedValue = format ? value.toFormat(format) : areaValue;
|
|
564
|
+
const channelRange = normalizedValue.getChannelRange(channel);
|
|
565
|
+
const channelValue = normalizedValue.getChannelValue(channel);
|
|
566
|
+
const offset = (channelValue - channelRange.minValue) / (channelRange.maxValue - channelRange.minValue);
|
|
537
567
|
const placementStyles = orientation === "horizontal" ? { left: `${offset * 100}%`, top: "50%" } : { top: `${offset * 100}%`, left: "50%" };
|
|
538
568
|
return normalize.element({
|
|
539
569
|
...parts.channelSliderThumb.attrs,
|
|
@@ -546,8 +576,8 @@ function connect(state, send, normalize) {
|
|
|
546
576
|
"data-orientation": orientation,
|
|
547
577
|
"aria-disabled": dataAttr(disabled),
|
|
548
578
|
"aria-orientation": orientation,
|
|
549
|
-
"aria-valuemax": maxValue,
|
|
550
|
-
"aria-valuemin": minValue,
|
|
579
|
+
"aria-valuemax": channelRange.maxValue,
|
|
580
|
+
"aria-valuemin": channelRange.minValue,
|
|
551
581
|
"aria-valuenow": channelValue,
|
|
552
582
|
"aria-valuetext": `${channel} ${channelValue}`,
|
|
553
583
|
style: {
|
|
@@ -563,7 +593,7 @@ function connect(state, send, normalize) {
|
|
|
563
593
|
onKeyDown(event) {
|
|
564
594
|
if (event.defaultPrevented) return;
|
|
565
595
|
if (!interactive) return;
|
|
566
|
-
const step = getEventStep(event) *
|
|
596
|
+
const step = getEventStep(event) * channelRange.step;
|
|
567
597
|
const keyMap = {
|
|
568
598
|
ArrowUp() {
|
|
569
599
|
send({ type: "CHANNEL_SLIDER.ARROW_UP", channel, step });
|
|
@@ -604,7 +634,7 @@ function connect(state, send, normalize) {
|
|
|
604
634
|
getChannelInputProps(props) {
|
|
605
635
|
const { channel } = props;
|
|
606
636
|
const isTextField = channel === "hex" || channel === "css";
|
|
607
|
-
const
|
|
637
|
+
const channelRange = getChannelRange(value, channel);
|
|
608
638
|
return normalize.input({
|
|
609
639
|
...parts.channelInput.attrs,
|
|
610
640
|
dir: state.context.dir,
|
|
@@ -617,9 +647,9 @@ function connect(state, send, normalize) {
|
|
|
617
647
|
"data-disabled": dataAttr(disabled),
|
|
618
648
|
readOnly: state.context.readOnly,
|
|
619
649
|
defaultValue: getChannelValue(value, channel),
|
|
620
|
-
min:
|
|
621
|
-
max:
|
|
622
|
-
step:
|
|
650
|
+
min: channelRange?.minValue,
|
|
651
|
+
max: channelRange?.maxValue,
|
|
652
|
+
step: channelRange?.step,
|
|
623
653
|
onBeforeInput(event) {
|
|
624
654
|
if (isTextField || !interactive) return;
|
|
625
655
|
const value2 = event.currentTarget.value;
|
|
@@ -949,22 +979,22 @@ function machine(userContext) {
|
|
|
949
979
|
actions: ["setActiveChannel"]
|
|
950
980
|
},
|
|
951
981
|
"AREA.ARROW_LEFT": {
|
|
952
|
-
actions: ["
|
|
982
|
+
actions: ["decrementAreaXChannel"]
|
|
953
983
|
},
|
|
954
984
|
"AREA.ARROW_RIGHT": {
|
|
955
|
-
actions: ["
|
|
985
|
+
actions: ["incrementAreaXChannel"]
|
|
956
986
|
},
|
|
957
987
|
"AREA.ARROW_UP": {
|
|
958
|
-
actions: ["
|
|
988
|
+
actions: ["incrementAreaYChannel"]
|
|
959
989
|
},
|
|
960
990
|
"AREA.ARROW_DOWN": {
|
|
961
|
-
actions: ["
|
|
991
|
+
actions: ["decrementAreaYChannel"]
|
|
962
992
|
},
|
|
963
993
|
"AREA.PAGE_UP": {
|
|
964
|
-
actions: ["
|
|
994
|
+
actions: ["incrementAreaXChannel"]
|
|
965
995
|
},
|
|
966
996
|
"AREA.PAGE_DOWN": {
|
|
967
|
-
actions: ["
|
|
997
|
+
actions: ["decrementAreaXChannel"]
|
|
968
998
|
},
|
|
969
999
|
"CHANNEL_SLIDER.ARROW_LEFT": {
|
|
970
1000
|
actions: ["decrementChannel"]
|
|
@@ -1139,11 +1169,11 @@ function machine(userContext) {
|
|
|
1139
1169
|
}
|
|
1140
1170
|
});
|
|
1141
1171
|
},
|
|
1142
|
-
trackPointerMove(ctx2,
|
|
1172
|
+
trackPointerMove(ctx2, evt, { send }) {
|
|
1143
1173
|
return trackPointerMove(dom.getDoc(ctx2), {
|
|
1144
1174
|
onPointerMove({ point }) {
|
|
1145
1175
|
const type = ctx2.activeId === "area" ? "AREA.POINTER_MOVE" : "CHANNEL_SLIDER.POINTER_MOVE";
|
|
1146
|
-
send({ type, point });
|
|
1176
|
+
send({ type, point, format: evt.format });
|
|
1147
1177
|
},
|
|
1148
1178
|
onPointerUp() {
|
|
1149
1179
|
const type = ctx2.activeId === "area" ? "AREA.POINTER_UP" : "CHANNEL_SLIDER.POINTER_UP";
|
|
@@ -1179,22 +1209,24 @@ function machine(userContext) {
|
|
|
1179
1209
|
ctx2.activeOrientation = null;
|
|
1180
1210
|
},
|
|
1181
1211
|
setAreaColorFromPoint(ctx2, evt) {
|
|
1212
|
+
const normalizedValue = evt.format ? ctx2.value.toFormat(evt.format) : ctx2.areaValue;
|
|
1182
1213
|
const { xChannel, yChannel } = evt.channel || ctx2.activeChannel;
|
|
1183
1214
|
const percent = dom.getAreaValueFromPoint(ctx2, evt.point);
|
|
1184
1215
|
if (!percent) return;
|
|
1185
|
-
const xValue =
|
|
1186
|
-
const yValue =
|
|
1187
|
-
const color =
|
|
1216
|
+
const xValue = normalizedValue.getChannelPercentValue(xChannel, percent.x);
|
|
1217
|
+
const yValue = normalizedValue.getChannelPercentValue(yChannel, 1 - percent.y);
|
|
1218
|
+
const color = normalizedValue.withChannelValue(xChannel, xValue).withChannelValue(yChannel, yValue);
|
|
1188
1219
|
set.value(ctx2, color);
|
|
1189
1220
|
},
|
|
1190
1221
|
setChannelColorFromPoint(ctx2, evt) {
|
|
1191
1222
|
const channel = evt.channel || ctx2.activeId;
|
|
1223
|
+
const normalizedValue = evt.format ? ctx2.value.toFormat(evt.format) : ctx2.areaValue;
|
|
1192
1224
|
const percent = dom.getChannelSliderValueFromPoint(ctx2, evt.point, channel);
|
|
1193
1225
|
if (!percent) return;
|
|
1194
1226
|
const orientation = ctx2.activeOrientation || "horizontal";
|
|
1195
1227
|
const channelPercent = orientation === "horizontal" ? percent.x : percent.y;
|
|
1196
|
-
const value =
|
|
1197
|
-
const color =
|
|
1228
|
+
const value = normalizedValue.getChannelPercentValue(channel, channelPercent);
|
|
1229
|
+
const color = normalizedValue.withChannelValue(channel, value);
|
|
1198
1230
|
set.value(ctx2, color);
|
|
1199
1231
|
},
|
|
1200
1232
|
setValue(ctx2, evt) {
|
|
@@ -1238,22 +1270,22 @@ function machine(userContext) {
|
|
|
1238
1270
|
const color = ctx2.value.decrementChannel(evt.channel, evt.step);
|
|
1239
1271
|
set.value(ctx2, color);
|
|
1240
1272
|
},
|
|
1241
|
-
|
|
1273
|
+
incrementAreaXChannel(ctx2, evt) {
|
|
1242
1274
|
const { xChannel } = evt.channel;
|
|
1243
1275
|
const color = ctx2.areaValue.incrementChannel(xChannel, evt.step);
|
|
1244
1276
|
set.value(ctx2, color);
|
|
1245
1277
|
},
|
|
1246
|
-
|
|
1278
|
+
decrementAreaXChannel(ctx2, evt) {
|
|
1247
1279
|
const { xChannel } = evt.channel;
|
|
1248
1280
|
const color = ctx2.areaValue.decrementChannel(xChannel, evt.step);
|
|
1249
1281
|
set.value(ctx2, color);
|
|
1250
1282
|
},
|
|
1251
|
-
|
|
1283
|
+
incrementAreaYChannel(ctx2, evt) {
|
|
1252
1284
|
const { yChannel } = evt.channel;
|
|
1253
1285
|
const color = ctx2.areaValue.incrementChannel(yChannel, evt.step);
|
|
1254
1286
|
set.value(ctx2, color);
|
|
1255
1287
|
},
|
|
1256
|
-
|
|
1288
|
+
decrementAreaYChannel(ctx2, evt) {
|
|
1257
1289
|
const { yChannel } = evt.channel;
|
|
1258
1290
|
const color = ctx2.areaValue.decrementChannel(yChannel, evt.step);
|
|
1259
1291
|
set.value(ctx2, color);
|