Skip to content

Commit d7cec2e

Browse files
authored
refactor(react-overflow,priority-overflow): pure manager + strict-mode-safe lifecycle (#36262)
1 parent 35720ad commit d7cec2e

8 files changed

Lines changed: 454 additions & 194 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "minor",
3+
"comment": "feat: createOverflowManager accepts initialOptions, new setOptions method, observe now returns its cleanup, and the OverflowManagerOptions type is exported.",
4+
"packageName": "@fluentui/priority-overflow",
5+
"email": "bsunderhus@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "fix: make Overflow container strict-mode safe",
4+
"packageName": "@fluentui/react-overflow",
5+
"email": "bsunderhus@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

packages/react-components/priority-overflow/etc/priority-overflow.api.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
55
```ts
66

7-
// @internal (undocumented)
8-
export function createOverflowManager(): OverflowManager;
7+
// @internal
8+
export function createOverflowManager(initialOptions?: Partial<ObserveOptions>): OverflowManager;
99

10-
// @public (undocumented)
10+
// @public
1111
export interface ObserveOptions {
1212
hasHiddenItems?: boolean;
1313
minimumVisible?: number;
@@ -18,67 +18,61 @@ export interface ObserveOptions {
1818
padding?: number;
1919
}
2020

21-
// @public (undocumented)
21+
// @public
2222
export type OnUpdateItemVisibility = (data: OnUpdateItemVisibilityPayload) => void;
2323

24-
// @public (undocumented)
24+
// @public
2525
export interface OnUpdateItemVisibilityPayload {
26-
// (undocumented)
2726
item: OverflowItemEntry;
28-
// (undocumented)
2927
visible: boolean;
3028
}
3129

3230
// @public
3331
export type OnUpdateOverflow = (data: OverflowEventPayload) => void;
3432

35-
// @public (undocumented)
33+
// @public
3634
export type OverflowAxis = 'horizontal' | 'vertical';
3735

38-
// @public (undocumented)
36+
// @public
3937
export type OverflowDirection = 'start' | 'end';
4038

41-
// @public (undocumented)
39+
// @public
4240
export interface OverflowDividerEntry {
4341
element: HTMLElement;
44-
// (undocumented)
4542
groupId: string;
4643
}
4744

4845
// @public
4946
export interface OverflowEventPayload {
50-
// (undocumented)
5147
groupVisibility: Record<string, OverflowGroupState>;
52-
// (undocumented)
5348
invisibleItems: OverflowItemEntry[];
54-
// (undocumented)
5549
visibleItems: OverflowItemEntry[];
5650
}
5751

58-
// @public (undocumented)
52+
// @public
5953
export type OverflowGroupState = 'visible' | 'hidden' | 'overflow';
6054

61-
// @public (undocumented)
55+
// @public
6256
export interface OverflowItemEntry {
6357
element: HTMLElement;
64-
// (undocumented)
6558
groupId?: string;
6659
id: string;
6760
pinned?: boolean;
6861
priority: number;
6962
}
7063

71-
// @internal (undocumented)
64+
// @internal
7265
export interface OverflowManager {
7366
addDivider: (divider: OverflowDividerEntry) => void;
7467
addItem: (items: OverflowItemEntry) => void;
7568
addOverflowMenu: (element: HTMLElement) => void;
7669
disconnect: () => void;
7770
forceUpdate: () => void;
78-
observe: (container: HTMLElement, options: ObserveOptions) => void;
71+
observe: (container: HTMLElement, options?: ObserveOptions) => void;
7972
removeDivider: (groupId: string) => void;
8073
removeItem: (itemId: string) => void;
8174
removeOverflowMenu: () => void;
75+
setOptions: (options: Partial<ObserveOptions>) => void;
8276
update: () => void;
8377
}
8478

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import { createOverflowManager } from './overflowManager';
2+
import type { ObserveOptions, OverflowEventPayload } from './types';
3+
4+
describe('overflowManager', () => {
5+
beforeAll(() => {
6+
global.ResizeObserver = class ResizeObserver {
7+
public observe() {
8+
// do nothing
9+
}
10+
11+
public unobserve() {
12+
// do nothing
13+
}
14+
15+
public disconnect() {
16+
// do nothing
17+
}
18+
} as unknown as typeof ResizeObserver;
19+
});
20+
21+
const createElementWithSize = (tagName: string, width: number) => {
22+
const element = document.createElement(tagName);
23+
Object.defineProperty(element, 'offsetWidth', { configurable: true, value: width });
24+
Object.defineProperty(element, 'offsetHeight', { configurable: true, value: width });
25+
26+
return element;
27+
};
28+
29+
const createContainer = (width: number) => {
30+
const container = document.createElement('div');
31+
Object.defineProperty(container, 'clientWidth', { configurable: true, value: width });
32+
Object.defineProperty(container, 'clientHeight', { configurable: true, value: width });
33+
34+
return container;
35+
};
36+
37+
const createObserveOptions = (options: Partial<ObserveOptions> = {}): ObserveOptions => ({
38+
overflowAxis: 'horizontal',
39+
overflowDirection: 'end',
40+
padding: 10,
41+
minimumVisible: 0,
42+
hasHiddenItems: false,
43+
onUpdateItemVisibility: jest.fn(),
44+
onUpdateOverflow: jest.fn(),
45+
...options,
46+
});
47+
48+
const lastDispatch = (onUpdateOverflow: jest.Mock): OverflowEventPayload =>
49+
onUpdateOverflow.mock.calls[onUpdateOverflow.mock.calls.length - 1][0];
50+
51+
it('should dispatch overflow update after forceUpdate', () => {
52+
const onUpdateOverflow = jest.fn();
53+
const options = createObserveOptions({ onUpdateOverflow });
54+
const manager = createOverflowManager(options);
55+
const container = createContainer(100);
56+
const itemA = createElementWithSize('button', 40);
57+
const itemB = createElementWithSize('button', 40);
58+
const menu = createElementWithSize('button', 20);
59+
60+
manager.addItem({ element: itemA, id: 'a', priority: 1 });
61+
manager.addItem({ element: itemB, id: 'b', priority: 0 });
62+
manager.addOverflowMenu(menu);
63+
manager.observe(container);
64+
manager.forceUpdate();
65+
66+
const dispatch = lastDispatch(onUpdateOverflow);
67+
expect(dispatch.visibleItems.map(item => item.id).sort()).toEqual(['a', 'b']);
68+
expect(dispatch.invisibleItems).toEqual([]);
69+
expect(dispatch.groupVisibility).toEqual({});
70+
});
71+
72+
it('should re-dispatch when setOptions changes a relevant option', () => {
73+
const onUpdateOverflow = jest.fn();
74+
const options = createObserveOptions({ onUpdateOverflow });
75+
const manager = createOverflowManager(options);
76+
const container = createContainer(100);
77+
const itemA = createElementWithSize('button', 40);
78+
const itemB = createElementWithSize('button', 40);
79+
const menu = createElementWithSize('button', 20);
80+
81+
manager.addItem({ element: itemA, id: 'a', priority: 1 });
82+
manager.addItem({ element: itemB, id: 'b', priority: 0 });
83+
manager.addOverflowMenu(menu);
84+
manager.observe(container);
85+
manager.forceUpdate();
86+
87+
onUpdateOverflow.mockClear();
88+
manager.setOptions({ padding: 30 });
89+
90+
expect(onUpdateOverflow).toHaveBeenCalled();
91+
const dispatch = lastDispatch(onUpdateOverflow);
92+
expect(dispatch.visibleItems.map(item => item.id)).toEqual(['a']);
93+
expect(dispatch.invisibleItems.map(item => item.id)).toEqual(['b']);
94+
});
95+
96+
it('should not re-dispatch when setOptions is called with a partial that does not change anything', () => {
97+
const onUpdateOverflow = jest.fn();
98+
const options = createObserveOptions({ onUpdateOverflow });
99+
const manager = createOverflowManager(options);
100+
const container = createContainer(100);
101+
const itemA = createElementWithSize('button', 40);
102+
103+
manager.addItem({ element: itemA, id: 'a', priority: 1 });
104+
manager.observe(container, options);
105+
manager.forceUpdate();
106+
107+
onUpdateOverflow.mockClear();
108+
manager.setOptions({ padding: 10 }); // padding is already 10; no real change
109+
110+
expect(onUpdateOverflow).not.toHaveBeenCalled();
111+
});
112+
113+
it('disconnect stops observation and re-observe restarts dispatching', () => {
114+
const onUpdateOverflow = jest.fn();
115+
const options = createObserveOptions({ onUpdateOverflow });
116+
const manager = createOverflowManager(options);
117+
const container = createContainer(100);
118+
const item = createElementWithSize('button', 40);
119+
120+
manager.addItem({ element: item, id: 'a', priority: 1 });
121+
manager.observe(container);
122+
manager.forceUpdate();
123+
expect(lastDispatch(onUpdateOverflow).visibleItems.map(i => i.id)).toEqual(['a']);
124+
125+
manager.disconnect();
126+
onUpdateOverflow.mockClear();
127+
128+
manager.addItem({ element: item, id: 'a', priority: 1 });
129+
manager.observe(container);
130+
manager.forceUpdate();
131+
expect(onUpdateOverflow).toHaveBeenCalled();
132+
expect(lastDispatch(onUpdateOverflow).visibleItems.map(i => i.id)).toEqual(['a']);
133+
});
134+
135+
it('should remove items through removeItem', () => {
136+
const onUpdateOverflow = jest.fn();
137+
const options = createObserveOptions({ onUpdateOverflow });
138+
const manager = createOverflowManager(options);
139+
const container = createContainer(100);
140+
const item = createElementWithSize('button', 40);
141+
142+
manager.addItem({ element: item, id: 'a', priority: 1 });
143+
manager.observe(container);
144+
manager.forceUpdate();
145+
146+
expect(lastDispatch(onUpdateOverflow).visibleItems.map(i => i.id)).toEqual(['a']);
147+
148+
manager.removeItem('a');
149+
manager.forceUpdate();
150+
151+
const dispatch = lastDispatch(onUpdateOverflow);
152+
expect(dispatch.visibleItems).toEqual([]);
153+
expect(dispatch.invisibleItems).toEqual([]);
154+
});
155+
});

0 commit comments

Comments
 (0)