-
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.
Add a SingleSelectAccordion component
Plumb in a way for it to pass data back to a containing component when one of its items is clicked on.
- Loading branch information
1 parent
6ed5fcd
commit 456adc6
Showing
3 changed files
with
46 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<script> | ||
import { setContext } from 'svelte'; | ||
import { writable, derived } from 'svelte/store'; | ||
// Create a store to track the active item and data. | ||
const activeItem = writable(null); | ||
const activeData = 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, | ||
activeData: $activeData | ||
}); | ||
} | ||
// Provide context for child AccordionItems | ||
setContext('accordion', { | ||
registerItem: (id) => { | ||
const isActive = derived(activeItem, ($activeItem) => $activeItem === id); | ||
return { | ||
isActive, | ||
activate: (data) => { | ||
const newId = $activeItem === id ? null : id; | ||
activeItem.set(newId); | ||
activeData.set(newId ? data : null); | ||
} | ||
}; | ||
} | ||
}); | ||
</script> | ||
|
||
<div | ||
class="divide-y divide-gray-200 border-y border-gray-200 dark:divide-gray-700 dark:border-gray-700" | ||
> | ||
<slot /> | ||
</div> |