Skip to content
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

feat(core): creates props for a component that opens the popup while double click #83

Merged
merged 3 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ connect components easily:
- `bindToggle`: creates props for a component that toggles the popup when clicked.
- `bindHover`: creates props for a component that opens the popup while hovered.
**NOTE**: See [this guidance](#using-popover-and-menu-with-bindhover) if you are using `bindHover` with `Popover` or `Menu`.
- `bindFocus`: creates props for a component that opens the popup while hovered.
- `bindFocus`: creates props for a component that opens the popup while focus.
- `bindDoubleClick`: creates props for a component that opens the popup while double click.

To use one of these functions, you should call it with the object
returned by `usePopupState` and spread the return value into the desired
Expand Down
12 changes: 12 additions & 0 deletions demo/Root.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import FocusPopover from './examples/FocusPopover'
import FocusPopoverCode from '!!raw-loader!./examples/FocusPopover'
import FocusPopoverHooks from './examples/FocusPopover.hooks'
import FocusPopoverHooksCode from '!!raw-loader!./examples/FocusPopover.hooks'
import DoubleClickPopover from './examples/DoubleClickPopover'
import DoubleClickPopoverCode from '!!raw-loader!./examples/DoubleClickPopover'
import DoubleClickPopoverHooks from './examples/DoubleClickPopover.hooks'
import DoubleClickPopoverHooksCode from '!!raw-loader!./examples/DoubleClickPopover.hooks'
import HoverMenu from './examples/HoverMenu'
import HoverMenuCode from '!!raw-loader!./examples/HoverMenu'
import HoverMenuHooks from './examples/HoverMenu.hooks'
Expand Down Expand Up @@ -106,6 +110,14 @@ const Root = ({ classes }) => (
hooksExample={<FocusPopoverHooks />}
hooksCode={FocusPopoverHooksCode}
/>
<Demo
title="DoubleClick Poper"
headerId="doubleClick-poper"
example={<DoubleClickPopover />}
code={DoubleClickPopoverCode}
hooksExample={<DoubleClickPopoverHooks />}
hooksCode={DoubleClickPopoverHooksCode}
/>
<Demo
title="Hover Menu"
headerId="hover-menu"
Expand Down
41 changes: 41 additions & 0 deletions demo/examples/DoubleClickPopover.hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react'
import Typography from '@mui/material/Typography'
import Popover from '@mui/material/Popover'
import Button from '@mui/material/Button'
import {
usePopupState,
bindDoubleClick,
bindPopover,
} from 'material-ui-popup-state/hooks'

const DoubleClickPoperPopupState = ({ classes }) => {
const popupState = usePopupState({
variant: 'popover',
popupId: 'demoPopover',
})

return (
<div>
<Button {...bindDoubleClick(popupState)} variant="text">
Double click to open poper
</Button>
<Popover
{...bindPopover(popupState)}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'center',
}}
>
<Typography style={{ margin: 10 }}>
The content of the Popover.
</Typography>
</Popover>
</div>
)
}

export default DoubleClickPoperPopupState
38 changes: 38 additions & 0 deletions demo/examples/DoubleClickPopover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react'
import Typography from '@mui/material/Typography'
import Popover from '@mui/material/Popover'
import Button from '@mui/material/Button'
import PopupState, {
bindDoubleClick,
bindPopper,
} from 'material-ui-popup-state'
import { ClickAwayListener } from '@mui/material'

const DoubleClickPoperPopupState = ({ classes }) => (
<PopupState variant="popover" popupId="demoPopover">
{(popupState) => (
<ClickAwayListener onClickAway={popupState.close}>
<Button {...bindDoubleClick(popupState)} variant="text">
Double click to open poper
</Button>
<Popover
{...bindPopper(popupState)}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'center',
}}
>
<Typography style={{ margin: 10 }}>
The content of the Popover.
</Typography>
</Popover>
</ClickAwayListener>
)}
</PopupState>
)

export default DoubleClickPoperPopupState
13 changes: 13 additions & 0 deletions src/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ export function bindFocus(popupState: PopupState): ControlAriaProps & {
onBlur: (event: FocusEvent<any>) => void
}

/**
* Creates props for a component that opens the popup while double click.
*
* @param {object} popupState the argument passed to the child function of
* `PopupState`
*/
export function bindDoubleClick(popupState: PopupState): {
'aria-controls'?: string
'aria-describedby'?: string
'aria-haspopup': true | undefined
onDoubleClick: (event: SyntheticEvent<any>) => any
}

/**
* Creates props for a `Popover` component.
*
Expand Down
27 changes: 27 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,33 @@ export function bindFocus(popupState: PopupState): {|
}
}

/**
* Creates props for a component that opens the popup while double click.
*
* @param {object} popupState the argument passed to the child function of
* `PopupState`
*/
export function bindDoubleClick({
isOpen,
open,
popupId,
variant,
}: PopupState): {|
'aria-controls'?: ?string,
'aria-describedby'?: ?string,
'aria-haspopup': ?true,
onDoubleClick: (event: SyntheticEvent<any>) => any,
|} {
return {
// $FlowFixMe
[variant === 'popover' ? 'aria-controls' : 'aria-describedby']: isOpen
? popupId
: null,
'aria-haspopup': variant === 'popover' ? true : undefined,
onDoubleClick: open,
}
}

/**
* Creates props for a `Popover` component.
*
Expand Down
2 changes: 2 additions & 0 deletions src/hooks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
bindToggle,
bindHover,
bindFocus,
bindDoubleClick,
bindMenu,
bindPopover,
bindPopper,
Expand All @@ -20,6 +21,7 @@ export {
bindToggle,
bindHover,
bindFocus,
bindDoubleClick,
bindMenu,
bindPopover,
bindPopper,
Expand Down
2 changes: 2 additions & 0 deletions src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
bindToggle,
bindHover,
bindFocus,
bindDoubleClick,
bindMenu,
bindPopover,
bindPopper,
Expand All @@ -33,6 +34,7 @@ export {
bindToggle,
bindHover,
bindFocus,
bindDoubleClick,
bindMenu,
bindPopover,
bindPopper,
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
bindMenu,
bindPopover,
bindPopper,
bindDoubleClick,
bindDialog,
type Variant,
type CoreState,
Expand All @@ -32,6 +33,7 @@ export {
bindMenu,
bindPopover,
bindPopper,
bindDoubleClick,
bindDialog,
}
export type { Variant, InjectedProps }
Expand Down