Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/sites/demos/apis/drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ export default {
mode: ['pc'],
pcDemo: 'tips-props',
hideSaas: true
},
{
name: 'close-on-press-escape',
type: 'boolean',
defaultValue: 'false',
desc: {
'zh-CN': 'ESC 键关闭抽屉',
'en-US': 'ESC key to close drawer'
},
mode: ['pc'],
pcDemo: 'closeOnPressEscape',
meta: {
stable: '3.28.0'
}
}
Comment on lines +211 to 224
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Inconsistent pcDemo value format.

The pcDemo value uses camelCase (closeOnPressEscape) while other props use kebab-case matching their demoId (e.g., before-close, mask-closable). This may cause the demo link to not resolve correctly.

-          pcDemo: 'closeOnPressEscape',
+          pcDemo: 'close-on-press-escape',
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{
name: 'close-on-press-escape',
type: 'boolean',
defaultValue: 'false',
desc: {
'zh-CN': 'ESC 键关闭抽屉',
'en-US': 'ESC key to close drawer'
},
mode: ['pc'],
pcDemo: 'closeOnPressEscape',
meta: {
stable: '3.28.0'
}
}
{
name: 'close-on-press-escape',
type: 'boolean',
defaultValue: 'false',
desc: {
'zh-CN': 'ESC 键关闭抽屉',
'en-US': 'ESC key to close drawer'
},
mode: ['pc'],
pcDemo: 'close-on-press-escape',
meta: {
stable: '3.28.0'
}
}
🤖 Prompt for AI Agents
In examples/sites/demos/apis/drawer.js around lines 211 to 224, the pcDemo value
uses camelCase ("closeOnPressEscape") while the rest use kebab-case matching
demoIds; update the pcDemo string to the kebab-case demo id
("close-on-press-escape") so the demo link resolves consistently with other
props.

],
events: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<template>
<div>
<tiny-button @click="openDrawer" type="primary"> 抽屉组件 </tiny-button>
<tiny-drawer
title="标题"
:visible="visible"
@update:visible="visible = $event"
@confirm="confirm"
:close-on-press-escape="true"
>
<div>内容区域</div>
</tiny-drawer>
</div>
</template>

<script setup>
import { ref } from 'vue'
import { TinyDrawer, TinyButton } from '@opentiny/vue'
const visible = ref(false)
function openDrawer() {
visible.value = true
}
function confirm() {
visible.value = false
}
</script>
19 changes: 19 additions & 0 deletions examples/sites/demos/pc/app/drawer/close-on-press-escape.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { test, expect } from '@playwright/test'

test('按 Esc 键关闭 Drawer(close-on-press-escape)', async ({ page }) => {
page.on('pageerror', (exception) => expect(exception).toBeNull())

await page.goto('drawer#close-on-press-escape')

const drawer = page.locator('.tiny-drawer__main')

// 打开 Drawer(用文本更稳定)
await page.getByText('抽屉组件').click()
await expect(drawer).toBeVisible()

// 按 Esc
await page.keyboard.press('Escape')

// Drawer 关闭
await expect(drawer).toBeHidden()
})
38 changes: 38 additions & 0 deletions examples/sites/demos/pc/app/drawer/close-on-press-escape.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<template>
<div>
<tiny-button @click="openDrawer" type="primary"> 抽屉组件 </tiny-button>
<tiny-drawer
title="标题"
:visible="visible"
@update:visible="visible = $event"
@confirm="confirm"
:close-on-press-escape="true"
>
<div>内容区域</div>
</tiny-drawer>
</div>
</template>

<script>
import { TinyDrawer, TinyButton } from '@opentiny/vue'
export default {
components: {
TinyDrawer,
TinyButton
},
data() {
return {
visible: false
}
},
methods: {
openDrawer() {
this.visible = true
},
confirm() {
this.visible = false
}
}
}
</script>
12 changes: 12 additions & 0 deletions examples/sites/demos/pc/app/drawer/webdoc/drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ export default {
},
codeFiles: ['basic-usage.vue']
},
{
demoId: 'close-on-press-escape',
name: {
'zh-CN': '按下 ESC 关闭抽屉',
'en-US': ''
},
desc: {
'zh-CN': '<p>添加 <code>close-on-press-escape</code> 属性可以控制是否可以通过 ESC 关闭抽屉。</p>',
'en-US': ''
},
codeFiles: ['close-on-press-escape.vue']
},
{
demoId: 'use-through-method',
name: { 'zh-CN': '通过方法调用', 'en-US': 'Use through method' },
Expand Down
30 changes: 30 additions & 0 deletions packages/renderless/src/drawer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ export const handleClose =
}
}

/* ================= Esc 关闭(受控) ================= */
export const keydown =
({ api, state, props }: Pick<IDrawerRenderlessParams, 'api' | 'state' | 'props'>) =>
(event: KeyboardEvent) => {
if (!state.visible) {
return
}

if (!props.closeOnPressEscape) {
return
}

if (event.key === 'Escape' || event.key === 'Esc') {
api.handleClose('esc', true)
}
}

export const addKeydownEvent =
({ api }: { api: IDrawerApi }) =>
() => {
document.addEventListener('keydown', api.keydown)
}

export const removeKeydownEvent =
({ api }: { api: IDrawerApi }) =>
() => {
document.removeEventListener('keydown', api.keydown)
}
/* ================================================== */

export const mousedown =
({ state, vm }: { vm: ISharedRenderlessParamUtils<IDrawerCT>['vm']; state: IDrawerState }) =>
(event) => {
Expand Down
14 changes: 11 additions & 3 deletions packages/renderless/src/drawer/vue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import {
handleClose,
computedWidth,
computedHeight,
open
open,
keydown,
addKeydownEvent,
removeKeydownEvent
} from './index'
import type {
IDrawerProps,
Expand Down Expand Up @@ -50,8 +53,11 @@ export const renderless = (
mousedown: mousedown({ state, vm }),
mousemove: mousemove({ state, props, emit }),
mouseup: mouseup({ state }),
addDragEvent: addDragEvent({ api: api as IDrawerApi, vm }),
removeDragEvent: removeDragEvent({ api: api as IDrawerApi, vm }),
keydown: keydown({ api, state, props }),
addKeydownEvent: addKeydownEvent({ api }),
removeKeydownEvent: removeKeydownEvent({ api }),
addDragEvent: addDragEvent({ api, vm }),
removeDragEvent: removeDragEvent({ api, vm }),
watchVisible: watchVisible({ state, api }),
showScrollbar: showScrollbar(lockScrollClass),
hideScrollbar: hideScrollbar(lockScrollClass),
Expand All @@ -61,13 +67,15 @@ export const renderless = (

onMounted(() => {
props.dragable && api.addDragEvent()
api.addKeydownEvent()
if (props.lockScroll && props.visible) {
api.showScrollbar()
}
})

onBeforeUnmount(() => {
props.dragable && api.removeDragEvent()
api.removeKeydownEvent()
props.lockScroll && api.hideScrollbar()
})

Expand Down
3 changes: 2 additions & 1 deletion packages/vue/src/drawer/src/pc.vue
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ export default defineComponent({
'zIndex',
'beforeClose',
'tipsProps',
'customSlots'
'customSlots',
'closeOnPressEscape'
],
emits: ['update:visible', 'open', 'close', 'confirm', 'drag'],
setup(props, context) {
Expand Down
Loading