1.0.3 • Published 6 months ago

reactjs-popover v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

ReactJS Popover

A lightweight, accessible popover component for React applications which is built on top of the Popover API.

Installation

npm install reactjs-popover

Installation

npm install reactjs-popover

Why use the Popover API in React

Using the Popover API in React offers several advantages:

  • Performance: Popovers are promoted to their own layer, eliminating the need for z-index tricks and ensuring efficient rendering.
  • CSS Power: Leverage the full capabilities of CSS, including backdrops and entry/exit animations.
  • Accessibility: The Popover API enhances accessibility by managing focus trapping and providing ESC key dismissal when the type is set to auto.

Usage

import { Popover } from 'reactjs-popover';

const POPOVER_ID = "my-popover";

function Example() {
    return (
        <>
            <button popovertarget={POPOVER_ID}>Toggle popover</button>
            
            <Popover remountChildrenOnShow ={true} position="top" id={POPOVER_ID}>
                {({ showPopover, hidePopover, togglePopover, state }) => (
                    <>
                        <h1>Popover in state {state}</h1>
                        <button onClick={hidePopover}>Hide </button> 
                    </>
                )}
            </Popover>
        </>
    )
}

Props

PropTypeDefaultDescription
children(options: PopoverOptions) => ReactNodeRequiredPopover content
idstringRequiredUnique identifier for the popover and the popovertarget attribute of the button controlling it
type'auto' \| 'manual''auto'Controls popover behavior (see MDN for more information)
classNamestring''Custom CSS class names
showHideButtonbooleanfalseShow close button in popover
closeOnScrollbooleantrueClose popover when its container is scrolled
remountChildrenOnShowbooleanfalseRemount children when popover visibility is toggled (see next paragraph for more information)
position'top' \| 'top-start' \| 'top-end' \| 'bottom' \| 'bottom-start' \| 'bottom-end' \| 'left' \| 'right''bottom'Position of the popover relative to anchor

Rendering behaviour

By default, this component uses the native JS Popover API to manage its visibility. This means that the children elements are rendered once when the parent component renders, regardless of the popover's visibility. They remain hidden in the DOM until the popover is opened. The children will only re-render if their own props change.

However, in some cases, you may want the children to re-render every time the popover is toggled. For example, if the popover contains a form that should reset each time it is opened. To achieve this, you can use the remountChildrenOnShow property. When set to true, the children will be re-mounted every time the popover's visibility changes. Additionally, the state value will be either open or closed, allowing you to delay heavy rendering until the popover is actually opened.

PopoverOptions

The children render function receives an object with the following properties:

PropertyTypeDescription
state'open' \| 'closed'Current state of the popover (NOTE: this value is only available when remountChildrenOnShow is set to true)
hidePopover() => voidFunction to hide the popover
showPopover() => voidFunction to show the popover
togglePopover() => voidFunction to toggle popover state

Considerations:

Polyfills are available for the Popover API and anchor positioning.

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

8 months ago

1.0.0

8 months ago