feat(ui-shell): support HeaderNavMenu with current item

This commit is contained in:
metonym 2021-11-12 16:00:49 -08:00
commit bb2912b762
2 changed files with 28 additions and 1 deletions

View file

@ -16,6 +16,13 @@
/** Obtain a reference to the HTML anchor element */
export let ref = null;
import { getContext } from "svelte";
const id = "ccs-" + Math.random().toString(36);
const ctx = getContext("HeaderNavMenu");
$: if (ctx) ctx.updateSelectedItems({ id, isSelected });
</script>
<li>

View file

@ -14,7 +14,23 @@
/** Obtain a reference to the HTML anchor element */
export let ref = null;
import { setContext } from "svelte";
import { writable } from "svelte/store";
import ChevronDown16 from "../../icons/ChevronDown16.svelte";
const selectedItems = writable({});
setContext("HeaderNavMenu", {
updateSelectedItems(item) {
selectedItems.update((_items) => ({
..._items,
[item.id]: item.isSelected,
}));
},
});
$: isCurrentSubmenu =
Object.values($selectedItems).filter(Boolean).length > 0;
</script>
<svelte:window
@ -29,7 +45,10 @@
}}"
/>
<li class:bx--header__submenu="{true}">
<li
class:bx--header__submenu="{true}"
class:bx--header__submenu--current="{isCurrentSubmenu}"
>
<a
bind:this="{ref}"
role="menuitem"
@ -41,6 +60,7 @@
class:bx--header__menu-item="{true}"
class:bx--header__menu-title="{true}"
{...$$restProps}
style="{$$restProps.style}; z-index: 1"
on:keydown
on:keydown="{({ key }) => {
if (key === 'Enter') {