跳转到主内容
基础 · Resizable
伸缩框
根据用户的鼠标拖拽,改变组件的大小,支持单个组件伸缩与组合伸缩

Vue 使用说明:从 @aifuxi/semi-ui-vue/resizable 子路径引入组件,浏览器构建会自动按需加载默认主题。本页 Vue API 与示例以当前公开实现为准。文中功能版本与 API 表的版本列记录 Semi Design 上游历史。

代码演示 ​

如何引入 ​

查看代码 · js
import { Resizable } from '@aifuxi/semi-ui-vue';
import { ResizeItem, ResizeHandler, ResizeGroup } from '@aifuxi/semi-ui-vue';

单个组件 基本使用 ​

通过 defaultSize 设置初始大小,并监听 resizeStart、change、resizeEnd 事件。

查看代码 · ts
interface Size {
    width: string | number;
    height: string | number;
}
Drag edge to resize

控制伸缩方向 ​

通过设置enable的值开启/关闭特定伸缩方向,默认值均为true

查看代码 · ts
interface Enable {
    left: Boolean;
    right: Boolean;
    top: Boolean;
    bottom: Boolean;
    topLeft: Boolean;
    topRight: Boolean;
    bottomLeft: Boolean;
    bottomRight: Boolean;
}
disable
enable.left:false

设置变化比例 ​

通过ratio设置拖动和实际变化的比例

ratio=2

锁定横纵比 ​

通过lockAspectRatio设置锁定横纵比,可以为boolean或number,为number时表示横纵比为number,为true时锁定初始横纵比

lock
16 / 9

设置最大,最小宽高 ​

可通过 maxHeight,maxWidth,minHeight,minWidth 设置最大,最小宽高

width在50到200之间,height在50到300之间

受控宽高 ​

可通过 size 控制元素的宽高

受控

设置缩放值 ​

通过设置 scale,整体缩放元素

scale 0.5

根据元素限制元素宽高 ​

通过 boundElement 设置用于限制宽高的元素,支持 string('parent'|'window')

bound:parent

自定义边角 handler 样式 ​

可通过 handleNode 设置不同方向的拖动元素节点,可通过 handleStyle,handleClassName 设置不同方向上的样式

查看代码 · ts
import type { StyleValue, VNodeChild } from 'vue';

type HandleNode = {
    left: VNodeChild,
    right: VNodeChild,
    top: VNodeChild,
    bottom: VNodeChild,
    topLeft: VNodeChild,
    topRight: VNodeChild,
    bottomLeft: VNodeChild,
    bottomRight: VNodeChild,
};

type HandleStyle = {
    left: StyleValue,
    right: StyleValue,
    top: StyleValue,
    bottom: StyleValue,
    topLeft: StyleValue,
    topRight: StyleValue,
    bottomLeft: StyleValue,
    bottomRight: StyleValue,
};

type HandleClass = {
    left: string,
    right: string,
    top: string,
    bottom: string,
    topLeft: string,
    topRight: string,
    bottomLeft: string,
    bottomRight: string,
};
right

允许阶段性调整宽高 ​

可通过 grid,snap 属性允许逐渐调整宽高。 grid 属性用于指定调整大小应对齐的增量。默认为 [1, 1]。 snap 属性用于指定调整大小时应对齐的绝对像素值。 x 和 y 都是可选的,允许仅包含要定义的轴。默认为空。以上两个参数可结合 snapGap 使用,该参数用于指定移动到下一个目标所需的最小间隙。默认为 0,这意味着始终使用 grid/snap 设定的目标。

查看代码 · ts
interface Snap {
    x: number[];
    y: number[];
}
snap

组合组件 基本使用 ​

通过 direction 设置伸缩方向,可选 horizontal 或 vertical;可监听 resizeStart、change、resizeEnd 事件,并通过 min、max 设置尺寸范围。

Drag to resize min:10%
Drag to resize min:10% max:30%
Drag to resize
Drag to resize

嵌套使用 ​

通过direction设置伸缩方向,可选值为horizontal和vertical

header
tab
Drag to resize
Drag to resize min:10% max:30%
Drag to resize min:10%
Drag to resize min:10% max:40%
Drag to resize
Drag to resize max:30%
tab
content

动态方向 ​

drag to resize
drag to resize
drag to resize

API 参考 ​

Vue 契约 ​

