mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
* refactor(search): resolve svelte file paths * docs(Search): add reactive example * refactor(tag): use class name directive for tag types, resolve svelte icon path * chore(button-skeleton): deprecate small prop * fix(breadcrumb-item): type default slot * refactor(breadcrumb-skeleton): omit unused index * refactor(aspect-ratio): use class name directive * refactor(accordion): use the class name directive, resolve svelte icon paths * refactor(code-snippet): use class name directive, resolve svelte icon paths * fix(code-snippet-skeleton): CodeSnippetSkeleton can only be single or multi * refactor(content-switcher): use class name directive * docs(content-switcher): add reactive example * docs(content-switcher): remove unused import * docs(toggle): add reactive example * refactor(tooltip-definition): use class name directive * refactor(tooltip-icon): use class name directive * refactor(tooltip): resolve svelte icon import * fix(select): type dispatched change event * refactor(select): resolve svelte icon import * feat(select-item): spread rest props, avoid $ variable name * fix(pagination-nav): type dispatched events * refactor(pagination): resolve svelte imports * fix(pagination): type dispatched update event * fix(overflow-menu): type dispatched close event * fix(number-input): type dispatched change event * refactor(modal): use class name directive, resolve svelte imports * refactor(inline-loading): use class name directive, resolve svelte imports * refactor(composed-modal): resolve svelte icon imports * refactor(combo-box): resolve svelte imports * fix(fluid-form): rest props should not override Form class * refactor(progress-step): resolve svelte icon imports
70 lines
1.5 KiB
Svelte
70 lines
1.5 KiB
Svelte
<script>
|
|
/**
|
|
* Specify the switch text
|
|
* Alternatively, use the "text" slot (e.g., <span slot="text">...</span>)
|
|
*/
|
|
export let text = "Provide text";
|
|
|
|
/** Set to `true` for the switch to be selected */
|
|
export let selected = false;
|
|
|
|
/** Set to `true` to disable the switch */
|
|
export let disabled = false;
|
|
|
|
/** Set an id for the button element */
|
|
export let id = "ccs-" + Math.random().toString(36);
|
|
|
|
/** Obtain a reference to the button HTML element */
|
|
export let ref = null;
|
|
|
|
import { afterUpdate, getContext, onDestroy } from "svelte";
|
|
|
|
const ctx = getContext("ContentSwitcher");
|
|
|
|
ctx.add({ id, text, selected });
|
|
|
|
const unsubscribe = ctx.currentId.subscribe(($) => {
|
|
selected = $ === id;
|
|
});
|
|
|
|
afterUpdate(() => {
|
|
if (selected) {
|
|
ref.focus();
|
|
}
|
|
});
|
|
|
|
onDestroy(() => {
|
|
unsubscribe();
|
|
});
|
|
</script>
|
|
|
|
<button
|
|
bind:this="{ref}"
|
|
role="tab"
|
|
tabindex="{selected ? '0' : '-1'}"
|
|
aria-selected="{selected}"
|
|
disabled="{disabled}"
|
|
id="{id}"
|
|
class:bx--content-switcher-btn="{true}"
|
|
class:bx--content-switcher--selected="{selected}"
|
|
{...$$restProps}
|
|
on:click
|
|
on:click|preventDefault="{() => {
|
|
ctx.update(id);
|
|
}}"
|
|
on:mouseover
|
|
on:mouseenter
|
|
on:mouseleave
|
|
on:keydown
|
|
on:keydown="{({ key }) => {
|
|
if (key === 'ArrowRight') {
|
|
ctx.change(1);
|
|
} else if (key === 'ArrowLeft') {
|
|
ctx.change(-1);
|
|
}
|
|
}}"
|
|
>
|
|
<span class:bx--content-switcher__label="{true}">
|
|
<slot>{text}</slot>
|
|
</span>
|
|
</button>
|