-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
onOverflowChange
callback (#33106)
- Loading branch information
Showing
7 changed files
with
194 additions
and
9 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-overflow-a31beb0d-76b2-491e-add6-1f3fd27db85f.json
This file contains 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: add `onOverflowChange` callback", | ||
"packageName": "@fluentui/react-overflow", | ||
"email": "lingfangao@hotmail.com", | ||
"dependentChangeType": "patch" | ||
} |
This file contains 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 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 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 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
121 changes: 121 additions & 0 deletions
121
packages/react-components/react-overflow/stories/src/Overflow/ListenToChanges.stories.tsx
This file contains 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,121 @@ | ||
import * as React from 'react'; | ||
import { | ||
makeStyles, | ||
Button, | ||
Menu, | ||
MenuTrigger, | ||
MenuPopover, | ||
MenuList, | ||
MenuItem, | ||
MenuButton, | ||
tokens, | ||
mergeClasses, | ||
Overflow, | ||
OverflowItem, | ||
OverflowItemProps, | ||
useIsOverflowItemVisible, | ||
useOverflowMenu, | ||
} from '@fluentui/react-components'; | ||
|
||
const useStyles = makeStyles({ | ||
container: { | ||
display: 'flex', | ||
flexWrap: 'nowrap', | ||
minWidth: 0, | ||
overflow: 'hidden', | ||
}, | ||
|
||
resizableArea: { | ||
minWidth: '200px', | ||
maxWidth: '800px', | ||
border: `2px solid ${tokens.colorBrandBackground}`, | ||
padding: '20px 10px 10px 10px', | ||
position: 'relative', | ||
resize: 'horizontal', | ||
'::after': { | ||
content: `'Resizable Area'`, | ||
position: 'absolute', | ||
padding: '1px 4px 1px', | ||
top: '-2px', | ||
left: '-2px', | ||
fontFamily: 'monospace', | ||
fontSize: '15px', | ||
fontWeight: 900, | ||
lineHeight: 1, | ||
letterSpacing: '1px', | ||
color: tokens.colorNeutralForegroundOnBrand, | ||
backgroundColor: tokens.colorBrandBackground, | ||
}, | ||
}, | ||
}); | ||
|
||
export const ListenToChanges = () => { | ||
const styles = useStyles(); | ||
|
||
const itemIds = new Array(8).fill(0).map((_, i) => i.toString()); | ||
const [overflowState, setOverflowState] = React.useState<object>({}); | ||
|
||
return ( | ||
<> | ||
<Overflow onOverflowChange={(e, data) => setOverflowState(data)}> | ||
<div className={mergeClasses(styles.container, styles.resizableArea)}> | ||
{itemIds.map(i => ( | ||
<OverflowItem key={i} id={i}> | ||
<Button>Item {i}</Button> | ||
</OverflowItem> | ||
))} | ||
<OverflowMenu itemIds={itemIds} /> | ||
</div> | ||
</Overflow> | ||
<pre>{JSON.stringify(overflowState, null, 2)}</pre> | ||
</> | ||
); | ||
}; | ||
|
||
const OverflowMenuItem: React.FC<Pick<OverflowItemProps, 'id'>> = props => { | ||
const { id } = props; | ||
const isVisible = useIsOverflowItemVisible(id); | ||
|
||
if (isVisible) { | ||
return null; | ||
} | ||
|
||
// As an union between button props and div props may be conflicting, casting is required | ||
return <MenuItem>Item {id}</MenuItem>; | ||
}; | ||
|
||
const OverflowMenu: React.FC<{ itemIds: string[] }> = ({ itemIds }) => { | ||
const { ref, overflowCount, isOverflowing } = useOverflowMenu<HTMLButtonElement>(); | ||
|
||
if (!isOverflowing) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<Menu> | ||
<MenuTrigger disableButtonEnhancement> | ||
<MenuButton ref={ref}>+{overflowCount} items</MenuButton> | ||
</MenuTrigger> | ||
|
||
<MenuPopover> | ||
<MenuList> | ||
{itemIds.map(i => { | ||
return <OverflowMenuItem key={i} id={i} />; | ||
})} | ||
</MenuList> | ||
</MenuPopover> | ||
</Menu> | ||
); | ||
}; | ||
|
||
ListenToChanges.parameters = { | ||
docs: { | ||
description: { | ||
story: [ | ||
'You can listen to changes with the `onOnverflowChange` prop which will return the overflow', | ||
'state. This can be useful when you have other UI features that need to be triggered on changes', | ||
'to item visibility.', | ||
].join('\n'), | ||
}, | ||
}, | ||
}; |
This file contains 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