Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-popover-a11y

rpearce858BSD-31.2.1

Accessible React popover component

accessibility, a11y, react, react-component

readme

react-popover-a11y

All Contributors npm version npm downloads Build Status Coverage Status Maintainability

Installation

$ npm i react-popover-a11y

or

$ yarn add react-popover-a11y

Usage

import PopoverA11y from 'react-popover-a11y'

export default class App extends Component {
  constructor(...params) {
    super(...params)
    this.handleClose = this.handleClose.bind(this)
    this.handleOpen = this.handleOpen.bind(this)
    this.state = { isOpen: false }
  }

  handleClose() {
    this.setState({ isOpen: false })
  }

  handleOpen() {
    this.setState({ isOpen: true })
  }

  render() {
    const { isOpen } = this.state
    const content = <div className="content">Popover content</div>
    const trigger = <div className="btn">Click me</div>

    return (
      <PopoverA11y
        bottom
        right
        content={content}
        isOpen={isOpen}
        offset={10}
        onClose={this.handleClose}
        onOpen={this.handleOpen}
        trigger={trigger}
      />
    )
  }
}

Adding PopoverContent style

import PopoverA11y, { PopoverContent } from 'react-popover-a11y'

export default class App extends Component {
  constructor(...params) {
    super(...params)
    this.handleClose = this.handleClose.bind(this)
    this.handleOpen = this.handleOpen.bind(this)
    this.state = { isOpen: false }
  }

  handleClose() {
    this.setState({ isOpen: false })
  }

  handleOpen() {
    this.setState({ isOpen: true })
  }

  render() {
    const { isOpen } = this.state
    const content = <div className="content">Popover content</div>
    const trigger = (
      <PopoverContent style={{ zIndex: '9999' }}>
        <div className="btn">Click me</div>
      </PopoverContent>
    )

    return (
      <PopoverA11y
        bottom
        right
        content={content}
        isOpen={isOpen}
        offset="-0.5rem"
        onClose={this.handleClose}
        onOpen={this.handleOpen}
        trigger={trigger}
      />
    )
  }
}

Can compose tangential directions

You can pass both bottom and left or top and right, for example, or simply one of those.

On window boundaries

If you specify top and right, but the popover would open outside the window to the top and right, this component will adjust it to be inside the window – in this case, bottom and left – so that it will remain visible.

API

Prop Type Required Default Details
bottom bool no none Have popover appear at the bottom
content node yes none This is the popover content element. Can be a normal React node or import PopoverContent itself to override its style
isOpen bool yes false As a controlled component, you must pass isOpen to tell the component what to do
label string no none Provide a label to be used as aria-label when no appropriate trigger text is provided
left bool no none Have popover appear at the left
offset number / CSS unit no 0px Amount in pixels (or CSS unit value, like -0.5rem) for popover to be offset from trigger
onClose function yes Function.prototype Callback that is triggered when element should close
onOpen function yes Function.prototype Callback that is triggered when element should open
right bool no none Have popover appear at the right
top bool no none Have popover appear at the top
trigger node yes none This is the popover trigger element

Contributors

Thanks goes to these wonderful people (emoji key):


Robert Pearce

💻 📖 💡 🤔 ⚠️

Luke Mallory

🐛 💻

This project follows the all-contributors specification. Contributions of any kind welcome!

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[1.2.1] - 2022-01-10

Fixed

  • "Switch directions refinement" (#33)
    • Only switch directions for a direction when at a boundary if that direction is specified in the first place

[1.2.0] - 2019-01-02

Changed

  • fix commonjs bundle
  • bump button lib
  • higher react peer dependency (from 16.3 to 16.8)

[1.1.0] - 2019-12-29

Changed

  • main and module field locations
  • now building umd
  • can now import PopoverContent directly, if for some reason you want to
  • changed license from ISC to BSD-3
  • bumped heaps of dependencies
  • moved API docs back to README
  • correctly now listing react-dom as a peer dependency

Added

  • added prop-types as dependency

[1.0.0] - 2019-09-13

Added

  • added prop-types as dependency

Changed

  • build output is now in dist/ folder
  • building with rollup.js
  • outputting commonjs & ESM files now instead of just commonjs

[0.2.1] - 2019-01-03

Changed

  • minor code quality changes

[0.2.0] - 2019-01-03

Added

  • support for either a Number or any valid CSS unit value that can be used with calc for the offset property (#6)

[0.1.2] - 2018-12-29

Changed

  • swapped uniqueId implementation with @rpearce/simple-uniqueid

[0.1.1] - 2018-12-29

Changed

  • bumped react-button-a11y to v2.0.0.
  • a Popover.test.js for componentWillUnmount was accidentally commented out

[0.1.0] - 2018-12-24

Added

  • Added missing functionality (a lot). See API docs for all you can now do
    • bottom
    • left
    • offset
    • right
    • top
  • Can compose options; e.g., bottom and left or top and right

Fixed

  • responds based on window boundaries (if it's to be opened to the left, but there's no room, it'll open to the right)

[0.0.1] - 2018-12-19

Added

  • Added initial alpha functionality