Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stick popover with element that is not child of popover #28

Open
hern12 opened this issue Feb 20, 2019 · 8 comments
Open

How to stick popover with element that is not child of popover #28

hern12 opened this issue Feb 20, 2019 · 8 comments

Comments

@hern12
Copy link

hern12 commented Feb 20, 2019

I use contentLocation and get client position from another element that it is not the child of popover component it shows correctly when open but if I scroll up or down it follows too how to fix this. Thank you

@kovinchuk
Copy link

+1

@Russellmd
Copy link

have the same issue

@BudgieInWA
Copy link

If this is still relevant to anyone, I made a little wrapper which swaps the API around for this use case:

const RefTarget = React.forwardRef(({ target }, ref) => {
  if (typeof ref === 'function') ref(target); // Forward target to callback refs.
  else ref.current = target; // Forward target to object refs.
  return null; // Don't render anything.
});

const TargetedPopover = ({ targetElement, children, ...props }) =>
  <Popover isOpen={true} content={children} {...props}>
    <RefTarget target={targetElement} />
  </Popover>

Use it like this:

const MyPage = () => {
  const [target, setTarget] = useState(null); // Reference a DOM node however you like.
  return (
    <article>
      <h1>Hi</h1>
      <TargetedPopover targetElement={target}>This is the Popover content.</TargetedPopover>
      <MyFancyComponent childRef={setTarget} />
    </article>
  );
}

@alexkatz
Copy link
Owner

I'm not quite sure I understand what the issue here is.

Here's a code sandbox demonstrating the ability to position a popover that doesn't follow the target when scrolling, using the contentLocation prop.

@BudgieInWA
Copy link

I'm not quite sure I understand what the issue here is.

The problem is that we do want to follow an element, but we don't want to nest it under <Popover /> for whatever reason.

Here's a code sandbox demonstrating the ability to position a popover that doesn't follow the target when scrolling, using the contentLocation prop.

This demonstrates our problem, because our desired behaviour is that we create a popover that does follow some element that is already existing.

Supporting something like a targetElement prop would be very ergonomic for this use case.

@alexkatz
Copy link
Owner

@BudgieInWA In that case, the wrapper you wrote above is a clever way to do this. I may try fiddling with some kind of targetElement prop to use as an alternative to children. It's a decent idea, and I can understand situations where one may not have the desire or ability wrap the Popover component around existing JSX.

It seemed like the trouble @hern12 was having involved a popover following its target element on scroll when they'd rather have it remain stationary. That's what I tried addressing in the sandbox.

@BudgieInWA
Copy link

It seemed like the trouble @hern12 was having involved a popover following its target element on scroll when they'd rather have it remain stationary. That's what I tried addressing in the sandbox.

Ah, I had missed that. Thanks for the example, direct control like that does look powerful too.

@hern12
Copy link
Author

hern12 commented Aug 5, 2022

It's been a long time I forgot my problem lol. Anyway thank you so much for reply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants