| | import clsx from 'clsx'; |
| | import { useRtl } from 'i18n-calypso'; |
| | import { defer } from 'lodash'; |
| | import PropTypes from 'prop-types'; |
| | import { createRef, useState, useEffect, Component } from 'react'; |
| | import ReactDom from 'react-dom'; |
| | import RootChild from '../root-child'; |
| | import { |
| | bindWindowListeners, |
| | unbindWindowListeners, |
| | onViewportChange, |
| | suggested as suggestPosition, |
| | constrainLeft, |
| | offset, |
| | } from './util'; |
| |
|
| | import './style.scss'; |
| |
|
| | const noop = () => {}; |
| |
|
| | class PopoverInner extends Component { |
| | static defaultProps = { |
| | autoPosition: true, |
| | autoRtl: true, |
| | className: '', |
| | closeOnEsc: true, |
| | isRtl: false, |
| | focusOnShow: true, |
| | position: 'top', |
| | onShow: noop, |
| | onClose: noop, |
| | onMouseEnter: noop, |
| | onMouseLeave: noop, |
| | hideArrow: false, |
| | autoRepositionOnInitialLoad: false, |
| | ignoreViewportSize: false, |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | scheduledPositionUpdate = null; |
| |
|
| | |
| | |
| | |
| | |
| | scheduledFocus = null; |
| |
|
| | popoverNodeRef = createRef(); |
| | popoverInnerNodeRef = createRef(); |
| |
|
| | state = { |
| | left: -99999, |
| | top: -99999, |
| | positionClass: this.getPositionClass( this.props.position ), |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | autoRepositionStability = 0; |
| |
|
| | componentDidMount() { |
| | |
| | |
| | onViewportChange(); |
| | this.bindListeners(); |
| | this.setPosition(); |
| | this.show(); |
| | this.autoRepositionOnInitialLoad(); |
| | } |
| |
|
| | componentDidUpdate() { |
| | |
| | |
| | |
| | |
| | if ( this.scheduledPositionUpdate == null ) { |
| | this.scheduledPositionUpdate = defer( () => { |
| | this.setPosition(); |
| | this.scheduledPositionUpdate = null; |
| | } ); |
| | } |
| | } |
| |
|
| | componentWillUnmount() { |
| | this.unbindListeners(); |
| | this.clearAutoRepositionOnInitialLoad(); |
| | } |
| |
|
| | bindListeners() { |
| | this.bindClickoutHandler(); |
| | this.bindEscKeyListener(); |
| | this.bindReposition(); |
| | bindWindowListeners(); |
| | } |
| |
|
| | unbindListeners() { |
| | this.unbindClickoutHandler(); |
| | this.unbindEscKeyListener(); |
| | this.unbindReposition(); |
| | unbindWindowListeners(); |
| |
|
| | |
| | if ( this.scheduledPositionUpdate != null ) { |
| | window.clearTimeout( this.scheduledPositionUpdate ); |
| | this.scheduledPositionUpdate = null; |
| | } |
| |
|
| | |
| | if ( this.scheduledFocus != null ) { |
| | window.clearTimeout( this.scheduledFocus ); |
| | this.scheduledFocus = null; |
| | } |
| | } |
| |
|
| | |
| | bindEscKeyListener() { |
| | if ( this.props.closeOnEsc ) { |
| | document.addEventListener( 'keydown', this.onKeydown, true ); |
| | } |
| | } |
| |
|
| | unbindEscKeyListener() { |
| | if ( this.props.closeOnEsc ) { |
| | document.removeEventListener( 'keydown', this.onKeydown, true ); |
| | } |
| | } |
| |
|
| | onKeydown = ( event ) => { |
| | if ( event.keyCode === 27 ) { |
| | const domContext = ReactDom.findDOMNode( this.props.context ); |
| | if ( domContext ) { |
| | domContext.focus(); |
| | } |
| |
|
| | this.close( true ); |
| | } |
| | }; |
| |
|
| | getTouchEvent = () => { |
| | if ( 'onpointerdown' in document ) { |
| | return 'pointerdown'; |
| | } |
| |
|
| | if ( 'ontouchstart' in document ) { |
| | return 'touchstart'; |
| | } |
| |
|
| | return 'click'; |
| | }; |
| |
|
| | |
| | bindClickoutHandler() { |
| | |
| | |
| | |
| | |
| | |
| | document.addEventListener( this.getTouchEvent(), this.onClickout, true ); |
| | } |
| |
|
| | unbindClickoutHandler() { |
| | document.removeEventListener( this.getTouchEvent(), this.onClickout, true ); |
| | } |
| |
|
| | onClickout = ( event ) => { |
| | const popoverContext = this.popoverInnerNodeRef.current; |
| | let shouldClose = popoverContext && ! popoverContext.contains( event.target ); |
| |
|
| | if ( shouldClose && this.props.context ) { |
| | const domContext = ReactDom.findDOMNode( this.props.context ); |
| | shouldClose = domContext && ! domContext.contains( event.target ); |
| | } |
| |
|
| | if ( shouldClose && this.props.ignoreContext ) { |
| | const ignoreContext = ReactDom.findDOMNode( this.props.ignoreContext ); |
| | shouldClose = ignoreContext && ! ignoreContext.contains( event.target ); |
| | } |
| |
|
| | if ( shouldClose ) { |
| | this.close(); |
| | } |
| | }; |
| |
|
| | |
| | bindReposition() { |
| | window.addEventListener( 'scroll', this.onWindowChange, true ); |
| | window.addEventListener( 'resize', this.onWindowChange, true ); |
| | } |
| |
|
| | unbindReposition() { |
| | window.removeEventListener( 'scroll', this.onWindowChange, true ); |
| | window.removeEventListener( 'resize', this.onWindowChange, true ); |
| | } |
| |
|
| | onWindowChange = () => { |
| | this.setPosition(); |
| | }; |
| |
|
| | focusPopover() { |
| | |
| | |
| | |
| | |
| | |
| | this.scheduledFocus = defer( () => { |
| | if ( this.popoverNodeRef.current ) { |
| | this.popoverNodeRef.current.focus(); |
| | } |
| | this.scheduledFocus = null; |
| | } ); |
| | } |
| |
|
| | getPositionClass( position ) { |
| | return `is-${ position.replace( /\s+/g, '-' ) }`; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | adjustRtlPosition( position ) { |
| | if ( this.props.isRtl ) { |
| | switch ( position ) { |
| | case 'top right': |
| | case 'right top': |
| | return 'top left'; |
| |
|
| | case 'right': |
| | return 'left'; |
| |
|
| | case 'bottom right': |
| | case 'right bottom': |
| | return 'bottom left'; |
| |
|
| | case 'bottom left': |
| | case 'left bottom': |
| | return 'bottom right'; |
| |
|
| | case 'left': |
| | return 'right'; |
| |
|
| | case 'top left': |
| | case 'left top': |
| | return 'top right'; |
| | } |
| | } |
| | return position; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | computePosition() { |
| | const { position, relativePosition } = this.props; |
| | const domContainer = this.popoverInnerNodeRef.current; |
| | const domContext = ReactDom.findDOMNode( this.props.context ); |
| |
|
| | if ( ! domContext ) { |
| | return null; |
| | } |
| |
|
| | let suggestedPosition = position; |
| |
|
| | if ( this.props.autoRtl ) { |
| | suggestedPosition = this.adjustRtlPosition( suggestedPosition ); |
| | } |
| |
|
| | if ( this.props.autoPosition ) { |
| | suggestedPosition = suggestPosition( suggestedPosition, domContainer, domContext ); |
| | } |
| |
|
| | const reposition = Object.assign( |
| | {}, |
| | constrainLeft( |
| | offset( suggestedPosition, domContainer, domContext, relativePosition ), |
| | domContainer, |
| | this.props.ignoreViewportSize |
| | ), |
| | { positionClass: this.getPositionClass( suggestedPosition ) } |
| | ); |
| |
|
| | return reposition; |
| | } |
| |
|
| | setPosition = () => { |
| | let position; |
| |
|
| | |
| | if ( this.props.customPosition ) { |
| | position = Object.assign( |
| | { |
| | |
| | positionClass: this.getPositionClass( this.constructor.defaultProps.position ), |
| | }, |
| | this.props.customPosition |
| | ); |
| | } else { |
| | position = this.computePosition(); |
| | } |
| |
|
| | if ( position ) { |
| | this.setState( position ); |
| | } |
| | return position; |
| | }; |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | autoRepositionOnInitialLoad = () => { |
| | if ( this.props.autoRepositionOnInitialLoad ) { |
| | this.autoRepositionOnInitialLoadInterval = setInterval( () => { |
| | const lastPosition = this.state; |
| | const { left, top } = this.setPosition(); |
| | if ( lastPosition.left === left || lastPosition.top === top ) { |
| | this.autoRepositionStability += 1; |
| | } |
| | |
| | if ( this.autoRepositionStability > 5 ) { |
| | clearInterval( this.autoRepositionOnInitialLoadInterval ); |
| | } |
| | }, 500 ); |
| | } |
| | }; |
| |
|
| | clearAutoRepositionOnInitialLoad = () => { |
| | if ( this.autoRepositionOnInitialLoadInterval ) { |
| | clearInterval( this.autoRepositionOnInitialLoadInterval ); |
| | } |
| | }; |
| |
|
| | getStylePosition() { |
| | const { left, top } = this.state; |
| | return { left, top }; |
| | } |
| |
|
| | show() { |
| | if ( this.props.focusOnShow ) { |
| | this.focusPopover(); |
| | } |
| |
|
| | this.props.onShow(); |
| | } |
| |
|
| | close( wasCanceled = false ) { |
| | this.props.onClose( wasCanceled ); |
| | } |
| |
|
| | handleOnMouseEnter = () => { |
| | const { onMouseEnter } = this.props; |
| |
|
| | onMouseEnter?.(); |
| | }; |
| |
|
| | handleOnMouseLeave = () => { |
| | const { onMouseLeave } = this.props; |
| |
|
| | onMouseLeave?.(); |
| | }; |
| |
|
| | render() { |
| | if ( ! this.props.context ) { |
| | return null; |
| | } |
| |
|
| | const classes = clsx( 'popover', this.props.className, this.state.positionClass ); |
| |
|
| | return ( |
| | <div |
| | ref={ this.popoverNodeRef } |
| | aria-label={ this.props[ 'aria-label' ] } |
| | id={ this.props.id } |
| | role="tooltip" |
| | tabIndex="-1" |
| | style={ this.getStylePosition() } |
| | className={ classes } |
| | onMouseEnter={ this.handleOnMouseEnter } |
| | onMouseLeave={ this.handleOnMouseLeave } |
| | > |
| | { ! this.props.hideArrow ? <div className="popover__arrow" /> : null } |
| | <div ref={ this.popoverInnerNodeRef } className="popover__inner"> |
| | { this.props.children } |
| | </div> |
| | </div> |
| | ); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | function Popover( { isVisible = false, showDelay = 0, hideArrow = false, ...props } ) { |
| | const isRtl = useRtl(); |
| | const [ show, setShow ] = useState( isVisible ); |
| |
|
| | |
| | |
| | useEffect( () => { |
| | if ( showDelay > 0 && show !== isVisible && isVisible ) { |
| | const showDelayTimer = setTimeout( () => { |
| | setShow( true ); |
| | }, showDelay ); |
| |
|
| | return () => { |
| | clearTimeout( showDelayTimer ); |
| | }; |
| | } |
| | }, [ showDelay, isVisible, show ] ); |
| |
|
| | |
| | |
| | |
| | if ( show !== isVisible && ( showDelay === 0 || ! isVisible ) ) { |
| | setShow( isVisible ); |
| | } |
| |
|
| | if ( ! show ) { |
| | return null; |
| | } |
| |
|
| | return ( |
| | <RootChild> |
| | <PopoverInner { ...props } isRtl={ isRtl } hideArrow={ hideArrow } /> |
| | </RootChild> |
| | ); |
| | } |
| |
|
| | |
| | |
| | const PropTypeElement = PropTypes.oneOfType( [ |
| | PropTypes.instanceOf( Component ), |
| | PropTypes.instanceOf( typeof window !== 'undefined' ? window.Element : Object ), |
| | ] ); |
| |
|
| | Popover.propTypes = { |
| | hideArrow: PropTypes.bool, |
| | autoPosition: PropTypes.bool, |
| | autoRtl: PropTypes.bool, |
| | className: PropTypes.string, |
| | closeOnEsc: PropTypes.bool, |
| | id: PropTypes.string, |
| | context: PropTypeElement, |
| | ignoreContext: PropTypeElement, |
| | isVisible: PropTypes.bool, |
| | focusOnShow: PropTypes.bool, |
| | position: PropTypes.oneOf( [ |
| | 'top', |
| | 'top right', |
| | 'right', |
| | 'bottom right', |
| | 'bottom', |
| | 'bottom left', |
| | 'left', |
| | 'top left', |
| | ] ), |
| | showDelay: PropTypes.number, |
| | onShow: PropTypes.func, |
| | onClose: PropTypes.func, |
| | relativePosition: PropTypes.shape( { left: PropTypes.number } ), |
| | |
| | customPosition: PropTypes.shape( { |
| | top: PropTypes.number, |
| | left: PropTypes.number, |
| | positionClass: PropTypes.oneOf( [ 'top', 'right', 'bottom', 'left' ] ), |
| | } ), |
| | }; |
| |
|
| | export default Popover; |
| |
|