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

Add Storybook story for useDetectClickOutside hook with several demos #4923

Merged
merged 2 commits into from
Jun 28, 2023
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
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>
),
],
};