Skip to content

Commit 87fa3c5

Browse files
authored
feat: add dropdown-close modifier (#4238)
ref #4237
1 parent b906703 commit 87fa3c5

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

packages/daisyui/src/components/dropdown.css

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
.dropdown-content {
1010
@apply absolute;
1111
}
12+
&.dropdown-close .dropdown-content,
1213
&:not(details, .dropdown-open, .dropdown-hover:hover, :focus-within) .dropdown-content,
1314
&.dropdown-hover:not(:hover)
1415
[tabindex]:first-child:focus:not(:focus-visible)
@@ -36,21 +37,24 @@
3637
}
3738
}
3839

39-
&.dropdown-open,
40-
&:not(.dropdown-hover):focus,
41-
&:focus-within {
42-
> [tabindex]:first-child {
43-
@apply pointer-events-none;
44-
}
45-
.dropdown-content {
46-
@apply opacity-100;
40+
&:not(.dropdown-close) {
41+
&.dropdown-open,
42+
&:not(.dropdown-hover):focus,
43+
&:focus-within {
44+
> [tabindex]:first-child {
45+
@apply pointer-events-none;
46+
}
47+
.dropdown-content {
48+
@apply opacity-100;
49+
scale: 100%;
50+
}
4751
}
48-
}
4952

50-
&.dropdown-hover:hover {
51-
.dropdown-content {
52-
@apply opacity-100;
53-
scale: 100%;
53+
&.dropdown-hover:hover {
54+
.dropdown-content {
55+
@apply opacity-100;
56+
scale: 100%;
57+
}
5458
}
5559
}
5660

@@ -62,14 +66,6 @@
6266
}
6367
}
6468

65-
&.dropdown-open,
66-
&:focus,
67-
&:focus-within {
68-
.dropdown-content {
69-
scale: 100%;
70-
}
71-
}
72-
7369
/* experimental */
7470
&:where([popover]) {
7571
background: #0000;
@@ -85,6 +81,7 @@
8581
@supports not (position-area: bottom) {
8682
@apply m-auto;
8783

84+
&.dropdown-close,
8885
&.dropdown-open:not(:popover-open) {
8986
@apply hidden origin-top opacity-0;
9087
scale: 95%;
@@ -95,6 +92,7 @@
9592
}
9693
}
9794

95+
&.dropdown-close,
9896
&:not(.dropdown-open, :popover-open) {
9997
@apply hidden origin-top opacity-0;
10098
scale: 95%;

packages/docs/src/routes/(routes)/components/dropdown/+page.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ classnames:
3232
desc: Opens on hover too
3333
- class: dropdown-open
3434
desc: Force open
35+
- class: dropdown-close
36+
desc: Force close
3537
---
3638

3739
<script>
@@ -527,6 +529,26 @@ The content gets displayed when the button is focused.
527529
</ul>
528530
</div>
529531
```
532+
533+
### ~Force close
534+
<div class="dropdown dropdown-close mb-32">
535+
<div tabindex="0" role="button" class="m-1 btn">Button</div>
536+
<ul tabindex="-1" class="p-2 shadow-sm menu dropdown-content z-1 bg-base-100 rounded-box w-52">
537+
<li><button>Item 1</button></li>
538+
<li><button>Item 2</button></li>
539+
</ul>
540+
</div>
541+
542+
```html
543+
<div class="$$dropdown $$dropdown-close">
544+
<div tabindex="0" role="button" class="$$btn m-1">Button</div>
545+
<ul tabindex="-1" class="$$dropdown-content $$menu bg-base-100 rounded-box z-1 w-52 p-2 shadow-sm">
546+
<li><a>Item 1</a></li>
547+
<li><a>Item 2</a></li>
548+
</ul>
549+
</div>
550+
```
551+
530552
## More examples
531553

532554
### ~Card as dropdown

packages/docs/static/llms.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ Dropdown can open a menu or any other element when the button is clicked
663663
- component: `dropdown`
664664
- part: `dropdown-content`
665665
- placement: `dropdown-start`, `dropdown-center`, `dropdown-end`, `dropdown-top`, `dropdown-bottom`, `dropdown-left`, `dropdown-right`
666-
- modifier: `dropdown-hover`, `dropdown-open`
666+
- modifier: `dropdown-hover`, `dropdown-open`, `dropdown-close`
667667

668668
#### Syntax
669669
Using details and summary

0 commit comments

Comments
 (0)