|
| 1 | +import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; |
| 2 | +import { Popover } from '@ui5/webcomponents-react/lib/Popover'; |
| 3 | +import { Button } from '@ui5/webcomponents-react/lib/Button'; |
| 4 | +import { PopoverHorizontalAlign } from '@ui5/webcomponents-react/lib/PopoverHorizontalAlign'; |
| 5 | +import { Title } from '@ui5/webcomponents-react/lib/Title'; |
| 6 | +import { PlacementType } from '@ui5/webcomponents-react/lib/PlacementType'; |
| 7 | +import { PopoverVerticalAlign } from '@ui5/webcomponents-react/lib/PopoverVerticalAlign'; |
| 8 | +import { Icon } from '@ui5/webcomponents-react/lib/Icon'; |
| 9 | +import { Bar } from '@ui5/webcomponents-react/lib/Bar'; |
| 10 | +import { List } from '@ui5/webcomponents-react/lib/List'; |
| 11 | +import { StandardListItem } from '@ui5/webcomponents-react/lib/StandardListItem'; |
| 12 | +import { CSSProperties, useRef } from 'react'; |
| 13 | +import { createSelectArgTypes } from '@shared/stories/createSelectArgTypes'; |
| 14 | +import { DocsHeader } from '@shared/stories/DocsHeader'; |
| 15 | +import '@ui5/webcomponents-icons/dist/icons/settings.js'; |
| 16 | + |
| 17 | +<Meta |
| 18 | + title="UI5 Web Components / Popover" |
| 19 | + component={Popover} |
| 20 | + argTypes={{ |
| 21 | + ...createSelectArgTypes({ |
| 22 | + horizontalAlign: PopoverHorizontalAlign, |
| 23 | + placementType: PlacementType, |
| 24 | + verticalAlign: PopoverVerticalAlign |
| 25 | + }), |
| 26 | + footer: { control: { disable: true } }, |
| 27 | + header: { control: { disable: true } }, |
| 28 | + children: { control: { disable: true } }, |
| 29 | + slot: { control: { disable: true } }, |
| 30 | + ref: { control: { disable: true } }, |
| 31 | + style: { |
| 32 | + type: CSSProperties, |
| 33 | + description: |
| 34 | + '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.' |
| 35 | + }, |
| 36 | + className: { |
| 37 | + type: 'string', |
| 38 | + description: |
| 39 | + '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.' |
| 40 | + }, |
| 41 | + tooltip: { type: 'string', description: 'A tooltip which will be shown on hover' } |
| 42 | + }} |
| 43 | + args={{ |
| 44 | + horizontalAlign: PopoverHorizontalAlign.Center, |
| 45 | + placementType: PlacementType.Right, |
| 46 | + verticalAlign: PopoverVerticalAlign.Center, |
| 47 | + style: {}, |
| 48 | + className: '', |
| 49 | + tooltip: '', |
| 50 | + slot: '', |
| 51 | + ref: null |
| 52 | + }} |
| 53 | +/> |
| 54 | + |
| 55 | +<DocsHeader /> |
| 56 | + |
| 57 | +<br /> |
| 58 | + |
| 59 | +### Opening Popovers |
| 60 | + |
| 61 | +`Popovers` can only be opened by attaching a `ref` to the component and then call the corresponding **`openBy`** method. The method receives the target element - _on which the `Popover` is to be opened_ - as parameter. |
| 62 | + |
| 63 | +```JSX |
| 64 | + const PopoverComponent = () => { |
| 65 | + const popoverRef = useRef(); |
| 66 | + const onButtonClick = (e) => { |
| 67 | + popoverRef.current.openBy(e.target); |
| 68 | + }; |
| 69 | + return ( |
| 70 | + <> |
| 71 | + <Button onClick={onButtonClick}>Open Popover</Button> |
| 72 | + <Popover ref={popoverRef}>Some Content</Popover> |
| 73 | + </> |
| 74 | + ); |
| 75 | + }; |
| 76 | +``` |
| 77 | + |
| 78 | +<Canvas> |
| 79 | + <Story name="Default"> |
| 80 | + {(args) => { |
| 81 | + const popoverRef = useRef(null); |
| 82 | + const onButtonClick = (e) => { |
| 83 | + popoverRef.current.openBy(e.target); |
| 84 | + }; |
| 85 | + return ( |
| 86 | + <> |
| 87 | + <Button onClick={onButtonClick}>Open Popover</Button> |
| 88 | + <Popover {...args} ref={popoverRef} /> |
| 89 | + </> |
| 90 | + ); |
| 91 | + }} |
| 92 | + </Story> |
| 93 | +</Canvas> |
| 94 | + |
| 95 | +<ArgsTable story="Default" /> |
| 96 | + |
| 97 | +<div style={{ fontFamily: 'var(--sapFontFamily)', fontSize: 'var(--sapFontSize)', color: 'var(--sapTextColor)' }}> |
| 98 | + <h3>Structure</h3> |
| 99 | + The popover has three main areas: |
| 100 | + <ul> |
| 101 | + <li>Header (optional)</li> |
| 102 | + <li>Content</li> |
| 103 | + <li>Footer (optional)</li> |
| 104 | + </ul> |
| 105 | + <b>Note:</b> The <code>Popover</code> is closed when the user clicks or taps outside the popover or selects an action within |
| 106 | + the popover. You can prevent this with the <code>modal</code> property. |
| 107 | +</div> |
| 108 | + |
| 109 | +### Using Popovers inside other components |
| 110 | + |
| 111 | +`Popovers` are often used within other components, when opened this could sometimes have unwanted side effects. |
| 112 | +In this case, we recommend using [createPortal](https://reactjs.org/docs/portals.html) to mount the `Popover` outside of the DOM hierarchy of the parent component. |
| 113 | +In most cases the root node should be sufficient. |
| 114 | + |
| 115 | +```JSX |
| 116 | +const PopoverComponent = () => { |
| 117 | + const popoverRef = useRef(null); |
| 118 | + const onButtonClick = (e) => { |
| 119 | + popoverRef.current.openBy(e.target); |
| 120 | + }; |
| 121 | + return ( |
| 122 | + <> |
| 123 | + <Button onClick={onButtonClick}>Open Popover</Button> |
| 124 | + {createPortal(<Popover ref={popoverRef} />, document.body)} |
| 125 | + </> |
| 126 | + ); |
| 127 | +}; |
| 128 | +``` |
| 129 | + |
| 130 | +### Closing Popovers |
| 131 | + |
| 132 | +Closing `Popovers` works in the same way as opening them. So again an attached `ref` is needed on which the corresponding `close` method is called. |
| 133 | + |
| 134 | +```JSX |
| 135 | + const PopoverComponent = () => { |
| 136 | + const popoverRef = useRef(); |
| 137 | + const handleOpen = (e) => { |
| 138 | + popoverRef.current.openBy(e.target); |
| 139 | + }; |
| 140 | + const handleClose = () => { |
| 141 | + popoverRef.current.close(); |
| 142 | + }; |
| 143 | + return ( |
| 144 | + <> |
| 145 | + <Button onClick={handleOpen}>Open Popover</Button> |
| 146 | + <Popover ref={popoverRef}> |
| 147 | + <Button onClick={handleClose}>Close Popover</Button> |
| 148 | + </Popover> |
| 149 | + </> |
| 150 | + ); |
| 151 | + }; |
| 152 | +``` |
| 153 | + |
| 154 | +# Stories |
| 155 | + |
| 156 | +## Popover with content |
| 157 | + |
| 158 | +<Canvas> |
| 159 | + <Story name="w/ content"> |
| 160 | + {(args) => { |
| 161 | + const popoverRef = useRef(null); |
| 162 | + const onButtonClick = (e) => { |
| 163 | + popoverRef.current.openBy(e.target); |
| 164 | + }; |
| 165 | + const handleClose = () => { |
| 166 | + popoverRef.current.close(); |
| 167 | + }; |
| 168 | + return ( |
| 169 | + <> |
| 170 | + <Button onClick={onButtonClick}>Open Popover</Button> |
| 171 | + <Popover |
| 172 | + {...args} |
| 173 | + ref={popoverRef} |
| 174 | + header={<Bar contentMiddle={<Title>Popover</Title>} contentRight={<Icon name="settings" />} />} |
| 175 | + footer={<Bar contentRight={<Button onClick={handleClose}>Close</Button>} />} |
| 176 | + > |
| 177 | + <List style={{ width: '200px' }}> |
| 178 | + <StandardListItem info="3">List Item 1</StandardListItem> |
| 179 | + <StandardListItem info="2">List Item 2</StandardListItem> |
| 180 | + <StandardListItem info="1">List Item 3</StandardListItem> |
| 181 | + </List> |
| 182 | + </Popover> |
| 183 | + </> |
| 184 | + ); |
| 185 | + }} |
| 186 | + </Story> |
| 187 | +</Canvas> |
0 commit comments