@destyler/toggle 0.2.10 → 0.2.12
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 +8 -11
- package/dist/index.mjs +51 -52
- package/package.json +6 -6
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import * as _destyler_anatomy0 from "@destyler/anatomy";
|
|
2
|
-
import * as _destyler_xstate0 from "@destyler/xstate";
|
|
3
1
|
import { AnyEventObject, Machine, XSend, XState } from "@destyler/xstate";
|
|
4
2
|
import { CommonProperties, DirectionProperty, NormalizeProps, Orientation, PropTypes, RequiredBy } from "@destyler/types";
|
|
5
|
-
|
|
6
3
|
//#region src/anatomy.d.ts
|
|
7
|
-
declare const anatomy:
|
|
4
|
+
export declare const anatomy: import("@destyler/anatomy").AnatomyInstance<"root" | "item">;
|
|
8
5
|
//#endregion
|
|
9
6
|
//#region src/types.d.ts
|
|
10
7
|
interface ValueChangeDetails {
|
|
@@ -130,15 +127,15 @@ interface MachineApi<T extends PropTypes = PropTypes> {
|
|
|
130
127
|
}
|
|
131
128
|
//#endregion
|
|
132
129
|
//#region src/connect.d.ts
|
|
133
|
-
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
130
|
+
export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
134
131
|
//#endregion
|
|
135
132
|
//#region src/machine.d.ts
|
|
136
|
-
declare function machine(userContext: UserDefinedContext):
|
|
133
|
+
export declare function machine(userContext: UserDefinedContext): import("@destyler/xstate").Machine<MachineContext, MachineState, import("@destyler/xstate").AnyEventObject>;
|
|
137
134
|
//#endregion
|
|
138
135
|
//#region src/props.d.ts
|
|
139
|
-
declare const props: ("value" | "ids" | "disabled" | "defaultValue" | "onValueChange" | "loopFocus" | "rovingFocus" | "orientation" | "multiple" | "dir" | "id" | "getRootNode")[];
|
|
140
|
-
declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "ids" | "disabled" | "defaultValue" | "onValueChange" | "loopFocus" | "rovingFocus" | "orientation" | "multiple" | "dir" | "id" | "getRootNode">];
|
|
141
|
-
declare const itemProps: (keyof ItemProps)[];
|
|
142
|
-
declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, keyof ItemProps>];
|
|
136
|
+
export declare const props: ("value" | "ids" | "disabled" | "defaultValue" | "onValueChange" | "loopFocus" | "rovingFocus" | "orientation" | "multiple" | "dir" | "id" | "getRootNode")[];
|
|
137
|
+
export declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "ids" | "disabled" | "defaultValue" | "onValueChange" | "loopFocus" | "rovingFocus" | "orientation" | "multiple" | "dir" | "id" | "getRootNode">];
|
|
138
|
+
export declare const itemProps: (keyof ItemProps)[];
|
|
139
|
+
export declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, keyof ItemProps>];
|
|
143
140
|
//#endregion
|
|
144
|
-
export {
|
|
141
|
+
export type { MachineApi as Api, UserDefinedContext as Context, ElementIds, ItemProps, ItemState, Orientation, Service, ValueChangeDetails };
|
package/dist/index.mjs
CHANGED
|
@@ -3,11 +3,9 @@ import { createScope, dataAttr, getEventKey, isSafari, isSelfTarget, nextById, p
|
|
|
3
3
|
import { add, compact, createSplitProps, first, isControlled, isEqual, isPropUserProvided, last, remove, resolveControllableProp, withControllableProvided } from "@destyler/utils";
|
|
4
4
|
import { createMachine, guards } from "@destyler/xstate";
|
|
5
5
|
import { createProps } from "@destyler/types";
|
|
6
|
-
|
|
7
6
|
//#region src/anatomy.ts
|
|
8
7
|
const anatomy = createAnatomy("toggle-group").parts("root", "item");
|
|
9
8
|
const parts = anatomy.build();
|
|
10
|
-
|
|
11
9
|
//#endregion
|
|
12
10
|
//#region src/dom.ts
|
|
13
11
|
const dom = createScope({
|
|
@@ -23,7 +21,6 @@ const dom = createScope({
|
|
|
23
21
|
getNextEl: (ctx, id) => nextById(dom.getElements(ctx), id, ctx.currentLoopFocus),
|
|
24
22
|
getPrevEl: (ctx, id) => prevById(dom.getElements(ctx), id, ctx.currentLoopFocus)
|
|
25
23
|
});
|
|
26
|
-
|
|
27
24
|
//#endregion
|
|
28
25
|
//#region src/connect.ts
|
|
29
26
|
function connect(state, send, normalize) {
|
|
@@ -32,21 +29,21 @@ function connect(state, send, normalize) {
|
|
|
32
29
|
const isSingle = !state.context.multiple;
|
|
33
30
|
const rovingFocus = state.context.rovingFocus;
|
|
34
31
|
const isHorizontal = state.context.orientation === "horizontal";
|
|
35
|
-
function getItemState(props
|
|
36
|
-
const id = dom.getItemId(state.context, props
|
|
32
|
+
function getItemState(props) {
|
|
33
|
+
const id = dom.getItemId(state.context, props.value);
|
|
37
34
|
return {
|
|
38
35
|
id,
|
|
39
|
-
disabled: Boolean(props
|
|
40
|
-
pressed: !!value.includes(props
|
|
36
|
+
disabled: Boolean(props.disabled || disabled),
|
|
37
|
+
pressed: !!value.includes(props.value),
|
|
41
38
|
focused: state.context.focusedId === id
|
|
42
39
|
};
|
|
43
40
|
}
|
|
44
41
|
return {
|
|
45
42
|
value,
|
|
46
|
-
setValue(value
|
|
43
|
+
setValue(value) {
|
|
47
44
|
send({
|
|
48
45
|
type: "VALUE.SET",
|
|
49
|
-
value
|
|
46
|
+
value
|
|
50
47
|
});
|
|
51
48
|
},
|
|
52
49
|
getRootProps() {
|
|
@@ -66,7 +63,8 @@ function connect(state, send, normalize) {
|
|
|
66
63
|
},
|
|
67
64
|
onFocus(event) {
|
|
68
65
|
if (disabled) return;
|
|
69
|
-
|
|
66
|
+
const evt = event.nativeEvent || event;
|
|
67
|
+
if (!isSelfTarget(evt) || !!state.context.isClickFocus || state.context.isTabbingBackward) return;
|
|
70
68
|
send("ROOT.FOCUS");
|
|
71
69
|
},
|
|
72
70
|
onBlur() {
|
|
@@ -76,8 +74,8 @@ function connect(state, send, normalize) {
|
|
|
76
74
|
});
|
|
77
75
|
},
|
|
78
76
|
getItemState,
|
|
79
|
-
getItemProps(props
|
|
80
|
-
const itemState = getItemState(props
|
|
77
|
+
getItemProps(props) {
|
|
78
|
+
const itemState = getItemState(props);
|
|
81
79
|
const rovingTabIndex = itemState.focused ? 0 : -1;
|
|
82
80
|
return normalize.button({
|
|
83
81
|
...parts.item.attrs,
|
|
@@ -106,7 +104,7 @@ function connect(state, send, normalize) {
|
|
|
106
104
|
send({
|
|
107
105
|
type: "TOGGLE.CLICK",
|
|
108
106
|
id: itemState.id,
|
|
109
|
-
value: props
|
|
107
|
+
value: props.value
|
|
110
108
|
});
|
|
111
109
|
if (isSafari()) event.currentTarget.focus({ preventScroll: true });
|
|
112
110
|
},
|
|
@@ -115,8 +113,8 @@ function connect(state, send, normalize) {
|
|
|
115
113
|
if (!isSelfTarget(event)) return;
|
|
116
114
|
if (itemState.disabled) return;
|
|
117
115
|
const exec = {
|
|
118
|
-
Tab(event
|
|
119
|
-
const isShiftTab = event
|
|
116
|
+
Tab(event) {
|
|
117
|
+
const isShiftTab = event.shiftKey;
|
|
120
118
|
send({
|
|
121
119
|
type: "TOGGLE.SHIFT_TAB",
|
|
122
120
|
isShiftTab
|
|
@@ -156,7 +154,6 @@ function connect(state, send, normalize) {
|
|
|
156
154
|
}
|
|
157
155
|
};
|
|
158
156
|
}
|
|
159
|
-
|
|
160
157
|
//#endregion
|
|
161
158
|
//#region src/machine.ts
|
|
162
159
|
const { not, and } = guards;
|
|
@@ -195,7 +192,7 @@ function machine(userContext) {
|
|
|
195
192
|
isClickFocus: false,
|
|
196
193
|
isWithinToolbar: false
|
|
197
194
|
},
|
|
198
|
-
computed: { currentLoopFocus: (ctx
|
|
195
|
+
computed: { currentLoopFocus: (ctx) => ctx.loopFocus && !ctx.isWithinToolbar },
|
|
199
196
|
entry: ["checkIfWithinToolbar"],
|
|
200
197
|
on: {
|
|
201
198
|
"VALUE.SET": { actions: ["setValue"] },
|
|
@@ -232,64 +229,67 @@ function machine(userContext) {
|
|
|
232
229
|
}
|
|
233
230
|
}, {
|
|
234
231
|
guards: {
|
|
235
|
-
isClickFocus: (ctx
|
|
236
|
-
isTabbingBackward: (ctx
|
|
232
|
+
isClickFocus: (ctx) => ctx.isClickFocus,
|
|
233
|
+
isTabbingBackward: (ctx) => ctx.isTabbingBackward
|
|
237
234
|
},
|
|
238
235
|
actions: {
|
|
239
|
-
setIsTabbingBackward(ctx
|
|
240
|
-
ctx
|
|
236
|
+
setIsTabbingBackward(ctx) {
|
|
237
|
+
ctx.isTabbingBackward = true;
|
|
241
238
|
},
|
|
242
|
-
clearIsTabbingBackward(ctx
|
|
243
|
-
ctx
|
|
239
|
+
clearIsTabbingBackward(ctx) {
|
|
240
|
+
ctx.isTabbingBackward = false;
|
|
244
241
|
},
|
|
245
|
-
setClickFocus(ctx
|
|
246
|
-
ctx
|
|
242
|
+
setClickFocus(ctx) {
|
|
243
|
+
ctx.isClickFocus = true;
|
|
247
244
|
},
|
|
248
|
-
clearClickFocus(ctx
|
|
249
|
-
ctx
|
|
245
|
+
clearClickFocus(ctx) {
|
|
246
|
+
ctx.isClickFocus = false;
|
|
250
247
|
},
|
|
251
|
-
checkIfWithinToolbar(ctx
|
|
252
|
-
ctx
|
|
248
|
+
checkIfWithinToolbar(ctx) {
|
|
249
|
+
ctx.isWithinToolbar = !!dom.getRootEl(ctx)?.closest("[role=toolbar]");
|
|
253
250
|
},
|
|
254
|
-
setFocusedId(ctx
|
|
255
|
-
ctx
|
|
251
|
+
setFocusedId(ctx, evt) {
|
|
252
|
+
ctx.focusedId = evt.id;
|
|
256
253
|
},
|
|
257
|
-
clearFocusedId(ctx
|
|
258
|
-
ctx
|
|
254
|
+
clearFocusedId(ctx) {
|
|
255
|
+
ctx.focusedId = null;
|
|
259
256
|
},
|
|
260
|
-
setValue(ctx
|
|
261
|
-
if (
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
257
|
+
setValue(ctx, evt) {
|
|
258
|
+
if (evt.type === "VALUE.SET") {
|
|
259
|
+
set.value(ctx, Array.from(evt.value));
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (evt.value == null) return;
|
|
263
|
+
let next = Array.from(ctx.value);
|
|
264
|
+
if (ctx.multiple) next = next.includes(evt.value) ? remove(next, evt.value) : add(next, evt.value);
|
|
265
|
+
else next = isEqual(ctx.value, [evt.value]) ? [] : [evt.value];
|
|
266
|
+
set.value(ctx, next);
|
|
266
267
|
},
|
|
267
|
-
focusNextToggle(ctx
|
|
268
|
+
focusNextToggle(ctx) {
|
|
268
269
|
raf(() => {
|
|
269
|
-
if (!ctx
|
|
270
|
-
dom.getNextEl(ctx
|
|
270
|
+
if (!ctx.focusedId) return;
|
|
271
|
+
dom.getNextEl(ctx, ctx.focusedId)?.focus({ preventScroll: true });
|
|
271
272
|
});
|
|
272
273
|
},
|
|
273
|
-
focusPrevToggle(ctx
|
|
274
|
+
focusPrevToggle(ctx) {
|
|
274
275
|
raf(() => {
|
|
275
|
-
if (!ctx
|
|
276
|
-
dom.getPrevEl(ctx
|
|
276
|
+
if (!ctx.focusedId) return;
|
|
277
|
+
dom.getPrevEl(ctx, ctx.focusedId)?.focus({ preventScroll: true });
|
|
277
278
|
});
|
|
278
279
|
},
|
|
279
|
-
focusFirstToggle(ctx
|
|
280
|
+
focusFirstToggle(ctx) {
|
|
280
281
|
raf(() => {
|
|
281
|
-
dom.getFirstEl(ctx
|
|
282
|
+
dom.getFirstEl(ctx)?.focus({ preventScroll: true });
|
|
282
283
|
});
|
|
283
284
|
},
|
|
284
|
-
focusLastToggle(ctx
|
|
285
|
+
focusLastToggle(ctx) {
|
|
285
286
|
raf(() => {
|
|
286
|
-
dom.getLastEl(ctx
|
|
287
|
+
dom.getLastEl(ctx)?.focus({ preventScroll: true });
|
|
287
288
|
});
|
|
288
289
|
}
|
|
289
290
|
}
|
|
290
291
|
});
|
|
291
292
|
}
|
|
292
|
-
|
|
293
293
|
//#endregion
|
|
294
294
|
//#region src/props.ts
|
|
295
295
|
const props = createProps()([
|
|
@@ -309,6 +309,5 @@ const props = createProps()([
|
|
|
309
309
|
const splitProps = createSplitProps(props);
|
|
310
310
|
const itemProps = createProps()(["value", "disabled"]);
|
|
311
311
|
const splitItemProps = createSplitProps(itemProps);
|
|
312
|
-
|
|
313
312
|
//#endregion
|
|
314
|
-
export { anatomy, connect, itemProps, machine, props, splitItemProps, splitProps };
|
|
313
|
+
export { anatomy, connect, itemProps, machine, props, splitItemProps, splitProps };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@destyler/toggle",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.12",
|
|
5
5
|
"description": "A two-state button that can be either on or off.",
|
|
6
6
|
"author": "Elone Hoo <elonehoo@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@destyler/
|
|
26
|
-
"@destyler/
|
|
27
|
-
"@destyler/
|
|
28
|
-
"@destyler/
|
|
29
|
-
"@destyler/
|
|
25
|
+
"@destyler/anatomy": "0.2.12",
|
|
26
|
+
"@destyler/dom": "0.2.12",
|
|
27
|
+
"@destyler/types": "0.2.12",
|
|
28
|
+
"@destyler/utils": "0.2.12",
|
|
29
|
+
"@destyler/xstate": "0.2.12"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|