Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next: cleanup #68

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions docs/src/content/examples/collapsible-panes.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,9 @@ Here's the high-level structure of the example above:
</script>

{#if collapsed}
<button
onclick={() => {
paneOne.expand();
}}
>
Expand Pane One
</button>
<button onclick={paneOne.expand}> Expand Pane One </button>
{:else}
<button
onclick={() => {
paneOne.collapse();
}}
>
Collapse Pane One
</button>
<button onclick={paneOne.collapse}> Collapse Pane One </button>
{/if}
<PaneGroup direction="horizontal">
<Pane
Expand Down
32 changes: 32 additions & 0 deletions docs/src/routes/(docs)/docs/sink/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script lang="ts">
import { PaneGroup, Pane, PaneResizer } from "paneforge";
import DotsSixVertical from "phosphor-svelte/lib/DotsSixVertical";
let innerWidth = $state(0);

let panelMin = $derived(Math.ceil((300 / innerWidth) * 100));
</script>

<svelte:window bind:innerWidth />
<div class="flex w-full flex-col">
<span>Panel Min: {panelMin}</span>

<PaneGroup direction="horizontal" class="w-full rounded-lg">
<Pane defaultSize={panelMin} minSize={panelMin} class="bg-muted rounded-lg">
<div class="flex h-[400px] items-center justify-center p-6">
<span class="font-semibold">Left</span>
</div>
</Pane>
<PaneResizer class="bg-background relative flex w-2 items-center justify-center">
<div
class="bg-brand z-10 flex h-7 min-w-5 items-center justify-center rounded-sm border"
>
<DotsSixVertical class="size-4 text-black" weight="bold" />
</div>
</PaneResizer>
<Pane class="bg-muted rounded-lg">
<div class="flex h-full items-center justify-center p-6">
<span class="font-semibold">Right</span>
</div>
</Pane>
</PaneGroup>
</div>
5 changes: 3 additions & 2 deletions packages/paneforge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"devDependencies": {
"@sveltejs/kit": "^2.17.2",
"@sveltejs/package": "^2.3.0",
"@sveltejs/package": "^2.3.10",
"@sveltejs/vite-plugin-svelte": "4.0.0",
"@testing-library/dom": "^10.2.0",
"@testing-library/jest-dom": "^6.4.6",
Expand All @@ -46,7 +46,7 @@
"jsdom": "^24.0.0",
"publint": "^0.1.9",
"resize-observer-polyfill": "^1.5.1",
"svelte": "^5.20.1",
"svelte": "^5.20.2",
"svelte-check": "^4.1.4",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
Expand All @@ -57,6 +57,7 @@
"types": "./dist/index.d.ts",
"type": "module",
"dependencies": {
"runed": "^0.23.4",
"svelte-toolbelt": "^0.7.1"
}
}
2 changes: 1 addition & 1 deletion packages/paneforge/src/lib/components/pane-group.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

export const getLayout = () => paneGroupState.layout;
export const setLayout = paneGroupState.setLayout;
export const getId = () => paneGroupState.id.current;
export const getId = () => paneGroupState.opts.id.current;

const mergedProps = $derived(mergeProps(restProps, paneGroupState.props));
</script>
Expand Down
61 changes: 31 additions & 30 deletions packages/paneforge/src/lib/internal/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Direction, DragState, PaneConstraints, PaneData, ResizeEvent } from "./types.js";
import type { PaneState } from "$lib/paneforge.svelte.js";
import type { Direction, DragState, PaneConstraints, ResizeEvent } from "./types.js";
import { calculateAriaValues } from "./utils/aria.js";
import { assert } from "./utils/assert.js";
import { areNumbersAlmostEqual } from "./utils/compare.js";
Expand All @@ -10,27 +11,26 @@ export function noop() {}
export function updateResizeHandleAriaValues({
groupId,
layout,
paneDataArray,
panesArray,
}: {
groupId: string;
layout: number[];
paneDataArray: PaneData[];
panesArray: PaneState[];
}) {
const resizeHandleElements = getResizeHandleElementsForGroup(groupId);

for (let index = 0; index < paneDataArray.length - 1; index++) {
for (let index = 0; index < panesArray.length - 1; index++) {
const { valueMax, valueMin, valueNow } = calculateAriaValues({
layout,
panesArray: paneDataArray,
panesArray: panesArray,
pivotIndices: [index, index + 1],
});

const resizeHandleEl = resizeHandleElements[index];

if (isHTMLElement(resizeHandleEl)) {
const paneData = paneDataArray[index];

resizeHandleEl.setAttribute("aria-controls", paneData.id);
const paneData = panesArray[index];
resizeHandleEl.setAttribute("aria-controls", paneData.opts.id.current);
resizeHandleEl.setAttribute("aria-valuemax", `${Math.round(valueMax)}`);
resizeHandleEl.setAttribute("aria-valuemin", `${Math.round(valueMin)}`);
resizeHandleEl.setAttribute(
Expand Down Expand Up @@ -73,13 +73,13 @@ export function getPivotIndices(
return index != null ? [index, index + 1] : [-1, -1];
}

export function paneDataHelper(paneDataArray: PaneData[], paneData: PaneData, layout: number[]) {
const paneConstraintsArray = paneDataArray.map((paneData) => paneData.constraints);
export function paneDataHelper(panesArray: PaneState[], pane: PaneState, layout: number[]) {
const paneConstraintsArray = panesArray.map((paneData) => paneData.constraints);

const paneIndex = findPaneDataIndex(paneDataArray, paneData);
const paneIndex = findPaneDataIndex(panesArray, pane);
const paneConstraints = paneConstraintsArray[paneIndex];

const isLastPane = paneIndex === paneDataArray.length - 1;
const isLastPane = paneIndex === panesArray.length - 1;
const pivotIndices = isLastPane ? [paneIndex - 1, paneIndex] : [paneIndex, paneIndex + 1];

const paneSize = layout[paneIndex];
Expand All @@ -91,30 +91,31 @@ export function paneDataHelper(paneDataArray: PaneData[], paneData: PaneData, la
};
}

export function findPaneDataIndex(paneDataArray: readonly PaneData[], paneData: PaneData) {
return paneDataArray.findIndex((prevPaneData) => prevPaneData.id === paneData.id);
export function findPaneDataIndex(panesArray: readonly PaneState[], pane: PaneState) {
return panesArray.findIndex(
(prevPaneData) => prevPaneData.opts.id.current === pane.opts.id.current
);
}

// Layout should be pre-converted into percentages
export function callPaneCallbacks(
paneArray: PaneData[],
panesArray: PaneState[],
layout: number[],
paneIdToLastNotifiedSizeMap: Record<string, number>
) {
for (let index = 0; index < layout.length; index++) {
const size = layout[index];
const paneData = paneArray[index];
const paneData = panesArray[index];
assert(paneData);

const { callbacks, constraints, id: paneId } = paneData;
const { collapsedSize = 0, collapsible } = constraints;
const { collapsedSize = 0, collapsible } = paneData.constraints;

const lastNotifiedSize = paneIdToLastNotifiedSizeMap[paneId];
const lastNotifiedSize = paneIdToLastNotifiedSizeMap[paneData.opts.id.current];
// invert the logic from below
if (!(lastNotifiedSize == null || size !== lastNotifiedSize)) continue;
paneIdToLastNotifiedSizeMap[paneId] = size;
paneIdToLastNotifiedSizeMap[paneData.opts.id.current] = size;

const { onCollapse, onExpand, onResize } = callbacks;
const { onCollapse, onExpand, onResize } = paneData.callbacks;

onResize?.(size, lastNotifiedSize);

Expand All @@ -138,16 +139,16 @@ export function callPaneCallbacks(
}
}

export function getUnsafeDefaultLayout({ paneDataArray }: { paneDataArray: PaneData[] }): number[] {
const layout = Array<number>(paneDataArray.length);
export function getUnsafeDefaultLayout({ panesArray }: { panesArray: PaneState[] }): number[] {
const layout = Array<number>(panesArray.length);

const paneConstraintsArray = paneDataArray.map((paneData) => paneData.constraints);
const paneConstraintsArray = panesArray.map((paneData) => paneData.constraints);

let numPanesWithSizes = 0;
let remainingSize = 100;

// Distribute default sizes first
for (let index = 0; index < paneDataArray.length; index++) {
for (let index = 0; index < panesArray.length; index++) {
const paneConstraints = paneConstraintsArray[index];
assert(paneConstraints);
const { defaultSize } = paneConstraints;
Expand All @@ -160,7 +161,7 @@ export function getUnsafeDefaultLayout({ paneDataArray }: { paneDataArray: PaneD
}

// Remaining size should be distributed evenly between panes without default sizes
for (let index = 0; index < paneDataArray.length; index++) {
for (let index = 0; index < panesArray.length; index++) {
const paneConstraints = paneConstraintsArray[index];
assert(paneConstraints);
const { defaultSize } = paneConstraints;
Expand All @@ -169,7 +170,7 @@ export function getUnsafeDefaultLayout({ paneDataArray }: { paneDataArray: PaneD
continue;
}

const numRemainingPanes = paneDataArray.length - numPanesWithSizes;
const numRemainingPanes = panesArray.length - numPanesWithSizes;
const size = remainingSize / numRemainingPanes;

numPanesWithSizes++;
Expand Down Expand Up @@ -373,14 +374,14 @@ export function getResizeEventCursorPosition(dir: Direction, e: ResizeEvent): nu
export function getResizeHandlePaneIds(
groupId: string,
handleId: string,
panesArray: PaneData[]
panesArray: PaneState[]
): [idBefore: string | null, idAfter: string | null] {
const handle = getResizeHandleElement(handleId);
const handles = getResizeHandleElementsForGroup(groupId);
const index = handle ? handles.indexOf(handle) : -1;

const idBefore: string | null = panesArray[index]?.id ?? null;
const idAfter: string | null = panesArray[index + 1]?.id ?? null;
const idBefore: string | null = panesArray[index]?.opts.id.current ?? null;
const idAfter: string | null = panesArray[index + 1]?.opts.id.current ?? null;

return [idBefore, idAfter];
}
6 changes: 3 additions & 3 deletions packages/paneforge/src/lib/internal/utils/aria.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PaneData } from "../types.js";
import type { PaneState } from "$lib/paneforge.svelte.js";

/**
* A utility function that calculates the `aria-valuemax`, `aria-valuemin`,
Expand All @@ -10,7 +10,7 @@ export function calculateAriaValues({
pivotIndices,
}: {
layout: number[];
panesArray: PaneData[];
panesArray: PaneState[];
pivotIndices: number[];
}) {
let currentMinSize = 0;
Expand All @@ -22,7 +22,7 @@ export function calculateAriaValues({

// A pane's effective min/max sizes also need to account for other pane's sizes.
for (let i = 0; i < panesArray.length; i++) {
const { constraints } = panesArray[i];
const constraints = panesArray[i].constraints;
const { maxSize = 100, minSize = 0 } = constraints;

if (i === firstIndex) {
Expand Down
70 changes: 0 additions & 70 deletions packages/paneforge/src/lib/internal/utils/createContext.ts

This file was deleted.

Loading
Loading