An open source design system that's fully customizable and agent ready
TypeScript
fix(button): preserve edge compensation when a ghost button has a tooltip (#3576)
Cindy Zhang8598e513f6ee
diff --git a/.changeset/button-tooltip-edge-comp.md b/.changeset/button-tooltip-edge-comp.mdnew file mode 100644index 0000000000..084989c99a--- /dev/null+++ b/.changeset/button-tooltip-edge-comp.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] Button: keep edge compensation working when a ghost button also has a tooltip. The tooltip is now attached via the tooltip hook instead of a wrapper element, so the button stays a direct child of its container — no extra DOM node, no layout shift, and containers (Toolbar, Banner) still detect the edge-compensation marker via their direct-child `:has()` selector and pull the button flush to the optical edge. (#2578)+@cixzhangdiff --git a/packages/core/src/Button/Button.tsx b/packages/core/src/Button/Button.tsxindex 4ba90d9218..09ec4ad8b9 100644--- a/packages/core/src/Button/Button.tsx+++ b/packages/core/src/Button/Button.tsx@@ -21,6 +21,7 @@import {useRef, useTransition, type ReactNode} from 'react';import type {BaseProps} from '../BaseProps';import * as stylex from '@stylexjs/stylex';+import {useTooltip} from '../Tooltip/useTooltip';import {colorVars,sizeVars,@@ -32,14 +33,13 @@ import {fontWeightVars,typeScaleVars,} from '../theme/tokens.stylex';-import {Tooltip} from '../Tooltip/Tooltip';import {Spinner} from '../Spinner';import {VisuallyHidden} from '../VisuallyHidden';import {EDGE_COMP_ATTR} from '../Layout/edgeCompensation.stylex';import {useSize} from '../SizeContext/SizeContext';import {useButtonGroup} from '../ButtonGroup/ButtonGroupContext';-import {mergeProps} from '../utils';+import {mergeProps, mergeRefs} from '../utils';import {useLinkComponent} from '../Link/useLinkComponent';import type {LinkComponentType} from '../Link/types';import {themeProps} from '../utils/themeProps';@@ -580,6 +580,16 @@ export function Button({// for keyboard users to reach the tooltip. Otherwise use native disabled.const useAriaDisabled = tooltip != null && buttonDisabled;+ // Attach tooltip behavior via the hook rather than wrapping the button in a+ // <Tooltip> element. The hook adds hover/focus triggers to the button itself,+ // so no extra DOM node is inserted — the button stays a direct child of its+ // container (no layout shift, and edge-compensation markers remain+ // discoverable through the container's direct-child `:has()` selector).+ const tooltipHook = useTooltip({+ placement: 'above',+ isEnabled: tooltip != null,+ });+const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {// The ref guard dedupes fire-once actions. Interruptible callers skip it so// a re-click while pending starts a fresh action that interrupts the prior.@@ -696,18 +706,39 @@ export function Button({(children != null && children !== label);const ariaLabelProp = needsAriaLabel ? {'aria-label': label} : null;+ // When a tooltip is attached via the hook, point aria-describedby at the+ // tooltip content (composing with any consumer-provided value).+ const describedByProp =+ tooltip != null+ ? {+ 'aria-describedby':+ [props['aria-describedby'], tooltipHook.describedBy]+ .filter(Boolean)+ .join(' ') || undefined,+ }+ : null;++ // Merge the consumer ref with the tooltip hook's trigger ref so both point at+ // the same element. mergeRefs tolerates undefined, so this is a no-op for the+ // tooltip side when no tooltip is set.+ const mergedButtonRef = mergeRefs(+ ref,+ tooltip != null ? tooltipHook.ref : undefined,+ );+let element: ReactNode;if (renderAsLink) {element = (<LinkComponent- ref={ref as React.Ref<HTMLAnchorElement>}+ ref={mergedButtonRef as React.Ref<HTMLAnchorElement>}href={href}target={target}rel={rel}{...sharedMergedProps}{...props}{...ariaLabelProp}+ {...describedByProp}{...edgeCompAttr}onClick={handleClick}>{buttonContent}@@ -716,12 +747,13 @@ export function Button({} else {element = (<button- ref={ref}+ ref={mergedButtonRef}type={type}disabled={useAriaDisabled ? undefined : buttonDisabled}{...sharedMergedProps}{...props}{...ariaLabelProp}+ {...describedByProp}{...edgeCompAttr}aria-busy={isLoadingState || undefined}aria-disabled={useAriaDisabled || undefined}@@ -734,9 +766,10 @@ export function Button({if (tooltip) {return (- <Tooltip content={tooltip} placement="above">+ <>{element}- </Tooltip>+ {tooltipHook.renderTooltip(tooltip)}+ </>);}diff --git a/packages/core/src/Layout/__tests__/edgeCompensation.test.tsx b/packages/core/src/Layout/__tests__/edgeCompensation.test.tsxindex 036f83c796..a3d815c1f1 100644--- a/packages/core/src/Layout/__tests__/edgeCompensation.test.tsx+++ b/packages/core/src/Layout/__tests__/edgeCompensation.test.tsx@@ -116,4 +116,76 @@ describe('Edge Compensation', () => {expect(dismissButton).toHaveAttribute(EDGE_COMP_ATTR);});});++ // The container's compensation selector is a direct-child combinator:+ // :has(> [data-astryx-edge-comp]:last-child)+ // so an edge-comp marker only counts when it sits on a *direct* child of the+ // slot. Attaching a tooltip must not bury the marker one level deeper — the+ // Button uses the tooltip hook (no wrapper element) so the marker stays on the+ // button itself (#2578).+ describe('Tooltip attachment preserves marker discoverability', () => {+ // Resolve the toolbar slot (the flex wrapper that carries the negative+ // margin) — it is the direct child of the [role="toolbar"] row.+ const findSlot = (+ button: HTMLElement,+ toolbar: HTMLElement,+ ): HTMLElement => {+ let node = button;+ while (node.parentElement && node.parentElement !== toolbar) {+ node = node.parentElement;+ }+ return node;+ };++ it('keeps the marker on a direct child of the slot without a tooltip', () => {+ const {container} = render(+ <Toolbar+ label="Actions"+ endContent={+ <Button+ label="Close"+ variant="ghost"+ isIconOnly+ icon={<span>x</span>}+ />+ }+ />,+ );+ const button = screen.getByRole('button', {name: 'Close'});+ const toolbar = container.querySelector(+ '[role="toolbar"]',+ ) as HTMLElement;+ const slot = findSlot(button, toolbar);+ const directMarked = slot.querySelector(`:scope > [${EDGE_COMP_ATTR}]`);+ expect(directMarked).not.toBeNull();+ });++ it('keeps the marker on a direct child of the slot when tooltip is set', () => {+ const {container} = render(+ <Toolbar+ label="Actions"+ endContent={+ <Button+ label="Close"+ variant="ghost"+ isIconOnly+ icon={<span>x</span>}+ tooltip="Close panel"+ />+ }+ />,+ );+ const button = screen.getByRole('button', {name: 'Close'});+ const toolbar = container.querySelector(+ '[role="toolbar"]',+ ) as HTMLElement;+ const slot = findSlot(button, toolbar);+ // The tooltip is attached via the hook (no wrapper element), so the+ // marker stays on the button — a direct child of the container's slot —+ // and the container's direct-child `:has(> [marker]:last-child)` selector+ // still matches.+ const directMarked = slot.querySelector(`:scope > [${EDGE_COMP_ATTR}]`);+ expect(directMarked).not.toBeNull();+ });+ });});