);
}
```
### Controlling Resize Directions
You can enable or disable specific resizing directions by setting the value of enable. All directions are enabled by default.
```tsx
interface Enable {
left: Boolean;
right: Boolean;
top: Boolean;
bottom: Boolean;
topLeft: Boolean;
topRight: Boolean;
bottomLeft: Boolean;
bottomRight: Boolean;
}
```
```jsx live=true
import React, { useState } from 'react';
import { Resizable, Switch, Typography } from '@douyinfe/semi-ui';
function Demo() {
const [b, setB] = useState(false)
const { Title } = Typography;
return (
{b ? 'able' : 'disable'}
{'enable.left:' + b}
);
}
```
### Setting Resizing Ratio
You can set the drag and resize ratio using ratio.
```jsx live=true
import React, { useState } from 'react';
import { Resizable } from '@douyinfe/semi-ui';
function Demo() {
return (
ratio=2
);
}
```
### Locking Aspect Ratio
You can lock the aspect ratio by setting lockAspectRatio. It can be a boolean or a number. If true, it locks to the initial aspect ratio; if a number, it locks to the given ratio.
```jsx live=true
import React, { useState } from 'react';
import { Resizable } from '@douyinfe/semi-ui';
function Demo() {
return (
lock
16 / 9
);
}
```
### Setting Maximum and Minimum Width/Height
You can set the maximum and minimum width and height using maxHeight, maxWidth, minHeight, and minWidth.
```jsx live=true
import React, { useState } from 'react';
import { Resizable } from '@douyinfe/semi-ui';
function Demo() {
return (
width is between 50 and 200, height is between 50 and 300
);
}
```
### Control Width/Height
You can control the size of the element through the size prop.
```jsx live=true
import React, { useState } from 'react';
import { Resizable } from '@douyinfe/semi-ui';
function Demo() {
const [size, setSize] = useState({ width: 200, height: 100 });
const onButtonClick = (() => {
let realSize = { width: size.width + 10, height: size.height + 10 };
setSize(realSize);
})
const onChange = (s) => { setSize(s); }
return (
Control Width/Height
);
}
```
### Setting Scale
You can scale the entire element by setting the scale prop.
```jsx live=true
import React, { useState } from 'react';
import { Resizable } from '@douyinfe/semi-ui';
function Demo() {
return (
scale 0.5
);
}
```
### Restricting Width/Height by an Element
You can restrict the width and height by setting the boundElement, which supports string values like 'parent' or 'window'.
```jsx live=true
import React, { useState } from 'react';
import { Resizable } from '@douyinfe/semi-ui';
function Demo() {
return (
bound:parent
);
}
```
### Customizing Corner Handler Styles
You can customize the drag handles for each direction using handleNode, and apply different styles using handleStyle and handleClassName.
```jsx
type HandleNode = {
left: ReactNode;
right: ReactNode;
top: ReactNode;
bottom: ReactNode;
topLeft: ReactNode;
topRight: ReactNode;
bottomLeft: ReactNode;
bottomRight: ReactNode;
}
type HandleStyle = {
left: React.CSSProperties;
right: React.CSSProperties;
top: React.CSSProperties;
bottom: React.CSSProperties;
topLeft: React.CSSProperties;
topRight: React.CSSProperties;
bottomLeft: React.CSSProperties;
bottomRight: React.CSSProperties;
}
type HandleClass = {
left: string;
right: string;
top: string;
bottom: string;
topLeft: string;
topRight: string;
bottomLeft: string;
bottomRight: string;
}
```
```jsx live=true
import React, { useState } from 'react';
import { Resizable, Button } from '@douyinfe/semi-ui';
function Demo() {
return (
}}
>
right
);
}
```
### Allowing Incremental Width and Height Adjustment
You can allow gradual adjustments in width and height using the grid and snap properties. The grid property specifies the increments to which resizing should snap. The default value is [1, 1]. The snap property specifies the absolute pixel values to which resizing should snap. Both x and y are optional, allowing you to define only the desired axis. These two parameters can be combined with the snapGap property, which specifies the minimum gap required to move to the next target. The default is 0, meaning the target defined by grid/snap is always used.
```tsx
interface Snap {
x: number[];
y: number[];
}
```
```jsx live=true
import React, { useState } from 'react';
import { Resizable } from '@douyinfe/semi-ui';
function Demo() {
return (
snap
);
}
```
### Group Component
The parent element of `ResizeGroup` needs to have a size in the main axis direction.
It's best not to set padding for ResizeItem, as it may cause the minimum size to not match the expected value. You can set padding for child elements instead.
Use the direction prop to set the resizing direction. Options are horizontal and vertical. Supports onResizeStart, onResize, and onResizeEnd callbacks, as well as setting min and max to control the maximum and minimum width/height.
```jsx live=true dir="column"
import React, { useState } from 'react';
import { ResizeItem, ResizeHandler, ResizeGroup, Toast } from '@douyinfe/semi-ui';
function Demo() {
const [text, setText] = useState('Drag to resize')
return (
);
}
```
## API
### Resizable
| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --------- | ----------------------------------------------------------------------------- | ----------------------- | ---------- | ------ |
| className | ClassName | string | | |
| size | Controls the size of the resizable box, supports both numeric and string (px/vw/vh/%) formats | [Size](#basic-usage-and-callbacks) | | |
| defaultSize | Sets the initial width and height, supports both numeric and string (px/vw/vh/%) formats | [Size](#basic-usage-and-callbacks) | | |
| minWidth | Specifies the minimum width of the resizable box | string \| number | | |
| maxWidth | Specifies the maximum width of the resizable box | string \| number | | |
| minHeight | Specifies the minimum height of the resizable box | string \| number | | |
| maxHeight | Specifies the maximum height of the resizable box | string \| number | |
| lockAspectRatio | Locks the aspect ratio of the resizable box when true, using the initial width and height as the ratio | boolean \| number | | |
| enable | Specifies the directions in which the resizable box can be resized. If not set, all directions are enabled by default | [Enable](#controlling-resize-directions)
| scale | The scale ratio of the resizable element | number | 1 | |
| boundElement | Restricts the size of the resizable element within a specific element. Pass "parent" to set the parent element as the bounding element | string | | |
| handleNode | Custom nodes for the drag handles in each direction | [HandleNode](#customizing-corner-handler-styles) | | |
| handleStyle | Styles for the drag handles in each direction | [HandleNode](#customizing-corner-handler-styles) | | |
| handleClass | Class names for the drag handles in each direction | [HandleNode](#customizing-corner-handler-styles) | | |
| style | Style | CSSProperties | |
| snapGap | Specifies the minimum gap required to snap to the next target | number | 0 | |
| snap | Specifies the pixel values to snap to during resizing. Both x and y are optional, allowing the definition of specific axes only | [Snap](#allowing-incremental-width-and-height-adjustment) | null | |
| grid | Specifies the increment to align to when resizing | \[number, number\] | \[1,1\] | |
| onChange | Callback during the dragging process | (size: Size; e: Event; direction: String) => void | - | |
| onResizeStart | Callback when resizing starts | (e: Event; direction: String) => void | - | |
| onResizeEnd | Callback when resizing ends | (size: Size; e: Event; direction: String) => void | - | |
### ResizeGroup
| 参数 | 说明 | 类型 | 默认值 | 版本 |
| ----------- | --------------------------------------------------------------------------------------------------------------------------- | ---------------------------------- | ------ | ---- |
| className | ClassName | string | | |
| direction | Specifies the resize direction within the group | 'horizontal' \| 'vertical' | 'horizontal' | |
### ResizeHandler
| 参数 | 说明 | 类型 | 默认值 | 版本 |
| ----------- | --------------------------------------------------------------------------------------------------------------------------- | ---------------------------------- | ------ | ---- |
| className | ClassName | string | | |
| style | Style | CSSProperties | |
### ResizeItem
| 参数 | 说明 | 类型 | 默认值 | 版本 |
| --------- | ----------------------------------------------------------------------------- | ----------------------- | ---------- | ------ |
| className | ClassName | string | | |
| defaultSize | Used to set the initial width and height. **The string supports % and px units, and when the string is a pure number or a number is set directly, it represents the proportional allocation of the remaining space based on the value.** | string \| number | | |
| min | Specifies the minimum size of the resizable box (as percentage or pixel) | string | | |
| max | Specifies the maximum size of the resizable box (as percentage or pixel) | string | | |
| style | Style | CSSProperties | |
| onChange | Callback during the dragging process | (size: Size; e: Event; direction: String) => void | - | |
| onResizeStart | Callback when resizing starts | (e: Event; direction: String) => void | - | |
| onResizeEnd | Callback when resizing ends | (size: Size; e: Event; direction: String) => void | - | |
## Design Tokens