-
-
Notifications
You must be signed in to change notification settings - Fork 229
Pass target directly to popper #92
Pass target directly to popper #92
Conversation
|
Does this support to pass an object as reference element? |
|
This supports passing a DOM Element. That's the same thing a React Is there another possible input that we should support? I hadn't thought of any other use cases. |
|
Interesting; I didn't know about those. I suspect the only thing stopping this change from being compatible with |
The Popper target previously was assumed to be made available via context from the Manager component. Instead it can now be passed in directly via props. This change required making the popover manager in the context optional. Closes #17
There doesn't seem to be any way to use PropTypes to validate that a value is passed to a component via props *or* context. To make up for this, an Error is now thrown in that situation.
|
The I also did some cleanup, mostly around lints and organization conventions that were different from when I originally branched. |
I'm glad I looked in Pull Requests because this is exactly what I needed. I was considering not using react-popper because reliance on context requires the target and popper to be rendered in the same context. In my case, I want to render the popper in a component that receives the target via a callback.
I'm using the same pattern in my application. Controlling rendering of the popper based on state makes popper more composable and flexible for more complex scenarios. 👍 |
|
I used to do the same because of some older version of |
|
Any idea when you plan to merge this PR ? |
|
I have tried this, but the position did not seem to change when the the |
|
@ahx I think you're right. I'll look at fixing this now. |
|
I believe it should update now when the target changes. Let me know if it works. Thanks. |
|
@Gudahtt It works as expected, thank you! react-popper is much easier to use for us now. I really like this PR. 👍 |
Allows passing in a
targetprop to the Popper component directly, bypassing the need for theManagerandTargetcomponents. I implemented this feature for personal use initially, as it was needed in the development of a custom UI library.The documentation and examples have been updated. I did try to make clear that the
ManagerandTargetpattern is recommended and easier to use, and should be preferred in most cases.It was challenging to think of a good example for this feature. I saw two options; get a
refand use that, or get an element directly from the DOM. I didn't want to do the latter, because it's generally recommended to avoid that when using React. But therefwas problematic as well. If I just grabbed a ref and used it right away, the resulting component would fail to pass in atargetprop on the first render, because the ref doesn't get called until after the child component mounts. I considered briefly just usingforceUpdateto trigger a second render incomponentDidMount, but that was too confusing for an example.Instead, I side-stepped the issue by writing an example that defaulted to not showing the popper component at all. That avoids the ref issue, because the popper is always hidden on first render, so it doesn't need to be shown. However, this gave me another concern - that people would look at the README for a quick example of how to make an open/close-able popover and grab that example without considering whether using
Managerwould suit their use case better. So to avoid that circumstance, I also added an example doing the same thing withManagerandTargetfirst.