0.3.5 • Published 7 years ago

react-popover-portal v0.3.5

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Build Status

react-popover-portal

A simple 'shared' popover that renders to the body. Accelerated translation between nodes.

npm install -S react-popover-portal

Demo

GIF

Example

  1. Make sure that you have installed packages in root folder and examples folder.
  2. To start webpack dev server, run npm start inside /examples folder.
  3. Navigate to localhost:8080 .

Usage

In your React component:

render(){
  return (<div>
      <p onMouseEnter={this.displayPopup} onMouseLeave={this.hidePopup} id='parent'> 
      The parent 
      </p>

      <Popover prefix='popup' parent='#parent' open={this.state.open}
          onMouseEnter={this.displayPopup} onMouseLeave={this.hidePopup}>

          <div className={'popup-content'}>
              Popup
          </div>
      </Popover>
      
  </div>);
}

Adding onMouseEnter and onMouseLeave to Popover prevents it from disappearing when hovered

In your CSS file:

// - With 'popup as prefix'
.popup {
  background: rgba(0, 0, 0, 0.7);
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
}

.popup-content{
  // - Add padding here to the content 
  padding: 10px;
}

Do not add margins to children class .popup-content. The onMouseEnter and onMouseLeave does not work on empty space.
If you set .popup height you also need to make sure the children div is covering the whole area. See issues section below.

Props

PropDescriptionTypeRequiredDefault Value
parentThe DOM element to attach the popover todocument queryrequired-
openIf the popover should open or notboolrequired-
groupa string which is used to create independent popupsboolnot required-
arrowWidthSize of your carretnumbernot required-
popupWidthSize of your popup, required if you animate widthnumbernot required-
prefixA prefix used to add classes to the portal nodestringnot requiredrpp
timeoutThe time it takes until popover disappears (ms)numbernot required1000
animationTimeThe duration of your appear and disappear animations (ms)numbernot required350
translateSpeedThe speed of how fast popover translates between parentsnumbernot required300
translateEaseTranslate ease of when popover translates between parentstringnot required'ease'
transitionsList of transitions {name: opacity , ease: 'linear'}Array of objectsnot required{name:'all', ease:'ease'}
offsetThe offset between the parent and popover (pixels)numbernot required10
onMouseEnterCallback for when the mouse entered the popover content.functionnot required-
onMouseLeaveCallback for when the mouse leaved the popup contentfunctionnot required-
getArrowPositionCallback for arrow positioning (left position)functionnot required-

If you are using a carret specify the width with arrowWidth prop.

Customize

You are free to customize the popup. react-popover-portal does not care on how your popup looks.
However you should use the prefix to add styles and animation as it is top level parent.

Animate

You can animate the popup when it appears or disappears. Simply add these classes to your CSS (with popup as prefix):

.popup__hidden{
  opacity: 0;
}

.popup__active{
  opacity: 1;
}

Prefixes {prefix}__active and {prefix}__hidden will be added to the portal node.
See /examples/src/styles/animated.scss and /examples/src/components/AnimatedPopover.jsx

Clear all popups

Use removeAllPopups() to clear all popups from DOM including the portal.
This will prevent popup from staying when you're navigating with react-router etc (see components/App.jsx in examples).

Known issues

  • When you set the height of the popup in your CSS file (eg. animating it from 0px to 200px) the children will still have a fixed height. Because the onMouseEnter and onMouseLeave is listening on child events, the popup will close even when the user is hovering the popup. If you need to set the height, simply add display:flex to the popup class and height:100% to the container child to force it to cover the popup.

  • Animating the width is problematic as it is used to calculate popover position before attaching it to the parent. A solution to fix this problem is by passing the final width as a prop.

Features to add

  • Possible to specify position of the popup relative to parent: 'left', 'bottom', 'top' and 'right'
  • Force the arrow to point to a specific position on the parent, eg parent's right corner.
0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago