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
12 changes: 12 additions & 0 deletions .changeset/35997-header-actions-width-cap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@object-ui/components': patch
---

fix(components): the record header re-measures its width fold on another record

Clause-②: no
Metadata-protocol: none

In a record page's header bar the action row folds by width: while its buttons wrap past one line, the page's own actions fold into the `⋯` one at a time. The lowered budget used to come back only when the bar changed width. Stepping to another record (previous / next, or another record in the drawer) keeps the same row, so after a record with many actions a record with fewer kept the old, lower budget and folded actions that fit.

The lowered budget now holds only for the record and the action set it was measured on. Another record, or a changed action set, draws the full budget (3, or the page's `maxVisible`) and the next layout measures again, so a row that still does not fit folds as before. Nothing an author writes changes.
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,83 @@ describe('page:header in the host bar folds by width (objectstack#35853)', () =>
await waitFor(() => expect(screen.getAllByRole('button').map((b) => b.textContent?.trim())).toContain('Send email'));
});
});

/**
* objectstack#35997 — the width cap belongs to the record and the action set
* it was measured on. The host steps between records (previous / next, the
* drawer) without remounting the row, so a cap lowered on a record with many
* actions must not outlive it: the next record draws the full budget and the
* next layout measures again. The row's line is stubbed as above, with the
* wrap point moved between the two records.
*/
describe('page:header in the host bar re-measures the width fold on another record (objectstack#35997)', () => {
afterEach(() => vi.restoreAllMocks());

const onlyContacted = { visible: 'record.status == "contacted"' };
const SWITCH_ACTIONS = [
act('convert', 'Convert', { variant: 'primary' }),
act('log_call', 'Log call'),
act('send_email', 'Send email', onlyContacted),
act('schedule', 'Schedule', onlyContacted),
];
const OTHER = { id: 'lead-2', name: 'Ada Park', status: 'new' };

function stubWrap() {
const line = { wrapAt: 0 };
vi.spyOn(HTMLElement.prototype, 'offsetTop', 'get').mockImplementation(function (this: HTMLElement) {
const row = this.parentElement;
if (!row || row.getAttribute('role') !== 'toolbar') return 0;
return Array.from(row.children).indexOf(this) >= line.wrapAt ? 40 : 0;
});
return line;
}

const tree = (record: typeof LEAD) => (
<BarHost claim={vi.fn()}>
<ActionProvider>
<RecordContextProvider
objectName="crm_lead"
recordId={record.id}
data={record}
objectSchema={LEAD_SCHEMA}
headerSystemActions={HOST_ACTIONS}
>
<RecordHeaderStateContext.Provider value={null}>
<PageHeader schema={{ type: 'page:header', actions: SWITCH_ACTIONS }} />
</RecordHeaderStateContext.Provider>
</RecordContextProvider>
</ActionProvider>
</BarHost>
);

it('a record with fewer actions that fit draws min(maxVisible, count) again', async () => {
const line = stubWrap();
line.wrapAt = 3;
const { rerender } = render(tree(LEAD));
const actionSlot = screen.getByTestId('action-slot');
await waitFor(() => expect(inlineButtons(actionSlot)).toContain('Convert'));
await waitFor(() => expect(inlineButtons(actionSlot)).not.toContain('Log call'));

line.wrapAt = 99;
rerender(tree(OTHER));
await waitFor(() => expect(inlineButtons(actionSlot)).toContain('Log call'));
const labels = inlineButtons(actionSlot);
expect(labels).toContain('Convert');
expect(labels).toContain('Edit');
expect(labels).not.toContain('Send email');
});

it('CONTROL: a record whose row still wraps after the switch folds again', async () => {
const line = stubWrap();
line.wrapAt = 3;
const { rerender } = render(tree(LEAD));
const actionSlot = screen.getByTestId('action-slot');
await waitFor(() => expect(inlineButtons(actionSlot)).not.toContain('Log call'));

line.wrapAt = 2;
rerender(tree(OTHER));
await waitFor(() => expect(inlineButtons(actionSlot)).toContain('Convert'));
await waitFor(() => expect(inlineButtons(actionSlot)).not.toContain('Log call'));
expect(inlineButtons(actionSlot)).toContain('Edit');
});
});
26 changes: 17 additions & 9 deletions ui/packages/components/src/renderers/layout/containers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,8 @@ export interface PageHeaderActionsProps {
* actions fold into the `⋯` one at a time, down to the primary. The record
* header's bar passes it, so a narrow drawer shows fewer than 3. The budget
* comes back when the host's bar (`[data-record-top-bar]`, else the row's
* parent) changes width. The host's buttons (Edit) still never fold.
* parent) changes width, or when the row draws another record or another
* action set (objectstack#35997). The host's buttons (Edit) still never fold.
*/
fitWidth?: boolean;
}
Expand Down Expand Up @@ -2027,18 +2028,19 @@ export const PageHeaderActions: React.FC<PageHeaderActionsProps> = ({
}) => {
const ctx = useRecordContext();
// objectstack#35853 — the width fold ({@link PageHeaderActionsProps.fitWidth}):
// the cap the row's wrapping has lowered the budget to, the row itself, and
// the budget the last render drew (read after it lays out).
const [widthCap, setWidthCap] = React.useState<number | null>(null);
// the cap the row's wrapping has lowered the budget to, keyed by the record
// and the action set it was measured on; the row itself; and the budget the
// last render drew with its key (read after it lays out).
const [widthCap, setWidthCap] = React.useState<{ key: string; cap: number } | null>(null);
const [fitRow, setFitRow] = React.useState<HTMLDivElement | null>(null);
const fitDrawn = React.useRef({ inline: 0, floor: 0 });
const fitDrawn = React.useRef({ inline: 0, floor: 0, key: '' });
React.useLayoutEffect(() => {
if (!fitWidth || !fitRow) return;
const items = Array.from(fitRow.children) as HTMLElement[];
if (items.length < 2) return;
const wrapped = items[items.length - 1].offsetTop > items[0].offsetTop + 1;
const { inline, floor } = fitDrawn.current;
if (wrapped && inline > floor) setWidthCap(inline - 1);
const { inline, floor, key } = fitDrawn.current;
if (wrapped && inline > floor) setWidthCap({ key, cap: inline - 1 });
});
React.useEffect(() => {
if (!fitWidth || !fitRow || typeof ResizeObserver === 'undefined') return;
Expand Down Expand Up @@ -2723,10 +2725,16 @@ export const PageHeaderActions: React.FC<PageHeaderActionsProps> = ({
.filter((a, i, all) => all.indexOf(a) === i)
: [];
// The width fold lowers the desktop row's budget, never below the primary.
// A cap holds only for the record and action set it was measured on: the
// host steps between records without remounting this row, so another
// record (objectstack#35997) draws the full budget and the next layout
// measures again. A string key, so an equal set never re-arms the fold.
const fitsWidth = fitWidth && !barFace && !isMobile;
const rowBudget = fitsWidth && widthCap != null ? Math.min(maxVisible, widthCap) : maxVisible;
const fitKey = JSON.stringify([ctx?.recordId, authoredButtons.map((a: any) => a?.name || a?.id)]);
const rowCap = widthCap?.key === fitKey ? widthCap.cap : null;
const rowBudget = fitsWidth && rowCap != null ? Math.min(maxVisible, rowCap) : maxVisible;
if (fitsWidth) {
fitDrawn.current = { inline: Math.min(rowBudget, authoredButtons.length), floor: authoredButtons.length > 0 ? 1 : 0 };
fitDrawn.current = { inline: Math.min(rowBudget, authoredButtons.length), floor: authoredButtons.length > 0 ? 1 : 0, key: fitKey };
}
const inlineAuthored = new Set<unknown>(authoredButtons.slice(0, rowBudget));
const inlineActions = barFace
Expand Down
Loading