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
}