import { Story, Canvas, ArgsTable } from '@storybook/addon-docs'; import { Popover } from './popover'; # Popover Popover is an overlay that appears by the trigger used for menus, additional contents, and contains at least one focusable element. ## Props The `Popover` accepts all props below as well as all [Box](/docs/ui-components-ui-box-box-stories-js--default-story#props) component props ### Reference Element The `referenceElement` prop is required and used to position the popover relative to the reference element. ```jsx import { useState } from 'react'; import { Popover } from '../../ui/component-library'; const [referenceElement, setReferenceElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; Reference Element ``` ### Children Popover accepts any children and has a default padding of `4` (16px). ```jsx import { Popover, Text, Icon, IconSize, IconName, } from '../../ui/component-library'; Demo of popover with children. ; ``` ### Position Use the `position` prop with the `PopoverPosition` enum to set the position of the popover relative to the reference element. Default is `PopoverPosition.Auto` ```jsx import { Popover, PopoverPosition } from '../../ui/component-library'; Auto AutoStart AutoEnd Top TopStart TopEnd Right RightStart RightEnd Bottom BottomStart BottomEnd Left LeftStart LeftEnd ``` ### Is Portal The `isPortal` prop is a boolean that when set to true, causes the Popover to be rendered as a separate DOM element at the end of the document body. Default `false` ```jsx import { Popover } from '../../ui/component-library'; Popover using create portal; ``` ### Has Arrow Use the `hasArrow` boolean to add an arrow to the popover. ```jsx import { Popover } from '../../ui/component-library'; Popover with arrow; ``` ### Is Open Use the `isOpen` boolean to control the visibility of the popover. ```jsx import { Popover } from '../../ui/component-library'; Popover with arrow; ``` ### Flip Use the `flip` boolean to flip the popover to the opposite side of the reference element if there is not enough space. For `PopoverPosition.Auto` this will become true. ```jsx import { Popover } from '../../ui/component-library'; Flip demo; ``` ### Prevent Overflow Use the `preventOverflow` boolean to prevent the popover from overflowing the viewport. For `PopoverPosition.Auto` this will become true. ```jsx import { Popover } from '../../ui/component-library'; Prevent overflow demo; ``` ### Reference Hidden Use the `referenceHidden` boolean to hide the Popover when the reference element is no longer visible in the viewport. ```jsx import { Popover } from '../../ui/component-library'; Reference hidden demo; ``` ### Match Width Use the `matchWidth` boolean to match the width of the popover to the reference element. ```jsx import { Popover } from '../../ui/component-library'; Match width demo; ``` ### Role Use the `role` prop with `PopoverRole` enum to set the role of the popover. `PopoverRole.Dialog` if the content is interactive, or `PopoverRole.Tooltip` for purely informational popovers. Default: `PopoverRole.Tooltip` ```jsx import { Popover, PopoverRole } from '../../ui/component-library'; PopoverRole.Tooltip; PopoverRole.Dialog; ``` ### Offset Use the `offset` prop to pass an array of two numbers to offset the popover from the reference element. Default is `[0, 8]` First number controls the skidding offset and the second number controls the distance offset. ```jsx import { Popover } from '../../ui/component-library'; offset override to [0,32]; ``` ### On Press Esc Key `onPressEscKey` is a callback function that is invoked when the 'Escape' key is pressed within the `Popover` component ```jsx import { Popover } from '../../ui/component-library'; const [isOpen, setIsOpen] = useState(false); const handleClick = () => { setIsOpen(!isOpen); }; setIsOpen(false)}> Press esc key to close ; ``` ### With PopoverHeader Using the `PopoverHeader` component to add a header to the `Popover` component. The `PopoverHeader` is used to show common elements such as title, back button, and close button. ```jsx import { Popover } from '../../ui/component-library'; console.log('close')} onBack={() => console.log('back')} > Popover Title Title should be short and concise. It should be sentence case and no period. ; ``` ### Mouse Event Demo Not built into the `Popover` component, but a demo of `onMouseEnter` and `onMouseLeave` events on the reference element to control the visibility of the popover ```jsx import { Popover } from '../../ui/component-library'; const [isOpen, setIsOpen] = useState(false); const handleMouseEnter = () => { setIsOpen(true); }; const handleMouseLeave = () => { setIsOpen(false); }; <> Hover onMouseEnter and onMouseLeave ; ``` ### On Focus/Blur Demo Not built into the `Popover` component, but a demo of `onFocus` and `onBlur` events on the reference element to control the visibility of the popover ```jsx import { Popover } from '../../ui/component-library'; const [isOpen, setIsOpen] = useState(false); // Example of how open popover with focus and pair with onBlur to close popover const handleFocus = () => { setIsOpen(true); }; const handleClose = () => { setIsOpen(false); }; <> Focus to open onFocus to open and onBlur to close ; ```