diff --git a/.changeset/37239-table-seam-shadow-widen.md b/.changeset/37239-table-seam-shadow-widen.md new file mode 100644 index 00000000000..886e57034e6 --- /dev/null +++ b/.changeset/37239-table-seam-shadow-widen.md @@ -0,0 +1,10 @@ +--- +'@object-ui/components': patch +--- + +fix(components): a wide table's right seam shadow shows when the table widens after its rows are drawn + +Clause-②: no +Metadata-protocol: none + +- A table wider than its box draws a shadow at its right seam while columns remain to the right. The box only re-read its overflow when the box itself or the element directly inside it changed size. A table sits inside a full-width wrapper that keeps the box's width, so when the columns widened the table after the rows were drawn (no row added, the box unchanged), the shadow never appeared, about half the time on the Studio metadata list at 1440 px. The box now also watches what sits inside that wrapper, so the shadow follows the table's own width. The same fix applies to every page that puts a table in the shared overflow box (for example the approvals inbox). diff --git a/ui/packages/components/src/__tests__/horizontal-overflow-12708.test.tsx b/ui/packages/components/src/__tests__/horizontal-overflow-12708.test.tsx index 41bb05e23e4..0a3589afeb1 100644 --- a/ui/packages/components/src/__tests__/horizontal-overflow-12708.test.tsx +++ b/ui/packages/components/src/__tests__/horizontal-overflow-12708.test.tsx @@ -13,7 +13,7 @@ * `data-table-scroll-shadow-12463` stubs it. */ -import { describe, it, expect } from 'vitest'; +import { describe, it, expect, beforeAll, afterAll, afterEach, vi } from 'vitest'; import { render, fireEvent, act } from '@testing-library/react'; import '@testing-library/jest-dom'; import React from 'react'; @@ -85,3 +85,77 @@ describe('HorizontalOverflowBox (objectui#12708)', () => { expect(hasOverflowRight({ scrollLeft: 0, clientWidth: 700, scrollWidth: 702 })).toBe(true); }); }); + +/** + * objectstack#37239 — a `` inside a full-width wrapper (the shadcn + * `Table`'s container, `overflow-visible`, as the approvals inbox mounts it) + * widens past the box while the wrapper and the box keep their size. The box + * re-reads its overflow when that table resizes. jsdom's `ResizeObserver` is an + * inert polyfill, so a recording one stands in: `resize(el)` calls back only + * the observers watching `el`, as a browser does when only `el` changed size. + */ +describe('HorizontalOverflowBox follows a table that widens inside a wrapper (objectstack#37239)', () => { + const observed = new Map>(); + class RecordingResizeObserver { + private readonly targets = new Set(); + constructor(private readonly callback: ResizeObserverCallback) {} + observe(el: Element) { + this.targets.add(el); + if (!observed.has(el)) observed.set(el, new Set()); + observed.get(el)!.add(this.callback); + } + unobserve(el: Element) { + this.targets.delete(el); + observed.get(el)?.delete(this.callback); + } + disconnect() { + for (const el of this.targets) observed.get(el)?.delete(this.callback); + this.targets.clear(); + } + } + function resize(el: Element) { + act(() => { + for (const callback of Array.from(observed.get(el) ?? [])) callback([], {} as ResizeObserver); + }); + } + function setWidths(box: HTMLElement, scrollWidth: number, clientWidth: number) { + Object.defineProperty(box, 'scrollWidth', { configurable: true, value: scrollWidth }); + Object.defineProperty(box, 'clientWidth', { configurable: true, value: clientWidth }); + } + function renderWrapped() { + return render( + +
+
wide
+ + , + ); + } + + beforeAll(() => { vi.stubGlobal('ResizeObserver', RecordingResizeObserver); }); + afterEach(() => { observed.clear(); }); + afterAll(() => { vi.unstubAllGlobals(); }); + + it('shows the shadow once the wrapped table alone grows past the box', () => { + const { getByTestId, queryByTestId, container } = renderWrapped(); + const box = getByTestId('box'); + const table = container.querySelector('table')!; + setWidths(box, 718, 718); + resize(table); + expect(queryByTestId('horizontal-overflow-shadow')).toBeNull(); + + setWidths(box, 1071, 718); + resize(table); + expect(getByTestId('horizontal-overflow-shadow')).toBeInTheDocument(); + expect(box).toHaveAttribute('data-overflow-right', 'true'); + }); + + it('CONTROL: a resize of the wrapper itself still re-reads, as it did before', () => { + const { getByTestId, queryByTestId } = renderWrapped(); + const box = getByTestId('box'); + setWidths(box, 1071, 718); + // The wrapper was watched before this fix; the pin above is about the table. + resize(getByTestId('wrapper')); + expect(queryByTestId('horizontal-overflow-shadow')).not.toBeNull(); + }); +}); diff --git a/ui/packages/components/src/custom/use-horizontal-overflow.ts b/ui/packages/components/src/custom/use-horizontal-overflow.ts index 744641801d6..7965aaa9a90 100644 --- a/ui/packages/components/src/custom/use-horizontal-overflow.ts +++ b/ui/packages/components/src/custom/use-horizontal-overflow.ts @@ -16,8 +16,9 @@ export function hasOverflowRight(box: Pick, @@ -38,8 +39,18 @@ export function useHorizontalOverflow( observer = new ResizeObserver(read); observer.observe(box); // The content's own size moves `scrollWidth` without resizing the box - // (rows arrive, a column is resized). - for (const child of Array.from(box.children)) observer.observe(child); + // (rows arrive, a column is resized). The content is each child of the + // box AND each child's own children: a table usually sits in a wrapper + // that keeps the box's width (the shadcn `Table`'s `w-full` container, + // `overflow-visible` inside a box that scrolls), so the wrapper never + // resizes while the `` in it widens past the box. Measured on + // the Studio metadata list (objectstack#37239): the rows drew at the + // box's width, then the columns widened the table from 1134 to 1826 px + // with no row added, and a read keyed on the wrapper alone never ran. + for (const child of Array.from(box.children)) { + observer.observe(child); + for (const grandchild of Array.from(child.children)) observer.observe(grandchild); + } } return () => { box.removeEventListener('scroll', read); diff --git a/ui/packages/components/src/renderers/complex/__tests__/data-table-scroll-shadow-12463.test.tsx b/ui/packages/components/src/renderers/complex/__tests__/data-table-scroll-shadow-12463.test.tsx index 07621302e61..9154e2c2685 100644 --- a/ui/packages/components/src/renderers/complex/__tests__/data-table-scroll-shadow-12463.test.tsx +++ b/ui/packages/components/src/renderers/complex/__tests__/data-table-scroll-shadow-12463.test.tsx @@ -20,7 +20,7 @@ * The layout itself (rows scrolling under a sticky header, the footer in * view) was read in a real browser and is reported on the pull request. */ -import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'; +import { describe, it, expect, beforeAll, afterAll, afterEach, vi } from 'vitest'; import { render, fireEvent, act } from '@testing-library/react'; import '@testing-library/jest-dom'; import React from 'react'; @@ -187,3 +187,78 @@ describe('data-table scrollbar at rest while the table scrolls sideways (objects } }); }); + +/** + * objectstack#37239 — the shadow follows the TABLE's own width, not only the + * box's and the rows'. The Studio metadata list draws its rows while the table + * still fits its box, then its columns widen the `
` past the box with no + * row added and no change to the box (measured in a real browser: 1134 → 1826 + * px inside a 1134 px box). The `
` sits in the shadcn `Table` wrapper, + * which keeps the box's width, so only an observer on the `
` itself sees + * that change. + * + * jsdom has no layout and its `ResizeObserver` is an inert polyfill, so this + * stands in a recording one: `resize(el)` calls back every observer watching + * `el`, and no other, which is what a browser does when only `el` changed size. + */ +describe('data-table shadow when the table widens inside an unchanged box (objectstack#37239)', () => { + const observed = new Map>(); + class RecordingResizeObserver { + private readonly targets = new Set(); + constructor(private readonly callback: ResizeObserverCallback) {} + observe(el: Element) { + this.targets.add(el); + if (!observed.has(el)) observed.set(el, new Set()); + observed.get(el)!.add(this.callback); + } + unobserve(el: Element) { + this.targets.delete(el); + observed.get(el)?.delete(this.callback); + } + disconnect() { + for (const el of this.targets) observed.get(el)?.delete(this.callback); + this.targets.clear(); + } + } + function resize(el: Element) { + act(() => { + for (const callback of Array.from(observed.get(el) ?? [])) callback([], {} as ResizeObserver); + }); + } + function setWidths(box: HTMLElement, scrollWidth: number, clientWidth: number) { + Object.defineProperty(box, 'scrollWidth', { configurable: true, value: scrollWidth }); + Object.defineProperty(box, 'clientWidth', { configurable: true, value: clientWidth }); + } + + beforeAll(() => { vi.stubGlobal('ResizeObserver', RecordingResizeObserver); }); + afterEach(() => { observed.clear(); }); + afterAll(() => { vi.unstubAllGlobals(); }); + + it('draws the shadow once the
alone grows past the box, with no scroll and no row added', () => { + const { getByTestId, queryByTestId, container } = renderTable(baseSchema); + const box = getByTestId('data-table-scroll-box'); + const table = container.querySelector('table')!; + // The wrapper between the box and the table: the element a read keyed on + // the box's children alone would watch. It is not the table. + expect(table.parentElement).not.toBe(box); + expect(table.parentElement!.parentElement).toBe(box); + + // The rows are drawn and the table fits its box: no shadow. + setWidths(box, 1134, 1134); + resize(table); + expect(queryByTestId('data-table-scroll-shadow-right')).toBeNull(); + + // The columns widen the table; the box, its wrapper and the rows do not change. + setWidths(box, 1826, 1134); + resize(table); + expect(getByTestId('data-table-scroll-shadow-right')).toBeInTheDocument(); + }); + + it('CONTROL: a table that grows yet still fits its box draws no shadow', () => { + const { getByTestId, queryByTestId, container } = renderTable(baseSchema); + const box = getByTestId('data-table-scroll-box'); + setWidths(box, 900, 1134); + resize(container.querySelector('table')!); + expect(queryByTestId('data-table-scroll-shadow-right')).toBeNull(); + }); +});