question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: Popover.Button is clobbering the `onClick` prop

See original GitHub issue

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

v1.4.1

What browser are you using?

Chrome, Firefox, etc.

Reproduction repository

https://github.com/tailwindlabs/headlessui/blob/main/packages/%40headlessui-react/src/components/popover/popover.tsx#L475

Describe your issue

Looking at the source, it seems Popover.Button is clobbering any existing onClick prop on the button.

This prevents any code to be run before the Popover.Button’s own handleClick logic.

Such code would be useful to override the Popper JS positioning logic dynamically before the popper itself is displayed, something that can’t always be done with the static options passed to usePopper(). For example, I’m trying to position the popper horizontally with respect to the event.clientX, as shown by Popper JS’s author here.

I believe calling any existing onClick prop at the top on handleClick, with the current event, would address this problem.

Thank you

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:7

github_iconTop GitHub Comments

2reactions
vnugentcommented, Mar 22, 2022

I’m having a similar issue – I need to save user selections on the Popover to a global state object before closing. This hack appears to work for me:

<div onClick={() => {
              console.log('#onclick')
              doSomething() // save states or do something imperatively
            }}>
      <Popover.Button>Apply</Popover.Button>
</div>
1reaction
RobinMalfaitcommented, Mar 22, 2022

Hey! Thank you for your bug report! Much appreciated! 🙏

This should be fixed by #1265, and will be available in the next release. The PR allows you to write your own event handlers like onClick, onKeyDown, … even if we also provide those. Your handlers will be executed before ours which gives you an opportunity to do anything to the event (even event.preventDefault()) if you would like to for whatever reason.

You can already try it using npm install @headlessui/react@insiders or yarn add @headlessui/react@insiders.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactJS - popover onClick insde data-content doesn't work
I'm using a button and inside I have an Icon and when click should popover its data-content, and it does! but the onClick...
Read more >
HeadlessUI: Popover. Part 3 - JavaScript in Plain English
Part 2: Styling an open panel's button — In this part, we are going to add the Chevron right icon at the Popover...
Read more >
LineChart chart/line
Renders a button and calls the given `onClick` function, if given, when clicked. Any children passed will be redirected inside the button. Passing...
Read more >
Ext.tip.ToolTip | Ext JS 7.3.1 - Sencha Documentation
This object holds a map of config properties that will update their binding as they are modified. For example, value is a key...
Read more >
Auto Draft - IndustrialSage
getSnapshotBeforeUpdate(p,m)),C=null!=c&&c.type===y&&null==c.key?c.props. ... fireEvent("on"+t,a)}u.wlog.error("neither dispatchEvent nor fireEvent is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found