Drawer
Drawer
lets you create accessible drawers that slide in and out đ.
-->
Drawer
lets you create accessible drawers that slide in and out đ.
You just have to specify an id
for the Drawer to get started.
We donât force you into a predefined layout in your drawer content so you can put your custom close button anywhere you desire.
Drawers can be placed on the top
, bottom
, left
, and right
of the screen. Just add placement
to DrawerContent
to change its placement.
You can use static
, absolute
, and fixed
to adjust how drawers show up.
static
is good for disclosureabsolute
is great for megamenusfixed
is awesome for sitewide navigationStatic Drawer Content
This content is placed in the normal document flow.
Absolute Drawer Content
This content is placed relative to the nearest ancestor with position: relative
.
Fixed Drawer Content
This content is placed relative to the nearest stacking context.
Drawer
designed to be as accessible as possible from the get-go. Allow us to explain what these features are.
First â the most basic â you can close the drawer by clicking outside or with the Esc button)
Try it by opening the drawer and press the Esc button!
Unlike many other drawer components, Drawer
helps you focus on the right thing at the right time.
DrawerContent
so keyboard users and screen readers can start interacting with it immediately.DrawerTrigger
so keyboard and screen reader users navigate to the next item without using a mouse.DrawerContent
: Focus is handled differently depending on its position
. For more details, see the accessibility tab.Get a feel of how we handle focus by tabbing through these drawers
Static Drawer Content
Absolute Drawer Content
Fixed Drawer Content
Drawers are usually dialogs so we built them with an implict dialog
role. (This can be changed if you wish to).
And since dialogs need accessible names, we also built-in an accessibility warning and wrote a section to help you understand how to create these names.
As you can see, we are pretty serious about accessibility!
A basic drawer can be created with DrawerTrigger
and DrawerContent
.
When creating a drawer, you need to pass the same value to the triggerâs target
and contentâs id
property.
You can create a close button with DrawerCloseButton
.
Use Splendid Layouts
to create a layout for your drawer
content easily.
If you do not wish to manually name the id
and target
properties, you can use randomString
from Splendid Utils
to create a DOM-ready random string. This can make things a tiny bit easier for you.
DrawerTrigger
creates a trigger button that opens DrawerContent
when clicked.
Content that is passed into DrawerTrigger
will be rendered inside a button
element.
If you pass icon
to DrawerTrigger
without any content, it will render an icon with SVG
.
Use SVG
to if you want to create a button with both text and icon.
If you want to show the drawer by default (great for debugging purposes), you can set show
to true.
DrawerTrigger
is built on top of DisclosureTrigger
and inherits all of its options. In addition to the above properties, you can pass other DisclosureTrigger
properties into DrawerTrigger
.
All other unnamed properties will be passed directly to the underlying button
element.
DrawerContent
contains the content that should be hidden within the drawer. It is shown when DrawerTrigger
gets clicked.
There are multiple accessible ways to dismiss DrawerContent
.
DrawerContent
can be placed on the top
, bottom
, left
, and right
of the screen. To change the placement, add the placement
option to DrawerContent
.
Placements can only be used when position
is absolute
or fixed
.
The default position for DrawerContent
is fixed
, but you can change it to absolute
or static
. These behave the same way as the CSS position
property.
static
- drawer content is placed in the normal document flowabsolute
- drawer content is placed relative to the nearest ancestor with position: relative
.fixed
- drawer content is placed relative to the nearest stacking context.Static Drawer Content
This content is placed in the normal document flow.
Absolute Drawer Content
This content is placed relative to the nearest ancestor with position: relative
.
Fixed Drawer Content
This content is placed relative to the nearest stacking context.
DrawerContent
with absolute
position is great for creating mega-menus!
By default, DrawerContent
will be portalled out of their default DOM locations and inserted as a direct descendant of the <body>
element. This prevents DrawerContent
from being cut-off by elements with overflow: hidden
.
If you wish to disable this behaviour, set the portal
option to false
. (This is how we contained fixed
position drawers in our demos).
When DrawerContent
opens, you can ask it to automatically scroll to the current anchor (an <a>
element with href
matching the current URL hash).
To see an example of this, open this page on your mobile browser and open the submenu. DrawerContent
will automatically scroll to Drawer
.
Drawer
is built on top of Disclosure
and inherits all of its options. In addition to the above properties, you can pass other DisclosureContent
properties into DrawerContent
.
DrawerContent
emits the following events:
open
- emitted when the DrawerContent
is openedclose
- emitted when the DrawerContent
is closedDrawerCloseButton
creates a button that closes the drawer when clicked. It should be used inside DrawerContent
.
DrawerCloseButton
uses Tablerâs x
icon by default.
If you wish to change the icon, simply set the icon
property to your desired icon. See SVG
for more information on using icon
.
Drawer Content
Like DrawerTrigger
, you can insert HTML into DrawerCloseButton
if you wish to create a custom close button.
Drawer Content
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
A basic drawer can be created with DrawerTrigger
and DrawerContent
.
When creating a drawer, you need to pass the same value to the triggerâs target
and contentâs id
property.
You can create a close button with DrawerCloseButton
.
Use Splendid Layouts
to create a layout for your drawer
content easily.
If you do not wish to manually name the id
and target
properties, you can use randomString
from Splendid Utils
to create a DOM-ready random string. This can make things a tiny bit easier for you.
DrawerTrigger
creates a trigger button that opens DrawerContent
when clicked.
Content that is passed into DrawerTrigger
will be rendered inside a button
element.
If you pass icon
to DrawerTrigger
without any content, it will render an icon with SVG
.
Use SVG
to if you want to create a button with both text and icon.
If you want to show the drawer by default (great for debugging purposes), you can set show
to true.
DrawerTrigger
is built on top of DisclosureTrigger
and inherits all of its options. In addition to the above properties, you can pass other DisclosureTrigger
properties into DrawerTrigger
.
All other unnamed properties will be passed directly to the underlying button
element.
DrawerContent
contains the content that should be hidden within the drawer. It is shown when DrawerTrigger
gets clicked.
There are multiple accessible ways to dismiss DrawerContent
.
DrawerContent
can be placed on the top
, bottom
, left
, and right
of the screen. To change the placement, add the placement
option to DrawerContent
.
Placements can only be used when position
is absolute
or fixed
.
The default position for DrawerContent
is fixed
, but you can change it to absolute
or static
. These behave the same way as the CSS position
property.
static
- drawer content is placed in the normal document flowabsolute
- drawer content is placed relative to the nearest ancestor with position: relative
.fixed
- drawer content is placed relative to the nearest stacking context.Static Drawer Content
This content is placed in the normal document flow.
Absolute Drawer Content
This content is placed relative to the nearest ancestor with position: relative
.
Fixed Drawer Content
This content is placed relative to the nearest stacking context.
DrawerContent
with absolute
position is great for creating mega-menus!
By default, DrawerContent
will be portalled out of their default DOM locations and inserted as a direct descendant of the <body>
element. This prevents DrawerContent
from being cut-off by elements with overflow: hidden
.
If you wish to disable this behaviour, set the portal
option to false
. (This is how we contained fixed
position drawers in our demos).
When DrawerContent
opens, you can ask it to automatically scroll to the current anchor (an <a>
element with href
matching the current URL hash).
To see an example of this, open this page on your mobile browser and open the submenu. DrawerContent
will automatically scroll to Drawer
.
Drawer
is built on top of Disclosure
and inherits all of its options. In addition to the above properties, you can pass other DisclosureContent
properties into DrawerContent
.
DrawerContent
emits the following events:
open
- emitted when the DrawerContent
is openedclose
- emitted when the DrawerContent
is closedDrawerCloseButton
creates a button that closes the drawer when clicked. It should be used inside DrawerContent
.
DrawerCloseButton
uses Tablerâs x
icon by default.
If you wish to change the icon, simply set the icon
property to your desired icon. See SVG
for more information on using icon
.
Drawer Content
Like DrawerTrigger
, you can insert HTML into DrawerCloseButton
if you wish to create a custom close button.
Drawer Content
Drawer is built on top of Disclosure
and inherits all its accessibility features. For your convenience, we have listed some of the most important features and how to use them here.
Users can dismiss the drawer via three main ways:
DrawerContent
*: âClickingâ includes using Enter and Space keys because we used the standard <button>
element.
You can disable the second and third dismissal methods by setting their respective options to false
:
This section covers how Drawer
handles focus.
When DrawerContent
opens, it will automatically be focused, so keyboard users and screen readers can start interacting with it immediately.
You can direct focus onto the first item instead DrawerContent
by specifying focusTargetWhenOpening
to first
.
If a user closes DrawerContent
with the Esc key or by clicking on DrawerCloseButton
, we will focus on the DrawerTrigger
. This lets keyboard and screen reader users navigate to the next item without using a mouse.
To disable this behaviour, you can set focusTriggerOnClose
to false
:
DrawerContent
handles internal focus differently depending on its position
. By default, the following applies:
fixed
position: DrawerContent
will trap focus.absolute
or static
positions: DrawerContent
will manage focus.When focus is trapped, users will not be able to Tab or Shift + Tab out of DrawerContent
.
Open the drawer and try it: You canât Tab out of drawer content.
When focus is managed, users will be able to tap out of DrawerContent
, but the following applies:
DrawerContent
will close, and focus goes to the next item after DrawerTrigger
.DrawerContent
will not close, and focus goes to DrawerTrigger
.We built this behaviour because it seems to be most sensible for screen readers and keyboard users.
Drawers are dialogs so we have set DrawerContent
âs role to dialog
. At the same time, we have also set DrawerTrigger
âs aria-haspopup
to dialog
to match the role
value.
If you want to change the aria-haspopup
and role
, you can pass them into their respective components.
Labelling DrawerTrigger
is optional if the text within it is clear enough. However, since DrawerContent
is a dialog
, it must have an accessible name.
You can use aria-label
or aria-labelledby
to give both components accessible names.
The default accessible name for DrawerCloseButton
is âClose dialogâ. If you want to change it, you can use aria-label
as well.
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
Drawer is built on top of Disclosure
and inherits all its accessibility features. For your convenience, we have listed some of the most important features and how to use them here.
Users can dismiss the drawer via three main ways:
DrawerContent
*: âClickingâ includes using Enter and Space keys because we used the standard <button>
element.
You can disable the second and third dismissal methods by setting their respective options to false
:
This section covers how Drawer
handles focus.
When DrawerContent
opens, it will automatically be focused, so keyboard users and screen readers can start interacting with it immediately.
You can direct focus onto the first item instead DrawerContent
by specifying focusTargetWhenOpening
to first
.
If a user closes DrawerContent
with the Esc key or by clicking on DrawerCloseButton
, we will focus on the DrawerTrigger
. This lets keyboard and screen reader users navigate to the next item without using a mouse.
To disable this behaviour, you can set focusTriggerOnClose
to false
:
DrawerContent
handles internal focus differently depending on its position
. By default, the following applies:
fixed
position: DrawerContent
will trap focus.absolute
or static
positions: DrawerContent
will manage focus.When focus is trapped, users will not be able to Tab or Shift + Tab out of DrawerContent
.
Open the drawer and try it: You canât Tab out of drawer content.
When focus is managed, users will be able to tap out of DrawerContent
, but the following applies:
DrawerContent
will close, and focus goes to the next item after DrawerTrigger
.DrawerContent
will not close, and focus goes to DrawerTrigger
.We built this behaviour because it seems to be most sensible for screen readers and keyboard users.
Drawers are dialogs so we have set DrawerContent
âs role to dialog
. At the same time, we have also set DrawerTrigger
âs aria-haspopup
to dialog
to match the role
value.
If you want to change the aria-haspopup
and role
, you can pass them into their respective components.
Labelling DrawerTrigger
is optional if the text within it is clear enough. However, since DrawerContent
is a dialog
, it must have an accessible name.
You can use aria-label
or aria-labelledby
to give both components accessible names.
The default accessible name for DrawerCloseButton
is âClose dialogâ. If you want to change it, you can use aria-label
as well.
You can import the basic styles for Drawer
through Splendid Styles.
DrawerTrigger
produces the following output:
If you used icon
and did not provide any HTML into the default slot, DrawerTrigger
will produce the following output:
DrawerContent
produces the following output in fixed
position.
In static
and absolute
positions, DrawerContent
does not include the DrawerBackdrop
element.
DrawerCloseButton
produces the following HTML:
If content is passed into DrawerCloseButton
, the content will replace the <svg>
element.
The following classes are used by DrawerTrigger
Class | Description |
---|---|
Button | Generic styles used for buttons. See Button . |
DrawerTrigger | Styles for drawer trigger |
These properties can be used to add classes to DrawerTrigger
.
Class | Description |
---|---|
class | Adds classes to the button |
iconClass | Adds classes to icon |
The following classes are used by DrawerContent
Class | Description |
---|---|
DrawerBackdrop | Styles the backdrop. |
DrawerBackdrop-blur | Blurs the backdrop |
DrawerContent | Styles the content |
These properties can be used to add classes to DrawerContent
.
Class | Description |
---|---|
backdropClass | Adds classes to backdrop |
class | Adds classes to the content |
The following classes are used by DrawerCloseButton
Class | Description |
---|---|
Button-icon | Generic styles used for button icons. See Button . |
DrawerCloseButton | Styles for the close button |
These properties can be used to add classes to DrawerCloseButton
.
Class | Description |
---|---|
class | Adds classes to the button |
iconClass | Adds classes to icon |
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
You can import the basic styles for Drawer
through Splendid Styles.
DrawerTrigger
produces the following output:
If you used icon
and did not provide any HTML into the default slot, DrawerTrigger
will produce the following output:
DrawerContent
produces the following output in fixed
position.
In static
and absolute
positions, DrawerContent
does not include the DrawerBackdrop
element.
DrawerCloseButton
produces the following HTML:
If content is passed into DrawerCloseButton
, the content will replace the <svg>
element.
The following classes are used by DrawerTrigger
Class | Description |
---|---|
Button | Generic styles used for buttons. See Button . |
DrawerTrigger | Styles for drawer trigger |
These properties can be used to add classes to DrawerTrigger
.
Class | Description |
---|---|
class | Adds classes to the button |
iconClass | Adds classes to icon |
The following classes are used by DrawerContent
Class | Description |
---|---|
DrawerBackdrop | Styles the backdrop. |
DrawerBackdrop-blur | Blurs the backdrop |
DrawerContent | Styles the content |
These properties can be used to add classes to DrawerContent
.
Class | Description |
---|---|
backdropClass | Adds classes to backdrop |
class | Adds classes to the content |
The following classes are used by DrawerCloseButton
Class | Description |
---|---|
Button-icon | Generic styles used for button icons. See Button . |
DrawerCloseButton | Styles for the close button |
These properties can be used to add classes to DrawerCloseButton
.
Class | Description |
---|---|
class | Adds classes to the button |
iconClass | Adds classes to icon |
You can provide the following properties to DrawerTrigger
.
Property | Type | Description |
---|---|---|
target | string | id of the DrawerContent . Required. |
show | boolean | Position of the drawer. Default is fixed . |
aria-haspopup | string | aria-haspopup value. Default is dialog . |
icon | string | Icon to use when content is not passed into default slot. See SVG . |
options | object | Additional options. This inherits from DisclosureTrigger . |
You can provide the following properties to DrawerContent
.
Property | Type | Description |
---|---|---|
id | string | id of DrawerContent . Required. |
role | boolean | Role for the drawer content |
options | object | Additional options. See Options below. |
DrawerContent
has these options:
Property | Enum | Description |
---|---|---|
position | static absolute fixed | Position of the drawer. Default is fixed |
placement | top bottom left right | Placement of the drawer. |
... | Other DisclosureContent options |
DrawerContent
contains different default options based on its position
:
You can provide the following properties to DrawerCloseButton
.
Property | Type | Description |
---|---|---|
icon | string | Icon to use when content is not passed into default slot. See SVG . |
Splendid Labz is free because we love developers and we want to contribute back to the ecosystem.
We only charge for access to the documentation to keep the project going. If you don't wish to pay, feel free to use the content from the features tab, our blogs or our videos as your guide.
You can provide the following properties to DrawerTrigger
.
Property | Type | Description |
---|---|---|
target | string | id of the DrawerContent . Required. |
show | boolean | Position of the drawer. Default is fixed . |
aria-haspopup | string | aria-haspopup value. Default is dialog . |
icon | string | Icon to use when content is not passed into default slot. See SVG . |
options | object | Additional options. This inherits from DisclosureTrigger . |
You can provide the following properties to DrawerContent
.
Property | Type | Description |
---|---|---|
id | string | id of DrawerContent . Required. |
role | boolean | Role for the drawer content |
options | object | Additional options. See Options below. |
DrawerContent
has these options:
Property | Enum | Description |
---|---|---|
position | static absolute fixed | Position of the drawer. Default is fixed |
placement | top bottom left right | Placement of the drawer. |
... | Other DisclosureContent options |
DrawerContent
contains different default options based on its position
:
You can provide the following properties to DrawerCloseButton
.
Property | Type | Description |
---|---|---|
icon | string | Icon to use when content is not passed into default slot. See SVG . |