@destyler/splitter 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
@@ -30,9 +30,14 @@ interface PublicContext extends DirectionProperty, CommonProperties {
30
30
  */
31
31
  orientation: 'horizontal' | 'vertical';
32
32
  /**
33
- * The size data of the panels
33
+ * The controlled size data of the panels
34
34
  */
35
- size: PanelSizeData[];
35
+ size?: PanelSizeData[] | undefined;
36
+ /**
37
+ * The initial size data when it is not controlled.
38
+ * Prefer this over seeding with `size` for uncontrolled usage.
39
+ */
40
+ defaultSize?: PanelSizeData[] | undefined;
36
41
  /**
37
42
  * Function called when the splitter is resized.
38
43
  */
@@ -153,8 +158,8 @@ declare function connect<T extends PropTypes>(state: State, send: Send, normaliz
153
158
  declare function machine(userContext: UserDefinedContext): _destyler_xstate0.Machine<MachineContext, MachineState, _destyler_xstate0.AnyEventObject>;
154
159
  //#endregion
155
160
  //#region src/props.d.ts
156
- declare const props: ("id" | "orientation" | "size" | "onSizeChange" | "onSizeChangeEnd" | "ids" | "dir" | "getRootNode")[];
157
- declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "id" | "orientation" | "size" | "onSizeChange" | "onSizeChangeEnd" | "ids" | "dir" | "getRootNode">];
161
+ declare const props: ("orientation" | "size" | "defaultSize" | "onSizeChange" | "onSizeChangeEnd" | "ids" | "dir" | "id" | "getRootNode")[];
162
+ declare const splitProps: <Props extends Partial<UserDefinedContext>>(props: Props) => [Partial<UserDefinedContext>, Omit<Props, "orientation" | "size" | "defaultSize" | "onSizeChange" | "onSizeChangeEnd" | "ids" | "dir" | "id" | "getRootNode">];
158
163
  declare const panelProps: (keyof PanelProps)[];
159
164
  declare const splitPanelProps: <Props extends PanelProps>(props: Props) => [PanelProps, Omit<Props, keyof PanelProps>];
160
165
  declare const resizeTriggerProps: (keyof ResizeTriggerProps)[];
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createAnatomy } from "@destyler/anatomy";
2
2
  import { createScope, dataAttr, getEventKey, getEventStep, queryAll, raf, trackPointerMove } from "@destyler/dom";
3
- import { compact, createSplitProps } from "@destyler/utils";
3
+ import { compact, createSplitProps, isControlled, isEqual, isPropUserProvided, resolveControllableProp, withControllableProvided } from "@destyler/utils";
4
4
  import { createMachine } from "@destyler/xstate";
5
5
  import { createProps } from "@destyler/types";
6
6
 
