-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
862d084
commit a04933d
Showing
4 changed files
with
84 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,35 @@ | ||
<script> | ||
import { setContext } from 'svelte'; | ||
import { writable, derived } from 'svelte/store'; | ||
// Create a store to track the active item | ||
const activeItem = writable(null); | ||
// Create dispatch for activeChanged event | ||
import { createEventDispatcher } from 'svelte'; | ||
const dispatch = createEventDispatcher(); | ||
// Watch for changes to activeItem and dispatch event | ||
$: { | ||
dispatch('activeChanged', { activeItem: $activeItem }); | ||
} | ||
// Provide context for child AccordionItems | ||
setContext('accordion', { | ||
registerItem: (id) => { | ||
const isActive = derived(activeItem, $activeItem => $activeItem === id); | ||
return { | ||
isActive, | ||
activate: () => { | ||
activeItem.set($activeItem === id ? null : id); | ||
} | ||
}; | ||
} | ||
}); | ||
</script> | ||
|
||
<div class="divide-y divide-gray-200 border-y border-gray-200 dark:divide-gray-700 dark:border-gray-700"> | ||
<slot /> | ||
</div> | ||
import { setContext } from 'svelte'; | ||
import { writable, derived } from 'svelte/store'; | ||
// Create a store to track the active item | ||
const activeItem = writable(null); | ||
// Create dispatch for activeChanged event | ||
import { createEventDispatcher } from 'svelte'; | ||
const dispatch = createEventDispatcher(); | ||
// Watch for changes to activeItem and dispatch event | ||
$: { | ||
dispatch('activeChanged', { activeItem: $activeItem }); | ||
} | ||
// Provide context for child AccordionItems | ||
setContext('accordion', { | ||
registerItem: (id) => { | ||
const isActive = derived(activeItem, ($activeItem) => $activeItem === id); | ||
return { | ||
isActive, | ||
activate: () => { | ||
activeItem.set($activeItem === id ? null : id); | ||
} | ||
}; | ||
} | ||
}); | ||
</script> | ||
|
||
<div | ||
class="divide-y divide-gray-200 border-y border-gray-200 dark:divide-gray-700 dark:border-gray-700" | ||
> | ||
<slot /> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,46 @@ | ||
<script> | ||
import { getContext } from 'svelte'; | ||
import { slide } from 'svelte/transition'; | ||
import { getContext } from 'svelte'; | ||
import { slide } from 'svelte/transition'; | ||
const id = crypto.randomUUID(); | ||
const { registerItem } = getContext('accordion'); | ||
const { isActive, activate } = registerItem(id); | ||
const id = crypto.randomUUID(); | ||
const { registerItem } = getContext('accordion'); | ||
const { isActive, activate } = registerItem(id); | ||
function toggle() { | ||
activate(); | ||
} | ||
function toggle() { | ||
activate(); | ||
} | ||
</script> | ||
|
||
<div class="relative"> | ||
<!-- Added sticky positioning to the button wrapper --> | ||
<div class="sticky top-0 z-10 bg-white dark:bg-gray-800"> | ||
<button | ||
type="button" | ||
class="flex w-full items-center justify-between py-3 text-left text-base font-medium text-gray-500 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white" | ||
class:text-gray-900={$isActive} | ||
class:dark:text-white={$isActive} | ||
on:click={toggle} | ||
aria-expanded={$isActive} | ||
> | ||
<slot name="header" /> | ||
<svg | ||
class="h-6 w-6 shrink-0 transition-transform" | ||
class:rotate-180={$isActive} | ||
fill="none" | ||
stroke="currentColor" | ||
viewBox="0 0 24 24" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" /> | ||
</svg> | ||
</button> | ||
</div> | ||
{#if $isActive} | ||
<div transition:slide|local> | ||
<div class="py-3"> | ||
<slot /> | ||
</div> | ||
</div> | ||
{/if} | ||
</div> | ||
<!-- Added sticky positioning to the button wrapper --> | ||
<div class="sticky top-0 z-10 bg-white dark:bg-gray-800"> | ||
<button | ||
type="button" | ||
class="flex w-full items-center justify-between py-3 text-left text-base font-medium text-gray-500 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white" | ||
class:text-gray-900={$isActive} | ||
class:dark:text-white={$isActive} | ||
on:click={toggle} | ||
aria-expanded={$isActive} | ||
> | ||
<slot name="header" /> | ||
<svg | ||
class="h-6 w-6 shrink-0 transition-transform" | ||
class:rotate-180={$isActive} | ||
fill="none" | ||
stroke="currentColor" | ||
viewBox="0 0 24 24" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7" /> | ||
</svg> | ||
</button> | ||
</div> | ||
|
||
{#if $isActive} | ||
<div transition:slide|local> | ||
<div class="py-3"> | ||
<slot /> | ||
</div> | ||
</div> | ||
{/if} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters