diff --git a/src/core/Popover/Popover.stories.tsx b/src/core/Popover/Popover.stories.tsx index 77bac40c..b30d3699 100644 --- a/src/core/Popover/Popover.stories.tsx +++ b/src/core/Popover/Popover.stories.tsx @@ -80,45 +80,28 @@ storiesOf('core/Popover', module) return ( - Status: {isOpen ? 'opened' : 'closed'} setIsOpen(!isOpen)} + onClickOutside={(_, event) => { + const target = event.target as HTMLElement; + if (target.textContent !== 'Show popover') { + setIsOpen(false); + } + }} > - -

- Note: you can take complete control over the Popover by using the{' '} - isOpen prop. Once you make it true or{' '} - false the hover behavior will be disabled. -

-
- ); - }) - - .add('on click outside', () => { - const [isOpen, setIsOpen] = useState(false); - - return ( - - Status: {isOpen ? 'opened' : 'closed'} - setIsOpen(false)} - target="Open" - tag="div" - className="text-center" - > - - - +

Status: {isOpen ? 'opened' : 'closed'}

+

Note: you can take complete control over the Popover by using the{' '} - isOpen prop. Once you make it true or{' '} - false the hover behavior will be disabled. + isOpen and onClick props. Once you change{' '} + isOpen to true or false, the + hover behavior will be disabled.

In combination with onClickOutside you can close the diff --git a/src/core/Popover/Popover.tsx b/src/core/Popover/Popover.tsx index 72115448..712b1121 100644 --- a/src/core/Popover/Popover.tsx +++ b/src/core/Popover/Popover.tsx @@ -1,5 +1,5 @@ import React, { CSSProperties } from 'react'; -import Tippy from '@tippyjs/react'; +import Tippy, { TippyProps } from '@tippyjs/react'; import { TippyPlacement } from '../types'; type Props = { @@ -17,11 +17,17 @@ type Props = { */ openOnClick?: boolean; + /** + * Optional callback that gets triggered when the target is clicked. + * Is useful for when wanting to take complete control over the popover. + */ + onClick?: () => void; + /** * Optionally callback that gets triggered when clicked outside the popover. * Is useful for when wanting to take complete control over the popover. */ - onClickOutside?: () => void; + onClickOutside?: TippyProps['onClickOutside']; /** * Content shown inside the popover. @@ -88,6 +94,7 @@ export function Popover({ tag = 'span', className, isOpen, + onClick, openOnClick, onClickOutside, style, @@ -108,7 +115,13 @@ export function Popover({ maxWidth={maxWidth} trigger={openOnClick ? 'click' : 'mouseenter focus'} > - + {target}