Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion design-system/packages/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export interface ConfirmDialogProps {
type?: ConfirmDialogType;
}

const ANCHORED_TITLE_LINES = 3;

const defaultIcons: Record<ConfirmDialogType, ReactNode> = {
error: <Icon glyph={AlertCircle} />,
info: <Icon name="info" />,
Expand Down Expand Up @@ -103,6 +105,9 @@ export const ConfirmDialog = forwardRef<HTMLDivElement, ConfirmDialogProps>(
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;
Expand Down Expand Up @@ -139,7 +144,7 @@ export const ConfirmDialog = forwardRef<HTMLDivElement, ConfirmDialogProps>(
}, [busy, onOpenChange]);

const cancelButton = showCancel ? (
<Button ref={cancelButtonRef} disabled={busy} onClick={() => requestClose("cancel-button")} variant="fill">
<Button ref={cancelButtonRef} disabled={busy} onClick={() => requestClose("cancel-button")} size={buttonSize} variant="fill">
{resolvedCancelText}
</Button>
) : null;
Expand All @@ -149,6 +154,7 @@ export const ConfirmDialog = forwardRef<HTMLDivElement, ConfirmDialogProps>(
labelBehavior={secondaryAtStart ? "static" : undefined}
loading={pendingAction === "secondary"}
onClick={() => void runAction("secondary", onSecondary)}
size={buttonSize}
variant={secondaryAtStart ? "text" : "outline"}
>
{secondaryText}
Expand All @@ -160,6 +166,7 @@ export const ConfirmDialog = forwardRef<HTMLDivElement, ConfirmDialogProps>(
loading={pendingAction === "confirm"}
onClick={() => void runAction("confirm", onConfirm)}
ref={confirmButtonRef}
size={buttonSize}
tone={confirmDanger || type === "error" ? "danger" : "neutral"}
variant="primary"
>
Expand All @@ -182,7 +189,7 @@ export const ConfirmDialog = forwardRef<HTMLDivElement, ConfirmDialogProps>(
>
<DialogHeader>
<DialogHeading>
<DialogTitle>{title}</DialogTitle>
<DialogTitle lines={anchored ? ANCHORED_TITLE_LINES : undefined}>{title}</DialogTitle>
</DialogHeading>
{showCloseButton ? <DialogClose disabled={busy} /> : null}
</DialogHeader>
Expand Down
27 changes: 21 additions & 6 deletions design-system/packages/ui/src/components/Dialog/Dialog.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
20 changes: 16 additions & 4 deletions design-system/packages/ui/src/components/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,14 @@ const OverlaySurface = forwardRef<HTMLDivElement, OverlaySurfaceProps>(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);
Expand Down Expand Up @@ -281,12 +288,17 @@ export const DialogHeading = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivEl
},
);

export const DialogTitle = forwardRef<HTMLHeadingElement, HTMLAttributes<HTMLHeadingElement>>(
function DialogTitle({ children, className, id, ...props }, ref) {
export interface DialogTitleProps extends HTMLAttributes<HTMLHeadingElement> {
/** Wrap the title up to this many lines instead of truncating it to one. */
lines?: number;
}

export const DialogTitle = forwardRef<HTMLHeadingElement, DialogTitleProps>(
function DialogTitle({ children, className, id, lines, ...props }, ref) {
const context = useDialogContext("DialogTitle");
return (
<h2 {...props} className={classNames(styles.title, className)} data-openbitfun-part="title" id={id ?? context.titleId} ref={ref}>
<OverflowText>{children}</OverflowText>
<OverflowText lines={lines}>{children}</OverflowText>
</h2>
);
},
Expand Down
1 change: 1 addition & 0 deletions design-system/packages/ui/src/components/Dialog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export {
type DialogPreset,
type DialogProps,
type DialogSize,
type DialogTitleProps,
type SheetPlacement,
type SheetProps,
type SheetSize,
Expand Down
1 change: 1 addition & 0 deletions design-system/packages/ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export {
type DialogPreset,
type DialogProps,
type DialogSize,
type DialogTitleProps,
type SheetPlacement,
type SheetProps,
type SheetSize,
Expand Down
15 changes: 12 additions & 3 deletions design-system/packages/ui/src/internal/useAnchoredLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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<HTMLElement | null>;
layerRef: RefObject<HTMLElement | null>;
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. */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion design-system/packages/ui/tests/confirm-dialog.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test("ConfirmDialog composes semantic content and actions on Dialog", async () =
assert.match(source, /<Dialog/);
assert.match(source, /role="alertdialog"/);
assert.match(source, /<DialogHeader>/);
assert.match(source, /<DialogTitle>\{title\}<\/DialogTitle>/);
assert.match(source, /<DialogTitle lines=\{anchored \? ANCHORED_TITLE_LINES : undefined\}>\{title\}<\/DialogTitle>/);
assert.match(source, /<DialogBody>/);
assert.match(source, /<DialogFooter>/);
assert.match(source, /data-openbitfun-component="confirm-dialog" data-openbitfun-part="content"/);
Expand Down
13 changes: 13 additions & 0 deletions design-system/packages/ui/tests/dialog.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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"/);
Expand Down Expand Up @@ -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;/);
});
15 changes: 11 additions & 4 deletions src/web-ui/src/shared/ui/ConfirmationInteraction.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<ConfirmDialog open anchor={captureConfirmationAnchor(trigger)} title="Delete record?"
message="This cannot be undone." cancelText="Cancel" confirmText="Delete" onConfirm={vi.fn()} onOpenChange={vi.fn()} />));
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();
Expand All @@ -40,7 +46,8 @@ describe('confirmation at the action origin', () => {
const anchor = captureConfirmationAnchor(trigger);
trigger.remove();
act(() => root.render(<ConfirmDialog open anchor={anchor} title="Delete?" onConfirm={vi.fn()} onOpenChange={vi.fn()} />));
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');
});

Expand All @@ -53,7 +60,7 @@ describe('confirmation at the action origin', () => {
vi.mocked(trigger.getBoundingClientRect).mockReturnValue(new DOMRect(940, 560, 40, 32));
act(() => root.render(<ConfirmDialog open anchor={captureConfirmationAnchor(trigger)} title="Delete?" onConfirm={vi.fn()} onOpenChange={vi.fn()} />));
expect(surface().dataset.placement).toBe('top');
expect(surface().style.left).toBe('672px');
expect(surface().style.left).toBe('660px');
expect(surface().style.top).toBe('396px');
});

Expand Down
Loading