-
Notifications
You must be signed in to change notification settings - Fork 2.9k
refactor(react-overflow,priority-overflow): pure manager + strict-mode-safe lifecycle #36262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bsunderhus
merged 7 commits into
microsoft:master
from
bsunderhus:react-overflow/pr1-strict-mode-lifecycle
May 29, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
84a5341
refactor(react-overflow,priority-overflow): make manager creation pur…
bsunderhus ae93c45
chore: add change files for PR1 strict-mode/lifecycle refactor
bsunderhus be892d0
chore(react-overflow,priority-overflow): address PR1 review feedback
bsunderhus 2053b3b
fix(react-overflow): restore default values for missing observe options
bsunderhus a5256a2
refactor(react-overflow,priority-overflow): preserve OverflowManager …
bsunderhus d2a62d6
refactor(react-overflow): use ref-based lazy initializer for the over…
bsunderhus b14ca91
test(priority-overflow): guard setOptions partial input against false…
bsunderhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "feat: createOverflowManager accepts initialOptions, new setOptions method, observe now returns its cleanup, and the OverflowManagerOptions type is exported.", | ||
| "packageName": "@fluentui/priority-overflow", | ||
| "email": "bsunderhus@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "patch", | ||
| "comment": "fix: make Overflow container strict-mode safe", | ||
| "packageName": "@fluentui/react-overflow", | ||
| "email": "bsunderhus@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
packages/react-components/priority-overflow/src/overflowManager.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import { createOverflowManager } from './overflowManager'; | ||
| import type { ObserveOptions, OverflowEventPayload } from './types'; | ||
|
|
||
| describe('overflowManager', () => { | ||
| beforeAll(() => { | ||
| global.ResizeObserver = class ResizeObserver { | ||
| public observe() { | ||
| // do nothing | ||
| } | ||
|
|
||
| public unobserve() { | ||
| // do nothing | ||
| } | ||
|
|
||
| public disconnect() { | ||
| // do nothing | ||
| } | ||
| } as unknown as typeof ResizeObserver; | ||
| }); | ||
|
|
||
| const createElementWithSize = (tagName: string, width: number) => { | ||
| const element = document.createElement(tagName); | ||
| Object.defineProperty(element, 'offsetWidth', { configurable: true, value: width }); | ||
| Object.defineProperty(element, 'offsetHeight', { configurable: true, value: width }); | ||
|
|
||
| return element; | ||
| }; | ||
|
|
||
| const createContainer = (width: number) => { | ||
| const container = document.createElement('div'); | ||
| Object.defineProperty(container, 'clientWidth', { configurable: true, value: width }); | ||
| Object.defineProperty(container, 'clientHeight', { configurable: true, value: width }); | ||
|
|
||
| return container; | ||
| }; | ||
|
|
||
| const createObserveOptions = (options: Partial<ObserveOptions> = {}): ObserveOptions => ({ | ||
| overflowAxis: 'horizontal', | ||
| overflowDirection: 'end', | ||
| padding: 10, | ||
| minimumVisible: 0, | ||
| hasHiddenItems: false, | ||
| onUpdateItemVisibility: jest.fn(), | ||
| onUpdateOverflow: jest.fn(), | ||
| ...options, | ||
| }); | ||
|
|
||
| const lastDispatch = (onUpdateOverflow: jest.Mock): OverflowEventPayload => | ||
| onUpdateOverflow.mock.calls[onUpdateOverflow.mock.calls.length - 1][0]; | ||
|
|
||
| it('should dispatch overflow update after forceUpdate', () => { | ||
| const onUpdateOverflow = jest.fn(); | ||
| const options = createObserveOptions({ onUpdateOverflow }); | ||
| const manager = createOverflowManager(options); | ||
| const container = createContainer(100); | ||
| const itemA = createElementWithSize('button', 40); | ||
| const itemB = createElementWithSize('button', 40); | ||
| const menu = createElementWithSize('button', 20); | ||
|
|
||
| manager.addItem({ element: itemA, id: 'a', priority: 1 }); | ||
| manager.addItem({ element: itemB, id: 'b', priority: 0 }); | ||
| manager.addOverflowMenu(menu); | ||
| manager.observe(container); | ||
| manager.forceUpdate(); | ||
|
|
||
| const dispatch = lastDispatch(onUpdateOverflow); | ||
| expect(dispatch.visibleItems.map(item => item.id).sort()).toEqual(['a', 'b']); | ||
| expect(dispatch.invisibleItems).toEqual([]); | ||
| expect(dispatch.groupVisibility).toEqual({}); | ||
| }); | ||
|
|
||
| it('should re-dispatch when setOptions changes a relevant option', () => { | ||
| const onUpdateOverflow = jest.fn(); | ||
| const options = createObserveOptions({ onUpdateOverflow }); | ||
| const manager = createOverflowManager(options); | ||
| const container = createContainer(100); | ||
| const itemA = createElementWithSize('button', 40); | ||
| const itemB = createElementWithSize('button', 40); | ||
| const menu = createElementWithSize('button', 20); | ||
|
|
||
| manager.addItem({ element: itemA, id: 'a', priority: 1 }); | ||
| manager.addItem({ element: itemB, id: 'b', priority: 0 }); | ||
| manager.addOverflowMenu(menu); | ||
| manager.observe(container); | ||
| manager.forceUpdate(); | ||
|
|
||
| onUpdateOverflow.mockClear(); | ||
| manager.setOptions({ padding: 30 }); | ||
|
|
||
| expect(onUpdateOverflow).toHaveBeenCalled(); | ||
| const dispatch = lastDispatch(onUpdateOverflow); | ||
| expect(dispatch.visibleItems.map(item => item.id)).toEqual(['a']); | ||
| expect(dispatch.invisibleItems.map(item => item.id)).toEqual(['b']); | ||
| }); | ||
|
|
||
| it('should not re-dispatch when setOptions is called with a partial that does not change anything', () => { | ||
| const onUpdateOverflow = jest.fn(); | ||
| const options = createObserveOptions({ onUpdateOverflow }); | ||
| const manager = createOverflowManager(options); | ||
| const container = createContainer(100); | ||
| const itemA = createElementWithSize('button', 40); | ||
|
|
||
| manager.addItem({ element: itemA, id: 'a', priority: 1 }); | ||
| manager.observe(container, options); | ||
| manager.forceUpdate(); | ||
|
|
||
| onUpdateOverflow.mockClear(); | ||
| manager.setOptions({ padding: 10 }); // padding is already 10; no real change | ||
|
|
||
| expect(onUpdateOverflow).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('disconnect stops observation and re-observe restarts dispatching', () => { | ||
| const onUpdateOverflow = jest.fn(); | ||
| const options = createObserveOptions({ onUpdateOverflow }); | ||
| const manager = createOverflowManager(options); | ||
| const container = createContainer(100); | ||
| const item = createElementWithSize('button', 40); | ||
|
|
||
| manager.addItem({ element: item, id: 'a', priority: 1 }); | ||
| manager.observe(container); | ||
| manager.forceUpdate(); | ||
| expect(lastDispatch(onUpdateOverflow).visibleItems.map(i => i.id)).toEqual(['a']); | ||
|
|
||
| manager.disconnect(); | ||
| onUpdateOverflow.mockClear(); | ||
|
|
||
| manager.addItem({ element: item, id: 'a', priority: 1 }); | ||
| manager.observe(container); | ||
| manager.forceUpdate(); | ||
| expect(onUpdateOverflow).toHaveBeenCalled(); | ||
| expect(lastDispatch(onUpdateOverflow).visibleItems.map(i => i.id)).toEqual(['a']); | ||
| }); | ||
|
|
||
| it('should remove items through removeItem', () => { | ||
| const onUpdateOverflow = jest.fn(); | ||
| const options = createObserveOptions({ onUpdateOverflow }); | ||
| const manager = createOverflowManager(options); | ||
| const container = createContainer(100); | ||
| const item = createElementWithSize('button', 40); | ||
|
|
||
| manager.addItem({ element: item, id: 'a', priority: 1 }); | ||
| manager.observe(container); | ||
| manager.forceUpdate(); | ||
|
|
||
| expect(lastDispatch(onUpdateOverflow).visibleItems.map(i => i.id)).toEqual(['a']); | ||
|
|
||
| manager.removeItem('a'); | ||
| manager.forceUpdate(); | ||
|
|
||
| const dispatch = lastDispatch(onUpdateOverflow); | ||
| expect(dispatch.visibleItems).toEqual([]); | ||
| expect(dispatch.invisibleItems).toEqual([]); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.