Skip to content

Commit b193237

Browse files
jaysin586huntabyte
andauthored
enhancement: Animation expanding/collapsing - CSS (#69)
Co-authored-by: Hunter Johnston <johnstonhuntera@gmail.com>
1 parent f7290dc commit b193237

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

.changeset/pre.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"silent-rivers-sneeze",
1313
"silly-keys-own",
1414
"tall-guests-study",
15-
"tame-dancers-wait"
15+
"tame-dancers-wait",
16+
"rude-vans-eat"
1617
]
1718
}

.changeset/rude-vans-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"paneforge": patch
3+
---
4+
5+
enhancement: animation expanding/collapsing - CSS

packages/paneforge/src/lib/internal/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export type PaneConstraints = {
1818
minSize?: number | undefined;
1919
};
2020

21+
export type PaneTransitionState = "" | "collapsing" | "expanding";
22+
2123
export type PaneData = {
2224
callbacks: PaneCallbacks;
2325
constraints: PaneConstraints;

packages/paneforge/src/lib/paneforge.svelte.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
addEventListener,
55
executeCallbacks,
66
useRefById,
7+
afterTick,
78
} from "svelte-toolbelt";
89
import { onMount, untrack } from "svelte";
910
import { Context, watch } from "runed";
@@ -40,6 +41,7 @@ import type {
4041
PaneOnExpand,
4142
PaneOnResize,
4243
PaneResizeHandleOnDragging,
44+
PaneTransitionState,
4345
ResizeEvent,
4446
ResizeHandler,
4547
} from "$lib/internal/types.js";
@@ -717,6 +719,7 @@ type PaneStateProps = WithRefProps<
717719
>;
718720

719721
export class PaneState {
722+
#paneTransitionState: PaneTransitionState = $state("");
720723
callbacks = $derived.by(() => ({
721724
onCollapse: this.opts.onCollapse.current,
722725
onExpand: this.opts.onExpand.current,
@@ -731,11 +734,44 @@ export class PaneState {
731734
minSize: this.opts.minSize.current,
732735
}));
733736

737+
#handleTransition = (state: PaneTransitionState) => {
738+
this.#paneTransitionState = state;
739+
afterTick(() => {
740+
if (this.opts.ref.current) {
741+
const element = this.opts.ref.current;
742+
const computedStyle = getComputedStyle(element);
743+
744+
const hasTransition = computedStyle.transitionDuration !== "0s";
745+
746+
if (!hasTransition) {
747+
this.#paneTransitionState = "";
748+
return;
749+
}
750+
const handleTransitionEnd = (event: TransitionEvent) => {
751+
// Only handle width/flex transitions
752+
if (event.propertyName === "flex-grow") {
753+
this.#paneTransitionState = "";
754+
element.removeEventListener("transitionend", handleTransitionEnd);
755+
}
756+
};
757+
758+
// Always add the listener - if there's no transition, it won't fire
759+
element.addEventListener("transitionend", handleTransitionEnd);
760+
} else {
761+
this.#paneTransitionState = "";
762+
}
763+
});
764+
};
765+
734766
pane = {
735767
collapse: () => {
768+
this.#handleTransition("collapsing");
736769
this.group.collapsePane(this);
737770
},
738-
expand: () => this.group.expandPane(this),
771+
expand: () => {
772+
this.#handleTransition("expanding");
773+
this.group.expandPane(this);
774+
},
739775
getSize: () => this.group.getPaneSize(this),
740776
isCollapsed: () => this.group.isPaneCollapsed(this),
741777
isExpanded: () => this.group.isPaneExpanded(this),
@@ -763,6 +799,14 @@ export class PaneState {
763799

764800
#isCollapsed = $derived.by(() => this.group.isPaneCollapsed(this));
765801

802+
#paneState = $derived.by(() =>
803+
this.#paneTransitionState !== ""
804+
? this.#paneTransitionState
805+
: this.#isCollapsed
806+
? "collapsed"
807+
: "expanded"
808+
);
809+
766810
props = $derived.by(
767811
() =>
768812
({
@@ -773,6 +817,7 @@ export class PaneState {
773817
"data-pane-group-id": this.group.opts.id.current,
774818
"data-collapsed": this.#isCollapsed ? "" : undefined,
775819
"data-expanded": this.#isCollapsed ? undefined : "",
820+
"data-pane-state": this.#paneState,
776821
}) as const
777822
);
778823
}

0 commit comments

Comments
 (0)