Popper-core: How to dynamically position popper with caret offset?

Created on 11 Jan 2018  路  6Comments  路  Source: popperjs/popper-core

What is the expected behavior?

I am trying to build an auto suggestion or mention plugin using popper.

  1. Popper position should update on caret position updates e.g on textarea.
  2. If popper reaches view port boundary position should also update.

What went wrong?

Right i can update the position but i cannot do the view port boundary

    this.popper = new Popper(this.ref, this.popEl, {
      removeOnDestroy: true,
      modifiers: {
        preventOverflow: {
          enabled: true,
        },
        updatePosition: {
          enabled: true,
          order: 800,
          fn: data => {
            console.log(this.props.coords);

            return {
              ...data,
              styles: {
                ...data.styles,
                top: this.props.coords.top + 20,
                left: this.props.coords.left - 25,
              },
            };
          },
        },
        showPopper: {
          enabled: true,
          order: 850,
          fn: data => {
            return {
              ...data,
              styles: {
                ...data.styles,
                display: this.props.isOpen ? 'block' : 'none',
              },
            };
          },
        },
      },
    });

Most helpful comment

I think the API is the same, you can adapt my code.

All 6 comments

@FezVrasta i was wondering if we can position the popper against the caret not the selection.

I am currently working on a project which is https://github.com/johndavedecano/react-mention-plugin

I think the API is the same, you can adapt my code.

@FezVrasta funny :) heres what worked for me https://github.com/johndavedecano/react-mention-plugin/blob/master/src/mentions/Suggestions.js i created an absolute div with z-index -1, width and height of 0 then use that as the reference. Then i just dynamically update its coords using my coords props then invoke popper.scheduleUpdate

OP's problem is rather simple to fix.

Right i can update the position but i cannot do the view port boundary

The issue is that his modifiers have order: 800 which means they are applied AFTER the viewport boundary modifiers are. Just change order: 800 to order: 220 to do it immediately after default offset modifier and it will work fine.

I'm glad to say the ordering system changed in v2 馃槀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FezVrasta picture FezVrasta  路  5Comments

skitterm picture skitterm  路  5Comments

gcanabate picture gcanabate  路  3Comments

DawnWright picture DawnWright  路  4Comments

NilsEnevoldsen picture NilsEnevoldsen  路  5Comments