mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-17 11:11:25 +00:00
* chore(deps-dev): remove @carbon/themes * chore(deps-dev): bump devDependencies * fix(tabs): forward click event to Tab * feat(toggle): dispatch toggle event * feat(tag): dispatch close event * feat(tooltip-icon): make tooltipText slottable * feat(dropdown): add hideLabel prop * docs(select): add "Hidden label" example * refactor(modal): use class directive * feat(modal): dispatch transitionend event * chore(deps-dev): upgrade carbon-components to 10.28.0-rc.0 * feat(date-picker): add warn state * feat(tag): support small size variant * fix(search): add semantic role * feat(toolbar-search): add disabled state * fix(composed-modal): add aria-label prop, update header semantic tags * chore(deps-dev): upgrade carbon-components to v10.28 * docs(overflow-menu): add light variant example * docs(link): document OutboundLink in Component API * chore(tooltip-icon): rename slot to "tooltipText" * docs(component-api): wrap code blocks to minimize width * docs(aspect-ratio): remove inline outline style * fix(tab): do not trigger focus when mounting * docs(tabs): add reactive example Closes #438
86 lines
1.8 KiB
Svelte
86 lines
1.8 KiB
Svelte
<script>
|
|
/**
|
|
* Specify the tab label
|
|
* Alternatively, use the default slot (e.g., <Tab><span>Label</span></Tab>)
|
|
*/
|
|
export let label = "";
|
|
|
|
/** Specify the href attribute */
|
|
export let href = "#";
|
|
|
|
/** Set to `true` to disable the tab */
|
|
export let disabled = false;
|
|
|
|
/** Specify the tabindex */
|
|
export let tabindex = "0";
|
|
|
|
/** Set an id for the top-level element */
|
|
export let id = "ccs-" + Math.random().toString(36);
|
|
|
|
/** Obtain a reference to the anchor HTML element */
|
|
export let ref = null;
|
|
|
|
import { onMount, afterUpdate, getContext, tick } from "svelte";
|
|
|
|
const { selectedTab, add, update, change } = getContext("Tabs");
|
|
|
|
add({ id, label, disabled });
|
|
|
|
let didMount = false;
|
|
|
|
$: selected = $selectedTab === id;
|
|
|
|
onMount(() => {
|
|
tick().then(() => {
|
|
didMount = true;
|
|
});
|
|
});
|
|
|
|
afterUpdate(() => {
|
|
if (didMount && selected && ref) {
|
|
ref.focus();
|
|
}
|
|
});
|
|
</script>
|
|
|
|
<li
|
|
tabindex="-1"
|
|
role="presentation"
|
|
class:bx--tabs__nav-item="{true}"
|
|
class:bx--tabs__nav-item--disabled="{disabled}"
|
|
class:bx--tabs__nav-item--selected="{selected}"
|
|
{...$$restProps}
|
|
on:click|preventDefault
|
|
on:click|preventDefault="{() => {
|
|
if (!disabled) {
|
|
update(id);
|
|
}
|
|
}}"
|
|
on:mouseover
|
|
on:mouseenter
|
|
on:mouseleave
|
|
on:keydown="{({ key }) => {
|
|
if (!disabled) {
|
|
if (key === 'ArrowRight') {
|
|
change(1);
|
|
} else if (key === 'ArrowLeft') {
|
|
change(-1);
|
|
} else if (key === ' ' || key === 'Enter') {
|
|
update(id);
|
|
}
|
|
}
|
|
}}"
|
|
>
|
|
<a
|
|
bind:this="{ref}"
|
|
role="tab"
|
|
tabindex="{disabled ? '-1' : tabindex}"
|
|
aria-selected="{selected}"
|
|
aria-disabled="{disabled}"
|
|
id="{id}"
|
|
href="{href}"
|
|
class:bx--tabs__nav-link="{true}"
|
|
>
|
|
<slot>{label}</slot>
|
|
</a>
|
|
</li>
|