mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 18:31: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 */
|
/** Obtain a reference to the anchor HTML element */
|
||||||
export let ref = null;
|
export let ref = null;
|
||||||
|
|
||||||
import { onMount, afterUpdate, getContext, tick } from "svelte";
|
import { getContext } from "svelte";
|
||||||
|
|
||||||
const { selectedTab, useAutoWidth, add, update, change } = getContext("Tabs");
|
const { selectedTab, useAutoWidth, add, update, change } = getContext("Tabs");
|
||||||
|
|
||||||
add({ id, label, disabled });
|
add({ id, label, disabled });
|
||||||
|
|
||||||
let didMount = false;
|
|
||||||
|
|
||||||
$: selected = $selectedTab === id;
|
$: selected = $selectedTab === id;
|
||||||
|
|
||||||
onMount(() => {
|
|
||||||
tick().then(() => {
|
|
||||||
didMount = true;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
afterUpdate(() => {
|
|
||||||
if (didMount && selected && ref) {
|
|
||||||
ref.focus();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
|
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
/** Specify the tab trigger href attribute */
|
/** Specify the tab trigger href attribute */
|
||||||
export let triggerHref = "#";
|
export let triggerHref = "#";
|
||||||
|
|
||||||
import { createEventDispatcher, afterUpdate, setContext } from "svelte";
|
import { createEventDispatcher, afterUpdate, setContext, tick } from "svelte";
|
||||||
import { writable, derived } from "svelte/store";
|
import { writable, derived } from "svelte/store";
|
||||||
import ChevronDown from "../icons/ChevronDown.svelte";
|
import ChevronDown from "../icons/ChevronDown.svelte";
|
||||||
|
|
||||||
|
@ -38,6 +38,8 @@
|
||||||
);
|
);
|
||||||
const selectedContent = writable(undefined);
|
const selectedContent = writable(undefined);
|
||||||
|
|
||||||
|
let refTabList = null;
|
||||||
|
|
||||||
setContext("Tabs", {
|
setContext("Tabs", {
|
||||||
tabs,
|
tabs,
|
||||||
contentById,
|
contentById,
|
||||||
|
@ -53,7 +55,7 @@
|
||||||
update: (id) => {
|
update: (id) => {
|
||||||
currentIndex = $tabsById[id].index;
|
currentIndex = $tabsById[id].index;
|
||||||
},
|
},
|
||||||
change: (direction) => {
|
change: async (direction) => {
|
||||||
let index = currentIndex + direction;
|
let index = currentIndex + direction;
|
||||||
|
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
|
@ -77,6 +79,11 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
currentIndex = index;
|
currentIndex = index;
|
||||||
|
|
||||||
|
await tick();
|
||||||
|
const activeTab =
|
||||||
|
refTabList?.querySelectorAll("[role='tab']")[currentIndex];
|
||||||
|
activeTab?.focus();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -145,6 +152,7 @@
|
||||||
<ChevronDown aria-hidden="true" title="{iconDescription}" />
|
<ChevronDown aria-hidden="true" title="{iconDescription}" />
|
||||||
</div>
|
</div>
|
||||||
<ul
|
<ul
|
||||||
|
bind:this="{refTabList}"
|
||||||
role="tablist"
|
role="tablist"
|
||||||
class:bx--tabs__nav="{true}"
|
class:bx--tabs__nav="{true}"
|
||||||
class:bx--tabs__nav--hidden="{dropdownHidden}"
|
class:bx--tabs__nav--hidden="{dropdownHidden}"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue