mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-17 03:01:25 +00:00
* chore(deps-dev): upgrade svelte to 3.58 * chore(a11y): ignore false positives Referencing the upstream Carbon React implementation, these warnings can be ignored. * fix(list-box-menu-item): set `tabindex` to `-1` * chore: fix `tabindex` capitalization The Svelte Language server does not detect `tabIndex` as a valid attribute. Note, however, that `tabIndex` is correct when using it in JavaScript (e.g., `node.tabIndex`).
74 lines
1.6 KiB
Svelte
74 lines
1.6 KiB
Svelte
<script>
|
|
/**
|
|
* @event {number} change
|
|
*/
|
|
|
|
/** Set the selected index of the switch item */
|
|
export let selectedIndex = 0;
|
|
|
|
/**
|
|
* Specify the size of the content switcher
|
|
* @type {"sm" | "xl"}
|
|
*/
|
|
export let size = undefined;
|
|
|
|
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>
|
|
|
|
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
|
|
<!-- svelte-ignore a11y-interactive-supports-focus -->
|
|
<div
|
|
role="tablist"
|
|
class:bx--content-switcher="{true}"
|
|
class:bx--content-switcher--sm="{size === 'sm'}"
|
|
class:bx--content-switcher--xl="{size === 'xl'}"
|
|
{...$$restProps}
|
|
on:click
|
|
on:mouseover
|
|
on:mouseenter
|
|
on:mouseleave
|
|
>
|
|
<slot />
|
|
</div>
|