@destyler/toggle 0.2.6-beta.1 → 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 +10 -6
- package/dist/index.mjs +50 -38
- package/package.json +6 -6
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import * as _destyler_anatomy0 from "@destyler/anatomy";
|
|
2
1
|
import * as _destyler_xstate0 from "@destyler/xstate";
|
|
3
2
|
import { AnyEventObject, Machine, XSend, XState } from "@destyler/xstate";
|
|
4
3
|
import { CommonProperties, DirectionProperty, NormalizeProps, Orientation, PropTypes, RequiredBy } from "@destyler/types";
|
|
5
4
|
|
|
6
5
|
//#region src/anatomy.d.ts
|
|
7
|
-
declare const anatomy:
|
|
6
|
+
declare const anatomy: any;
|
|
8
7
|
//#endregion
|
|
9
8
|
//#region src/types.d.ts
|
|
10
9
|
interface ValueChangeDetails {
|
|
@@ -24,9 +23,14 @@ interface PublicContext extends DirectionProperty, CommonProperties {
|
|
|
24
23
|
*/
|
|
25
24
|
disabled?: boolean | undefined;
|
|
26
25
|
/**
|
|
27
|
-
* The values of the toggles in the group.
|
|
26
|
+
* The controlled values of the toggles in the group.
|
|
28
27
|
*/
|
|
29
|
-
value
|
|
28
|
+
value?: string[] | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* The initial values when the toggle group is not controlled.
|
|
31
|
+
* Prefer this over seeding with `value` for uncontrolled usage.
|
|
32
|
+
*/
|
|
33
|
+
defaultValue?: string[] | undefined;
|
|
30
34
|
/**
|
|
31
35
|
* Function to call when the toggle is clicked.
|
|
32
36
|
*/
|
|
@@ -131,8 +135,8 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
|
|
|
131
135
|
declare function machine(userContext: UserDefinedContext): _destyler_xstate0.Machine<MachineContext, MachineState, _destyler_xstate0.AnyEventObject>;
|
|
132
136
|
//#endregion
|
|
133
137
|
//#region src/props.d.ts
|
|
134
|
-
declare const props: ("value" | "ids" | "disabled" | "onValueChange" | "loopFocus" | "rovingFocus" | "orientation" | "multiple" | "dir" | "id" | "getRootNode")[];
|
|
135
|
-
declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "value" | "ids" | "disabled" | "onValueChange" | "loopFocus" | "rovingFocus" | "orientation" | "multiple" | "dir" | "id" | "getRootNode">];
|
|
138
|
+
declare const props: ("value" | "ids" | "disabled" | "defaultValue" | "onValueChange" | "loopFocus" | "rovingFocus" | "orientation" | "multiple" | "dir" | "id" | "getRootNode")[];
|
|
139
|
+
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">];
|
|
136
140
|
declare const itemProps: (keyof ItemProps)[];
|
|
137
141
|
declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, keyof ItemProps>];
|
|
138
142
|
//#endregion
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createAnatomy } from "@destyler/anatomy";
|
|
2
2
|
import { createScope, dataAttr, getEventKey, isSafari, isSelfTarget, nextById, prevById, queryAll, raf } from "@destyler/dom";
|
|
3
|
-
import { add, compact, createSplitProps, first, isEqual, last, remove } from "@destyler/utils";
|
|
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
6
|
|
|
@@ -160,31 +160,42 @@ function connect(state, send, normalize) {
|
|
|
160
160
|
//#endregion
|
|
161
161
|
//#region src/machine.ts
|
|
162
162
|
const { not, and } = guards;
|
|
163
|
-
const invoke = { change(ctx) {
|
|
164
|
-
ctx.onValueChange?.({ value: Array.from(
|
|
163
|
+
const invoke = { change(ctx, value) {
|
|
164
|
+
ctx.onValueChange?.({ value: Array.from(value) });
|
|
165
165
|
} };
|
|
166
166
|
const set = { value(ctx, value) {
|
|
167
167
|
if (isEqual(ctx.value, value)) return;
|
|
168
|
+
if (isControlled(ctx, "value")) {
|
|
169
|
+
invoke.change(ctx, value);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
168
172
|
ctx.value = value;
|
|
169
|
-
invoke.change(ctx);
|
|
173
|
+
invoke.change(ctx, value);
|
|
170
174
|
} };
|
|
171
175
|
function machine(userContext) {
|
|
176
|
+
const ctx = compact(withControllableProvided(userContext, ["value"]));
|
|
177
|
+
const { initial: initialValue } = resolveControllableProp({
|
|
178
|
+
value: ctx.value,
|
|
179
|
+
defaultValue: ctx.defaultValue,
|
|
180
|
+
valueProvided: isPropUserProvided(ctx, "value"),
|
|
181
|
+
fallback: []
|
|
182
|
+
});
|
|
172
183
|
return createMachine({
|
|
173
184
|
id: "toggle-group",
|
|
174
185
|
initial: "idle",
|
|
175
186
|
context: {
|
|
176
|
-
value: [],
|
|
177
187
|
disabled: false,
|
|
178
188
|
orientation: "horizontal",
|
|
179
189
|
rovingFocus: true,
|
|
180
190
|
loopFocus: true,
|
|
181
|
-
...
|
|
191
|
+
...ctx,
|
|
192
|
+
value: initialValue,
|
|
182
193
|
focusedId: null,
|
|
183
194
|
isTabbingBackward: false,
|
|
184
195
|
isClickFocus: false,
|
|
185
196
|
isWithinToolbar: false
|
|
186
197
|
},
|
|
187
|
-
computed: { currentLoopFocus: (ctx) => ctx.loopFocus && !ctx.isWithinToolbar },
|
|
198
|
+
computed: { currentLoopFocus: (ctx$1) => ctx$1.loopFocus && !ctx$1.isWithinToolbar },
|
|
188
199
|
entry: ["checkIfWithinToolbar"],
|
|
189
200
|
on: {
|
|
190
201
|
"VALUE.SET": { actions: ["setValue"] },
|
|
@@ -221,58 +232,58 @@ function machine(userContext) {
|
|
|
221
232
|
}
|
|
222
233
|
}, {
|
|
223
234
|
guards: {
|
|
224
|
-
isClickFocus: (ctx) => ctx.isClickFocus,
|
|
225
|
-
isTabbingBackward: (ctx) => ctx.isTabbingBackward
|
|
235
|
+
isClickFocus: (ctx$1) => ctx$1.isClickFocus,
|
|
236
|
+
isTabbingBackward: (ctx$1) => ctx$1.isTabbingBackward
|
|
226
237
|
},
|
|
227
238
|
actions: {
|
|
228
|
-
setIsTabbingBackward(ctx) {
|
|
229
|
-
ctx.isTabbingBackward = true;
|
|
239
|
+
setIsTabbingBackward(ctx$1) {
|
|
240
|
+
ctx$1.isTabbingBackward = true;
|
|
230
241
|
},
|
|
231
|
-
clearIsTabbingBackward(ctx) {
|
|
232
|
-
ctx.isTabbingBackward = false;
|
|
242
|
+
clearIsTabbingBackward(ctx$1) {
|
|
243
|
+
ctx$1.isTabbingBackward = false;
|
|
233
244
|
},
|
|
234
|
-
setClickFocus(ctx) {
|
|
235
|
-
ctx.isClickFocus = true;
|
|
245
|
+
setClickFocus(ctx$1) {
|
|
246
|
+
ctx$1.isClickFocus = true;
|
|
236
247
|
},
|
|
237
|
-
clearClickFocus(ctx) {
|
|
238
|
-
ctx.isClickFocus = false;
|
|
248
|
+
clearClickFocus(ctx$1) {
|
|
249
|
+
ctx$1.isClickFocus = false;
|
|
239
250
|
},
|
|
240
|
-
checkIfWithinToolbar(ctx) {
|
|
241
|
-
ctx.isWithinToolbar = !!dom.getRootEl(ctx)?.closest("[role=toolbar]");
|
|
251
|
+
checkIfWithinToolbar(ctx$1) {
|
|
252
|
+
ctx$1.isWithinToolbar = !!dom.getRootEl(ctx$1)?.closest("[role=toolbar]");
|
|
242
253
|
},
|
|
243
|
-
setFocusedId(ctx, evt) {
|
|
244
|
-
ctx.focusedId = evt.id;
|
|
254
|
+
setFocusedId(ctx$1, evt) {
|
|
255
|
+
ctx$1.focusedId = evt.id;
|
|
245
256
|
},
|
|
246
|
-
clearFocusedId(ctx) {
|
|
247
|
-
ctx.focusedId = null;
|
|
257
|
+
clearFocusedId(ctx$1) {
|
|
258
|
+
ctx$1.focusedId = null;
|
|
248
259
|
},
|
|
249
|
-
setValue(ctx, evt) {
|
|
260
|
+
setValue(ctx$1, evt) {
|
|
250
261
|
if (!evt.value) return;
|
|
251
|
-
let next = Array.from(ctx.value);
|
|
252
|
-
if (ctx.multiple) next = next.includes(evt.value) ? remove(next, evt.value) : add(next, evt.value);
|
|
253
|
-
else next = isEqual(ctx.value, [evt.value]) ? [] : [evt.value];
|
|
254
|
-
set.value(ctx, next);
|
|
262
|
+
let next = Array.from(ctx$1.value);
|
|
263
|
+
if (ctx$1.multiple) next = next.includes(evt.value) ? remove(next, evt.value) : add(next, evt.value);
|
|
264
|
+
else next = isEqual(ctx$1.value, [evt.value]) ? [] : [evt.value];
|
|
265
|
+
set.value(ctx$1, next);
|
|
255
266
|
},
|
|
256
|
-
focusNextToggle(ctx) {
|
|
267
|
+
focusNextToggle(ctx$1) {
|
|
257
268
|
raf(() => {
|
|
258
|
-
if (!ctx.focusedId) return;
|
|
259
|
-
dom.getNextEl(ctx, ctx.focusedId)?.focus({ preventScroll: true });
|
|
269
|
+
if (!ctx$1.focusedId) return;
|
|
270
|
+
dom.getNextEl(ctx$1, ctx$1.focusedId)?.focus({ preventScroll: true });
|
|
260
271
|
});
|
|
261
272
|
},
|
|
262
|
-
focusPrevToggle(ctx) {
|
|
273
|
+
focusPrevToggle(ctx$1) {
|
|
263
274
|
raf(() => {
|
|
264
|
-
if (!ctx.focusedId) return;
|
|
265
|
-
dom.getPrevEl(ctx, ctx.focusedId)?.focus({ preventScroll: true });
|
|
275
|
+
if (!ctx$1.focusedId) return;
|
|
276
|
+
dom.getPrevEl(ctx$1, ctx$1.focusedId)?.focus({ preventScroll: true });
|
|
266
277
|
});
|
|
267
278
|
},
|
|
268
|
-
focusFirstToggle(ctx) {
|
|
279
|
+
focusFirstToggle(ctx$1) {
|
|
269
280
|
raf(() => {
|
|
270
|
-
dom.getFirstEl(ctx)?.focus({ preventScroll: true });
|
|
281
|
+
dom.getFirstEl(ctx$1)?.focus({ preventScroll: true });
|
|
271
282
|
});
|
|
272
283
|
},
|
|
273
|
-
focusLastToggle(ctx) {
|
|
284
|
+
focusLastToggle(ctx$1) {
|
|
274
285
|
raf(() => {
|
|
275
|
-
dom.getLastEl(ctx)?.focus({ preventScroll: true });
|
|
286
|
+
dom.getLastEl(ctx$1)?.focus({ preventScroll: true });
|
|
276
287
|
});
|
|
277
288
|
}
|
|
278
289
|
}
|
|
@@ -282,6 +293,7 @@ function machine(userContext) {
|
|
|
282
293
|
//#endregion
|
|
283
294
|
//#region src/props.ts
|
|
284
295
|
const props = createProps()([
|
|
296
|
+
"defaultValue",
|
|
285
297
|
"dir",
|
|
286
298
|
"disabled",
|
|
287
299
|
"getRootNode",
|
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.7",
|
|
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/anatomy": "0.2.
|
|
26
|
-
"@destyler/types": "0.2.
|
|
27
|
-
"@destyler/utils": "0.2.
|
|
28
|
-
"@destyler/
|
|
29
|
-
"@destyler/
|
|
25
|
+
"@destyler/anatomy": "0.2.7",
|
|
26
|
+
"@destyler/types": "0.2.7",
|
|
27
|
+
"@destyler/utils": "0.2.7",
|
|
28
|
+
"@destyler/xstate": "0.2.7",
|
|
29
|
+
"@destyler/dom": "0.2.7"
|
|
30
30
|
},
|
|
31
31
|
"publishConfig": {
|
|
32
32
|
"access": "public"
|