-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat: implement auto reflow for MessageBar layouts #29328
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
ling1726
merged 20 commits into
microsoft:master
from
ling1726:react-message-bar/feat/auto-reflow
Sep 29, 2023
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
102759c
feat: Initial implementation
ling1726 35d1189
resolve conflict
ling1726 a75a536
remove TODOs
ling1726 aeacd98
fix multiline alignment
ling1726 cf9c216
fix slot type misalign
ling1726 b482283
fix tests
ling1726 91ffc5e
rename to secondaryActions
ling1726 824404f
fix warnings
ling1726 dbc3145
break out into components
ling1726 0c03bfa
remove stories
ling1726 038781d
revert tsconfig changes
ling1726 47d8d31
Merge remote-tracking branch 'origin/master' into react-message-bar/f…
ling1726 c58ee70
remove outdated keyborg cypress test
ling1726 823248d
add keyborg test again
ling1726 e328ff0
add tsconfig paths to cypress
ling1726 076d08f
feat: Implement auto reflow for MessageBar layouts
ling1726 812c351
merge master
ling1726 9226725
update api
ling1726 be973b4
pr comments
ling1726 7ccaa4f
cleanup
ling1726 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
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
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
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
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
88 changes: 88 additions & 0 deletions
88
...act-components/react-message-bar-preview/src/components/MessageBar/useMessageBarReflow.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,88 @@ | ||
| import * as React from 'react'; | ||
| import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts'; | ||
| import { isHTMLElement } from '@fluentui/react-utilities'; | ||
|
|
||
| export function useMessageBarReflow(enabled: boolean = false) { | ||
| const { targetDocument } = useFluent(); | ||
| const forceUpdate = React.useReducer(() => ({}), {})[1]; | ||
| const reflowingRef = React.useRef(false); | ||
| const resizeObserverRef = React.useRef<ResizeObserver | null>(null); | ||
| const prevInlineSizeRef = React.useRef(-1); | ||
|
|
||
| const handleResize: ResizeObserverCallback = React.useCallback( | ||
| entries => { | ||
| // Resize observer is only owned by this component - one resize observer entry expected | ||
| // No need to support mutliple fragments - one border box entry expected | ||
| if (process.env.NODE_ENV !== 'production' && entries.length > 1) { | ||
| // eslint-disable-next-line no-console | ||
| console.error( | ||
| [ | ||
| 'useMessageBarReflow: Resize observer should only have one entry. ', | ||
| 'If multiple entries are observed, the first entry will be used.', | ||
| 'This is a bug, please report it to the Fluent UI team.', | ||
| ].join(' '), | ||
| ); | ||
| } | ||
|
|
||
| const entry = entries[0]; | ||
| const borderBoxSize = entry?.borderBoxSize[0]; | ||
| if (!borderBoxSize || !entry) { | ||
| return; | ||
| } | ||
|
|
||
| const { inlineSize } = borderBoxSize; | ||
| const { target } = entry; | ||
|
|
||
| if (!isHTMLElement(target)) { | ||
| return; | ||
| } | ||
|
|
||
| let nextReflowing: boolean | undefined; | ||
|
|
||
| // No easy way to really determine when the single line layout will fit | ||
| // Just keep try to set single line layout as long as the size is growing | ||
| // Will cause flickering when size is being adjusted gradually (i.e. drag) - but this should not be a common case | ||
| if (reflowingRef.current) { | ||
| if (prevInlineSizeRef.current < inlineSize) { | ||
| nextReflowing = false; | ||
| } | ||
| } else { | ||
| const scrollWidth = target.scrollWidth; | ||
| if (inlineSize < scrollWidth) { | ||
| nextReflowing = true; | ||
| } | ||
| } | ||
|
|
||
| prevInlineSizeRef.current = inlineSize; | ||
| if (typeof nextReflowing !== 'undefined' && reflowingRef.current !== nextReflowing) { | ||
| reflowingRef.current = nextReflowing; | ||
| forceUpdate(); | ||
| } | ||
|
ling1726 marked this conversation as resolved.
|
||
| }, | ||
| [forceUpdate], | ||
| ); | ||
|
|
||
| const ref = React.useCallback( | ||
| (el: HTMLElement | null) => { | ||
| if (!enabled || !el || !targetDocument?.defaultView) { | ||
| return; | ||
| } | ||
|
|
||
|
ling1726 marked this conversation as resolved.
|
||
| resizeObserverRef.current?.disconnect(); | ||
|
|
||
| const win = targetDocument.defaultView; | ||
| const resizeObserver = new win.ResizeObserver(handleResize); | ||
| resizeObserverRef.current = resizeObserver; | ||
| resizeObserver.observe(el, { box: 'border-box' }); | ||
| }, | ||
| [targetDocument, handleResize, enabled], | ||
| ); | ||
|
|
||
|
ling1726 marked this conversation as resolved.
|
||
| React.useEffect(() => { | ||
| return () => { | ||
| resizeObserverRef.current?.disconnect(); | ||
| }; | ||
| }, []); | ||
|
|
||
| return { ref, reflowing: reflowingRef.current }; | ||
| } | ||
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
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
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
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
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
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
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
2 changes: 1 addition & 1 deletion
2
packages/react-components/react-message-bar-preview/src/contexts/messageBarContext.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
32 changes: 32 additions & 0 deletions
32
...es/react-components/react-message-bar-preview/stories/MessageBar/ManualLayout.stories.tsx
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,32 @@ | ||
| import * as React from 'react'; | ||
| import { Button, Link, Switch } from '@fluentui/react-components'; | ||
| import { DismissRegular } from '@fluentui/react-icons'; | ||
| import { MessageBar, MessageBarActions, MessageBarBody, MessageBarTitle } from '@fluentui/react-message-bar-preview'; | ||
|
|
||
| const intents = ['info', 'warning', 'error', 'success'] as const; | ||
| export const ManualLayout = () => { | ||
| const [single, setSingle] = React.useState(true); | ||
| return ( | ||
| <> | ||
| <Switch | ||
| label={single ? 'Single line layout' : 'Multi line layout'} | ||
| checked={single} | ||
| onChange={(_, { checked }) => setSingle(checked)} | ||
| /> | ||
| <div style={{ display: 'flex', flexDirection: 'column', gap: '10px' }}> | ||
| {intents.map(intent => ( | ||
| <MessageBar key={intent} layout={single ? 'singleline' : 'multiline'} intent={intent}> | ||
|
ling1726 marked this conversation as resolved.
|
||
| <MessageBarBody> | ||
| <MessageBarTitle>Descriptive title</MessageBarTitle> | ||
| Message providing information to the user with actionable insights. <Link>Link</Link> | ||
| </MessageBarBody> | ||
| <MessageBarActions containerAction={<Button appearance="transparent" icon={<DismissRegular />} />}> | ||
| <Button>Action</Button> | ||
| <Button>Action</Button> | ||
| </MessageBarActions> | ||
| </MessageBar> | ||
| ))} | ||
| </div> | ||
| </> | ||
| ); | ||
| }; | ||
23 changes: 0 additions & 23 deletions
23
packages/react-components/react-message-bar-preview/stories/MessageBar/Multiline.stories.tsx
This file was deleted.
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.