-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[components] Add MenuButton for consistent dropdown menu behavior
- Loading branch information
1 parent
1c25dd1
commit 8c78720
Showing
10 changed files
with
228 additions
and
129 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './menuButton' |
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,42 @@ | ||
import Button from 'part:@sanity/components/buttons/default' | ||
import {ClickOutside} from 'part:@sanity/components/click-outside' | ||
import {Popover} from 'part:@sanity/components/popover' | ||
import React, {useCallback} from 'react' | ||
|
||
interface MenuButtonProps { | ||
// @todo: typings | ||
buttonProps?: any | ||
menu?: React.ReactNode | ||
placement?: string | ||
open?: boolean | ||
setOpen?: (val: boolean) => void | ||
} | ||
|
||
export function MenuButton(props: MenuButtonProps & React.HTMLProps<HTMLDivElement>) { | ||
const {buttonProps, children, menu, open, placement, setOpen, ...restProps} = props | ||
const handleClickOutside = useCallback(() => { | ||
if (!setOpen) return | ||
setOpen(false) | ||
}, [setOpen]) | ||
|
||
const handleButtonClick = useCallback(() => { | ||
if (!setOpen) return | ||
setOpen(!open) | ||
}, [open, setOpen]) | ||
|
||
return ( | ||
<ClickOutside onClickOutside={handleClickOutside}> | ||
{ref => ( | ||
<div {...restProps} ref={ref}> | ||
<Popover content={menu} open={open} placement={placement}> | ||
<div> | ||
<Button {...buttonProps} onClick={handleButtonClick}> | ||
{children} | ||
</Button> | ||
</div> | ||
</Popover> | ||
</div> | ||
)} | ||
</ClickOutside> | ||
) | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/@sanity/components/src/menuButton/stories/default.js
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,44 @@ | ||
import {MenuButton} from 'part:@sanity/components/menu-button' | ||
import {action} from 'part:@sanity/storybook/addons/actions' | ||
import {boolean, select} from 'part:@sanity/storybook/addons/knobs' | ||
import Sanity from 'part:@sanity/storybook/addons/sanity' | ||
import {CenteredContainer} from 'part:@sanity/storybook/components' | ||
import React from 'react' | ||
|
||
export function DefaultStory() { | ||
const open = boolean('Open', false, 'Props') | ||
const placement = select( | ||
'Placement', | ||
{ | ||
'top-start': 'Top start', | ||
top: 'Top', | ||
'top-end': 'Top end', | ||
'right-start': 'Right start', | ||
right: 'Right', | ||
'right-end': 'Right end', | ||
'bottom-start': 'Bottom start', | ||
bottom: 'Bottom', | ||
'bottom-end': 'Bottom end', | ||
'left-start': 'Left start', | ||
left: 'Left', | ||
'left-end': 'Left end' | ||
}, | ||
'bottom', | ||
'Props' | ||
) | ||
|
||
return ( | ||
<CenteredContainer> | ||
<Sanity part="part:@sanity/components/dialogs/default" propTables={[MenuButton]}> | ||
<MenuButton | ||
menu={<div style={{padding: 20}}>This is the menu</div>} | ||
open={open} | ||
placement={placement} | ||
setOpen={action('setOpen')} | ||
> | ||
Open menu | ||
</MenuButton> | ||
</Sanity> | ||
</CenteredContainer> | ||
) | ||
} |
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 @@ | ||
import {storiesOf} from 'part:@sanity/storybook' | ||
import {withKnobs} from 'part:@sanity/storybook/addons/knobs' | ||
import {DefaultStory} from './stories/default' | ||
|
||
storiesOf('@sanity/components/menu-button', module) | ||
.addDecorator(withKnobs) | ||
.add('Default', DefaultStory) |
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
Oops, something went wrong.