Skip to content

Commit 32e919e

Browse files
committed
feat: icon-only drawer sidebar, new variants is-drawer-open and is-drawer-close
1 parent 80667b1 commit 32e919e

File tree

5 files changed

+261
-36
lines changed

5 files changed

+261
-36
lines changed

packages/daisyui/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { base, components, utilities } from "./imports.js"
77

88
export default plugin.withOptions(
99
(options) => {
10-
return ({ addBase, addComponents, addUtilities }) => {
10+
return ({ addBase, addComponents, addUtilities, addVariant }) => {
1111
const {
1212
include,
1313
exclude,
@@ -41,6 +41,16 @@ export default plugin.withOptions(
4141
if (!shouldIncludeItem(name)) return
4242
item({ addUtilities, prefix })
4343
})
44+
45+
// drawer variants. Can not be nested in layers so defined here
46+
addVariant(
47+
"is-drawer-close",
48+
"&:where(.drawer-toggle:not(:checked) ~ .drawer-side, .drawer-toggle:not(:checked) ~ .drawer-side *)",
49+
)
50+
addVariant(
51+
"is-drawer-open",
52+
"&:where(.drawer-toggle:checked ~ .drawer-side, .drawer-toggle:checked ~ .drawer-side *)",
53+
)
4454
}
4555
},
4656
() => ({

packages/daisyui/src/components/drawer.css

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
@apply col-start-2 row-start-1 min-w-0;
99
}
1010

11+
:where(.drawer-side) {
12+
@apply overflow-x-hidden overflow-y-hidden;
13+
}
14+
1115
.drawer-side {
12-
@apply pointer-events-none invisible fixed start-0 top-0 z-10 col-start-1 row-start-1 grid w-full grid-cols-1 grid-rows-1 items-start justify-items-start overflow-x-hidden overflow-y-hidden overscroll-contain opacity-0;
16+
@apply pointer-events-none invisible fixed start-0 top-0 z-10 col-start-1 row-start-1 grid w-full grid-cols-1 grid-rows-1 items-start justify-items-start overscroll-contain opacity-0;
1317
transition:
1418
opacity 0.2s ease-out 0.1s allow-discrete,
1519
visibility 0.3s ease-out 0.1s allow-discrete;
@@ -27,7 +31,9 @@
2731

2832
> *:not(.drawer-overlay) {
2933
@apply will-change-transform;
30-
transition: translate 0.3s ease-out;
34+
transition:
35+
translate 0.3s ease-out,
36+
width 0.2s ease-out;
3137
translate: -100%;
3238

3339
[dir="rtl"] & {
@@ -82,7 +88,7 @@
8288
}
8389

8490
.drawer-open {
85-
> .drawer-side {
91+
:where(& > .drawer-side) {
8692
@apply overflow-y-auto;
8793
}
8894

packages/docs/src/lib/mdsvex/layout-components.svelte

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,59 @@
340340
data-tip={$t("CSS variable")}
341341
>
342342
<span class="badge badge-soft badge-info badge-sm whitespace-nowrap">
343+
<svg
344+
xmlns="http://www.w3.org/2000/svg"
345+
viewBox="0 0 24 24"
346+
class="size-3"
347+
>
348+
<g
349+
stroke-linejoin="round"
350+
stroke-linecap="round"
351+
stroke-width="2"
352+
fill="none"
353+
stroke="currentColor"
354+
>
355+
<path d="M10.1 2.18a9.93 9.93 0 0 1 3.8 0"></path>
356+
<path d="M17.6 3.71a9.95 9.95 0 0 1 2.69 2.7"></path>
357+
<path d="M21.82 10.1a9.93 9.93 0 0 1 0 3.8"></path>
358+
<path d="M20.29 17.6a9.95 9.95 0 0 1-2.7 2.69"></path>
359+
<path d="M13.9 21.82a9.94 9.94 0 0 1-3.8 0"></path>
360+
<path d="M6.4 20.29a9.95 9.95 0 0 1-2.69-2.7"></path>
361+
<path d="M2.18 13.9a9.93 9.93 0 0 1 0-3.8"></path>
362+
<path d="M3.71 6.4a9.95 9.95 0 0 1 2.7-2.69"></path>
363+
<circle cx="12" cy="12" r="1"></circle>
364+
</g>
365+
</svg>
343366
{@html $t("CSS variable")}
344367
</span>
345368
</div>
346369
{/if}
370+
{#if type == "variant"}
371+
<div
372+
class="tooltip cursor-help before:text-[0.6875rem]"
373+
data-tip={$t("Use like variant:utility to apply a utility conditionally")}
374+
>
375+
<span class="badge badge-soft badge-sm whitespace-nowrap">
376+
<svg
377+
xmlns="http://www.w3.org/2000/svg"
378+
viewBox="0 0 24 24"
379+
class="size-3"
380+
>
381+
<g
382+
stroke-linejoin="round"
383+
stroke-linecap="round"
384+
stroke-width="2"
385+
fill="none"
386+
stroke="currentColor"
387+
>
388+
<path d="M9.183 6.117a6 6 0 1 0 4.511 3.986"></path>
389+
<path d="M14.813 17.883a6 6 0 1 0 -4.496 -3.954"></path>
390+
</g>
391+
</svg>
392+
{@html $t("Variant")}
393+
</span>
394+
</div>
395+
{/if}
347396
</td>
348397
<td class="w-8/12 min-w-[20rem]">
349398
{item.desc}

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

Lines changed: 126 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ classnames:
2222
modifier:
2323
- class: drawer-open
2424
desc: Forces the drawer to be open
25+
variant:
26+
- class: "is-drawer-open:"
27+
desc: Applies styles when the drawer is open
28+
- class: "is-drawer-close:"
29+
desc: Applies styles when the drawer is closed
2530
---
2631

2732
<script>
@@ -60,13 +65,13 @@ You can check/uncheck the checkbox using JavaScript or by clicking the `label` t
6065
### ~Drawer
6166

6267
<div class="drawer h-56 rounded overflow-hidden">
63-
<input id="my-drawer" type="checkbox" class="drawer-toggle" />
68+
<input id="my-drawer-1" type="checkbox" class="drawer-toggle" />
6469
<div class="flex flex-col items-center justify-center drawer-content">
65-
<label for="my-drawer" class="btn btn-primary drawer-button">Open drawer</label>
70+
<label for="my-drawer-1" class="btn drawer-button">Open drawer</label>
6671
</div>
6772
<div class="drawer-side z-1002">
68-
<label for="my-drawer" aria-label="close sidebar" class="drawer-overlay"></label>
69-
<ul class="menu p-4 w-60 md:w-80 min-h-full bg-base-200 text-base-content">
73+
<label for="my-drawer-1" aria-label="close sidebar" class="drawer-overlay"></label>
74+
<ul class="menu p-4 w-60 md:w-80 min-h-full bg-base-200">
7075
<li><button>Sidebar Item 1</button></li>
7176
<li><button>Sidebar Item 2</button></li>
7277
</ul>
@@ -75,14 +80,14 @@ You can check/uncheck the checkbox using JavaScript or by clicking the `label` t
7580

7681
```html
7782
<div class="$$drawer">
78-
<input id="my-drawer" type="checkbox" class="$$drawer-toggle" />
83+
<input id="my-drawer-1" type="checkbox" class="$$drawer-toggle" />
7984
<div class="$$drawer-content">
8085
<!-- Page content here -->
81-
<label for="my-drawer" class="$$btn $$btn-primary $$drawer-button">Open drawer</label>
86+
<label for="my-drawer-1" class="$$btn $$$$drawer-button">Open drawer</label>
8287
</div>
8388
<div class="$$drawer-side">
84-
<label for="my-drawer" aria-label="close sidebar" class="$$drawer-overlay"></label>
85-
<ul class="$$menu bg-base-200 text-base-content min-h-full w-80 p-4">
89+
<label for="my-drawer-1" aria-label="close sidebar" class="$$drawer-overlay"></label>
90+
<ul class="$$menu bg-base-200 min-h-full w-80 p-4">
8691
<!-- Sidebar content here -->
8792
<li><a>Sidebar Item 1</a></li>
8893
<li><a>Sidebar Item 2</a></li>
@@ -96,11 +101,11 @@ You can check/uncheck the checkbox using JavaScript or by clicking the `label` t
96101
#### Change screen size to show/hide menu
97102

98103
<div class="drawer h-56 rounded overflow-hidden">
99-
<input id="my-drawer-3" type="checkbox" class="drawer-toggle" />
104+
<input id="my-drawer-2" type="checkbox" class="drawer-toggle" />
100105
<div class="flex flex-col drawer-content">
101106
<div class="w-full navbar bg-base-300">
102107
<div class="flex-none lg:hidden">
103-
<label for="my-drawer-3" aria-label="open sidebar" class="btn btn-square btn-ghost">
108+
<label for="my-drawer-2" aria-label="open sidebar" class="btn btn-square btn-ghost">
104109
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" class="inline-block w-6 h-6 stroke-current"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path></svg>
105110
</label>
106111
</div>
@@ -115,7 +120,7 @@ You can check/uncheck the checkbox using JavaScript or by clicking the `label` t
115120
<div class="flex justify-center items-center grow">Content</div>
116121
</div>
117122
<div class="drawer-side z-1002">
118-
<label for="my-drawer-3" aria-label="close sidebar" class="drawer-overlay"></label>
123+
<label for="my-drawer-2" aria-label="close sidebar" class="drawer-overlay"></label>
119124
<ul class="p-4 menu w-60 md:w-80 min-h-full bg-base-200">
120125
<li><button>Sidebar Item 1</button></li>
121126
<li><button>Sidebar Item 2</button></li>
@@ -125,12 +130,12 @@ You can check/uncheck the checkbox using JavaScript or by clicking the `label` t
125130

126131
```html
127132
<div class="$$drawer">
128-
<input id="my-drawer-3" type="checkbox" class="$$drawer-toggle" />
133+
<input id="my-drawer-2" type="checkbox" class="$$drawer-toggle" />
129134
<div class="$$drawer-content flex flex-col">
130135
<!-- Navbar -->
131136
<div class="$$navbar bg-base-300 w-full">
132137
<div class="flex-none lg:hidden">
133-
<label for="my-drawer-3" aria-label="open sidebar" class="$$btn $$btn-square $$btn-ghost">
138+
<label for="my-drawer-2" aria-label="open sidebar" class="$$btn $$btn-square $$btn-ghost">
134139
<svg
135140
xmlns="http://www.w3.org/2000/svg"
136141
fill="none"
@@ -159,7 +164,7 @@ You can check/uncheck the checkbox using JavaScript or by clicking the `label` t
159164
Content
160165
</div>
161166
<div class="$$drawer-side">
162-
<label for="my-drawer-3" aria-label="close sidebar" class="$$drawer-overlay"></label>
167+
<label for="my-drawer-2" aria-label="close sidebar" class="$$drawer-overlay"></label>
163168
<ul class="$$menu bg-base-200 min-h-full w-80 p-4">
164169
<!-- Sidebar content here -->
165170
<li><a>Sidebar Item 1</a></li>
@@ -174,13 +179,13 @@ You can check/uncheck the checkbox using JavaScript or by clicking the `label` t
174179
#### Sidebar is always visible on large screen, can be toggled on small screen because of lg:drawer-open class
175180

176181
<div class="drawer lg:drawer-open h-56 rounded overflow-hidden">
177-
<input id="my-drawer-2" type="checkbox" class="drawer-toggle" />
182+
<input id="my-drawer-3" type="checkbox" class="drawer-toggle" />
178183
<div class="flex flex-col items-center justify-center drawer-content">
179-
<label for="my-drawer-2" class="btn btn-primary drawer-button lg:hidden">Open drawer</label>
184+
<label for="my-drawer-3" class="btn drawer-button lg:hidden">Open drawer</label>
180185
</div>
181186
<div class="drawer-side max-lg:z-1002">
182-
<label for="my-drawer-2" aria-label="close sidebar" class="drawer-overlay"></label>
183-
<ul class="menu p-4 w-60 md:w-80 min-h-full bg-base-200 text-base-content">
187+
<label for="my-drawer-3" aria-label="close sidebar" class="drawer-overlay"></label>
188+
<ul class="menu p-4 w-60 md:w-80 min-h-full bg-base-200">
184189
<li><button>Sidebar Item 1</button></li>
185190
<li><button>Sidebar Item 2</button></li>
186191
</ul>
@@ -189,16 +194,16 @@ You can check/uncheck the checkbox using JavaScript or by clicking the `label` t
189194

190195
```html
191196
<div class="$$drawer lg:$$drawer-open">
192-
<input id="my-drawer-2" type="checkbox" class="$$drawer-toggle" />
197+
<input id="my-drawer-3" type="checkbox" class="$$drawer-toggle" />
193198
<div class="$$drawer-content flex flex-col items-center justify-center">
194199
<!-- Page content here -->
195-
<label for="my-drawer-2" class="$$btn $$btn-primary $$drawer-button lg:hidden">
200+
<label for="my-drawer-3" class="$$btn $$$$drawer-button lg:hidden">
196201
Open drawer
197202
</label>
198203
</div>
199204
<div class="$$drawer-side">
200-
<label for="my-drawer-2" aria-label="close sidebar" class="$$drawer-overlay"></label>
201-
<ul class="$$menu bg-base-200 text-base-content min-h-full w-80 p-4">
205+
<label for="my-drawer-3" aria-label="close sidebar" class="$$drawer-overlay"></label>
206+
<ul class="$$menu bg-base-200 min-h-full w-80 p-4">
202207
<!-- Sidebar content here -->
203208
<li><a>Sidebar Item 1</a></li>
204209
<li><a>Sidebar Item 2</a></li>
@@ -207,16 +212,107 @@ You can check/uncheck the checkbox using JavaScript or by clicking the `label` t
207212
</div>
208213
```
209214

215+
216+
### ~Icon-only drawer when it's closed. Using is-drawer-close and is-drawer-open variants
217+
218+
#### In this example instead of completely hiding the drawer sidebar, we only show the icons when the drawer is closed. To add styles based on the state of the drawer, we use the `is-drawer-open` and `is-drawer-close` variants. For example `is-drawer-close:hidden` or `is-drawer-open:w-80`. Also we have tooltips when the drawer is closed and a switch button to open/close the drawer and rotates the button icon based on the state of the drawer.
219+
220+
<div class="drawer drawer-open h-56 rounded overflow-hidden">
221+
<input id="my-drawer-4" type="checkbox" class="drawer-toggle" />
222+
<div class="h-56 drawer-content">
223+
<!-- Page content here -->
224+
</div>
225+
<div class="drawer-side is-drawer-close:overflow-visible h-full max-lg:z-1002">
226+
<label for="my-drawer-4" aria-label="close sidebar" class="drawer-overlay"></label>
227+
<div class="is-drawer-close:w-17 is-drawer-open:w-64 h-full bg-base-200 flex flex-col items-start">
228+
<ul class="menu p-4 w-full grow">
229+
<li>
230+
<button class="is-drawer-close:tooltip is-drawer-close:tooltip-right rtl:is-drawer-close:tooltip-left" data-tip="Homepage">
231+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="inline-block size-4 my-1.5">
232+
<path d="M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8"></path>
233+
<path d="M3 10a2 2 0 0 1 .709-1.528l7-5.999a2 2 0 0 1 2.582 0l7 5.999A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
234+
</svg>
235+
<span class="is-drawer-close:hidden">Homepage</span>
236+
</button>
237+
</li>
238+
<li>
239+
<button class="is-drawer-close:tooltip is-drawer-close:tooltip-right rtl:is-drawer-close:tooltip-left" data-tip="Settings">
240+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="inline-block size-4 my-1.5">>
241+
<path d="M20 7h-9"></path>
242+
<path d="M14 17H5"></path>
243+
<circle cx="17" cy="17" r="3"></circle>
244+
<circle cx="7" cy="7" r="3"></circle>
245+
</svg>
246+
<span class="is-drawer-close:hidden">Settings</span>
247+
</button>
248+
</li>
249+
</ul>
250+
<div class="m-3.5 is-drawer-close:tooltip is-drawer-close:tooltip-right rtl:is-drawer-close:tooltip-left" data-tip="Open">
251+
<label for="my-drawer-4" class="btn btn-ghost btn-circle drawer-button is-drawer-open:rotate-y-180">
252+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="inline-block size-4 my-1.5">
253+
<path d="M4 4m0 2a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2z"></path>
254+
<path d="M9 4v16"></path>
255+
<path d="M14 10l2 2l-2 2"></path>
256+
</svg>
257+
</label>
258+
</div>
259+
</div>
260+
</div>
261+
</div>
262+
263+
```html
264+
<div class="$$drawer $$drawer-open">
265+
<input id="my-drawer-4" type="checkbox" class="$$drawer-toggle" />
266+
<div class="$$drawer-content">
267+
<!-- Page content here -->
268+
</div>
269+
270+
<div class="$$drawer-side is-drawer-close:overflow-visible">
271+
<label for="my-drawer-4" aria-label="close sidebar" class="$$drawer-overlay"></label>
272+
<div class="is-drawer-close:w-17 is-drawer-open:w-64 bg-base-200 flex flex-col items-start min-h-full">
273+
<!-- Sidebar content here -->
274+
<ul class="$$menu w-full grow p-4">
275+
276+
<!-- list item -->
277+
<li>
278+
<button class="is-drawer-close:tooltip is-drawer-close:tooltip-right" data-tip="Homepage">
279+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="inline-block size-4 my-1.5"><path d="M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8"></path><path d="M3 10a2 2 0 0 1 .709-1.528l7-5.999a2 2 0 0 1 2.582 0l7 5.999A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path></svg>
280+
<span class="is-drawer-close:hidden">Homepage</span>
281+
</button>
282+
</li>
283+
284+
<!-- list item -->
285+
<li>
286+
<button class="is-drawer-close:tooltip is-drawer-close:tooltip-right" data-tip="Settings">
287+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="inline-block size-4 my-1.5"><path d="M20 7h-9"></path><path d="M14 17H5"></path><circle cx="17" cy="17" r="3"></circle><circle cx="7" cy="7" r="3"></circle></svg>
288+
<span class="is-drawer-close:hidden">Settings</span>
289+
</button>
290+
</li>
291+
</ul>
292+
293+
<!-- button to open/close drawer -->
294+
<div class="m-3.5 is-drawer-close:tooltip is-drawer-close:tooltip-right" data-tip="Open">
295+
<label for="my-drawer-4" class="$$btn $$btn-ghost $$btn-circle $$drawer-button is-drawer-open:rotate-y-180">
296+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" stroke-linejoin="round" stroke-linecap="round" stroke-width="2" fill="none" stroke="currentColor" class="inline-block size-4 my-1.5"><path d="M4 4m0 2a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2z"></path><path d="M9 4v16"></path><path d="M14 10l2 2l-2 2"></path></svg>
297+
</label>
298+
</div>
299+
300+
</div>
301+
</div>
302+
</div>
303+
```
304+
305+
210306
### ~Drawer that opens from right side of page
211307

212308
<div class="drawer drawer-end h-56 rounded overflow-hidden">
213-
<input id="my-drawer-4" type="checkbox" class="drawer-toggle" />
309+
<input id="my-drawer-5" type="checkbox" class="drawer-toggle" />
214310
<div class="flex flex-col items-center justify-center drawer-content">
215-
<label for="my-drawer-4" class="btn btn-primary drawer-button">Open drawer</label>
311+
<label for="my-drawer-5" class="btn drawer-button">Open drawer</label>
216312
</div>
217313
<div class="drawer-side z-1002">
218-
<label for="my-drawer-4" aria-label="close sidebar" class="drawer-overlay"></label>
219-
<ul class="menu p-4 w-60 md:w-80 min-h-full bg-base-200 text-base-content">
314+
<label for="my-drawer-5" aria-label="close sidebar" class="drawer-overlay"></label>
315+
<ul class="menu p-4 w-60 md:w-80 min-h-full bg-base-200">
220316
<li><button>Sidebar Item 1</button></li>
221317
<li><button>Sidebar Item 2</button></li>
222318
</ul>
@@ -225,14 +321,14 @@ You can check/uncheck the checkbox using JavaScript or by clicking the `label` t
225321

226322
```html
227323
<div class="$$drawer $$drawer-end">
228-
<input id="my-drawer-4" type="checkbox" class="$$drawer-toggle" />
324+
<input id="my-drawer-5" type="checkbox" class="$$drawer-toggle" />
229325
<div class="$$drawer-content">
230326
<!-- Page content here -->
231-
<label for="my-drawer-4" class="$$drawer-button $$btn $$btn-primary">Open drawer</label>
327+
<label for="my-drawer-5" class="$$drawer-button $$btn $$btn-primary">Open drawer</label>
232328
</div>
233329
<div class="$$drawer-side">
234-
<label for="my-drawer-4" aria-label="close sidebar" class="$$drawer-overlay"></label>
235-
<ul class="$$menu bg-base-200 text-base-content min-h-full w-80 p-4">
330+
<label for="my-drawer-5" aria-label="close sidebar" class="$$drawer-overlay"></label>
331+
<ul class="$$menu bg-base-200 min-h-full w-80 p-4">
236332
<!-- Sidebar content here -->
237333
<li><a>Sidebar Item 1</a></li>
238334
<li><a>Sidebar Item 2</a></li>

0 commit comments

Comments
 (0)