NavigationDrawer抽屉导航栏
The navigation drawer provides side navigation between pages on a website.
Typically, the <mdui-list> component is used within the navigation drawer to add navigation items.
Usage
Import the component:
import 'mdui/components/navigation-drawer.js';
Import the TypeScript type:
import type { NavigationDrawer } from 'mdui/components/navigation-drawer.js';
Example:
<mdui-navigation-drawer close-on-overlay-click class="example-drawer">
<mdui-button>Close Navigation Drawer</mdui-button>
</mdui-navigation-drawer>
<mdui-button>Open Navigation Drawer</mdui-button>
<script>
const navigationDrawer = document.querySelector(".example-drawer");
const openButton = navigationDrawer.nextElementSibling;
const closeButton = navigationDrawer.querySelector("mdui-button");
openButton.addEventListener("click", () => navigationDrawer.open = true);
closeButton.addEventListener("click", () => navigationDrawer.open = false);
</script>Notes:
By default, this component uses position: fixed.
When modal is false and the breakpoint is at least --mdui-breakpoint-md, it automatically adds padding-left or padding-right to the body to prevent content from being obscured.
However, it uses a position: absolute style in the following cases:
- When the
containedproperty istrue. - When the component is used inside the
<mdui-layout></mdui-layout>component. In this case,padding-leftorpadding-rightis not added.
Examples
In Container
By default, the navigation drawer appears on the left or right side of the viewport. To place it inside a container, add the contained attribute. This makes the navigation drawer position itself relative to the parent element (you need to add position: relative; overflow: hidden; styles to the parent element).
Modal
The modal attribute displays a modal overlay when the navigation drawer is open. Note that if the window or parent element width is less than --mdui-breakpoint-md, this attribute is ignored and the modal overlay is always displayed.
The close-on-esc attribute lets the navigation drawer close when the ESC key is pressed.
The close-on-overlay-click attribute lets the navigation drawer close when the modal overlay is clicked.
Right Placement
Use the placement attribute to place the navigation drawer on the right side.
API
属性
| HTML 属性 | JavaScript 属性 | Reflect | 类型 | 默认值 |
|---|---|---|---|---|
open | open | boolean | false | |
Opens the navigation drawer. | ||||
modal | modal | boolean | false | |
Displays an overlay when open. On narrow devices (screen width < | ||||
close-on-esc | closeOnEsc | boolean | false | |
Closes the drawer when the ESC key is pressed and an overlay is present. | ||||
close-on-overlay-click | closeOnOverlayClick | boolean | false | |
Closes the drawer when the overlay is clicked. | ||||
placement | placement | 'left' | 'right' | 'left' | |
Sets the drawer's position. Possible values:
| ||||
contained | contained | boolean | false | |
By default, the navigation drawer is positioned relative to the Note: You must manually set | ||||
order | order | number | - | |
Specifies the layout order within the | ||||
事件
| 名称 |
|---|
open |
Emitted when the navigation drawer starts to open. Can be prevented with |
opened |
Emitted after the navigation drawer has opened and the animations are completed. |
close |
Emitted when the navigation drawer starts to close. Can be prevented with |
closed |
Emitted after the navigation drawer has closed and the animations are completed. |
overlay-click |
Emitted when the overlay is clicked. |
CSS 自定义属性
| 名称 |
|---|
--shape-corner |
The corner radius of the component. You can use a specific pixel value, but it is recommended to reference design tokens. |
--z-index |
The CSS |