Skip to content

Commit

Permalink
Add skipAnimation option to Accordion
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronbrethorst committed Nov 25, 2024
1 parent 9000ae3 commit ade67de
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
18 changes: 14 additions & 4 deletions src/components/containers/Accordion.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// Props to track all possible item IDs
export let items = [];
// Store for animation state
const skipAnimation = writable(false);
// Create a store to track multiple active items using a Set
const activeItems = writable(new Set());
Expand All @@ -13,14 +16,20 @@
const dispatch = createEventDispatcher();
// Methods to open/close all items
export const openAll = () => {
export const openAll = (animate = true) => {
skipAnimation.set(!animate);
activeItems.set(new Set(items));
dispatch('openAll');
dispatch('openAll', { animated: animate });
// Reset skip animation after a short delay
setTimeout(() => skipAnimation.set(false), 0);
};
export const closeAll = () => {
export const closeAll = (animate = true) => {
skipAnimation.set(!animate);
activeItems.set(new Set());
dispatch('closeAll');
dispatch('closeAll', { animated: animate });
// Reset skip animation after a short delay
setTimeout(() => skipAnimation.set(false), 0);
};
// Watch for changes to activeItems and dispatch event
Expand All @@ -39,6 +48,7 @@
const isActive = derived(activeItems, ($activeItems) => $activeItems.has(id));
return {
isActive,
skipAnimation,
activate: () => {
activeItems.update((items) => {
const newItems = new Set(items);
Expand Down
8 changes: 6 additions & 2 deletions src/components/containers/AccordionItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { slide } from 'svelte/transition';
const id = crypto.randomUUID();
const { registerItem } = getContext('accordion');
const { isActive, activate } = registerItem(id);
const { isActive, skipAnimation, activate } = registerItem(id);
function toggle() {
activate();
}
Expand Down Expand Up @@ -34,7 +34,11 @@
</div>

{#if $isActive}
<div transition:slide|local>
<div
transition:slide|local={{
duration: $skipAnimation ? 0 : 300
}}
>
<div class="py-3">
<slot />
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/stops/[stopID]/schedule/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
onMount(async () => {
const formattedDate = currentDate.toISOString().split('T')[0];
await fetchScheduleForStop(stopId, formattedDate);
accordionComponent.openAll();
accordionComponent.openAll(false);
});
</script>
Expand Down

0 comments on commit ade67de

Please sign in to comment.