import React, { useState } from 'react';
import { useArgs } from '@storybook/client-api';
import { ComponentStory, ComponentMeta } from '@storybook/react';
import { BLOCK_SIZES, DISPLAY } from '../../../helpers/constants/design-system';
import Box from '../../ui/box';
import {
ModalOverlay,
ModalContent,
ModalHeader,
Text,
Button,
ButtonLink,
BUTTON_LINK_SIZES,
TextFieldSearch,
IconName,
} from '..';
import { Modal } from './modal';
import README from './README.mdx';
export default {
title: 'Components/ComponentLibrary/Modal',
component: Modal,
parameters: {
docs: {
page: README,
},
},
argTypes: {
isOpen: {
control: 'boolean',
},
onClose: {
action: 'onClose',
},
children: {
control: 'node',
},
className: {
control: 'string',
},
isClosedOnOutsideClick: {
control: 'boolean',
},
isClosedOnEscapeKey: {
control: 'boolean',
},
initialFocusRef: {
control: 'object',
},
finalFocusRef: {
control: 'object',
},
restoreFocus: {
control: 'boolean',
},
autoFocus: {
control: 'boolean',
},
},
args: {
children: (
ModalContent children after ModalHeader
),
},
} as ComponentMeta;
const LoremIpsum = (props) => (
Lorem ipsum dolor sit amet, conse{' '}
random focusable button
{' '}
ctetur adipiscing elit. Phasellus posuere nunc enim, quis efficitur dolor
tempus viverra. Vivamus pharetra tempor pulvinar. Sed at dui in nisi
fermentum volutpat. Proin ut tortor quis eros tincidunt molestie.
Suspendisse dictum ex vitae metus consequat, et efficitur dolor luctus.
Integer ultricies hendrerit turpis sed faucibus. Nam pellentesque metus a
turpis sollicitudin vehicula. Phasellus rutrum luctus pulvinar. Phasellus
quis accumsan urna. Praesent justo erat, bibendum ac volutpat ac, placerat
in dui. Cras gravida mi et risus feugiat vulputate. Integer vulputate diam
eu vehicula euismod. In laoreet quis eros sed tincidunt. Pellentesque purus
dui, luctus id sem sit amet, varius congue dui
);
const Template: ComponentStory = (args) => {
const [{ isOpen }, updateArgs] = useArgs();
const [showLoremIpsum, setShowLoremIpsum] = useState(false);
const [showMoreModalContent, setShowMoreModalContent] = useState(false);
const handleOnClick = () => {
updateArgs({ isOpen: true });
};
const handleOnClose = () => {
updateArgs({ isOpen: false });
};
const handleHideLoremIpsum = () => {
setShowLoremIpsum(!showLoremIpsum);
};
const handleMoreContent = () => {
setShowMoreModalContent(!showMoreModalContent);
};
return (
{showLoremIpsum ? 'Hide' : 'Show'} scrollable content
Modal Header
{args.children}
Show more content to check scrolling
{showMoreModalContent ? 'Hide' : 'Show more'}
{showMoreModalContent && (
<>
>
)}
{showLoremIpsum && (
<>
>
)}
);
};
export const DefaultStory = Template.bind({});
DefaultStory.storyName = 'Default';
export const Usage = Template.bind({});
export const IsClosedOnOutsideClick = Template.bind({});
IsClosedOnOutsideClick.args = {
isClosedOnOutsideClick: false,
children: (
This Modal has set isClosedOnOutsideClick: false. Clicking outside this
Modal WILL NOT close it
),
};
export const IsClosedOnEscapeKey = Template.bind({});
IsClosedOnEscapeKey.args = {
isClosedOnEscapeKey: false,
children: (
This Modal has set isClosedOnEscapeKey: false. Pressing the ESC key{' '}
WILL NOT close it
),
};
export const InitialFocusRef: ComponentStory = (args) => {
const inputRef = React.useRef(null);
const [{ isOpen }, updateArgs] = useArgs();
const handleOnClick = () => {
updateArgs({ isOpen: true });
};
const handleOnClose = () => {
updateArgs({ isOpen: false });
};
return (
<>
Modal Header
{args.children}
>
);
};
InitialFocusRef.args = {
children: (
This Modal has set initialFocusRef to the TextFieldSearch component. When
the Modal opens, the TextFieldSearch component will be focused.
),
};
export const FinalFocusRef: ComponentStory = (args) => {
const buttonRef = React.useRef(null);
const [{ isOpen }, updateArgs] = useArgs();
const handleOnClick = () => {
updateArgs({ isOpen: true });
};
const handleOnClose = () => {
updateArgs({ isOpen: false });
};
return (
<>
Modal Header
{args.children}
>
);
};
FinalFocusRef.args = {
children: (
This Modal has set finalFocusRef to the second button element. When the
Modal closes, the second button component will be focused. Use keyboard
navigation to see it clearly.
),
};
export const RestoreFocus = Template.bind({});
RestoreFocus.args = {
restoreFocus: true,
children: (
This Modal has set restoreFocus: true. When the Modal closes, the Button
component will be focused. Use keyboard navigation to see it clearly.
),
};
export const AutoFocus = Template.bind({});
AutoFocus.args = {
autoFocus: false,
children: (
This Modal has set autoFocus: false. When the Modal opens the first
element to focus WILL NOT be the first focusable element
in the Modal.
),
};