Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Conversation

@FezVrasta
Copy link
Member

@FezVrasta FezVrasta commented Apr 6, 2020

// @flow
import * as React from 'react';
import { usePopper } from 'react-popper';

const App = () => {
  const [referenceElement, setReferenceElement] = React.useState<?Element>(null);
  const [popperElement, setPopperElement] = React.useState<?HTMLElement>(null);
  const [arrowElement, setArrowElement] = React.useState<?HTMLElement>(null);

  const { styles, attributes } = usePopper(referenceElement, popperElement, {
    modifiers: [
      { name: 'arrow', options: { element: arrowElement } },
    ],
  });

  return (
    <>
      <button ref={setReferenceElement}>Reference Element</button>
      <div ref={setPopperElement} style={styles.popper} {...attributes.popper}>
        Popper Element
        <div ref={setArrowElement} style={styles.arrow} />
      </div>
    </>
  );
}

CodeSandbox demo:
https://codesandbox.io/s/cold-night-u1oji

@atomiks

This comment has been minimized.

@FezVrasta

This comment has been minimized.

@FezVrasta FezVrasta force-pushed the feat/use-popper branch 2 times, most recently from 719e5f4 to fcd1259 Compare April 6, 2020 10:03
@bengry

This comment has been minimized.

@FezVrasta

This comment has been minimized.

@bengry

This comment has been minimized.

@FezVrasta

This comment has been minimized.

@bengry

This comment has been minimized.

@FezVrasta

This comment has been minimized.

@bengry

This comment has been minimized.

@FezVrasta

This comment has been minimized.

@atomiks
Copy link
Collaborator

atomiks commented Apr 6, 2020

Looks great imo

@FezVrasta

This comment has been minimized.

@FezVrasta

This comment has been minimized.

@atomiks

This comment has been minimized.

@FezVrasta

This comment has been minimized.

@FezVrasta
Copy link
Member Author

FezVrasta commented Apr 8, 2020

So, I suppose the PR is ready for a final review. (I need to fix the tests though)

@FezVrasta
Copy link
Member Author

Tests fixed, I replaced Enzyme with react-testing-library since it seems to work better with modern React versions. I also added tests for the new hook.

@FezVrasta FezVrasta merged commit e093a2a into master Apr 9, 2020
@FezVrasta FezVrasta deleted the feat/use-popper branch April 9, 2020 13:43
@imballinst
Copy link

imballinst commented Apr 18, 2020

hi @FezVrasta, thank you for this amazing work! Question: why does in the CodeSandbox demo, the div for arrow is located below the popper instead of above the popper? Is this a bug from https://github.com/popperjs/popper-core?

I also face the same thing in my local, but I don't know whether it's because of this lib, upstream repository, or I'm configuring something wrong, though I copy-pasted exactly the same code like in the documentation for usePopper. Thanks again!

@atomiks
Copy link
Collaborator

atomiks commented Apr 18, 2020

@imballinst Popper only positions the arrow along a single axis in order to center it to the reference. For vertical placements it's the x axis. It's your responsibility to position it on the other axis in CSS.

@FezVrasta
Copy link
Member Author

I’m sorry I’m from mobile so I can’t look into it, but make sure you are applying the correct CSS to position the arrow. Popper provides only the offset to center it. You can look at the popper.js.org tutorial

@imballinst
Copy link

@atomiks @FezVrasta Ah, okay! Got it. That should be enough as a clue for me. Thanks and sorry for the noise. Have a nice weekend!

@pleerock
Copy link

shouldn't useRef be used instead of useState for element references?

@FezVrasta
Copy link
Member Author

useRef allows to react when the reference element changes identity.

@yuriy-sng
Copy link

yuriy-sng commented Jun 7, 2020

Is the demo's

// FIXME: we probably need some logic in the hook to detect when the provided elements are null

still applicable? I'm having some issues setting referenceElement to null in my code.

@FezVrasta
Copy link
Member Author

Is the demo's

// FIXME: we probably need some logic in the hook to detect when the provided elements are null

still applicable? I'm having some issues setting referenceElement to null in my code.

It should work with null elements

@sarathms
Copy link

The codesanbox example linked above doesn't seem to work anymore. This is what I see:
 - 43

How to get it to work?

@FezVrasta
Copy link
Member Author

It's working, it's just a silly example where the popper sits below the button 😅

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants