import React, { useState, useEffect } from 'react'; import { StoryFn, Meta } from '@storybook/react'; import Box from '../../ui/box/box'; import { AlignItems, BackgroundColor, BorderColor, Color, Display, JustifyContent, TextAlign, } from '../../../helpers/constants/design-system'; import { Icon, IconName, IconSize, PopoverHeader, Text } from '..'; import README from './README.mdx'; import { Popover, PopoverPosition, PopoverRole } from '.'; export default { title: 'Components/ComponentLibrary/Popover', component: Popover, parameters: { docs: { page: README, }, }, argTypes: { children: { control: 'text', }, position: { options: PopoverPosition, control: 'select', }, role: { options: PopoverRole, control: 'select', }, className: { control: 'text', }, }, args: { children: 'Popover', }, } as Meta; const Template: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const [isOpen, setIsOpen] = useState(true); const handleClick = () => { setIsOpen(!isOpen); }; const handleKeyDown = (e) => { if (e.key === 'Escape') { setIsOpen(false); } }; // Example of how to use keyboard events to close popover with escape key useEffect(() => { if (isOpen) { document.addEventListener('keydown', handleKeyDown); } else { document.removeEventListener('keydown', handleKeyDown); } }, [isOpen]); // Example of how to use ref to open popover const setBoxRef = (ref) => { setReferenceElement(ref); }; return ( <> Click to toggle popover ); }; export const DefaultStory = Template.bind({}); DefaultStory.storyName = 'Default'; DefaultStory.args = { position: PopoverPosition.BottomStart, children: 'Popover demo without PopoverHeader', isPortal: false, hasArrow: true, }; export const ReferenceElement: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; return ( <> Reference Element ); }; export const Children: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; return ( <> Demo of popover with children.{' '} ); }; export const Position: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const [referenceAutoElement, setReferenceAutoElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; const setRefAuto = (ref) => { setReferenceAutoElement(ref); }; return ( <> Position {PopoverPosition.TopStart} {PopoverPosition.Top} {PopoverPosition.TopEnd} {PopoverPosition.RightStart} {PopoverPosition.Right} {PopoverPosition.RightEnd} {PopoverPosition.BottomStart} {PopoverPosition.Bottom} {PopoverPosition.BottomEnd} {PopoverPosition.LeftStart} {PopoverPosition.Left} {PopoverPosition.LeftEnd} Position {PopoverPosition.Auto} ); }; export const IsPortal: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; return ( <> Inspect to view the popover in the DOM (isPortal true) Inspect to view the popover in the DOM (isPortal false) ); }; export const HasArrow: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; return ( <> Popover with arrow Popover with no arrow ); }; export const IsOpen: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const [isOpen, setIsOpen] = useState(true); const setBoxRef = (ref) => { setReferenceElement(ref); }; const handleClick = () => { setIsOpen(!isOpen); }; return ( <> Click to toggle popover isOpen always true isOpen tied to boolean ); }; export const Flip: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; return ( Scroll to see popover flip false true ); }; export const PreventOverflow: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; return ( Scroll to see popover preventOverflow false true ); }; export const ReferenceHidden: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; return ( Scroll to see popover referenceHidden false true ); }; export const MatchWidth: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; return ( <> Setting matchWidth to true will make the popover match the width of the reference element ); }; export const Role: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; return ( Inspect to view role {PopoverRole.Dialog} {PopoverRole.Tooltip} ); }; export const Offset: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const setBoxRef = (ref) => { setReferenceElement(ref); }; return ( Offset Demo offset default offset override to [0,32] ); }; export const OnPressEscKey: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const [isOpen, setIsOpen] = useState(false); // Set Popover Ref const setBoxRef = (ref) => { setReferenceElement(ref); }; const handleClick = () => { setIsOpen(!isOpen); }; return ( <> Click to open setIsOpen(false)} isOpen={isOpen} {...args} > Press esc key to close ); }; export const WithPopoverHeader: StoryFn = (args) => { const [refTitleElement, setRefTitleElement] = useState(); const [isOpen, setIsOpen] = useState(true); const handleClick = () => { setIsOpen(!isOpen); }; const setBoxRef = (ref) => { setRefTitleElement(ref); }; return ( <> console.log('back')} color={Color.inherit} marginBottom={4} > Popover Title Title should be short and concise. It should be sentence case and no period. ); }; export const MouseEventDemo: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const [isOpen, setIsOpen] = useState(false); // Set Popover Ref const setBoxRef = (ref) => { setReferenceElement(ref); }; // Example of how to use mouse events to open and close popover const handleMouseEnter = () => { setIsOpen(true); }; const handleMouseLeave = () => { setIsOpen(false); }; return ( <> Hover onMouseEnter and onMouseLeave ); }; export const OnFocusBlur: StoryFn = (args) => { const [referenceElement, setReferenceElement] = useState(); const [isOpen, setIsOpen] = useState(false); // Set Popover Ref const setBoxRef = (ref) => { setReferenceElement(ref); }; // Example of how open popover with focus and pair with onBlur to close popover const handleFocus = () => { setIsOpen(true); }; const handleClose = () => { setIsOpen(false); }; return ( <> Focus to open onFocus to open and onBlur to close ); };