@window-splitter/state 0.3.1 → 0.3.2
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/.turbo/turbo-build.log +4 -5
- package/CHANGELOG.md +17 -0
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +24 -9
- package/dist/commonjs/index.js.map +1 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +24 -9
- package/dist/esm/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +33 -13
- package/src/machine.test.ts +30 -2
package/src/index.ts
CHANGED
|
@@ -498,7 +498,7 @@ export function parseUnit(unit: Unit | "1fr"): ParsedUnit {
|
|
|
498
498
|
}
|
|
499
499
|
|
|
500
500
|
if (unit.endsWith("%")) {
|
|
501
|
-
return makePercentUnit(parseFloat(unit));
|
|
501
|
+
return makePercentUnit(parseFloat(unit) / 100);
|
|
502
502
|
}
|
|
503
503
|
|
|
504
504
|
throw new Error(`Invalid unit: ${unit}`);
|
|
@@ -527,7 +527,7 @@ function getUnitPixelValue(
|
|
|
527
527
|
const parsed = unit === "1fr" ? parseUnit(unit) : unit;
|
|
528
528
|
return parsed.type === "pixel"
|
|
529
529
|
? parsed.value
|
|
530
|
-
:
|
|
530
|
+
: parsed.value.mul(getGroupSize(context));
|
|
531
531
|
}
|
|
532
532
|
|
|
533
533
|
/** Clamp a new `currentValue` given the panel's constraints. */
|
|
@@ -743,7 +743,7 @@ function formatUnit(unit: ParsedUnit): Unit {
|
|
|
743
743
|
return `${unit.value.toNumber()}px`;
|
|
744
744
|
}
|
|
745
745
|
|
|
746
|
-
return `${unit.value.toNumber()}%`;
|
|
746
|
+
return `${unit.value.mul(100).toNumber()}%`;
|
|
747
747
|
}
|
|
748
748
|
|
|
749
749
|
export function getPanelGroupPixelSizes(context: GroupMachineContextValue) {
|
|
@@ -812,11 +812,28 @@ export function buildTemplate(context: GroupMachineContextValue) {
|
|
|
812
812
|
} else if (item.collapsible && item.collapsed) {
|
|
813
813
|
return formatUnit(item.collapsedSize);
|
|
814
814
|
} else if (item.default) {
|
|
815
|
-
|
|
816
|
-
|
|
815
|
+
const siblingHasFill = context.items.some(
|
|
816
|
+
(i) =>
|
|
817
|
+
isPanelData(i) &&
|
|
818
|
+
i.id !== item.id &&
|
|
819
|
+
!i.collapsed &&
|
|
820
|
+
(i.max === "1fr" ||
|
|
821
|
+
(i.max.type === "percent" && i.max.value.eq(100)))
|
|
822
|
+
);
|
|
823
|
+
|
|
824
|
+
// If a sibling has a fill, this item doesn't need to expand
|
|
825
|
+
// So we can just use the default value
|
|
826
|
+
if (siblingHasFill) {
|
|
827
|
+
return formatUnit(item.default);
|
|
828
|
+
}
|
|
817
829
|
|
|
818
|
-
|
|
819
|
-
|
|
830
|
+
// Use 1fr so that panel fills ths space if needed
|
|
831
|
+
const max = item.max === "1fr" ? "1fr" : formatUnit(item.max);
|
|
832
|
+
return `minmax(${formatUnit(item.default)}, ${max})`;
|
|
833
|
+
} else {
|
|
834
|
+
const max = item.max === "1fr" ? "1fr" : formatUnit(item.max);
|
|
835
|
+
return `minmax(${min}, ${max})`;
|
|
836
|
+
}
|
|
820
837
|
}
|
|
821
838
|
|
|
822
839
|
return formatUnit(item.size);
|
|
@@ -1421,7 +1438,7 @@ export const groupMachine = createMachine(
|
|
|
1421
1438
|
states: {
|
|
1422
1439
|
idle: {
|
|
1423
1440
|
on: {
|
|
1424
|
-
setActualItemsSize: { actions: ["recordActualItemSize"] },
|
|
1441
|
+
setActualItemsSize: { actions: ["recordActualItemSize", "onResize"] },
|
|
1425
1442
|
dragHandleStart: { target: "dragging" },
|
|
1426
1443
|
setPanelPixelSize: {
|
|
1427
1444
|
actions: [
|
|
@@ -1741,12 +1758,15 @@ export const groupMachine = createMachine(
|
|
|
1741
1758
|
onResize: ({ context }) => {
|
|
1742
1759
|
for (const item of context.items) {
|
|
1743
1760
|
if (isPanelData(item)) {
|
|
1761
|
+
const pixel = clampUnit(
|
|
1762
|
+
context,
|
|
1763
|
+
item,
|
|
1764
|
+
getUnitPixelValue(context, item.currentValue)
|
|
1765
|
+
);
|
|
1766
|
+
|
|
1744
1767
|
item.onResize?.current?.({
|
|
1745
|
-
pixel:
|
|
1746
|
-
percentage:
|
|
1747
|
-
getGroupSize(context),
|
|
1748
|
-
item.currentValue
|
|
1749
|
-
),
|
|
1768
|
+
pixel: pixel.toNumber(),
|
|
1769
|
+
percentage: pixel.div(getGroupSize(context)).toNumber(),
|
|
1750
1770
|
});
|
|
1751
1771
|
}
|
|
1752
1772
|
}
|
package/src/machine.test.ts
CHANGED
|
@@ -372,7 +372,12 @@ describe("constraints", () => {
|
|
|
372
372
|
],
|
|
373
373
|
},
|
|
374
374
|
}).start();
|
|
375
|
+
|
|
375
376
|
initializeSizes(actor, { width: 500, height: 200 });
|
|
377
|
+
expect(spy).toHaveBeenCalledWith({
|
|
378
|
+
percentage: 0.5,
|
|
379
|
+
pixel: 250,
|
|
380
|
+
});
|
|
376
381
|
|
|
377
382
|
capturePixelValues(actor, () => {
|
|
378
383
|
dragHandle(actor, { id: "resizer-1", delta: 100 });
|
|
@@ -464,7 +469,7 @@ describe("constraints", () => {
|
|
|
464
469
|
}).start();
|
|
465
470
|
|
|
466
471
|
expect(getTemplate(actor)).toMatchInlineSnapshot(
|
|
467
|
-
`"minmax(0px, 90%) 10px 30
|
|
472
|
+
`"minmax(0px, 90%) 10px minmax(30%, 1fr)"`
|
|
468
473
|
);
|
|
469
474
|
initializeSizes(actor, { width: 500, height: 200 });
|
|
470
475
|
|
|
@@ -474,6 +479,29 @@ describe("constraints", () => {
|
|
|
474
479
|
});
|
|
475
480
|
});
|
|
476
481
|
|
|
482
|
+
test("supports 1 panel being collapsed with another panel expanding to fill", () => {
|
|
483
|
+
const actor = createActor(groupMachine, {
|
|
484
|
+
input: {
|
|
485
|
+
groupId: "group",
|
|
486
|
+
initialItems: [
|
|
487
|
+
initializePanel({ id: "panel-1", default: "200px", min: "200px" }),
|
|
488
|
+
initializePanelHandleData({ id: "resizer-1", size: "10px" }),
|
|
489
|
+
initializePanel({
|
|
490
|
+
id: "panel-2",
|
|
491
|
+
min: "200px",
|
|
492
|
+
collapsible: true,
|
|
493
|
+
collapsedSize: "60px",
|
|
494
|
+
defaultCollapsed: true,
|
|
495
|
+
}),
|
|
496
|
+
],
|
|
497
|
+
},
|
|
498
|
+
}).start();
|
|
499
|
+
|
|
500
|
+
expect(getTemplate(actor)).toMatchInlineSnapshot(
|
|
501
|
+
`"minmax(200px, 1fr) 10px 60px"`
|
|
502
|
+
);
|
|
503
|
+
});
|
|
504
|
+
|
|
477
505
|
test("panel can have a min", () => {
|
|
478
506
|
const actor = createActor(groupMachine, {
|
|
479
507
|
input: { groupId: "group" },
|
|
@@ -727,7 +755,7 @@ describe("collapsible panel", () => {
|
|
|
727
755
|
capturePixelValues(actor, () => {
|
|
728
756
|
expect(getTemplate(actor)).toBe("490px 10px 0px");
|
|
729
757
|
|
|
730
|
-
// Stays
|
|
758
|
+
// Stays collapsed in the buffer
|
|
731
759
|
dragHandle(actor, { id: "resizer-1", delta: -30 });
|
|
732
760
|
expect(getTemplate(actor)).toBe("490px 10px 0px");
|
|
733
761
|
|