@sorens/artist-svelte
Version:
an opinionated and clean UI framework for SvelteKit with theme support built-in
31 lines (30 loc) • 1.08 kB
JavaScript
import { setContext } from 'svelte';
import { writable } from 'svelte/store';
export const accordionStates = writable({});
export const setupAccordionContext = (multiSelectable) => setContext('Accordion', {
accordionStates,
add: (newItemState) => {
accordionStates.update((allItemStates) => ({
...allItemStates,
[newItemState.id]: newItemState.expanded
}));
},
remove: (newItemState) => {
accordionStates.update((allItemStates) => {
const allStates = { ...allItemStates };
delete allStates[newItemState.id];
return allStates;
});
},
toggle: (newItemState) => {
accordionStates.update((allItemStates) => {
if (!multiSelectable) {
Object.keys(allItemStates).forEach((id) => (allItemStates[id] = false));
}
return { ...allItemStates, [newItemState.id]: newItemState.expanded };
});
}
});
export const accordionWrapperInit = (multiSelectable) => {
setupAccordionContext(multiSelectable);
};