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

Properly merge incoming props #1265

Merged
merged 7 commits into from
Mar 22, 2022
Merged

Properly merge incoming props #1265

merged 7 commits into from
Mar 22, 2022

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Mar 22, 2022

This PR will allow you to pass an onClick to a component even if the component already has an onClick. This will also be called before our code. This means that you can stop the default behaviour via event.preventDefault() if you need to. This is often not recommended, but there are probably use cases for this behaviour.

Ignoring the event.preventDefault() from above, it can still be useful to add your own event listeners in case you need to know about those events.

This is only a React fix because in Vue you can already have multiple event listeners. That said, we did similar renames in the Vue code for consistency.

Fixes: #846
Fixes: #1256

concise `incomingProps`

This is going to make a bit more sense in the next commits of this
branch, hold on!
This will allow us to merge the props with a bit more control. Instead
of overriding every prop from the user' props with our props, we can now
merge event listeners.
This will essentially do the exact same thing we were doing before:
```js
let props = { ...propsTheyControl, ...propsWeControl }
```

But instead of overriding everything, we will merge the event listener
related props like `onClick`, `onKeyDown`, ...
- Rename `propsWeControl` to `ourProps`
- Rename `propsTheyControl` to `theirProps`
@vercel
Copy link

vercel bot commented Mar 22, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

headlessui-vue – ./packages/playground-vue

🔍 Inspect: https://vercel.com/tailwindlabs/headlessui-vue/C8vEahosMWBASDZiU5fQSUBxp82x
✅ Preview: https://headlessui-vue-git-merge-incoming-props-tailwindlabs.vercel.app

headlessui-react – ./packages/playground-react

🔍 Inspect: https://vercel.com/tailwindlabs/headlessui-react/5Gz8dnfmgoKw87Budwxzfsi1NAbV
✅ Preview: https://headlessui-react-git-merge-incoming-props-tailwindlabs.vercel.app

@BrianHung
Copy link

Would this also solve #1122?

Radix solves that by checking if event.preventDefault() is called with the onSelect prop

Event handler called when the user selects an item (via mouse of keyboard). Calling event.preventDefault in this handler will prevent the dropdown menu from closing when selecting that item.

https://github.com/radix-ui/primitives/blob/4ee9d362fcd5e32edbce9f43f1d41f1bf1a015c1/packages/react/menu/src/Menu.tsx#L741

https://github.com/radix-ui/primitives/blob/4ee9d362fcd5e32edbce9f43f1d41f1bf1a015c1/packages/core/primitive/src/primitive.tsx#L1-L15

@RobinMalfait
Copy link
Member Author

@BrianHung yep that would "fix" that as well. The reason I say "fix", is because you can add an onClick with an event.preventDefault(), but if you later want to close it programmatically, that's not supported.

Whether it's a good idea or not to prevent the default logic depends on the context, but this should work:

// Example on the main button
function Example() {
  return (
    <Menu>
      {({ open }) => (
        <>
          <Menu.Button
            onClick={(event) => {
              if (open) {
                console.log('Closing? How about no...')
                event.preventDefault()
              } else {
                console.log('Time to open, all good!')
              }
            }}
          >
            Button
          </Menu.Button>
          ...
        </>
      )}
    </Menu>
  )
}
// Example on an individual item
function Example() {
  return (
    <Menu>
      <Menu.Button>Toggle</Menu.Button>
      <Menu.Items>
        <Menu.Item as="button" onClick={(event) => event.preventDefault()}>
          This stays open...
        </Menu.Item>
      </Menu.Items>
    </Menu>
  )
}

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

Successfully merging this pull request may close these issues.

ComboBox.Input add additional API props [Bug]: Popover.Button is clobbering the onClick prop
2 participants