ResizeBox
Resize a box from its edges using pointer or keyboard input, with size limits, relative initial dimensions, and Escape cancellation.
Import
import { HuiResizeBox } from '@nscode/handy-ui'
import type { ResizeBoxDirection, ResizeBoxSize, ResizeBoxProps } from '@nscode/handy-ui'
Examples
Right Edge and Two-Way Binding
The right edge is enabled by default. Dimension updates are pixel numbers.
<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)"
>Drag the right edge to resize this sidebar.</HuiResizeBox
>
<div style="flex: 1; padding: 16px; background: var(--hui-fill-color-light)">
Remaining content fills the available space.
</div>
</div>
<span>Width: {{ width }}px</span>
</HuiSpace>
</template>
Four Physical Directions
Left and right edges change width; top and bottom change height. Moving left or up increases size. The examples align the left-edge box to the right and the top-edge box to the bottom, keeping the opposite edge in place. Directions do not flip in 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 }} edge</HuiResizeBox
>
</div>
</div>
</HuiSpace>
</template>
Right and Bottom Edges
Only the edges included in directions show handles. ResizeBox changes dimensions, so parent layout still determines positioning when resizing from the left or top.
<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)"
>Resize the right and bottom edges while the top-left corner stays fixed. Focus a handle with
Tab to use arrow keys.</HuiResizeBox
>
<span>Width {{ width }}px, height {{ height }}px</span>
</HuiSpace>
</template>
Numbers, Pixels, Percentages, and Viewport Units
Numbers mean pixels. Initial sizes accept px, %, vw, and vh; the first interaction measures the actual rendered size, then writes a pixel number. Percentage limits are relative to the parent.
<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%'"
>Switch initial percentage</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)"
>Relative initial width and limits</HuiResizeBox
>
</div>
<span>Binding: {{ 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>
Minimum and Maximum Sizes
An omitted maximum is unlimited. A maximum below the minimum uses the minimum. Interaction cannot produce a negative dimension.
<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)"
>Width: 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)"
>Height: 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)"
>Maximum below minimum: interactions use 200px</HuiResizeBox
>
</HuiSpace>
</template>
Disabled and Dynamic Directions
Disabling the box blocks pointer and keyboard adjustments. Removing an active edge ends its drag and keeps the latest valid size.
<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>Disabled</span><HuiSwitch v-model="disabled" /><span>Bottom edge</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)"
>Toggle the switches, then try pointer or keyboard input.</HuiResizeBox
>
</HuiSpace>
</template>
Custom Handles
The handle slot replaces handle visuals while the outer element keeps pointer, keyboard, focus, and accessibility behavior.
<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)"
>
Drag the icon on the right.
<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>
Events, Keyboard, and Escape
Focus handles with Tab. Arrow keys adjust by 10px, or 1px with Shift. Home selects the minimum; End selects a finite maximum. Escape during a pointer drag restores its starting pixel size and emits 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 ? ' (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('Start', $event)"
@resize="log('Resize', $event)"
@resize-end="(size, cancelled) => log('End', size, cancelled)"
style="padding: 16px; border: 1px solid var(--hui-border-color)"
>Drag, then press Escape. Watch the dimension and end event.</HuiResizeBox
>
<span>Width: {{ width }}px</span>
<ul>
<li v-for="(item, index) in logs" :key="index">{{ item }}</li>
</ul>
</HuiSpace>
</template>
Parent Snapping Feedback
Parent snapping or clamping preserves the starting size and original pointer delta throughout the drag. The end event reports the actual size after parent feedback.
<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('Not resized yet')
const updateWidth = (value: number) => {
width.value = Math.min(400, Math.round(value / 50) * 50)
}
const onEnd = (size: ResizeBoxSize, cancelled: boolean) => {
ended.value = `Actual width: ${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)"
>Snap to 50px, up to 400px. Dragging back from the limit still works.</HuiResizeBox
>
<span>Bound width: {{ width }}px; {{ ended }}</span>
</HuiSpace>
</template>
Root Tag
<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)"
>The root is a section; default slot content is unchanged.</HuiResizeBox
>
</template>
Internal dimension state and controlled updates
Width and height are controlled independently by prop presence. An omitted dimension, including listener-only usage, starts at automatic layout size and stores later adjustments. Supplied props stay controlled even without update listeners; explicit undefined keeps automatic size. Removing a prop restores its saved internal dimension. Control mode changes warn in development and stop the old drag. CSS, ARIA and resizeEnd reflect accepted dimensions; resize still reports candidates. Changing external state waits for the new layout before reporting its final size, while ordinary pointer cancellation keeps its existing timing.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiResizeBox, HuiSpace } from '@nscode/handy-ui'
const sizeRedraw = ref(0)
const sizeLast = ref('No request')
const sizeEnd = ref('Not ended')
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++">Parent render {{ sizeRedraw }}</HuiButton>
<div
style="
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 24px;
"
>
<div class="size-local">
<p>Omitted dimensions</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);
"
>
Content
</div>
</HuiResizeBox>
</div>
<div class="size-listener">
<p>Dimension listeners only</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);
"
>
Content
</div>
</HuiResizeBox>
</div>
<div class="size-empty">
<p>Explicit undefined keeps automatic size</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);
"
>
Content
</div>
</HuiResizeBox>
</div>
<div class="size-fixed">
<p>Fixed 220 by 100 rejects updates</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);
"
>
Content
</div>
</HuiResizeBox>
</div>
<div class="size-mixed">
<p>Fixed width 200 with internal height</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);
"
>
Content
</div>
</HuiResizeBox>
</div>
<div class="size-bound">
<p>Two-way dimension binding</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);
"
>
Content
</div>
</HuiResizeBox>
</div>
</div>
<p class="size-status">
Bound size: {{ sizeWidth }}×{{ sizeHeight }};Latest request: {{ sizeLast }};Actual end:
{{ sizeEnd }}
</p>
</HuiSpace>
</template>
Props
| Property | Type | Default | Description |
|---|---|---|---|
| width | number | string | — | Supplied props are controlled, including undefined as automatic width; omitted props save width; updates are pixels |
| height | number | string | — | Supplied props are controlled, including undefined as automatic height; omitted props save height |
| directions | ResizeBoxDirection[] | ['right'] | Enabled physical edges; independent of RTL |
| minWidth | number | string | 0 | Minimum width; percentages use the parent |
| maxWidth | number | string | — | Maximum width; unlimited when omitted; never below minimum |
| minHeight | number | string | 0 | Minimum height |
| maxHeight | number | string | — | Maximum height |
| disabled | boolean | false | Block pointer and keyboard resizing |
| tag | string | 'div' | Native root element |
Events
| Event | Arguments | Description |
|---|---|---|
| update:width | (width: number) | Updated width in pixels |
| update:height | (height: number) | Updated height in pixels |
| resizeStart | (size: ResizeBoxSize) | A pointer or keyboard adjustment starts |
| resize | (size: ResizeBoxSize) | Updated size, including restoration on Escape |
| resizeEnd | (size: ResizeBoxSize, cancelled: boolean) | An adjustment ends; true only for Escape cancellation |
The public start and end events are declared and emitted as resizeStart and resizeEnd. Vue templates can still use @resize-start and @resize-end; render functions use onResizeStart and onResizeEnd. Only the new declared events are emitted. Candidate sizes, the final accepted two-axis size, cancelled flag and notification order remain unchanged.
Slots
| Slot | Props | Description |
|---|---|---|
| default | — | Box content |
| handle | ResizeBoxHandleSlotProps | Visual contents of each edge handle |
Methods
No public methods. Set width and height through props.
Types
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
}