|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import type { Meta, StoryObj } from "@storybook/react"; |
| 6 | + |
| 7 | +import { useOverlayTriggerState } from "react-stately"; |
| 8 | +import { useOverlayTrigger } from "react-aria"; |
| 9 | +import Image from "next/image"; |
| 10 | +import Illustration from "../assets/modal-default-img.svg"; |
| 11 | +import { ModalOverlay } from "../dialog/ModalOverlay"; |
| 12 | +import { Dialog } from "../dialog/Dialog"; |
| 13 | +import { Button } from "../Button"; |
| 14 | + |
| 15 | +export type ModalDialogProps = { |
| 16 | + withDialog?: boolean; |
| 17 | + withTitle?: boolean; |
| 18 | + withIllustration?: boolean; |
| 19 | +}; |
| 20 | + |
| 21 | +const ModalDialog = (props: ModalDialogProps) => { |
| 22 | + const modalState = useOverlayTriggerState({ defaultOpen: true }); |
| 23 | + const modalTrigger = useOverlayTrigger({ type: "dialog" }, modalState); |
| 24 | + return ( |
| 25 | + <> |
| 26 | + <Button {...modalTrigger.triggerProps} variant="primary"> |
| 27 | + Open modal {props.withDialog && " dialog"} |
| 28 | + </Button> |
| 29 | + {modalState.isOpen && ( |
| 30 | + <ModalOverlay |
| 31 | + state={modalState} |
| 32 | + {...modalTrigger.overlayProps} |
| 33 | + isDismissable={true} |
| 34 | + > |
| 35 | + {props.withDialog ? ( |
| 36 | + <Dialog |
| 37 | + title={props.withTitle ? "Here's a dialog for you" : undefined} |
| 38 | + illustration={ |
| 39 | + props.withIllustration ? ( |
| 40 | + <Image src={Illustration} alt="" /> |
| 41 | + ) : undefined |
| 42 | + } |
| 43 | + onDismiss={() => modalState.close()} |
| 44 | + > |
| 45 | + <p>Some dialog content.</p> |
| 46 | + </Dialog> |
| 47 | + ) : ( |
| 48 | + <> |
| 49 | + <p> |
| 50 | + This is modal content; note that it's not possible to |
| 51 | + interact with the content behind the modal overlay. Here's |
| 52 | + a button to close the modal: |
| 53 | + </p> |
| 54 | + <Button variant="primary" onPress={() => modalState.close()}> |
| 55 | + Close modal |
| 56 | + </Button> |
| 57 | + </> |
| 58 | + )} |
| 59 | + </ModalOverlay> |
| 60 | + )} |
| 61 | + </> |
| 62 | + ); |
| 63 | +}; |
| 64 | + |
| 65 | +// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction |
| 66 | +const meta: Meta<typeof ModalDialog> = { |
| 67 | + title: "Design Systems/Molecules/Modal dialog", |
| 68 | + component: ModalDialog, |
| 69 | + argTypes: { |
| 70 | + withDialog: { |
| 71 | + control: "boolean", |
| 72 | + name: "With dialog", |
| 73 | + }, |
| 74 | + withTitle: { |
| 75 | + control: "boolean", |
| 76 | + name: "With title", |
| 77 | + }, |
| 78 | + withIllustration: { |
| 79 | + control: "boolean", |
| 80 | + name: "With illustration", |
| 81 | + }, |
| 82 | + }, |
| 83 | +}; |
| 84 | +export default meta; |
| 85 | +type Story = StoryObj<typeof ModalDialog>; |
| 86 | + |
| 87 | +export const Modal: Story = { |
| 88 | + name: "Modal without a dialog", |
| 89 | + args: {}, |
| 90 | +}; |
| 91 | + |
| 92 | +export const ModalWithDialog: Story = { |
| 93 | + name: "Modal dialog", |
| 94 | + args: { |
| 95 | + withDialog: true, |
| 96 | + }, |
| 97 | +}; |
| 98 | + |
| 99 | +export const ModalDialogWithTitle: Story = { |
| 100 | + name: "With title", |
| 101 | + args: { |
| 102 | + withDialog: true, |
| 103 | + withTitle: true, |
| 104 | + }, |
| 105 | +}; |
| 106 | + |
| 107 | +export const ModalDialogWithIllustration: Story = { |
| 108 | + name: "With title and illustration", |
| 109 | + args: { |
| 110 | + withDialog: true, |
| 111 | + withTitle: true, |
| 112 | + withIllustration: true, |
| 113 | + }, |
| 114 | +}; |
0 commit comments