From 4d36fe3e54c03282140824ead3859659a6c79657 Mon Sep 17 00:00:00 2001 From: anatolzak <53095479+anatolzak@users.noreply.github.com> Date: Mon, 15 Apr 2024 19:08:25 +0300 Subject: [PATCH] fix accordion presence transition --- .../lib/bits/accordion/accordion.svelte.ts | 32 ++++++++++--------- .../components/accordion-content.svelte | 19 ++++------- .../components/demos/accordion-demo.svelte | 6 ++-- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/packages/bits-ui/src/lib/bits/accordion/accordion.svelte.ts b/packages/bits-ui/src/lib/bits/accordion/accordion.svelte.ts index 5fe896f6a..863f07ebb 100644 --- a/packages/bits-ui/src/lib/bits/accordion/accordion.svelte.ts +++ b/packages/bits-ui/src/lib/bits/accordion/accordion.svelte.ts @@ -287,11 +287,14 @@ type AccordionContentStateProps = BoxedValues<{ class AccordionContentState { item = undefined as unknown as AccordionItemState; - currentStyle = box>({}); - isMountAnimationPrevented = box(false); + currentStyle = box<{ transitionDuration: string; animationName: string } | undefined>( + undefined + ); + isMountAnimationPrevented = $state(false); width = box(0); height = box(0); presentEl: Box = box(undefined); + present = $derived(this.item.isSelected); #attrs: Record = $derived({ "data-state": getDataOpenClosed(this.item.isSelected), @@ -299,19 +302,23 @@ class AccordionContentState { "data-value": this.item.value, "data-accordion-content": "", style: styleToString({ - "--bits-accordion-content-height": `${this.height.value}px`, - "--bits-accordion-content-width": `${this.width.value}px`, + "--bits-accordion-content-height": this.height.value + ? `${this.height.value}px` + : undefined, + "--bits-accordion-content-width": this.width.value + ? `${this.width.value}px` + : undefined, }), }); constructor(props: AccordionContentStateProps, item: AccordionItemState) { this.item = item; - this.isMountAnimationPrevented.value = this.item.isSelected; + this.isMountAnimationPrevented = this.item.isSelected; this.presentEl = props.presentEl; onMount(() => { requestAnimationFrame(() => { - this.isMountAnimationPrevented.value = false; + this.isMountAnimationPrevented = false; }); }); @@ -336,15 +343,10 @@ class AccordionContentState { this.width.value = rect.width; // unblock any animations/transitions that were originally set if not the initial render - if (!this.isMountAnimationPrevented.value) { - const transitionDuration = this.currentStyle.value.transitionDuration; - const animationName = this.currentStyle.value.animationName; - if (transitionDuration) { - node.style.transitionDuration = transitionDuration; - } - if (animationName) { - node.style.animationName = animationName; - } + if (!this.isMountAnimationPrevented) { + const { animationName, transitionDuration } = this.currentStyle.value; + node.style.transitionDuration = transitionDuration; + node.style.animationName = animationName; } }); } diff --git a/packages/bits-ui/src/lib/bits/accordion/components/accordion-content.svelte b/packages/bits-ui/src/lib/bits/accordion/components/accordion-content.svelte index 5eb123b30..2ecbefeb2 100644 --- a/packages/bits-ui/src/lib/bits/accordion/components/accordion-content.svelte +++ b/packages/bits-ui/src/lib/bits/accordion/components/accordion-content.svelte @@ -3,7 +3,6 @@ import type { AccordionContentProps } from "../types.js"; import Presence from "$lib/bits/utilities/presence.svelte"; import { box } from "$lib/internal/box.svelte.js"; - import { styleToString } from "$lib/internal/style.js"; let { child, @@ -32,19 +31,15 @@ {#if asChild} {@render child?.({ props: { ...mergedProps, hidden: !present.value } })} {:else} - - -