feat(ui-shell): HeaderAction supports tooltip

Closes #2110
This commit is contained in:
Eric Liu 2025-03-08 11:18:47 -08:00
commit 763fd49c8f

View file

@ -22,12 +22,26 @@
export let closeIcon = Close; export let closeIcon = Close;
/** /**
* Specify the text. * Specify the text displayed next to the icon.
* Alternatively, use the named slot "text" (e.g., `<div slot="text">...</div>`) * Alternatively, use the named slot "text" (e.g., `<div slot="text">...</div>`)
* @type {string} * @type {string}
*/ */
export let text = undefined; export let text = undefined;
/**
* Specify an icon tooltip. The tooltip will not be displayed
* if either the `text` prop or a named slot="text" is used
* @type {string}
*/
export let iconDescription = undefined;
/**
* Set the alignment of the tooltip relative to the icon.
* Only applies when `iconDescription` is provided
* @type {"start" | "center" | "end"}
*/
export let tooltipAlignment = "center";
/** Obtain a reference to the button HTML element */ /** Obtain a reference to the button HTML element */
export let ref = null; export let ref = null;
@ -49,6 +63,17 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
let refPanel = null; let refPanel = null;
$: hasIconOnly = iconDescription && !(text || $$slots.text);
$: buttonClass = [
hasIconOnly && "bx--btn bx--btn--primary",
hasIconOnly && "bx--tooltip__trigger bx--tooltip--a11y",
hasIconOnly && "bx--btn--icon-only bx--btn--icon-only--bottom",
hasIconOnly && `bx--tooltip--align-${tooltipAlignment}`,
$$restProps.class,
]
.filter(Boolean)
.join(" ");
</script> </script>
<svelte:window <svelte:window
@ -72,12 +97,16 @@
class:bx--header__action--active={isOpen} class:bx--header__action--active={isOpen}
class:bx--header__action--text={text} class:bx--header__action--text={text}
{...$$restProps} {...$$restProps}
class={buttonClass}
on:click on:click
on:click|stopPropagation={() => { on:click|stopPropagation={() => {
isOpen = !isOpen; isOpen = !isOpen;
dispatch(isOpen ? "open" : "close"); dispatch(isOpen ? "open" : "close");
}} }}
> >
{#if hasIconOnly}
<span class:bx--assistive-text={true}>{iconDescription}</span>
{/if}
{#if isOpen} {#if isOpen}
<slot name="closeIcon"> <slot name="closeIcon">
<svelte:component this={closeIcon} size={20} /> <svelte:component this={closeIcon} size={20} />