mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
chore: lift components folder
This commit is contained in:
parent
76df51674d
commit
2200b29b92
301 changed files with 57 additions and 76 deletions
58
src/ContentSwitcher/ContentSwitcher.svelte
Normal file
58
src/ContentSwitcher/ContentSwitcher.svelte
Normal file
|
@ -0,0 +1,58 @@
|
|||
<script>
|
||||
export let selectedIndex = 0;
|
||||
|
||||
import { afterUpdate, createEventDispatcher, setContext } from "svelte";
|
||||
import { writable } from "svelte/store";
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
const currentId = writable(null);
|
||||
|
||||
$: currentIndex = -1;
|
||||
$: switches = [];
|
||||
$: if (switches[currentIndex]) {
|
||||
dispatch("change", currentIndex);
|
||||
currentId.set(switches[currentIndex].id);
|
||||
}
|
||||
|
||||
setContext("ContentSwitcher", {
|
||||
currentId,
|
||||
add: ({ id, text, selected }) => {
|
||||
if (selected) {
|
||||
selectedIndex = switches.length;
|
||||
}
|
||||
|
||||
switches = [...switches, { id, text, selected }];
|
||||
},
|
||||
update: id => {
|
||||
selectedIndex = switches.map(({ id }) => id).indexOf(id);
|
||||
},
|
||||
change: direction => {
|
||||
let index = currentIndex + direction;
|
||||
|
||||
if (index < 0) {
|
||||
index = switches.length - 1;
|
||||
} else if (index >= switches.length) {
|
||||
index = 0;
|
||||
}
|
||||
|
||||
selectedIndex = index;
|
||||
}
|
||||
});
|
||||
|
||||
afterUpdate(() => {
|
||||
if (selectedIndex !== currentIndex) {
|
||||
currentIndex = selectedIndex;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<div
|
||||
role="tablist"
|
||||
class:bx--content-switcher={true}
|
||||
{...$$restProps}
|
||||
on:click
|
||||
on:mouseover
|
||||
on:mouseenter
|
||||
on:mouseleave>
|
||||
<slot />
|
||||
</div>
|
Loading…
Add table
Add a link
Reference in a new issue