1.2.1 • Published 2 years ago

react-popover-a11y v1.2.1

Weekly downloads
347
License
BSD-3
Repository
github
Last release
2 years ago

react-popover-a11y

All Contributors npm version npm downloads Build Status Coverage Status Maintainability

Links

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

PropTypeRequiredDefaultDetails
bottomboolnononeHave popover appear at the bottom
contentnodeyesnoneThis is the popover content element. Can be a normal React node or import PopoverContent itself to override its style
isOpenboolyesfalseAs a controlled component, you must pass isOpen to tell the component what to do
labelstringnononeProvide a label to be used as aria-label when no appropriate trigger text is provided
leftboolnononeHave popover appear at the left
offsetnumber / CSS unitno0pxAmount in pixels (or CSS unit value, like -0.5rem) for popover to be offset from trigger
onClosefunctionyesFunction.prototypeCallback that is triggered when element should close
onOpenfunctionyesFunction.prototypeCallback that is triggered when element should open
rightboolnononeHave popover appear at the right
topboolnononeHave popover appear at the top
triggernodeyesnoneThis is the popover trigger element

Contributors

Thanks goes to these wonderful people (emoji key):

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

1.2.1

2 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.1

5 years ago

0.0.1-security

5 years ago