mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 10:21:05 +00:00
chore(ui-shell): apply toggle behavior to HeaderAction
Dispatch open/close events
This commit is contained in:
parent
ea12dbe503
commit
334ea7f063
2 changed files with 35 additions and 16 deletions
|
@ -18,9 +18,18 @@
|
||||||
*/
|
*/
|
||||||
export let text = undefined;
|
export let text = undefined;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtain a reference to the button HTML element
|
||||||
|
* @type {null | HTMLButtonElement} [ref=null]
|
||||||
|
*/
|
||||||
|
export let ref = null;
|
||||||
|
|
||||||
import { Icon } from "../../Icon";
|
import { Icon } from "../../Icon";
|
||||||
|
import { createEventDispatcher } from "svelte";
|
||||||
import { slide } from "svelte/transition";
|
import { slide } from "svelte/transition";
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
let refPanel = null;
|
let refPanel = null;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -40,17 +49,17 @@
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<svelte:window
|
<svelte:body
|
||||||
on:mouseup={({ target }) => {
|
on:click={({ target }) => {
|
||||||
if (target && refPanel) {
|
if (isOpen && !ref.contains(target) && !refPanel.contains(target)) {
|
||||||
if (!refPanel.contains(target)) {
|
isOpen = false;
|
||||||
isOpen = false;
|
dispatch('close');
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}} />
|
}} />
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<button
|
<button
|
||||||
|
bind:this={ref}
|
||||||
type="button"
|
type="button"
|
||||||
class:bx--header__action={true}
|
class:bx--header__action={true}
|
||||||
class:bx--header__action--active={isOpen}
|
class:bx--header__action--active={isOpen}
|
||||||
|
@ -58,7 +67,8 @@
|
||||||
{...$$restProps}
|
{...$$restProps}
|
||||||
on:click
|
on:click
|
||||||
on:click={() => {
|
on:click={() => {
|
||||||
isOpen = true;
|
isOpen = !isOpen;
|
||||||
|
dispatch(isOpen ? 'open' : 'close');
|
||||||
}}>
|
}}>
|
||||||
<Icon {...icon} />
|
<Icon {...icon} />
|
||||||
<slot name="text">
|
<slot name="text">
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
title: "Catalog",
|
title: "Catalog",
|
||||||
tabindex: "0",
|
tabindex: "0",
|
||||||
focusable: false,
|
focusable: false,
|
||||||
style: undefined
|
style: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
let iHelp = {
|
let iHelp = {
|
||||||
|
@ -47,7 +47,7 @@
|
||||||
title: "Help",
|
title: "Help",
|
||||||
tabindex: "0",
|
tabindex: "0",
|
||||||
focusable: false,
|
focusable: false,
|
||||||
style: undefined
|
style: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
let iAdjust = {
|
let iAdjust = {
|
||||||
|
@ -57,7 +57,7 @@
|
||||||
title: "Catalog",
|
title: "Catalog",
|
||||||
tabindex: "0",
|
tabindex: "0",
|
||||||
focusable: false,
|
focusable: false,
|
||||||
style: undefined
|
style: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
let iProtection = {
|
let iProtection = {
|
||||||
|
@ -67,7 +67,7 @@
|
||||||
title: "Catalog",
|
title: "Catalog",
|
||||||
tabindex: "0",
|
tabindex: "0",
|
||||||
focusable: false,
|
focusable: false,
|
||||||
style: undefined
|
style: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
let iSwitcher = {
|
let iSwitcher = {
|
||||||
|
@ -77,7 +77,7 @@
|
||||||
title: "App Switcher",
|
title: "App Switcher",
|
||||||
tabindex: "0",
|
tabindex: "0",
|
||||||
focusable: false,
|
focusable: false,
|
||||||
style: undefined
|
style: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
let iAccount = {
|
let iAccount = {
|
||||||
|
@ -87,7 +87,7 @@
|
||||||
title: "Account",
|
title: "Account",
|
||||||
tabindex: "0",
|
tabindex: "0",
|
||||||
focusable: false,
|
focusable: false,
|
||||||
style: undefined
|
style: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
let iNotifications = {
|
let iNotifications = {
|
||||||
|
@ -97,7 +97,7 @@
|
||||||
title: "Notifications",
|
title: "Notifications",
|
||||||
tabindex: "0",
|
tabindex: "0",
|
||||||
focusable: false,
|
focusable: false,
|
||||||
style: undefined
|
style: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
let iSettings = {
|
let iSettings = {
|
||||||
|
@ -107,7 +107,7 @@
|
||||||
title: "Settings",
|
title: "Settings",
|
||||||
tabindex: "0",
|
tabindex: "0",
|
||||||
focusable: false,
|
focusable: false,
|
||||||
style: undefined
|
style: undefined,
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -167,7 +167,16 @@
|
||||||
{:else if story === 'header-with-switcher'}
|
{:else if story === 'header-with-switcher'}
|
||||||
<Header {...$$props}>
|
<Header {...$$props}>
|
||||||
<HeaderUtilities>
|
<HeaderUtilities>
|
||||||
<HeaderAction type="Switcher" icon={iSwitcher} isOpen={true}>
|
<HeaderAction
|
||||||
|
type="Switcher"
|
||||||
|
icon={iSwitcher}
|
||||||
|
isOpen
|
||||||
|
on:open={() => {
|
||||||
|
console.log('on:open');
|
||||||
|
}}
|
||||||
|
on:close={() => {
|
||||||
|
console.log('on:close');
|
||||||
|
}}>
|
||||||
<HeaderPanelLinks>
|
<HeaderPanelLinks>
|
||||||
<HeaderPanelDivider>Switcher subject 1</HeaderPanelDivider>
|
<HeaderPanelDivider>Switcher subject 1</HeaderPanelDivider>
|
||||||
<HeaderPanelLink>Switcher item 1</HeaderPanelLink>
|
<HeaderPanelLink>Switcher item 1</HeaderPanelLink>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue