From 6c88eba1d2af6238480b59788a049b43d9014be7 Mon Sep 17 00:00:00 2001 From: Bob Lee Date: Mon, 9 Nov 2026 17:18:33 +0800 Subject: [PATCH] fix(ui): compact, end-aligned anchored confirmations Anchored confirmations reused the modal dialog title (24px bold, single line with a fade), so long item names were cut off and the title overpowered the popover. They also aligned their left edge to the trigger, so popovers opened from trailing actions spilled to the right. - Anchored ConfirmDialog titles use the section heading scale and wrap up to three lines; DialogTitle gains an optional `lines` prop. - Anchored surfaces get even 16px padding and small action buttons. - useAnchoredLayer accepts a logical `align` (start by default, RTL aware); anchored Dialogs align their end edge with the trigger. Menus, Select and Combobox keep start alignment. --- design-system/packages/ui/README.md | 3 ++- .../ConfirmDialog/ConfirmDialog.tsx | 11 ++++++-- .../src/components/Dialog/Dialog.module.css | 27 ++++++++++++++----- .../ui/src/components/Dialog/Dialog.tsx | 20 +++++++++++--- .../ui/src/components/Dialog/index.ts | 1 + design-system/packages/ui/src/index.ts | 1 + .../ui/src/internal/useAnchoredLayer.ts | 15 ++++++++--- .../packages/ui/tests/confirm-dialog.test.mjs | 2 +- .../packages/ui/tests/dialog.test.mjs | 13 +++++++++ .../ui/ConfirmationInteraction.test.tsx | 15 ++++++++--- 10 files changed, 87 insertions(+), 21 deletions(-) diff --git a/design-system/packages/ui/README.md b/design-system/packages/ui/README.md index 7c162cc644..ef14ce0b88 100644 --- a/design-system/packages/ui/README.md +++ b/design-system/packages/ui/README.md @@ -790,7 +790,8 @@ completion does not close the newly opened menu. Use `ConfirmDialog anchor={captureConfirmationAnchor(event.currentTarget)}` when a single-item action needs a short explanation beside its trigger. Capture the anchor before awaiting work or closing a menu. It retains the trigger's -rectangle and overlay owner, flips within the viewport, and restores focus to +rectangle and overlay owner, lines its end edge up with the trigger's end edge +(right in LTR), flips within the viewport, and restores focus to the trigger or its surviving owner. Without an anchor the dialog remains modal; keep this presentation for bulk deletion, overwrite, reset, and permission decisions. Confirmations focus Cancel first when it is available. diff --git a/design-system/packages/ui/src/components/ConfirmDialog/ConfirmDialog.tsx b/design-system/packages/ui/src/components/ConfirmDialog/ConfirmDialog.tsx index 8bb301a21a..8f2a7920bf 100644 --- a/design-system/packages/ui/src/components/ConfirmDialog/ConfirmDialog.tsx +++ b/design-system/packages/ui/src/components/ConfirmDialog/ConfirmDialog.tsx @@ -56,6 +56,8 @@ export interface ConfirmDialogProps { type?: ConfirmDialogType; } +const ANCHORED_TITLE_LINES = 3; + const defaultIcons: Record = { error: , info: , @@ -103,6 +105,9 @@ export const ConfirmDialog = forwardRef( const secondaryAtStart = hasSecondary && secondaryActionPlacement === "start"; const resolvedCancelText = cancelText ?? designSystem.messages.confirmCancel; const resolvedConfirmText = confirmText ?? designSystem.messages.confirmAction; + // Anchored confirmations are compact popovers beside their trigger. + const anchored = Boolean(anchor); + const buttonSize = anchored ? "sm" : undefined; useEffect(() => { mountedRef.current = true; @@ -139,7 +144,7 @@ export const ConfirmDialog = forwardRef( }, [busy, onOpenChange]); const cancelButton = showCancel ? ( - ) : null; @@ -149,6 +154,7 @@ export const ConfirmDialog = forwardRef( labelBehavior={secondaryAtStart ? "static" : undefined} loading={pendingAction === "secondary"} onClick={() => void runAction("secondary", onSecondary)} + size={buttonSize} variant={secondaryAtStart ? "text" : "outline"} > {secondaryText} @@ -160,6 +166,7 @@ export const ConfirmDialog = forwardRef( loading={pendingAction === "confirm"} onClick={() => void runAction("confirm", onConfirm)} ref={confirmButtonRef} + size={buttonSize} tone={confirmDanger || type === "error" ? "danger" : "neutral"} variant="primary" > @@ -182,7 +189,7 @@ export const ConfirmDialog = forwardRef( > - {title} + {title} {showCloseButton ? : null} diff --git a/design-system/packages/ui/src/components/Dialog/Dialog.module.css b/design-system/packages/ui/src/components/Dialog/Dialog.module.css index 640000a528..c7e4308360 100644 --- a/design-system/packages/ui/src/components/Dialog/Dialog.module.css +++ b/design-system/packages/ui/src/components/Dialog/Dialog.module.css @@ -51,19 +51,34 @@ .anchoredSurface { inline-size: min(var(--openbitfun-overlay-dialog-max-inline-size-small), calc(100vw - 2 * var(--openbitfun-space-2))); box-shadow: var(--openbitfun-shadow-overlay); - transform-origin: top left; + transform-origin: top right; animation: none; } - .anchoredSurface[data-placement="top"] { transform-origin: bottom left; } + .anchoredSurface[data-placement="top"] { transform-origin: bottom right; } + + .anchoredSurface > .header { + padding: var(--openbitfun-space-4) var(--openbitfun-space-4) var(--openbitfun-space-2); + } + + .anchoredSurface > .body { + padding: 0 var(--openbitfun-space-4) var(--openbitfun-space-2); + } - .anchoredSurface > .header, - .anchoredSurface > .body, .anchoredSurface > .footer { - padding: var(--openbitfun-space-3); + padding: var(--openbitfun-space-3) var(--openbitfun-space-4) var(--openbitfun-space-4); } - .anchoredSurface > .body { padding-block-start: 0; } + /* A popover beside its trigger reads as a compact question, not a page title. */ + .anchoredSurface > .header > .heading > .title { + font-family: var(--openbitfun-type-heading-section-font-family); + font-size: var(--openbitfun-type-heading-section-font-size); + font-weight: var(--openbitfun-type-heading-section-font-weight); + line-height: var(--openbitfun-line-height-snug); + letter-spacing: var(--openbitfun-type-heading-section-letter-spacing); + white-space: normal; + overflow-wrap: anywhere; + } .surface[data-preset="panel"] { --_dialog-panel-title-gap: calc(var(--openbitfun-overlay-dialog-header-padding-block-start) + var(--openbitfun-space-4)); diff --git a/design-system/packages/ui/src/components/Dialog/Dialog.tsx b/design-system/packages/ui/src/components/Dialog/Dialog.tsx index 5fbb532a9a..9be759110a 100644 --- a/design-system/packages/ui/src/components/Dialog/Dialog.tsx +++ b/design-system/packages/ui/src/components/Dialog/Dialog.tsx @@ -145,7 +145,14 @@ const OverlaySurface = forwardRef(function ownerRef.current = rendered.anchor?.owner ?? null; returnFocusRef.current = anchorRef.current?.isConnected ? anchorRef.current : ownerRef.current; const anchored = Boolean(rendered.anchor); - const layout = useAnchoredLayer({ open: present && anchored, anchorRef, layerRef: surfaceRef, fallbackRect: rendered.anchor?.rect }); + const layout = useAnchoredLayer({ + open: present && anchored, + anchorRef, + layerRef: surfaceRef, + // Confirmations sit under the trailing actions that open them. + align: "end", + fallbackRect: rendered.anchor?.rect, + }); const close = useCallback((reason: DialogCloseReason) => { if (open) onOpenChange(false, reason); @@ -281,12 +288,17 @@ export const DialogHeading = forwardRef>( - function DialogTitle({ children, className, id, ...props }, ref) { +export interface DialogTitleProps extends HTMLAttributes { + /** Wrap the title up to this many lines instead of truncating it to one. */ + lines?: number; +} + +export const DialogTitle = forwardRef( + function DialogTitle({ children, className, id, lines, ...props }, ref) { const context = useDialogContext("DialogTitle"); return (

- {children} + {children}

); }, diff --git a/design-system/packages/ui/src/components/Dialog/index.ts b/design-system/packages/ui/src/components/Dialog/index.ts index 580068281c..7c9270e1ed 100644 --- a/design-system/packages/ui/src/components/Dialog/index.ts +++ b/design-system/packages/ui/src/components/Dialog/index.ts @@ -19,6 +19,7 @@ export { type DialogPreset, type DialogProps, type DialogSize, + type DialogTitleProps, type SheetPlacement, type SheetProps, type SheetSize, diff --git a/design-system/packages/ui/src/index.ts b/design-system/packages/ui/src/index.ts index 4f0d7d6794..faaf5b1336 100644 --- a/design-system/packages/ui/src/index.ts +++ b/design-system/packages/ui/src/index.ts @@ -205,6 +205,7 @@ export { type DialogPreset, type DialogProps, type DialogSize, + type DialogTitleProps, type SheetPlacement, type SheetProps, type SheetSize, diff --git a/design-system/packages/ui/src/internal/useAnchoredLayer.ts b/design-system/packages/ui/src/internal/useAnchoredLayer.ts index 40e02cc677..f50d41c469 100644 --- a/design-system/packages/ui/src/internal/useAnchoredLayer.ts +++ b/design-system/packages/ui/src/internal/useAnchoredLayer.ts @@ -3,6 +3,8 @@ import { useEffect, useLayoutEffect, useRef, useState, type CSSProperties, type const useIsomorphicLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect; export type LayerPlacement = "top" | "bottom" | "left" | "right"; +/** Cross-axis edge shared with the anchor for top/bottom placement; logical, so RTL mirrors it. */ +export type LayerAlign = "start" | "end"; export type PortalTarget = Element | DocumentFragment | (() => Element | DocumentFragment | null) | null; /** Default portals stay inside the nearest theme scope; hosts may supply an overlay root. */ @@ -11,11 +13,12 @@ export function resolveLayerPortal(target: PortalTarget | undefined, anchor: HTM return target ?? anchor?.closest("[data-openbitfun-design-system-root]") ?? anchor?.ownerDocument.body ?? null; } -export function useAnchoredLayer({ open, anchorRef, layerRef, placement = "bottom", matchWidth = false, overlapAnchor = false, revision, point, fallbackRect }: { +export function useAnchoredLayer({ open, anchorRef, layerRef, placement = "bottom", align = "start", matchWidth = false, overlapAnchor = false, revision, point, fallbackRect }: { open: boolean; anchorRef: RefObject; layerRef: RefObject; placement?: LayerPlacement; + align?: LayerAlign; /** Match the anchor exactly, or use its width as the layer's minimum. */ matchWidth?: boolean | "minimum"; /** Position the layer over the anchor when the layer visually replaces it. */ @@ -79,7 +82,9 @@ export function useAnchoredLayer({ open, anchorRef, layerRef, placement = "botto ? overlapAnchor ? (useBefore ? rect.right - box.width : rect.left) : (useBefore ? rect.left - box.width - resolvedGap : rect.right + resolvedGap) - : rect.left; + : alignToRight(align, view.getComputedStyle(layer).direction === "rtl") + ? rect.right - box.width + : rect.left; const top = horizontal ? rect.top : overlapAnchor @@ -107,6 +112,10 @@ export function useAnchoredLayer({ open, anchorRef, layerRef, placement = "botto view.visualViewport?.removeEventListener("resize", update); view.visualViewport?.removeEventListener("scroll", update); }; - }, [open, anchorRef, layerRef, placement, matchWidth, overlapAnchor, revision, point?.x, point?.y, fallbackRect]); + }, [open, anchorRef, layerRef, placement, align, matchWidth, overlapAnchor, revision, point?.x, point?.y, fallbackRect]); return layout; } + +function alignToRight(align: LayerAlign, rtl: boolean) { + return (align === "end") !== rtl; +} diff --git a/design-system/packages/ui/tests/confirm-dialog.test.mjs b/design-system/packages/ui/tests/confirm-dialog.test.mjs index b8a32964f8..7b7e508174 100644 --- a/design-system/packages/ui/tests/confirm-dialog.test.mjs +++ b/design-system/packages/ui/tests/confirm-dialog.test.mjs @@ -11,7 +11,7 @@ test("ConfirmDialog composes semantic content and actions on Dialog", async () = assert.match(source, //); - assert.match(source, /\{title\}<\/DialogTitle>/); + assert.match(source, /\{title\}<\/DialogTitle>/); assert.match(source, //); assert.match(source, //); assert.match(source, /data-openbitfun-component="confirm-dialog" data-openbitfun-part="content"/); diff --git a/design-system/packages/ui/tests/dialog.test.mjs b/design-system/packages/ui/tests/dialog.test.mjs index ea1c490d8e..45299e1e60 100644 --- a/design-system/packages/ui/tests/dialog.test.mjs +++ b/design-system/packages/ui/tests/dialog.test.mjs @@ -15,6 +15,8 @@ test("Dialog and Sheet compose the shared overlay kernel and compound anatomy", assert.match(source, /useDismissibleLayer/); assert.match(source, /useFocusScope/); assert.match(source, /preventScroll=\{!anchored && preventScroll\}/); + // Anchored confirmations share the trigger's trailing edge. + assert.match(source, /useAnchoredLayer\(\{[^}]*align: "end"/); assert.match(source, /usePresence/); assert.match(source, /kind="dialog"/); assert.match(source, /kind="sheet"/); @@ -112,3 +114,14 @@ test("dialog separators are explicit and do not leak native attributes", () => { assert.doesNotMatch(markup, / separator=/); } }); + +test("anchored layers align their logical end edge to the anchor", async () => { + const source = await readFile( + new URL("../src/internal/useAnchoredLayer.ts", import.meta.url), + "utf8", + ); + + assert.match(source, /align = "start"/); + assert.match(source, /alignToRight\(align, view\.getComputedStyle\(layer\)\.direction === "rtl"\)\s*\? rect\.right - box\.width\s*: rect\.left/); + assert.match(source, /return \(align === "end"\) !== rtl;/); +}); diff --git a/src/web-ui/src/shared/ui/ConfirmationInteraction.test.tsx b/src/web-ui/src/shared/ui/ConfirmationInteraction.test.tsx index 042cbaaf85..95ffa33c2e 100644 --- a/src/web-ui/src/shared/ui/ConfirmationInteraction.test.tsx +++ b/src/web-ui/src/shared/ui/ConfirmationInteraction.test.tsx @@ -25,12 +25,18 @@ describe('confirmation at the action origin', () => { await act(async () => root.unmount()); host.remove(); trigger.remove(); vi.restoreAllMocks(); }); - it('places a compact confirmation next to its trigger and focuses Cancel', () => { + it('places a compact confirmation under its trigger, end-aligned, and focuses Cancel', () => { + const triggerRect = trigger.getBoundingClientRect(); + vi.spyOn(HTMLElement.prototype, 'getBoundingClientRect').mockImplementation(function () { + return this.getAttribute('role') === 'alertdialog' ? new DOMRect(0, 0, 200, 120) : new DOMRect(); + }); + vi.mocked(trigger.getBoundingClientRect).mockReturnValue(triggerRect); act(() => root.render()); expect(surface().dataset.presentation).toBe('anchored'); expect(surface().getAttribute('aria-modal')).toBe('false'); - expect(surface().style.left).toBe('240px'); + // The surface's right edge meets the trigger's right edge (288 - 200). + expect(surface().style.left).toBe('88px'); expect(surface().style.top).toBe('216px'); expect(document.activeElement).toBe(button('Cancel')); expect(document.querySelector('[data-openbitfun-overlay-modal="true"]')).toBeNull(); @@ -40,7 +46,8 @@ describe('confirmation at the action origin', () => { const anchor = captureConfirmationAnchor(trigger); trigger.remove(); act(() => root.render()); - expect(surface().style.left).toBe('240px'); + // jsdom gives the surface no width, so its end edge lands on the captured right edge. + expect(surface().style.left).toBe('288px'); expect(surface().style.top).toBe('216px'); }); @@ -53,7 +60,7 @@ describe('confirmation at the action origin', () => { vi.mocked(trigger.getBoundingClientRect).mockReturnValue(new DOMRect(940, 560, 40, 32)); act(() => root.render()); expect(surface().dataset.placement).toBe('top'); - expect(surface().style.left).toBe('672px'); + expect(surface().style.left).toBe('660px'); expect(surface().style.top).toBe('396px'); });