-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Allow manual closing of dropdown for links with preventDefault or possibly asynchronous operations #1630
Comments
I also noticed this happening when an Inertia |
I have the same issue with Inertia |
I encountered this with Inertia Link too. |
@RobinMalfait you think there is a solution for this? Is it an Inertia issue? |
I have the same problem with Inertia Link, somebody already solved it :)? |
@johanmolen It happens due to usage of preventDefault() so as a workaround you can try using normal anchor tag and Inertia's visit function. Make sure you import Inertia before doing this. import { Inertia } from "@inertiajs/inertia"; Before <Link href="http://example.com">Example</Link> After <a href="#" @click="Inertia.visit('http://example.com', { preserveState: true })"></a> In case you have the menu at the bottom of the page, and you want to prevent weird scroll issue before redirect due to click without preventDefault, then you can use button instead of anchor tag. <button @click="Inertia.visit('http://example.com', { preserveState: true })"></button> |
The workarounds @ravibpatel provided have one important shortcoming: right-click "open in new tab" will not work anymore. |
The issue is that For the Vue package, it looks like this was perhaps an unintentional side-effect in #1651 - the description states that when As far as I can tell, React does not support multiple click handler on the same component/element. Vue does not have this limitation, which is why this change was not introduced to the Vue package at the same time as to the React package. I understand how this is helpful for React users so they can use their own event handler, and I can understand how it can be useful to disable the default event handling both in Vue and React. Initially, I thought It's unlikely that the |
I can confirm that @RobinMalfait, any chances you could have a look at this issue? 🙏 |
Thanks for the issue and reproductions! The issue: However, we also have an escape hatch built-in where you can opt-out of default behaviour, in this case if you have an This is a typical problem you see with tools like Due to this The solution: This will allow you to imperatively call the <template>
<Menu>
<MenuButton>More</MenuButton>
<MenuItems>
<MenuItem v-slot="{ active, close }">
<Link href="/" preserve-state @click="close">Inertia Link {{ active }}</Link>
</MenuItem>
</MenuItems>
</Menu>
</template>
<script>
import { Link } from '@inertiajs/inertia-vue3'
import { Menu, MenuButton, MenuItems, MenuItem } from '@headlessui/vue'
export default {
components: {
Link,
Menu,
MenuButton,
MenuItems,
MenuItem,
},
}
</script> For React it is similar, but it is exposed from the render prop instead. import { Menu } from '@headlessui/react'
import { MyCustomLink } from './MyCustomLink'
function MyMenu() {
return (
<Menu>
<Menu.Button>Terms</Menu.Button>
<Menu.Items>
<Menu.Item>
{({ close }) => (
<MyCustomLink href="/" onClick={close}>
Read and accept
</MyCustomLink>
)}
</Menu.Item>
</Menu.Items>
</Menu>
)
} This should be fixed by #1897, and will be available in the next release. You can already try it using:
|
@RobinMalfait would it be possible to expose this for |
I think it could be interesting to add it in the documentation also @RobinMalfait, I don't see any references to the |
Hey! This should now be documented, sorry about that! |
@RobinMalfait can this be added to Combobox as well? My use-case is I am appending an element to the ComboboxOptions and I'd like to close the dropdown when I click that element. |
Hi @RobinMalfait, any updates on allowing manual close for |
Using
|
@claide |
@ian-travers I also encountered this issue with Inertia Link in newer versions. |
This does not close the menu on the latest version. I used v <MenuItem v-slot="{ close }">
<NuxtLink
@click.native="close"
to="/admin/account-settings"
>
Settings
</NuxtLink>
</MenuItem> |
Same situation here. Did you find a workaround? |
I think at that time I used something like
|
@RobinMalfait I am on the latest version of Headless UI and of Inertia JS which is available till today's date, still the
|
Is there any updates to this issue? Running into problems with Inertia/VueJS. Not able to force the closure of the menu when using Link unfortunately. |
This comment was marked as spam.
This comment was marked as spam.
|
[Vue] @cronin4392, @hymair, @mariegodon For Listbox: <template>
<Listbox v-model="...">
<ListboxButton ref="listboxBtnRef">Trigger button</ListboxButton>
<ListboxOptions>
<ListboxOption
v-for="..."
>
<NuxtLink
to="/hello"
@click="listboxBtnRef.$el.click()"
>
Link text
</NuxtLink>
</ListboxOption>
</ListboxOptions>
</Listbox>
</template>
<script setup>
const listboxBtnRef = ref(null);
</script> I'm just programmatically triggering a click on a ListboxButton For ComboBox: <template>
<Combobox v-model="...">
<ComboboxInput ref="comboboxInputRef" />
<ComboboxOptions>
<ComboboxOption
v-for="..."
>
<NuxtLink
to="/hello"
@click="comboboxInputRef.$el.blur()"
>
Link text
</NuxtLink>
</ComboboxOption>
</ComboboxOptions>
</Combobox>
</template>
<script setup>
const comboboxInputRef = ref(null);
</script> I haven't tested it, but I believe I can trigger the blur() action on the ComboboxInput |
What package within Headless UI are you using?
@headlessui/react
What version of that package are you using?
v1.6.6
What browser are you using?
N/A
Reproduction URL
https://github.com/agusterodin/headless-ui-dropdown-not-disappearing-after-click-bug-reproduction
Describe your issue
I am overriding a link's behavior with
event.preventDefault()
so that if the user clicks the link, logic runs to check if the user has any unsaved changes. Unfortunately, a recent update to Headless UI makes it so that the dropdown no longer disappears when clicking a link withevent.preventDefault()
. I could just make this a button element instead so I don't needevent.preventDefault()
but then I no longer get "open in new tab" functionality and other benefits that come with using proper semantic HTML links.Screen.Recording.2022-06-30.at.2.24.24.PM.mov
I noticed that the popover component has a
close
render prop for situations where you need to manually close the popover. Could we pretty please get aclose
render prop for Dropdown.Item as well?Thanks in advance. I absolutely love what you have built with the Tailwind ecosystem.
The text was updated successfully, but these errors were encountered: