Skip to content

Commit

Permalink
Add Storybook story for useDetectClickOutside hook with several demos (
Browse files Browse the repository at this point in the history
  • Loading branch information
sneridagh authored Jun 28, 2023
1 parent fe38829 commit c1810b7
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/4923.internal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add Storybook story for useDetectClickOutside hook with several demos @sneridagh
191 changes: 191 additions & 0 deletions src/helpers/Utils/UseDetectClickOutside.stories.jsx
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>
),
],
};

0 comments on commit c1810b7

Please sign in to comment.