main
html 69 lines 1.77 KB
Raw
1 <!--
2 Reusable Dropdown Component
3
4 Usage:
5 <x-component data-component="ui/dropdown" data-props='{"id": "my-dropdown"}'>
6 <template x-slot:trigger>
7 <button>Click me</button>
8 </template>
9 <template x-slot:menu>
10 <button class="dropdown-item" @click="...">Item 1</button>
11 <div class="dropdown-separator"></div>
12 <button class="dropdown-item" @click="...">Item 2</button>
13 </template>
14 </x-component>
15
16 Or use the simpler inline approach with x-data:
17 <div class="dropdown" x-data="{ open: false }" @click.outside="open = false">
18 <button class="dropdown-trigger" @click="open = !open">...</button>
19 <div class="dropdown-menu" x-show="open" x-transition>...</div>
20 </div>
21 -->
22 <html>
23 <head>
24 <!-- Dropdown uses global styles from index.css -->
25 </head>
26 <body>
27 <div
28 class="dropdown"
29 x-data="{
30 open: false,
31 toggle() { this.open = !this.open; },
32 close() { this.open = false; }
33 }"
34 @click.outside="close()"
35 @keydown.escape.window="close()"
36 >
37 <!-- Trigger slot -->
38 <div class="dropdown-trigger-wrapper" @click="toggle()">
39 <slot name="trigger">
40 <button class="dropdown-trigger" type="button">
41 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
42 <path d="M6 9l6 6 6-6"/>
43 </svg>
44 </button>
45 </slot>
46 </div>
47
48 <!-- Menu slot -->
49 <div
50 class="dropdown-menu"
51 x-show="open"
52 @click="close()"
53 >
54 <slot name="menu">
55 <!-- Default empty menu -->
56 </slot>
57 </div>
58 </div>
59
60 <style>
61 /* Component-specific styles (layout helpers) */
62 .dropdown-trigger-wrapper {
63 display: contents;
64 }
65
66 </style>
67 </body>
68 </html>
69