|
| 1 | +import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; |
| 2 | +import { Dialog } from '@ui5/webcomponents-react/lib/Dialog'; |
| 3 | +import { Button } from '@ui5/webcomponents-react/lib/Button'; |
| 4 | +import { Bar } from '@ui5/webcomponents-react/lib/Bar'; |
| 5 | +import { Icon } from '@ui5/webcomponents-react/lib/Icon'; |
| 6 | +import { List } from '@ui5/webcomponents-react/lib/List'; |
| 7 | +import { StandardListItem } from '@ui5/webcomponents-react/lib/StandardListItem'; |
| 8 | +import { Title } from '@ui5/webcomponents-react/lib/Title'; |
| 9 | +import { CSSProperties, useRef } from 'react'; |
| 10 | +import { createSelectArgTypes } from '@shared/stories/createSelectArgTypes'; |
| 11 | +import { DocsHeader } from '@shared/stories/DocsHeader'; |
| 12 | + |
| 13 | +<Meta |
| 14 | + title="UI5 Web Components / Dialog" |
| 15 | + component={Dialog} |
| 16 | + argTypes={{ |
| 17 | + ...createSelectArgTypes({}), |
| 18 | + footer: { control: { disable: true } }, |
| 19 | + header: { control: { disable: true } }, |
| 20 | + slot: { control: { disable: true } }, |
| 21 | + ref: { control: { disable: true } }, |
| 22 | + style: { |
| 23 | + type: CSSProperties, |
| 24 | + description: |
| 25 | + 'Element style which will be appended to the most outer element of a component. Use this prop carefully, some css properties might break the component.' |
| 26 | + }, |
| 27 | + className: { |
| 28 | + type: 'string', |
| 29 | + description: |
| 30 | + 'CSS Class Name which will be appended to the most outer element of a component. Use this prop carefully, overwriting CSS rules might break the component.' |
| 31 | + }, |
| 32 | + tooltip: { type: 'string', description: 'A tooltip which will be shown on hover' } |
| 33 | + }} |
| 34 | + args={{ |
| 35 | + children: 'Press "Escape" to close the Dialog.', |
| 36 | + headerText: 'Dialog Header', |
| 37 | + style: {}, |
| 38 | + className: '', |
| 39 | + tooltip: '', |
| 40 | + slot: '', |
| 41 | + ref: null |
| 42 | + }} |
| 43 | +/> |
| 44 | + |
| 45 | +<DocsHeader /> |
| 46 | + |
| 47 | +<br /> |
| 48 | + |
| 49 | +### Opening Dialogs |
| 50 | + |
| 51 | +`Dialogs` can only be opened by attaching a `ref` to the component and then call the corresponding **`open`** method. |
| 52 | + |
| 53 | +```JSX |
| 54 | + const DialogComponent = () => { |
| 55 | + const dialogRef = useRef(); |
| 56 | + const onButtonClick = () => { |
| 57 | + dialogRef.current.open(); |
| 58 | + }; |
| 59 | + return ( |
| 60 | + <> |
| 61 | + <Button onClick={onButtonClick}>Open Dialog</Button> |
| 62 | + <Dialog ref={dialogRef}>Some Content</Dialog> |
| 63 | + </> |
| 64 | + ); |
| 65 | + }; |
| 66 | +``` |
| 67 | + |
| 68 | +<Canvas> |
| 69 | + <Story name="Default"> |
| 70 | + {(args) => { |
| 71 | + const dialogRef = useRef(null); |
| 72 | + const onButtonClick = () => { |
| 73 | + dialogRef.current.open(); |
| 74 | + }; |
| 75 | + const handleClose = () => { |
| 76 | + dialogRef.current.close(); |
| 77 | + }; |
| 78 | + return ( |
| 79 | + <> |
| 80 | + <Button onClick={onButtonClick}>Open Dialog</Button> |
| 81 | + <Dialog {...args} ref={dialogRef} footer={<Button onClick={handleClose}>Close</Button>} /> |
| 82 | + </> |
| 83 | + ); |
| 84 | + }} |
| 85 | + </Story> |
| 86 | +</Canvas> |
| 87 | + |
| 88 | +<ArgsTable story="." /> |
| 89 | +<div style={{ fontFamily: 'var(--sapFontFamily)', fontSize: 'var(--sapFontSize)', color: 'var(--sapTextColor)' }}> |
| 90 | + <h3>Structure</h3>A <code>Dialog</code> consists of a header, content, and a footer for action buttons. The{' '} |
| 91 | + <code>Dialog</code> is usually displayed at the center of the screen. |
| 92 | +</div> |
| 93 | + |
| 94 | +### Using Dialogs inside other components |
| 95 | + |
| 96 | +`Dialogs` are often used within other components, when opened this could sometimes have unwanted side effects. |
| 97 | +In this case, we recommend using [createPortal](https://reactjs.org/docs/portals.html) to mount the `Dialog` outside of the DOM hierarchy of the parent component. |
| 98 | + |
| 99 | +```JSX |
| 100 | +const DialogComponent = () => { |
| 101 | + const dialogRef = useRef(null); |
| 102 | + const onButtonClick = () => { |
| 103 | + dialogRef.current.open(); |
| 104 | + }; |
| 105 | + return ( |
| 106 | + <> |
| 107 | + <Button onClick={onButtonClick}>Open Dialog</Button> |
| 108 | + {createPortal(<Dialog ref={dialogRef} />, document.body)} |
| 109 | + </> |
| 110 | + ); |
| 111 | +}; |
| 112 | +``` |
| 113 | + |
| 114 | +### Closing Dialogs |
| 115 | + |
| 116 | +Closing `Dialogs` works in the same way as opening them. So again an attached `ref` is needed on which the corresponding `close` method is called. |
| 117 | + |
| 118 | +```JSX |
| 119 | + const DialogComponent = () => { |
| 120 | + const dialogRef = useRef(); |
| 121 | + const handleOpen = (e) => { |
| 122 | + dialogRef.current.open(); |
| 123 | + }; |
| 124 | + const handleClose = () => { |
| 125 | + dialogRef.current.close(); |
| 126 | + }; |
| 127 | + return ( |
| 128 | + <> |
| 129 | + <Button onClick={handleOpen}>Open Dialog</Button> |
| 130 | + <Dialog ref={dialogRef}> |
| 131 | + <Button onClick={handleClose}>Close Dialog</Button> |
| 132 | + </Dialog> |
| 133 | + </> |
| 134 | + ); |
| 135 | + }; |
| 136 | +``` |
| 137 | + |
| 138 | +# Stories |
| 139 | + |
| 140 | +## Dialog with content |
| 141 | + |
| 142 | +<Canvas> |
| 143 | + <Story name="with content"> |
| 144 | + {(args) => { |
| 145 | + const dialogRef = useRef(null); |
| 146 | + const onButtonClick = () => { |
| 147 | + dialogRef.current.open(); |
| 148 | + }; |
| 149 | + const handleClose = () => { |
| 150 | + dialogRef.current.close(); |
| 151 | + }; |
| 152 | + return ( |
| 153 | + <> |
| 154 | + <Button onClick={onButtonClick}>Open Dialog</Button> |
| 155 | + <Dialog |
| 156 | + {...args} |
| 157 | + ref={dialogRef} |
| 158 | + header={<Bar contentMiddle={<Title>Dialog</Title>} contentRight={<Icon name="settings" />} />} |
| 159 | + footer={<Bar contentRight={<Button onClick={handleClose}>Close</Button>} />} |
| 160 | + > |
| 161 | + <List> |
| 162 | + <StandardListItem info="3">List Item 1</StandardListItem> |
| 163 | + <StandardListItem info="2">List Item 2</StandardListItem> |
| 164 | + <StandardListItem info="1">List Item 3</StandardListItem> |
| 165 | + </List> |
| 166 | + </Dialog> |
| 167 | + </> |
| 168 | + ); |
| 169 | + }} |
| 170 | + </Story> |
| 171 | +</Canvas> |
0 commit comments