mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-14 18:01:06 +00:00
fix(tabs): selected tab should not steal focus if updated programmatically (#1320)
Fixes #572
This commit is contained in:
parent
0801e1d078
commit
0f621c2545
2 changed files with 11 additions and 17 deletions
|
@ -20,27 +20,13 @@
|
|||
/** Obtain a reference to the anchor HTML element */
|
||||
export let ref = null;
|
||||
|
||||
import { onMount, afterUpdate, getContext, tick } from "svelte";
|
||||
import { getContext } from "svelte";
|
||||
|
||||
const { selectedTab, useAutoWidth, 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>
|
||||
|
||||
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
/** Specify the tab trigger href attribute */
|
||||
export let triggerHref = "#";
|
||||
|
||||
import { createEventDispatcher, afterUpdate, setContext } from "svelte";
|
||||
import { createEventDispatcher, afterUpdate, setContext, tick } from "svelte";
|
||||
import { writable, derived } from "svelte/store";
|
||||
import ChevronDown from "../icons/ChevronDown.svelte";
|
||||
|
||||
|
@ -38,6 +38,8 @@
|
|||
);
|
||||
const selectedContent = writable(undefined);
|
||||
|
||||
let refTabList = null;
|
||||
|
||||
setContext("Tabs", {
|
||||
tabs,
|
||||
contentById,
|
||||
|
@ -53,7 +55,7 @@
|
|||
update: (id) => {
|
||||
currentIndex = $tabsById[id].index;
|
||||
},
|
||||
change: (direction) => {
|
||||
change: async (direction) => {
|
||||
let index = currentIndex + direction;
|
||||
|
||||
if (index < 0) {
|
||||
|
@ -77,6 +79,11 @@
|
|||
}
|
||||
|
||||
currentIndex = index;
|
||||
|
||||
await tick();
|
||||
const activeTab =
|
||||
refTabList?.querySelectorAll("[role='tab']")[currentIndex];
|
||||
activeTab?.focus();
|
||||
},
|
||||
});
|
||||
|
||||
|
@ -145,6 +152,7 @@
|
|||
<ChevronDown aria-hidden="true" title="{iconDescription}" />
|
||||
</div>
|
||||
<ul
|
||||
bind:this="{refTabList}"
|
||||
role="tablist"
|
||||
class:bx--tabs__nav="{true}"
|
||||
class:bx--tabs__nav--hidden="{dropdownHidden}"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue