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