-
-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Storybook story for useDetectClickOutside hook with several demos (…
- Loading branch information
Showing
2 changed files
with
192 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add Storybook story for useDetectClickOutside hook with several demos @sneridagh |
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,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 ( | ||
<div ref={blockChooserRef} style={{ marginLeft: '20px' }}> | ||
Hello | ||
</div> | ||
); | ||
} | ||
|
||
function TestComponent(props) { | ||
const [isOpenMenu, setOpenMenu] = React.useState(false); | ||
|
||
return ( | ||
<div style={{ display: 'flex', marginBottom: '20px' }}> | ||
<button onClick={() => setOpenMenu(true)}>Click me</button> | ||
{isOpenMenu && <OpenedChooser setOpenMenu={setOpenMenu} />} | ||
</div> | ||
); | ||
} | ||
|
||
function StoryComponent(args) { | ||
return ( | ||
<> | ||
<TestComponent /> | ||
<TestComponent /> | ||
<TestComponent /> | ||
</> | ||
); | ||
} | ||
|
||
function OpenedChooserWithPortal(props) { | ||
const blockChooserRef = useDetectClickOutside({ | ||
onTriggered: () => props.setOpenMenu(false), | ||
triggerKeys: ['Escape'], | ||
}); | ||
|
||
return ( | ||
<Portal node={document.getElementById('body')}> | ||
<div ref={blockChooserRef}>{`Hello ${props.id}`}</div> | ||
</Portal> | ||
); | ||
} | ||
|
||
function TestComponentWithPortal(props) { | ||
const [isOpenMenu, setOpenMenu] = React.useState(false); | ||
|
||
return ( | ||
<div style={{ display: 'flex', marginBottom: '20px' }}> | ||
<button | ||
onClick={() => setOpenMenu(true)} | ||
>{`Click me ${props.id}`}</button> | ||
{isOpenMenu && ( | ||
<OpenedChooserWithPortal {...props} setOpenMenu={setOpenMenu} /> | ||
)} | ||
</div> | ||
); | ||
} | ||
|
||
function StoryComponentWithPortal(args) { | ||
return ( | ||
<> | ||
<TestComponentWithPortal id={1} /> | ||
<TestComponentWithPortal id={2} /> | ||
<TestComponentWithPortal id={3} /> | ||
</> | ||
); | ||
} | ||
|
||
function OpenedChooserWithPortalAndPopper(props) { | ||
const { showBlockChooser } = props; | ||
|
||
const blockChooserRef = useDetectClickOutside({ | ||
onTriggered: () => props.setOpenMenu(false), | ||
triggerKeys: ['Escape'], | ||
}); | ||
|
||
return showBlockChooser ? ( | ||
<BlockChooser | ||
// onMutateBlock={onMutateBlock} | ||
// currentBlock={block} | ||
showRestricted | ||
// blocksConfig={blocksConfig} | ||
ref={blockChooserRef} | ||
/> | ||
) : ( | ||
<div ref={blockChooserRef}>{`Hello ${props.id}`}</div> | ||
); | ||
} | ||
|
||
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 ( | ||
<div style={{ display: 'flex', marginBottom: '20px' }}> | ||
<button | ||
ref={setReferenceElement} | ||
onClick={() => setOpenMenu(true)} | ||
>{`Click me ${props.id}`}</button> | ||
<Portal node={document.getElementById('body')}> | ||
<div | ||
ref={setPopperElement} | ||
style={styles.popper} | ||
{...attributes.popper} | ||
> | ||
{isOpenMenu && ( | ||
<OpenedChooserWithPortalAndPopper | ||
{...props} | ||
setOpenMenu={setOpenMenu} | ||
/> | ||
)} | ||
</div> | ||
</Portal> | ||
</div> | ||
); | ||
} | ||
|
||
function StoryComponentWithPortalAndPopper(args) { | ||
const { showBlockChooser } = args; | ||
return ( | ||
<> | ||
<TestComponentWithPortalAndPopper | ||
id={1} | ||
showBlockChooser={showBlockChooser} | ||
/> | ||
<TestComponentWithPortalAndPopper | ||
id={2} | ||
showBlockChooser={showBlockChooser} | ||
/> | ||
<TestComponentWithPortalAndPopper | ||
id={3} | ||
showBlockChooser={showBlockChooser} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
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) => ( | ||
<div style={{ width: '600px' }}> | ||
<Story /> | ||
</div> | ||
), | ||
], | ||
}; |