diff --git a/news/4923.internal b/news/4923.internal new file mode 100644 index 0000000000..514988a530 --- /dev/null +++ b/news/4923.internal @@ -0,0 +1 @@ +Add Storybook story for useDetectClickOutside hook with several demos @sneridagh diff --git a/src/helpers/Utils/UseDetectClickOutside.stories.jsx b/src/helpers/Utils/UseDetectClickOutside.stories.jsx new file mode 100644 index 0000000000..702af0ca65 --- /dev/null +++ b/src/helpers/Utils/UseDetectClickOutside.stories.jsx @@ -0,0 +1,191 @@ +import React from 'react'; +import { useDetectClickOutside } from './useDetectClickOutside'; +import { Portal } from 'react-portal'; +import { usePopper } from 'react-popper'; +import { BlockChooser } from '@plone/volto/components'; + +function OpenedChooser(props) { + const blockChooserRef = useDetectClickOutside({ + onTriggered: () => props.setOpenMenu(false), + triggerKeys: ['Escape'], + }); + + return ( +
+ Hello +
+ ); +} + +function TestComponent(props) { + const [isOpenMenu, setOpenMenu] = React.useState(false); + + return ( +
+ + {isOpenMenu && } +
+ ); +} + +function StoryComponent(args) { + return ( + <> + + + + + ); +} + +function OpenedChooserWithPortal(props) { + const blockChooserRef = useDetectClickOutside({ + onTriggered: () => props.setOpenMenu(false), + triggerKeys: ['Escape'], + }); + + return ( + +
{`Hello ${props.id}`}
+
+ ); +} + +function TestComponentWithPortal(props) { + const [isOpenMenu, setOpenMenu] = React.useState(false); + + return ( +
+ + {isOpenMenu && ( + + )} +
+ ); +} + +function StoryComponentWithPortal(args) { + return ( + <> + + + + + ); +} + +function OpenedChooserWithPortalAndPopper(props) { + const { showBlockChooser } = props; + + const blockChooserRef = useDetectClickOutside({ + onTriggered: () => props.setOpenMenu(false), + triggerKeys: ['Escape'], + }); + + return showBlockChooser ? ( + + ) : ( +
{`Hello ${props.id}`}
+ ); +} + +function TestComponentWithPortalAndPopper(props) { + const [isOpenMenu, setOpenMenu] = React.useState(false); + const [referenceElement, setReferenceElement] = React.useState(null); + const [popperElement, setPopperElement] = React.useState(null); + const { styles, attributes } = usePopper(referenceElement, popperElement, { + placement: 'right', + modifiers: [ + { + name: 'offset', + options: { + offset: [-10, 10], + }, + }, + { + name: 'flip', + options: { + fallbackPlacements: ['top-start'], + }, + }, + ], + }); + return ( +
+ + +
+ {isOpenMenu && ( + + )} +
+
+
+ ); +} + +function StoryComponentWithPortalAndPopper(args) { + const { showBlockChooser } = args; + return ( + <> + + + + + ); +} + +export const Default = StoryComponent.bind({}); +Default.args = {}; + +export const WithPortal = StoryComponentWithPortal.bind({}); +WithPortal.args = {}; + +export const WithPortalAndPopper = StoryComponentWithPortalAndPopper.bind({}); +WithPortalAndPopper.args = {}; + +export const WithPortalAndPopperUsingBlockChooser = StoryComponentWithPortalAndPopper.bind( + {}, +); +WithPortalAndPopperUsingBlockChooser.args = { + showBlockChooser: true, +}; + +export default { + title: 'Internal Components/useDetectClickOutside', + component: TestComponent, + decorators: [ + (Story) => ( +
+ +
+ ), + ], +};