@@ -381,7 +381,41 @@ function connect(state, send, normalize) {
381
381
 
382
382
  //#endregion
383
383
  //#region src/machine.ts
384
+ function cloneSize(size) {
385
+ return size.map((panel) => ({ ...panel }));
386
+ }
387
+ const invoke = {
388
+ sizeChange(ctx, size) {
389
+ ctx.onSizeChange?.({
390
+ size: cloneSize(size),
391
+ activeHandleId: ctx.activeResizeId
392
+ });
393
+ },
394
+ sizeChangeEnd(ctx) {
395
+ ctx.onSizeChangeEnd?.({
396
+ size: cloneSize(ctx.size ?? []),
397
+ activeHandleId: ctx.activeResizeId
398
+ });
399
+ }
400
+ };
401
+ const set = { size(ctx, value) {
402
+ const next = cloneSize(value);
403
+ if (isEqual(ctx.size, next)) return;
404
+ if (isControlled(ctx, "size")) {
405
+ invoke.sizeChange(ctx, next);
406
+ return;
407
+ }
408
+ ctx.size = next;
409
+ invoke.sizeChange(ctx, ctx.size);
410
+ } };
384
411
  function machine(userContext) {
412
+ const ctx = compact(withControllableProvided(userContext, ["size"]));
413
+ const { initial: initialSize } = resolveControllableProp({
414
+ value: ctx.size,
415
+ defaultValue: ctx.defaultSize,
416
+ valueProvided: isPropUserProvided(ctx, "size"),
417
+ fallback: []
418
+ });
385
419
  return createMachine({
386
420
  id: "splitter",
387
421
  initial: "idle",
@@ -397,13 +431,14 @@ function machine(userContext) {
397
431
  },
398
432
  initialDragPoint: null,
399
433
  initialDragSizes: null,
400
- ...compact(userContext)
434
+ ...ctx,
435
+ size: cloneSize(initialSize)
401
436
  },
402
437
  created: ["setPreviousPanels", "setInitialSize"],
403
438
  watch: { size: ["setActiveResizeState"] },
404
439
  computed: {
405
- isHorizontal: (ctx) => ctx.orientation === "horizontal",
406
- panels: (ctx) => getNormalizedPanels(ctx)
440
+ isHorizontal: (ctx$1) => ctx$1.orientation === "horizontal",
441
+ panels: (ctx$1) => getNormalizedPanels(ctx$1)
407
442
  },
408
443
  on: { SET_PANEL_SIZE: { actions: "setPanelSize" } },
409
444
  states: {
@@ -475,11 +510,7 @@ function machine(userContext) {
475
510
  entry: ["focusResizeHandle", "storeInitialDragState"],
476
511
  activities: ["trackPointerMove"],
477
512
  on: {
478
- POINTER_MOVE: { actions: [
479
- "setPointerValue",
480
- "setGlobalCursor",
481
- "invokeOnResize"
482
- ] },
513
+ POINTER_MOVE: { actions: ["setPointerValue", "setGlobalCursor"] },
483
514
  POINTER_UP: {
484
515
  target: "focused",
485
516
  actions: [
@@ -493,8 +524,8 @@ function machine(userContext) {
493
524
  }
494
525
  }
495
526
  }, {
496
- activities: { trackPointerMove: (ctx, _evt, { send }) => {
497
- return trackPointerMove(dom.getDoc(ctx), {
527
+ activities: { trackPointerMove: (ctx$1, _evt, { send }) => {
528
+ return trackPointerMove(dom.getDoc(ctx$1), {
498
529
  onPointerMove(info) {
499
530
  send({
500
531
  type: "POINTER_MOVE",
@@ -507,145 +538,187 @@ function machine(userContext) {
507
538
  });
508
539
  } },
509
540
  guards: {
510
- isStartPanelAtMin: (ctx) => ctx.activeResizeState.isAtMin,
511
- isStartPanelAtMax: (ctx) => ctx.activeResizeState.isAtMax,
512
- isHorizontal: (ctx) => ctx.isHorizontal,
513
- isVertical: (ctx) => !ctx.isHorizontal
541
+ isStartPanelAtMin: (ctx$1) => ctx$1.activeResizeState.isAtMin,
542
+ isStartPanelAtMax: (ctx$1) => ctx$1.activeResizeState.isAtMax,
543
+ isHorizontal: (ctx$1) => ctx$1.isHorizontal,
544
+ isVertical: (ctx$1) => !ctx$1.isHorizontal
514
545
  },
515
546
  delays: { HOVER_DELAY: 250 },
516
547
  actions: {
517
- setGlobalCursor(ctx) {
518
- dom.setupGlobalCursor(ctx);
548
+ setGlobalCursor(ctx$1) {
549
+ dom.setupGlobalCursor(ctx$1);
519
550
  },
520
- clearGlobalCursor(ctx) {
521
- dom.removeGlobalCursor(ctx);
551
+ clearGlobalCursor(ctx$1) {
552
+ dom.removeGlobalCursor(ctx$1);
522
553
  },
523
- invokeOnResize(ctx) {
524
- ctx.onSizeChange?.({
525
- size: Array.from(ctx.size),
526
- activeHandleId: ctx.activeResizeId
527
- });
554
+ invokeOnResizeEnd(ctx$1) {
555
+ invoke.sizeChangeEnd(ctx$1);
528
556
  },
529
- invokeOnResizeEnd(ctx) {
530
- ctx.onSizeChangeEnd?.({
531
- size: Array.from(ctx.size),
532
- activeHandleId: ctx.activeResizeId
533
- });
534
- },
535
- setActiveHandleId(ctx, evt) {
536
- ctx.activeResizeId = evt.id;
557
+ setActiveHandleId(ctx$1, evt) {
558
+ ctx$1.activeResizeId = evt.id;
537
559
  },
538
- clearActiveHandleId(ctx) {
539
- ctx.activeResizeId = null;
560
+ clearActiveHandleId(ctx$1) {
561
+ ctx$1.activeResizeId = null;
540
562
  },
541
- setInitialSize(ctx) {
542
- ctx.initialSize = ctx.panels.slice().map((panel) => ({
563
+ setInitialSize(ctx$1) {
564
+ ctx$1.initialSize = ctx$1.panels.slice().map((panel) => ({
543
565
  id: panel.id,
544
566
  size: panel.size
545
567
  }));
546
568
  },
547
- setPanelSize(ctx, evt) {
569
+ setPanelSize(ctx$1, evt) {
548
570
  const { id, size } = evt;
549
- ctx.size = ctx.size.map((panel) => {
550
- const panelSize = clamp(size, panel.minSize ?? 0, panel.maxSize ?? 100);
551
- return panel.id === id ? {
552
- ...panel,
553
- size: panelSize
554
- } : panel;
555
- });
571
+ const next = cloneSize(ctx$1.size ?? []);
572
+ const index = next.findIndex((panel$1) => panel$1.id === id);
573
+ if (index === -1) return;
574
+ const panel = next[index];
575
+ const panelSize = clamp(size, panel.minSize ?? 0, panel.maxSize ?? 100);
576
+ next[index] = {
577
+ ...panel,
578
+ size: panelSize
579
+ };
580
+ set.size(ctx$1, next);
556
581
  },
557
- setStartPanelToMin(ctx) {
558
- const bounds = getPanelBounds(ctx);
582
+ setStartPanelToMin(ctx$1) {
583
+ const bounds = getPanelBounds(ctx$1);
559
584
  if (!bounds) return;
560
585
  const { before, after } = bounds;
561
- ctx.size[before.index].size = before.min;
562
- ctx.size[after.index].size = after.min;
586
+ const next = cloneSize(ctx$1.size ?? []);
587
+ next[before.index] = {
588
+ ...next[before.index],
589
+ size: before.min
590
+ };
591
+ next[after.index] = {
592
+ ...next[after.index],
593
+ size: after.min
594
+ };
595
+ set.size(ctx$1, next);
563
596
  },
564
- setStartPanelToMax(ctx) {
565
- const bounds = getPanelBounds(ctx);
597
+ setStartPanelToMax(ctx$1) {
598
+ const bounds = getPanelBounds(ctx$1);
566
599
  if (!bounds) return;
567
600
  const { before, after } = bounds;
568
- ctx.size[before.index].size = before.max;
569
- ctx.size[after.index].size = after.max;
601
+ const next = cloneSize(ctx$1.size ?? []);
602
+ next[before.index] = {
603
+ ...next[before.index],
604
+ size: before.max
605
+ };
606
+ next[after.index] = {
607
+ ...next[after.index],
608
+ size: after.max
609
+ };
610
+ set.size(ctx$1, next);
570
611
  },
571
- expandStartPanel(ctx, evt) {
572
- const bounds = getPanelBounds(ctx);
612
+ expandStartPanel(ctx$1, evt) {
613
+ const bounds = getPanelBounds(ctx$1);
573
614
  if (!bounds) return;
574
615
  const { before, after } = bounds;
575
- ctx.size[before.index].size = before.up(evt.step);
576
- ctx.size[after.index].size = after.down(evt.step);
616
+ const next = cloneSize(ctx$1.size ?? []);
617
+ next[before.index] = {
618
+ ...next[before.index],
619
+ size: before.up(evt.step)
620
+ };
621
+ next[after.index] = {
622
+ ...next[after.index],
623
+ size: after.down(evt.step)
624
+ };
625
+ set.size(ctx$1, next);
577
626
  },
578
- shrinkStartPanel(ctx, evt) {
579
- const bounds = getPanelBounds(ctx);
627
+ shrinkStartPanel(ctx$1, evt) {
628
+ const bounds = getPanelBounds(ctx$1);
580
629
  if (!bounds) return;
581
630
  const { before, after } = bounds;
582
- ctx.size[before.index].size = before.down(evt.step);
583
- ctx.size[after.index].size = after.up(evt.step);
631
+ const next = cloneSize(ctx$1.size ?? []);
632
+ next[before.index] = {
633
+ ...next[before.index],
634
+ size: before.down(evt.step)
635
+ };
636
+ next[after.index] = {
637
+ ...next[after.index],
638
+ size: after.up(evt.step)
639
+ };
640
+ set.size(ctx$1, next);
584
641
  },
585
- resetStartPanel(ctx, evt) {
586
- const bounds = getPanelBounds(ctx, evt.id);
642
+ resetStartPanel(ctx$1, evt) {
643
+ const bounds = getPanelBounds(ctx$1, evt.id);
587
644
  if (!bounds) return;
588
645
  const { before, after } = bounds;
589
- ctx.size[before.index].size = ctx.initialSize[before.index].size;
590
- ctx.size[after.index].size = ctx.initialSize[after.index].size;
646
+ const next = cloneSize(ctx$1.size ?? []);
647
+ next[before.index] = {
648
+ ...next[before.index],
649
+ size: ctx$1.initialSize[before.index].size
650
+ };
651
+ next[after.index] = {
652
+ ...next[after.index],
653
+ size: ctx$1.initialSize[after.index].size
654
+ };
655
+ set.size(ctx$1, next);
591
656
  },
592
- focusResizeHandle(ctx) {
657
+ focusResizeHandle(ctx$1) {
593
658
  raf(() => {
594
- dom.getActiveHandleEl(ctx)?.focus({ preventScroll: true });
659
+ dom.getActiveHandleEl(ctx$1)?.focus({ preventScroll: true });
595
660
  });
596
661
  },
597
- blurResizeHandle(ctx) {
662
+ blurResizeHandle(ctx$1) {
598
663
  raf(() => {
599
- dom.getActiveHandleEl(ctx)?.blur();
664
+ dom.getActiveHandleEl(ctx$1)?.blur();
600
665
  });
601
666
  },
602
- storeInitialDragState(ctx, evt) {
603
- const panels = getHandlePanels(ctx);
667
+ storeInitialDragState(ctx$1, evt) {
668
+ const panels = getHandlePanels(ctx$1);
604
669
  if (!panels) return;
605
670
  const { before, after } = panels;
606
- ctx.initialDragPoint = evt.point;
607
- ctx.initialDragSizes = {
671
+ ctx$1.initialDragPoint = evt.point;
672
+ ctx$1.initialDragSizes = {
608
673
  before: before.size,
609
674
  after: after.size
610
675
  };
611
676
  },
612
- setPreviousPanels(ctx) {
613
- ctx.previousPanels = ctx.panels.slice();
677
+ setPreviousPanels(ctx$1) {
678
+ ctx$1.previousPanels = ctx$1.panels.slice();
614
679
  },
615
- setActiveResizeState(ctx) {
616
- const panels = getPanelBounds(ctx);
680
+ setActiveResizeState(ctx$1) {
681
+ const panels = getPanelBounds(ctx$1);
617
682
  if (!panels) return;
618
683
  const { before } = panels;
619
- ctx.activeResizeState = {
684
+ ctx$1.activeResizeState = {
620
685
  isAtMin: before.isAtMin,
621
686
  isAtMax: before.isAtMax
622
687
  };
623
688
  },
624
- setPointerValue(ctx, evt) {
625
- const panels = getHandlePanels(ctx);
626
- const bounds = getHandleBounds(ctx);
689
+ setPointerValue(ctx$1, evt) {
690
+ const panels = getHandlePanels(ctx$1);
691
+ const bounds = getHandleBounds(ctx$1);
627
692
  if (!panels || !bounds) return;
628
- const rootEl = dom.getRootEl(ctx);
693
+ const rootEl = dom.getRootEl(ctx$1);
629
694
  if (!rootEl) return;
630
- if (!ctx.initialDragPoint || !ctx.initialDragSizes) return;
695
+ if (!ctx$1.initialDragPoint || !ctx$1.initialDragSizes) return;
631
696
  const { before, after } = panels;
632
- const rootSize = ctx.isHorizontal ? rootEl.offsetWidth : rootEl.offsetHeight;
633
- const currentPos = ctx.isHorizontal ? evt.point.x : evt.point.y;
634
- const initialPos = ctx.isHorizontal ? ctx.initialDragPoint.x : ctx.initialDragPoint.y;
635
- const deltaPercent = (ctx.dir === "rtl" && ctx.isHorizontal ? initialPos - currentPos : currentPos - initialPos) / rootSize * 100;
636
- let newBeforeSize = ctx.initialDragSizes.before + deltaPercent;
637
- let newAfterSize = ctx.initialDragSizes.after - deltaPercent;
638
- ctx.activeResizeState = {
697
+ const rootSize = ctx$1.isHorizontal ? rootEl.offsetWidth : rootEl.offsetHeight;
698
+ const currentPos = ctx$1.isHorizontal ? evt.point.x : evt.point.y;
699
+ const initialPos = ctx$1.isHorizontal ? ctx$1.initialDragPoint.x : ctx$1.initialDragPoint.y;
700
+ const deltaPercent = (ctx$1.dir === "rtl" && ctx$1.isHorizontal ? initialPos - currentPos : currentPos - initialPos) / rootSize * 100;
701
+ let newBeforeSize = ctx$1.initialDragSizes.before + deltaPercent;
702
+ let newAfterSize = ctx$1.initialDragSizes.after - deltaPercent;
703
+ ctx$1.activeResizeState = {
639
704
  isAtMin: newBeforeSize <= before.minSize,
640
705
  isAtMax: newBeforeSize >= before.maxSize
641
706
  };
642
707
  newBeforeSize = clamp(newBeforeSize, before.minSize, before.maxSize);
643
708
  newAfterSize = clamp(newAfterSize, after.minSize, after.maxSize);
644
- const totalSize = ctx.initialDragSizes.before + ctx.initialDragSizes.after;
709
+ const totalSize = ctx$1.initialDragSizes.before + ctx$1.initialDragSizes.after;
645
710
  if (newBeforeSize + newAfterSize > totalSize) if (deltaPercent > 0) newAfterSize = totalSize - newBeforeSize;
646
711
  else newBeforeSize = totalSize - newAfterSize;
647
- ctx.size[before.index].size = newBeforeSize;
648
- ctx.size[after.index].size = newAfterSize;
712
+ const next = cloneSize(ctx$1.size ?? []);
713
+ next[before.index] = {
714
+ ...next[before.index],
715
+ size: newBeforeSize
716
+ };
717
+ next[after.index] = {
718
+ ...next[after.index],
719
+ size: newAfterSize
720
+ };
721
+ set.size(ctx$1, next);
649
722
  }
650
723
  }
651
724
  });
@@ -654,6 +727,7 @@ function machine(userContext) {
654
727
  //#endregion
655
728
  //#region src/props.ts
656
729
  const props = createProps()([
730
+ "defaultSize",
657
731
  "dir",
658
732
  "getRootNode",
659
733
  "id",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@destyler/splitter",
3
3
  "type": "module",
4
- "version": "0.2.6",
4
+ "version": "0.2.9",
5
5
  "description": "A component that divides your layout into resizable sections.",
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/types": "0.2.9",
28
+ "@destyler/utils": "0.2.9",
29
+ "@destyler/xstate": "0.2.9"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"