以下内容以 packages/ui/src/resizable/types.ts、packages/ui/src/resizable/index.ts 的公开类型为准。

  • v-model:size 对应 size 与 update:size。

Vue 用法 ​

  • Resizable、ResizeGroup、ResizeItem、ResizeHandler 均为具名导出;组合模式按 Item / Handler / Item 顺序放入 ResizeGroup 默认插槽。
  • beforeResizeStart 是保留的 guard callback prop;resizeStart、change、resizeEnd 是组件事件。
  • class 与 style 作为 Vue 原生 attributes 透传,不是四个组件的声明式 props。
  • ResizeHandler 由所在 ResizeGroup 提供方向,不公开独立 props 或事件。

Vue 事件 ​

Resizable

事件参数说明
resizeStart[event: ResizeStartPointer, direction: ResizeDirection]开始调整尺寸
change[size: ResizeSize, event: ResizeStartPointer | ResizeMoveEvent, direction: ResizeDirection]调整过程中的尺寸变化
resizeEnd[size: ResizeSize, event: ResizeMoveEvent, direction: ResizeDirection]结束调整尺寸
update:size[size: ResizeSize]更新 v-model:size

ResizeItem

事件参数说明
resizeStart[event: ResizeStartPointer, direction: ResizeDirection]所在分隔条开始拖动
change[size: ResizeSize, event: ResizeMoveEvent, direction: ResizeDirection]组合项尺寸变化
resizeEnd[size: ResizeSize, event: ResizeMoveEvent, direction: ResizeDirection]所在分隔条结束拖动

Vue 插槽 ​

Resizable

插槽作用域参数说明
default{}可伸缩内容
handle-top{}顶部手柄
handle-right{}右侧手柄
handle-bottom{}底部手柄
handle-left{}左侧手柄
handle-topRight{}右上手柄
handle-bottomRight{}右下手柄
handle-bottomLeft{}左下手柄
handle-topLeft{}左上手柄

ResizeGroup

插槽作用域参数说明
default{}组合子项

ResizeItem

插槽作用域参数说明
default{}组合项内容

ResizeHandler

插槽作用域参数说明
default{}自定义分隔条

Resizable ​

单个伸缩框组件。

参数说明类型默认值版本
sizev-model:size 绑定的受控宽高ResizeSize—
defaultSize非受控初始宽高ResizeSize—
minWidth指定伸缩框最小宽度string | number—
minHeight指定伸缩框最小高度string | number—
maxWidth指定伸缩框最大宽度string | number—
maxHeight指定伸缩框最大高度string | number—
grid指定调整大小应对齐的增量number | readonly [number, number][1, 1]
snap指定调整大小时应对齐的绝对像素值。 x 和 y 都是可选的,允许仅包含要定义的轴{ x?: readonly number[]; y?: readonly number[] }null
snapGap用于指定移动到下一个目标所需的最小间隙。number0
boundElement限制可伸缩范围的 parent、window 或 HTMLElement'parent' | 'window' | HTMLElement—
boundsByDirection—booleanfalse
lockAspectRatio设置伸缩框横纵比,当为true时按照初始宽高锁定boolean | numberfalse
lockAspectRatioExtraWidth—number0
lockAspectRatioExtraHeight—number0
enable各方向开关;false 关闭全部手柄ResizeEnable | false{}
handleStyle各方向手柄样式ResizeHandleStyle—
handleClass各方向手柄类名ResizeHandleClass—
handleWrapperStyle—CSSProperties—
handleWrapperClass—string—
handleNode各方向手柄 VNode;对应 handle-* 插槽优先ResizeHandleNode—
scale可伸缩元素被缩放的比例number1
ratio—number | readonly [number, number]1
beforeResizeStart开始调整前的 guard callback;返回 false 可取消本次调整ResizeStartGuard—

ResizeGroup ​

参数说明类型默认值版本
direction指定Group内的伸缩方向ResizeGroupDirectionhorizontal

ResizeHandler ​

参数说明类型默认值版本

ResizeItem ​

参数说明类型默认值版本
min指定伸缩框最小尺寸(百分比或像素值)string—
max指定伸缩框最大尺寸(百分比或像素值)string—
defaultSize用于设置初始宽高,字符串支持%和px单位,当字符串为纯数字或直接设置数字时表示按照值的比例分配剩余空间string | number—