An open source design system that's fully customizable and agent ready
TypeScript
docs(Overlay): say why the radius Effect stays, and pin when the radius is read (#9870)
Cindy Zhanga343c796d4ca
diff --git a/.changeset/overlay-radius-effect-note.md b/.changeset/overlay-radius-effect-note.mdnew file mode 100644index 0000000000..3c1f4a80d1--- /dev/null+++ b/.changeset/overlay-radius-effect-note.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[docs] The source comment above the Effect that copies an `Overlay`'s first-child corner radius onto its wrapper now says why it stays an Effect. New tests pin when the radius is read. Nothing a consumer imports or renders changes.++@cixzhangdiff --git a/packages/core/src/Overlay/Overlay.test.tsx b/packages/core/src/Overlay/Overlay.test.tsxindex a976bf9c73..c435557d21 100644--- a/packages/core/src/Overlay/Overlay.test.tsx+++ b/packages/core/src/Overlay/Overlay.test.tsx@@ -11,8 +11,14 @@*/import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';-import {render, screen, renderHook, fireEvent} from '@testing-library/react';-import {createRef} from 'react';+import {+ act,+ render,+ screen,+ renderHook,+ fireEvent,+} from '@testing-library/react';+import {Activity, createRef} from 'react';import {Overlay} from './Overlay';import {useOverlay} from './useOverlay';@@ -244,6 +250,76 @@ describe('Overlay', () => {const root = container.firstElementChild as HTMLElement;expect(root.style.opacity).toBe('0.5');});++ it('takes the corner radius of its first child', () => {+ const {container} = render(+ <Overlay content={<span>c</span>}>+ <div style={{borderRadius: '12px'}}>b</div>+ </Overlay>,+ );+ const root = container.firstElementChild as HTMLElement;+ expect(root.style.borderRadius).toBe('12px');+ });++ it('sets no radius when its first child has none', () => {+ const {container} = render(+ <Overlay content={<span>c</span>}>+ <div>b</div>+ </Overlay>,+ );+ const root = container.firstElementChild as HTMLElement;+ expect(root.style.borderRadius).toBe('');+ });++ it('gives a callback ref the root that took the radius', () => {+ const ref = vi.fn();+ const {container} = render(+ <Overlay ref={ref} content={<span>c</span>}>+ <div style={{borderRadius: '8px'}}>b</div>+ </Overlay>,+ );+ const root = container.firstElementChild as HTMLElement;+ expect(ref).toHaveBeenCalledWith(root);+ expect(root.style.borderRadius).toBe('8px');+ });++ it('keeps a later radius when an inline callback ref re-renders', () => {+ const overlay = (label: string) => (+ <Overlay ref={element => void element} content={<span>{label}</span>}>+ <div style={{borderRadius: '12px'}}>b</div>+ </Overlay>+ );+ const {container, rerender} = render(overlay('first'));+ const root = container.firstElementChild as HTMLElement;+ expect(root.style.borderRadius).toBe('12px');++ root.style.borderRadius = '40px';+ rerender(overlay('second'));+ expect(container.firstElementChild).toBe(root);+ expect(root.style.borderRadius).toBe('40px');+ });++ it('reads the radius again when a hidden Activity shows it', async () => {+ const overlay = (mode: 'visible' | 'hidden') => (+ <Activity mode={mode}>+ <Overlay content={<span>c</span>}>+ <div data-testid="media" style={{borderRadius: '12px'}}>+ b+ </div>+ </Overlay>+ </Activity>+ );+ const {container, rerender} = render(overlay('visible'));+ const root = container.firstElementChild as HTMLElement;+ expect(root.style.borderRadius).toBe('12px');++ rerender(overlay('hidden'));+ await act(async () => Promise.resolve());+ screen.getByTestId('media').style.borderRadius = '48px';+ rerender(overlay('visible'));+ expect(container.firstElementChild).toBe(root);+ expect(root.style.borderRadius).toBe('48px');+ });});describe('host attributes', () => {diff --git a/packages/core/src/Overlay/Overlay.tsx b/packages/core/src/Overlay/Overlay.tsxindex 81ea27ff78..f1e7ba7263 100644--- a/packages/core/src/Overlay/Overlay.tsx+++ b/packages/core/src/Overlay/Overlay.tsx@@ -94,8 +94,13 @@ export function Overlay({align,});- // Border radius: mirror first child's radius onto the wrapper.- // Only the component needs this — hook consumers have their own radius.+ // Copies the first child's radius onto the wrapper, so the scrim clips to+ // the media's rounded corners. This is a layout read of the mounted DOM, and+ // no user interaction causes it. It runs again when a hidden Activity shows+ // the wrapper, because the child can change while it is hidden. A ref+ // callback runs again when a caller's ref changes identity, so it cannot+ // keep this once-per-show timing. A useOverlay consumer sets the radius on+ // its own element.useIsomorphicLayoutEffect(() => {const el = overlay.containerRef.current;if (!el) {