|
| 1 | +# Drawer |
| 2 | + |
| 3 | +A floating panel assembly that slides out from the edge of the screen. |
| 4 | + |
| 5 | +#### When To Use |
| 6 | + |
| 7 | +1. The drawer slides in from the edge of the parent form and covers part of the parent form's contents. Users don't have to leave the current task while operating inside the drawer, and can smoothly return to the original task when the operation is completed. |
| 8 | +2. When you need an additional panel to control the contents of the parent form, this panel is called out when needed. For example, to control the display style of the interface, add content to the interface. |
| 9 | +3. When temporary tasks need to be inserted into the current task flow to create or preview additional content. For example, displaying the terms of an agreement and creating subobjects. |
| 10 | + |
| 11 | +### Basic Usage |
| 12 | + |
| 13 | +:::demo Slides out from the right by default with a width of `300px`. |
| 14 | + |
| 15 | +```vue |
| 16 | +<template> |
| 17 | + <d-button variant="solid" color="primary" @click="showDrawer">Click Me</d-button> |
| 18 | + <d-drawer v-model="visible" style="padding: 20px;">Hello Drawer</d-drawer> |
| 19 | +</template> |
| 20 | +<script> |
| 21 | +import { defineComponent, ref } from 'vue'; |
| 22 | +
|
| 23 | +export default defineComponent({ |
| 24 | + setup() { |
| 25 | + const visible = ref(false); |
| 26 | + const showDrawer = () => (visible.value = true); |
| 27 | +
|
| 28 | + return { visible, showDrawer }; |
| 29 | + }, |
| 30 | +}); |
| 31 | +</script> |
| 32 | +``` |
| 33 | + |
| 34 | +::: |
| 35 | + |
| 36 | +### Left Pop-up |
| 37 | + |
| 38 | +:::demo Sets the left slideout via `position`. |
| 39 | + |
| 40 | +```vue |
| 41 | +<template> |
| 42 | + <d-button variant="solid" color="primary" @click="showDrawer">Click Me</d-button> |
| 43 | + <d-drawer v-model="visible" position="left" style="padding: 20px;">Left Drawer</d-drawer> |
| 44 | +</template> |
| 45 | +<script> |
| 46 | +import { defineComponent, ref } from 'vue'; |
| 47 | +
|
| 48 | +export default defineComponent({ |
| 49 | + setup() { |
| 50 | + const visible = ref(false); |
| 51 | + const showDrawer = () => (visible.value = true); |
| 52 | +
|
| 53 | + return { visible, showDrawer }; |
| 54 | + }, |
| 55 | +}); |
| 56 | +</script> |
| 57 | +``` |
| 58 | + |
| 59 | +::: |
| 60 | + |
| 61 | +### Background Scroll |
| 62 | + |
| 63 | +:::demo drawer After sliding out, the default background scroll is locked, which can be unlocked by setting `lock-scroll` to `false`. |
| 64 | + |
| 65 | +```vue |
| 66 | +<template> |
| 67 | + <d-button variant="solid" color="primary" @click="showDrawer">Click Me</d-button> |
| 68 | + <d-drawer v-model="visible" :lock-scroll="false" style="padding: 20px;">Background can be scrolled</d-drawer> |
| 69 | +</template> |
| 70 | +
|
| 71 | +<script> |
| 72 | +import { defineComponent, ref } from 'vue'; |
| 73 | +
|
| 74 | +export default defineComponent({ |
| 75 | + setup(props, ctx) { |
| 76 | + const visible = ref(false); |
| 77 | + const showDrawer = () => (visible.value = true); |
| 78 | +
|
| 79 | + return { visible, showDrawer }; |
| 80 | + }, |
| 81 | +}); |
| 82 | +</script> |
| 83 | +``` |
| 84 | + |
| 85 | +::: |
| 86 | + |
| 87 | +### Pre-close Callback |
| 88 | + |
| 89 | +:::demo `before-close` is called when the user closes the drawer, which can be done by executing the `done` function after certain asynchronous operations have been completed. |
| 90 | + |
| 91 | +```vue |
| 92 | +<template> |
| 93 | + <d-button variant="solid" color="primary" @click="showDrawer">Click Me</d-button> |
| 94 | + <d-drawer v-model="visible" :before-close="onBeforeClose" style="padding: 20px;">Delay Close</d-drawer> |
| 95 | +</template> |
| 96 | +
|
| 97 | +<script> |
| 98 | +import { defineComponent, ref } from 'vue'; |
| 99 | +
|
| 100 | +export default defineComponent({ |
| 101 | + setup() { |
| 102 | + const visible = ref(false); |
| 103 | + const showDrawer = () => (visible.value = true); |
| 104 | + const onBeforeClose = (done) => { |
| 105 | + new Promise((resolve) => { |
| 106 | + setTimeout(resolve, 1000); |
| 107 | + }).then(done); |
| 108 | + }; |
| 109 | +
|
| 110 | + return { visible, showDrawer, onBeforeClose }; |
| 111 | + }, |
| 112 | +}); |
| 113 | +</script> |
| 114 | +``` |
| 115 | + |
| 116 | +::: |
| 117 | + |
| 118 | +### Service Method |
| 119 | + |
| 120 | +The :::demo component registers `$drawerService` globally, which can be used as a service, with the content of the drawer passed in as the `content` parameter. The service returns the `close` method for closing the drawer. |
| 121 | + |
| 122 | +```vue |
| 123 | +<template> |
| 124 | + <d-button variant="solid" color="primary" @click.native="showDrawer()">服务方式</d-button> |
| 125 | +</template> |
| 126 | +
|
| 127 | +<script> |
| 128 | +import { defineComponent, h } from 'vue'; |
| 129 | +
|
| 130 | +export default defineComponent({ |
| 131 | + setup() { |
| 132 | + function showDrawer() { |
| 133 | + this.$drawerService.open({ |
| 134 | + content: () => h('div', { style: { padding: '20px' } }, [h('span', {}, ['Open drawer board in service mode'])]), |
| 135 | + }); |
| 136 | + } |
| 137 | +
|
| 138 | + return { showDrawer }; |
| 139 | + }, |
| 140 | +}); |
| 141 | +</script> |
| 142 | +``` |
| 143 | + |
| 144 | +::: |
| 145 | + |
| 146 | +### Drawer Props |
| 147 | + |
| 148 | +| Parameter | Type | Default | Description | Jump to Demo | |
| 149 | +| :--------------------- | :--------------- | :------ | :----------------------------------------------------------------------------------- | :---------------------------------------- | |
| 150 | +| v-model | `Boolean` | `false` | Optional, set whether the drawer panel is visible or not | [Basic Usage](#basic-usage) | |
| 151 | +| position | `String` | `right` | Optional, where the drawer panel appears, 'left' or 'right' | [Left Pop-up](#left-pop-up) | |
| 152 | +| show-overlay | `Boolean` | `true` | Optional, with or without masking layer | [Basic Usage](#basic-usage) | |
| 153 | +| lock-scroll | `Boolean` | `true` | Optional, whether to lock scrolling | [Background Scroll](#background-scroll) | |
| 154 | +| z-index | `Number` | `1000` | Optional, set the z-index value of the drawer | [Pre-close Callback](#pre-close-callback) | |
| 155 | +| esc-key-closeable | `Boolean` | `true` | Optional, set whether the drawer layer can be closed with the esc button. | [Basic Usage](#basic-usage) | |
| 156 | +| close-on-click-overlay | `Boolean` | `true` | Optional, sets whether the drawer layer can be closed by clicking on the background. | [Basic Usage](#basic-usage) | |
| 157 | +| before-close | `(done) => void` | `-` | Optional, callback before closing the window, call `done` to close the drawer. | [Pre-close Callback](#pre-close-callback) | |
| 158 | + |
| 159 | +### Drawer Events |
| 160 | + |
| 161 | +| Parameter | Type | Description | |
| 162 | +| :-------- | :--- | :--------------------------- | |
| 163 | +| open | `-` | drawer triggered when opened | |
| 164 | +| close | `-` | drawer triggered when closed | |
| 165 | + |
| 166 | +### Drawer Slots |
| 167 | + |
| 168 | +| Name | Type | Description | Jump to Demo | |
| 169 | +| :------ | :-------- | :-------------------- | :-------------------------- | |
| 170 | +| default | `default` | drawer board contents | [Basic Usage](#basic-usage) | |
0 commit comments