@destyler/toggle 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
@@ -24,9 +24,14 @@ interface PublicContext extends DirectionProperty, CommonProperties {
24
24
  */
25
25
  disabled?: boolean | undefined;
26
26
  /**
27
- * The values of the toggles in the group.
27
+ * The controlled values of the toggles in the group.
28
28
  */
29
- value: string[];
29
+ value?: string[] | undefined;
30
+ /**
31
+ * The initial values when the toggle group is not controlled.
32
+ * Prefer this over seeding with `value` for uncontrolled usage.
33
+ */
34
+ defaultValue?: string[] | undefined;
30
35
  /**
31
36
  * Function to call when the toggle is clicked.
32
37
  */
@@ -131,8 +136,8 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
131
136
  declare function machine(userContext: UserDefinedContext): _destyler_xstate0.Machine<MachineContext, MachineState, _destyler_xstate0.AnyEventObject>;
132
137
  //#endregion
133
138
  //#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">];
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">];
136
141
  declare const itemProps: (keyof ItemProps)[];
137
142
  declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, keyof ItemProps>];
138
143
  //#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(ctx.value) });
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
- ...compact(userContext),
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.6",
4
+ "version": "0.2.9",
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.6",
26
- "@destyler/dom": "0.2.6",
27
- "@destyler/types": "0.2.6",
28
- "@destyler/utils": "0.2.6",
29
- "@destyler/xstate": "0.2.6"
25
+ "@destyler/anatomy": "0.2.9",
26
+ "@destyler/dom": "0.2.9",
27
+ "@destyler/utils": "0.2.9",
28
+ "@destyler/types": "0.2.9",
29
+ "@destyler/xstate": "0.2.9"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"