ResizeBox 可调整尺寸容器
拖动边缘改变盒子的宽度或高度,也支持键盘调整、尺寸限制、百分比初始值和 Esc 取消。适合侧栏、编辑区域、预览面板。
引入
import { HuiResizeBox } from '@nscode/handy-ui'
import type { ResizeBoxDirection, ResizeBoxSize, ResizeBoxProps } from '@nscode/handy-ui'
示例
右侧拖动条与双向绑定
默认只显示右侧拖动条。尺寸绑定会收到 px 数字。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiResizeBox, HuiSpace } from '@nscode/handy-ui'
const width = ref(220)
</script>
<template>
<HuiSpace direction="vertical" fill>
<div style="display: flex; gap: 16px; min-height: 140px">
<HuiResizeBox
v-model:width="width"
:height="140"
:min-width="120"
:max-width="360"
style="flex: none; padding: 16px; border: 1px solid var(--hui-border-color)"
>
拖动右边缘调整侧栏。
</HuiResizeBox>
<div style="flex: 1; padding: 16px; background: var(--hui-fill-color-light)">
其余内容自动占据剩余空间。
</div>
</div>
<span>当前宽度:{{ width }}px</span>
</HuiSpace>
</template>
四个物理方向
left、right 改宽,top、bottom 改高。向左、向上拖是变大。下面左边缘的例子靠右布局,上边缘的例子靠下布局,让另一边保持原位;方向不随 RTL 翻转。
<script setup lang="ts">
import { reactive } from 'vue'
import { HuiResizeBox, HuiSpace } from '@nscode/handy-ui'
import type { ResizeBoxDirection } from '@nscode/handy-ui'
const boxes = reactive<{ direction: ResizeBoxDirection; width: number; height: number }[]>(
['left', 'right', 'top', 'bottom'].map((direction) => ({
direction: direction as ResizeBoxDirection,
width: 150,
height: 90
}))
)
</script>
<template>
<HuiSpace wrap align="start">
<div v-for="box in boxes" :key="box.direction">
<p>{{ box.direction }}:{{ box.width }} × {{ box.height }}</p>
<div
:style="{
width: '240px',
height: '160px',
display: 'flex',
justifyContent: box.direction === 'left' ? 'flex-end' : 'flex-start',
alignItems: box.direction === 'top' ? 'flex-end' : 'flex-start',
border: '1px dashed var(--hui-border-color)'
}"
>
<HuiResizeBox
v-model:width="box.width"
v-model:height="box.height"
:directions="[box.direction]"
:min-width="80"
:max-width="230"
:min-height="60"
:max-height="150"
style="flex: none; padding: 12px; background: var(--hui-fill-color-light)"
>
{{ box.direction }} 边
</HuiResizeBox>
</div>
</div>
</HuiSpace>
</template>
同时开放右边和下边
directions 可以包含多条边;没有列出的边不会显示把手。组件只调整尺寸,不改变文档流里的定位,向左或向上伸缩时仍需父布局配合。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiResizeBox, HuiSpace } from '@nscode/handy-ui'
const width = ref(220)
const height = ref(120)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiResizeBox
v-model:width="width"
v-model:height="height"
:directions="['right', 'bottom']"
:min-width="120"
:max-width="420"
:min-height="80"
:max-height="240"
style="padding: 20px; border: 1px solid var(--hui-border-color)"
>
右边和下边都可以调整,左上角保持原位。Tab 聚焦把手后也能用方向键。
</HuiResizeBox>
<span>宽 {{ width }}px,高 {{ height }}px</span>
</HuiSpace>
</template>
数字、px、百分比和视口单位
数字按 px。初始宽高可以写 px、%、vw、vh;第一次调整时测量实际尺寸,之后回写 px。百分比限制相对父元素。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiResizeBox, HuiSpace } from '@nscode/handy-ui'
const percentWidth = ref<number | string>('60%')
const exampleWidth1 = ref<number | string>('25vw')
const exampleHeight2 = ref<number | string>('12vh')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="percentWidth = percentWidth === '60%' ? '50%' : '60%'"
>切换初始百分比</HuiButton
>
<div style="width: 100%; height: 160px; border: 1px dashed var(--hui-border-color)">
<HuiResizeBox
v-model:width="percentWidth"
height="120px"
min-width="30%"
max-width="90%"
style="padding: 16px; background: var(--hui-fill-color-light)"
>百分比初始宽度与限制</HuiResizeBox
>
</div>
<span>当前绑定:{{ percentWidth }}</span>
<HuiResizeBox
v-model:width="exampleWidth1"
v-model:height="exampleHeight2"
:directions="['right', 'bottom']"
min-width="15vw"
max-width="50vw"
min-height="8vh"
max-height="30vh"
style="padding: 16px; border: 1px solid var(--hui-border-color)"
>vw / vh</HuiResizeBox
>
</HuiSpace>
</template>
最小值、最大值
未设最大值时不限。最大值小于最小值时按最小值;宽高不会调整为负数。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiResizeBox, HuiSpace } from '@nscode/handy-ui'
const exampleWidth1 = ref<number | string>(220)
const exampleWidth2 = ref<number | string>(240)
const exampleHeight3 = ref<number | string>(100)
const exampleWidth4 = ref<number | string>(200)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiResizeBox
v-model:width="exampleWidth1"
:height="100"
:min-width="160"
:max-width="320"
style="padding: 16px; border: 1px solid var(--hui-border-color)"
>宽度 160~320px</HuiResizeBox
>
<HuiResizeBox
v-model:width="exampleWidth2"
v-model:height="exampleHeight3"
:directions="['bottom']"
:min-height="80"
:max-height="180"
style="padding: 16px; border: 1px solid var(--hui-border-color)"
>高度 80~180px</HuiResizeBox
>
<HuiResizeBox
v-model:width="exampleWidth4"
:height="80"
:min-width="200"
:max-width="100"
style="padding: 16px; border: 1px solid var(--hui-border-color)"
>max 小于 min,调整时按 min=200px</HuiResizeBox
>
</HuiSpace>
</template>
禁用与动态方向
禁用会同时关闭鼠标、触摸和键盘调整。移除正在使用的方向也会结束该次拖动,保留最后有效尺寸。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiResizeBox, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const disabled = ref(false)
const vertical = ref(true)
const exampleWidth1 = ref<number | string>(260)
const exampleHeight2 = ref<number | string>(140)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace
><span>禁用</span><HuiSwitch v-model="disabled" /><span>开放下边缘</span
><HuiSwitch v-model="vertical"
/></HuiSpace>
<HuiResizeBox
v-model:width="exampleWidth1"
v-model:height="exampleHeight2"
:disabled="disabled"
:directions="vertical ? ['right', 'bottom'] : ['right']"
:min-width="140"
:max-width="420"
:min-height="80"
:max-height="240"
style="padding: 16px; border: 1px solid var(--hui-border-color)"
>切换开关,再尝试拖动或键盘调整。</HuiResizeBox
>
</HuiSpace>
</template>
自定义把手
handle 插槽只替换视觉内容,外层仍保留拖动、键盘、焦点和无障碍属性。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIcon, HuiResizeBox } from '@nscode/handy-ui'
const exampleWidth1 = ref<number | string>(260)
</script>
<template>
<HuiResizeBox
v-model:width="exampleWidth1"
:height="140"
:min-width="140"
:max-width="420"
style="padding: 16px; border: 1px solid var(--hui-border-color)"
>
拖动右侧图标。
<template #handle="{ direction, resizing }">
<span
:title="direction"
:style="{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
height: '100%',
color: resizing ? 'var(--hui-color-primary)' : 'var(--hui-text-color-secondary)'
}"
><HuiIcon name="drag"
/></span>
</template>
</HuiResizeBox>
</template>
事件、键盘与 Esc 取消
把手可 Tab 聚焦;方向键每次 10px,Shift+方向键每次 1px;Home 到最小值,设了最大值时 End 到最大值。拖动中按 Esc 恢复按下时的实际尺寸,并发出 cancelled=true。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiResizeBox, HuiSpace } from '@nscode/handy-ui'
import type { ResizeBoxSize } from '@nscode/handy-ui'
const width = ref(240)
const logs = ref<string[]>([])
const log = (name: string, size: ResizeBoxSize, cancelled = false) => {
logs.value = [
`${name}:${Math.round(size.width)} × ${Math.round(size.height)}${cancelled ? '(已取消)' : ''}`,
...logs.value
].slice(0, 6)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiResizeBox
v-model:width="width"
:height="140"
:min-width="120"
:max-width="420"
@resize-start="log('开始', $event)"
@resize="log('调整', $event)"
@resize-end="(size, cancelled) => log('结束', size, cancelled)"
style="padding: 16px; border: 1px solid var(--hui-border-color)"
>拖动一段距离后按 Esc,观察宽度和结束事件。</HuiResizeBox
>
<span>宽度:{{ width }}px</span>
<ul>
<li v-for="(item, index) in logs" :key="index">{{ item }}</li>
</ul>
</HuiSpace>
</template>
父组件吸附回填
父组件吸附或夹值后,拖动继续按起始尺寸加原始指针位移计算;resizeEnd 报告父组件处理后的实际尺寸。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiResizeBox, HuiSpace } from '@nscode/handy-ui'
import type { ResizeBoxSize } from '@nscode/handy-ui'
const width = ref(200)
const ended = ref('尚未调整')
const updateWidth = (value: number) => {
width.value = Math.min(400, Math.round(value / 50) * 50)
}
const onEnd = (size: ResizeBoxSize, cancelled: boolean) => {
ended.value = `实际宽度:${size.width}px;cancelled=${cancelled}`
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiResizeBox
:width="width"
:height="120"
:min-width="100"
@update:width="updateWidth"
@resize-end="onEnd"
style="padding: 16px; border: 1px solid var(--hui-border-color)"
>每50px吸附,最多400px;到上限后仍可回拖。</HuiResizeBox
>
<span>绑定宽度:{{ width }}px;{{ ended }}</span>
</HuiSpace>
</template>
外层标签
<script setup lang="ts">
import { ref } from 'vue'
import { HuiResizeBox } from '@nscode/handy-ui'
const exampleWidth1 = ref<number | string>(260)
</script>
<template>
<HuiResizeBox
tag="section"
v-model:width="exampleWidth1"
:height="120"
:min-width="160"
:max-width="420"
style="padding: 16px; border: 1px solid var(--hui-border-color)"
>外层使用 section,默认内容插槽不变。</HuiResizeBox
>
</template>
宽高自持与受控回填
width与height各自按属性是否传入判断受控。未传或仅监听时从自动布局尺寸起步,保存后续调整;传入即受控,不监听也不会自行改变,显式undefined保持自动尺寸。移除属性恢复该轴原内部状态,切换受控模式在开发环境警告并结束旧拖动。CSS、ARIA和resizeEnd读实际尺寸,resize仍报告候选;外部状态变化结束旧拖动时,等新布局生效再报告最终尺寸,普通指针取消保原时机。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiResizeBox, HuiSpace } from '@nscode/handy-ui'
const sizeRedraw = ref(0)
const sizeLast = ref('尚无请求')
const sizeEnd = ref('尚未结束')
const sizeWidth = ref(200)
const sizeHeight = ref(90)
</script>
<template>
<HuiSpace class="resize-box-q1-demo" direction="vertical" fill>
<HuiButton class="size-redraw" @click="sizeRedraw++">父重绘 {{ sizeRedraw }}</HuiButton>
<div
style="
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 24px;
"
>
<div class="size-local">
<p>未传宽高,自行保存</p>
<HuiResizeBox
:directions="['right', 'bottom']"
:min-width="120"
:max-width="280"
:min-height="60"
:max-height="160"
style="
display: inline-block;
outline: 1px solid var(--hui-border-color);
"
>
<div
class="size-seed"
style="
width: 200px;
max-width: 100%;
height: 90px;
max-height: 100%;
background: var(--hui-fill-color-light);
"
>
内容
</div>
</HuiResizeBox>
</div>
<div class="size-listener">
<p>仅监听宽高更新</p>
<HuiResizeBox
:directions="['right', 'bottom']"
:min-width="120"
:max-width="280"
:min-height="60"
:max-height="160"
style="
display: inline-block;
outline: 1px solid var(--hui-border-color);
"
@update:width="sizeLast = 'width:' + $event"
@update:height="sizeLast = 'height:' + $event"
@resize-end="sizeEnd = $event.width + '×' + $event.height"
>
<div
class="size-seed"
style="
width: 200px;
max-width: 100%;
height: 90px;
max-height: 100%;
background: var(--hui-fill-color-light);
"
>
内容
</div>
</HuiResizeBox>
</div>
<div class="size-empty">
<p>显式undefined,保持自动尺寸</p>
<HuiResizeBox
:width="undefined"
:height="undefined"
:directions="['right', 'bottom']"
:min-width="120"
:max-width="280"
:min-height="60"
:max-height="160"
style="
display: inline-block;
outline: 1px solid var(--hui-border-color);
"
>
<div
class="size-seed"
style="
width: 200px;
max-width: 100%;
height: 90px;
max-height: 100%;
background: var(--hui-fill-color-light);
"
>
内容
</div>
</HuiResizeBox>
</div>
<div class="size-fixed">
<p>固定220×100,父拒绝更新</p>
<HuiResizeBox
:width="220"
:height="100"
:directions="['right', 'bottom']"
:min-width="120"
:max-width="280"
:min-height="60"
:max-height="160"
style="
display: inline-block;
outline: 1px solid var(--hui-border-color);
"
>
<div
class="size-seed"
style="
width: 200px;
max-width: 100%;
height: 90px;
max-height: 100%;
background: var(--hui-fill-color-light);
"
>
内容
</div>
</HuiResizeBox>
</div>
<div class="size-mixed">
<p>固定宽度200,高度自行保存</p>
<HuiResizeBox
:width="200"
:directions="['right', 'bottom']"
:min-width="120"
:max-width="280"
:min-height="60"
:max-height="160"
style="
display: inline-block;
outline: 1px solid var(--hui-border-color);
"
>
<div
class="size-seed"
style="
width: 200px;
max-width: 100%;
height: 90px;
max-height: 100%;
background: var(--hui-fill-color-light);
"
>
内容
</div>
</HuiResizeBox>
</div>
<div class="size-bound">
<p>双向绑定宽高</p>
<HuiResizeBox
v-model:width="sizeWidth"
v-model:height="sizeHeight"
:directions="['right', 'bottom']"
:min-width="120"
:max-width="280"
:min-height="60"
:max-height="160"
style="
display: inline-block;
outline: 1px solid var(--hui-border-color);
"
>
<div
class="size-seed"
style="
width: 200px;
max-width: 100%;
height: 90px;
max-height: 100%;
background: var(--hui-fill-color-light);
"
>
内容
</div>
</HuiResizeBox>
</div>
</div>
<p class="size-status">
绑定尺寸:{{ sizeWidth }}×{{ sizeHeight }};最近请求:{{ sizeLast }};实际结束:{{ sizeEnd }}
</p>
</HuiSpace>
</template>
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| width | number | string | - | 传入即受控(含undefined,自动宽度);未传或仅监听时保存,支持v-model:width |
| height | number | string | - | 传入即受控(含undefined,自动高度);未传或仅监听时保存,支持v-model:height |
| directions | ResizeBoxDirection[] | ['right'] | 可调整的物理边,不随 RTL 翻转 |
| minWidth | number | string | 0 | 最小宽度,百分比相对父元素 |
| maxWidth | number | string | - | 最大宽度,不设时不限;小于最小值时按最小值 |
| minHeight | number | string | 0 | 最小高度 |
| maxHeight | number | string | - | 最大高度 |
| disabled | boolean | false | 禁用拖动和键盘调整 |
| tag | string | 'div' | 外层原生标签 |
Events
| 事件 | 参数 | 说明 |
|---|---|---|
| update:width | (width: number) | 调整宽度后的 px 数字 |
| update:height | (height: number) | 调整高度后的 px 数字 |
| resizeStart | (size: ResizeBoxSize) | 指针或键盘开始调整 |
| resize | (size: ResizeBoxSize) | 调整后的宽高;Esc 恢复时也触发 |
| resizeEnd | (size: ResizeBoxSize, cancelled: boolean) | 调整结束;只有 Esc 取消时为 true |
公开开始和结束事件声明及实际发射统一为 resizeStart、resizeEnd。Vue模板仍可写 @resize-start、@resize-end,渲染函数使用 onResizeStart、onResizeEnd,只发新声明事件。候选尺寸、父组件处理后的实际两轴尺寸、取消标记及通知顺序保持。
Slots
| 插槽 | 参数 | 说明 |
|---|---|---|
| default | - | 盒子内容 |
| handle | ResizeBoxHandleSlotProps | 自定义每条边的把手外观 |
方法
无公开方法,使用宽高属性设置尺寸。
类型
import type {
ResizeBoxDirection,
ResizeBoxSize,
ResizeBoxProps,
ResizeBoxEmits,
ResizeBoxHandleSlotProps
} from '@nscode/handy-ui'
type ResizeBoxDirection = 'left' | 'right' | 'top' | 'bottom'
interface ResizeBoxSize {
width: number
height: number
}
interface ResizeBoxHandleSlotProps {
direction: ResizeBoxDirection
resizing: boolean
}