How to open a dropdown menu automatically #782
Answered
by
shyakadavis
rhscjohn-dev
asked this question in
Help
-
Trying to get a dropdown menu to display with out using a button. According to the Primitives API Reference, on the DropdownMenu.root, there is a property called - open. I am assuming that setting that to 'true' would automatically display the dropdown menu. Unfortunately that does not seem the case. Code.
Maybe this is not the proper way? Any ideas on how to do this? Thanks. |
Beta Was this translation helpful? Give feedback.
Answered by
shyakadavis
Feb 21, 2024
Replies: 1 comment 1 reply
-
Hi @rhscjohn-dev ;
Simply do something like this: <script lang="ts">
import * as DropdownMenu from "$lib/components/ui/dropdown-menu";
let open = true;
</script>
<DropdownMenu.Root bind:open closeOnOutsideClick={false} closeOnItemClick={false}>
<DropdownMenu.Trigger class="hidden">Open</DropdownMenu.Trigger>
<DropdownMenu.Content>
<DropdownMenu.Group>
<DropdownMenu.Label>My Account</DropdownMenu.Label>
<DropdownMenu.Separator />
<DropdownMenu.Item>Profile</DropdownMenu.Item>
<DropdownMenu.Item>Billing</DropdownMenu.Item>
<DropdownMenu.Item>Team</DropdownMenu.Item>
<DropdownMenu.Item>Subscription</DropdownMenu.Item>
</DropdownMenu.Group>
</DropdownMenu.Content>
</DropdownMenu.Root> I think that covers most of the edge-cases. Does this meet your needs? LMK how it goes. 🙂 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
rhscjohn-dev
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @rhscjohn-dev ;
open
prop to a state variable.Trigger
component, you still need it present so that theContent
knows which place to anchor itself unto, and as a hack/workaround, just use css to hide it. Couple of problems with this, though:closeOnOutsideClick
prop tofalse
.closeOnItemClick
prop tofalse
.Simply d…