javascriptpopper.jsreact-popper

react-popper: re-position using scheduleUpdate


I'm using React-popper to show a date picker element after clicking a button.

JSX

<Manager>
    <Reference>
        {({ ref }) => (
            <button ref={ref} onClick={this.onDateRangeBtnClick}>click to show</button>
        )}
    </Reference>
    {ReactDOM.createPortal(
        <Popper placement="auto-end" >
            {({ ref, style, placement, arrowProps, scheduleUpdate }) => (
                <div className={`dayPickerOverlay ${this.state.showDatePicker ? "" : "hidden"}`} ref={ref} style={style} data-placement={placement}>
                    <DateRangePicker />
                </div>
            )}
        </Popper>,
        document.querySelector('#root')
    )}
</Manager>

When onDateRangeBtnClick is called after the button was clicked, I want to re-position the Popper element by calling scheduleUpdate method, but I do not know how to approach this.

How can I expose that specific scheduleUpdate to be called within the onDateRangeBtnClick or alternatively how can I conditionally call a function (scheduleUpdate for this matter) within JSX itself?


Solution

  • I would split the popper part into its own component and take advantage of the React lifecycle hooks.

    Inside componentDidUpdate you can check if the open state changed, and trigger the scheduleUpdate accordingly.

    // PopperElement.js
    export default class PopperElement extends React.Component {
      componentDidUpdate(prevProps) {
        if (this.props.open && this.props.open !== prevProps.open) {
          this.props.scheduleUpdate();
        }
      }
    
      render() {
        return (
          <div className={`dayPickerOverlay ${this.state.showDatePicker ? "" : "hidden"}`} ref={ref} style={style} data-placement={placement}>
            <DateRangePicker />
          </div>
        );
      }
    }
    
    // App.js
    <Manager>
      <Reference>
        {({ ref }) => (
          <button ref={ref} onClick={this.onDateRangeBtnClick}>click to show</button>
        )}
      </Reference>
      {ReactDOM.createPortal(
        <Popper placement="auto-end">
          {({ ref, style, placement, arrowProps, scheduleUpdate }) => (
            <PopperElement open={this.state.open} scheduleUpdate={scheduleUpdate} />
          )}
        </Popper>,
        document.querySelector('#root')
      )}
    </Manager>
    

    If you prefer a more concise approach, I think I'd use react-powerplug this way:

    import { Manager, Popper, Reference } from 'react-popper';
    import { State } from 'react-powerplug';
    
    const App = () => (
      <Manager>
        <Popper>
          {({ ref, style, scheduleUpdate }) => (
            <State initial={{ open: false }} onChange={scheduleUpdate}>
              {({ state, setState }) => (
                <Fragment>
                  <Reference>
                    {({ ref }) => (
                      <button
                        ref={ref}
                        onClick={() => setState({ open: true }})
                      >click to show</button>
                    )}
                  </Reference>
                  {open && <YourContent ref={ref} style={style} />}
                </Fragment>
              )}
            </State>
          )}
        </State>
      </Manager>
    );
    

    I avoided to repeat the React.createPortal part for conciseness, it should be in place of YourContent.