修饰符
用于 @expo/ui 组件的 Jetpack Compose 布局修饰符。
Jetpack Compose 修饰符允许你自定义 UI 组件的布局、外观和行为。修饰符是 Compose 中的样式属性的等价物——它们控制大小、内边距、背景、交互等。
🌐 Jetpack Compose modifiers allow you to customize the layout, appearance, and behavior of UI components. Modifiers are the Compose equivalent of style properties — they control sizing, padding, backgrounds, interactions, and more.
安装
🌐 Installation
- npx expo install @expo/uiIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
🌐 Usage
修饰符通过使用带有数组语法的 modifiers 属性应用于组件。你可以组合多个修饰符来创建复杂的样式和行为。修饰符按它们在数组中出现的顺序应用,这可能会影响最终结果(例如,在 background 之前应用 padding 会产生与相反顺序不同的结果)。
🌐 Modifiers are applied to components using the modifiers prop with an array syntax. You can combine multiple modifiers to create complex styling and behavior. Modifiers are applied in the order they appear in the array, which can affect the final result (for example, applying padding before background produces different results than the reverse).
import { Button, Host } from '@expo/ui/jetpack-compose'; import { paddingAll, fillMaxWidth, background, border, shadow, clickable, } from '@expo/ui/jetpack-compose/modifiers'; function ModifiersExample() { return ( <Host style={{ flex: 1 }}> {/* Basic styling modifiers */} <Button modifiers={[paddingAll(16), fillMaxWidth(), background('#FF6B6B')]}> Full-width padded button </Button> {/* Complex combination with border and shadow */} <Button modifiers={[paddingAll(12), background('#4ECDC4'), border(2, '#2C3E50'), shadow(4)]}> Styled with border and shadow </Button> </Host> ); }
填充
🌐 Padding
控制组件内容周围的间距。
🌐 Control the spacing around a component's content.
paddingAll(all)
在四个边上应用相等的填充。
🌐 Applies equal padding on all four sides.
| 参数 | 类型 | 描述 |
|---|---|---|
all | number | 以 dp 为单位的内边距值。 |
import { paddingAll } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[paddingAll(16)]}>Padded button</Button>;
padding(start, top, end, bottom)
对每一侧应用单独的填充。
🌐 Applies individual padding to each side.
| 参数 | 类型 | 描述 |
|---|---|---|
start | number | 左/起始填充(dp) |
top | number | 顶部填充(dp) |
end | number | 右/结束填充(dp) |
bottom | number | 底部填充(dp) |
import { padding } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[padding(16, 8, 16, 8)]}>Custom padding</Button>;
尺寸
🌐 Size
控制组件的尺寸。
🌐 Control the dimensions of a component.
size(width, height)
为组件设置精确尺寸。
🌐 Sets exact dimensions for the component.
| 参数 | 类型 | 描述 |
|---|---|---|
width | number | 宽度(dp)。 |
height | number | 高度(dp)。 |
import { size } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[size(200, 48)]}>Fixed size</Button>;
fillMaxSize(fraction?)
在两个维度上填充所有可用空间。
🌐 Fills all available space in both dimensions.
| 参数 | 类型 | 描述 |
|---|---|---|
fraction | number | 可用空间的比例(0.0–1.0)。默认值 1.0。 |
import { fillMaxSize } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[fillMaxSize()]}>Fill all space</Button> <Button modifiers={[fillMaxSize(0.5)]}>Fill half</Button>
fillMaxWidth(fraction?)
填充可用宽度。
🌐 Fills available width.
| 参数 | 类型 | 描述 |
|---|---|---|
fraction | number | 可用宽度的分数(0.0–1.0)。默认值为 1.0。 |
import { fillMaxWidth } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[fillMaxWidth()]}>Full width</Button>;
fillMaxHeight(fraction?)
填充可用高度。
🌐 Fills available height.
| 参数 | 类型 | 描述 |
|---|---|---|
fraction | number | 可用高度的比例(0.0–1.0)。默认值 1.0。 |
width(value)
设置一个精确的宽度。
🌐 Sets an exact width.
| 参数 | 类型 | 描述 |
|---|---|---|
value | number | 宽度(以 dp 为单位)。 |
height(value)
设置一个精确的高度。
🌐 Sets an exact height.
| 参数 | 类型 | 描述 |
|---|---|---|
value | number | 高度(以 dp 为单位)。 |
wrapContentWidth(alignment?)
调整组件的尺寸以适应其内容的宽度。
🌐 Sizes the component to wrap its content width.
| 参数 | 类型 | 描述 |
|---|---|---|
alignment | string | 内容的水平对齐。 |
wrapContentHeight(alignment?)
调整组件的尺寸以适应其内容的高度。
🌐 Sizes the component to wrap its content height.
| 参数 | 类型 | 描述 |
|---|---|---|
alignment | string | 内容的垂直对齐。 |
位置
🌐 Position
控制组件相对于其自然位置的位置。
🌐 Control the position of a component relative to its natural placement.
offset(x, y)
将组件从其自然位置偏移,而不影响周围组件的布局。
🌐 Offsets the component from its natural position without affecting the layout of surrounding components.
| 参数 | 类型 | 描述 |
|---|---|---|
x | number | 水平偏移量(dp)。 |
y | number | 垂直偏移量(dp)。 |
import { offset } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[offset(10, 5)]}>Offset button</Button>;
外观
🌐 Appearance
控制组件的视觉外观。
🌐 Control the visual appearance of a component.
background(color)
设置背景颜色。
🌐 Sets the background color.
| 参数 | 类型 | 描述 |
|---|---|---|
color | string | 背景颜色(十六进制字符串)。 |
import { background } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[background('#3498DB')]}>Blue background</Button>;
border(borderWidth, borderColor)
在组件周围添加边框。
🌐 Adds a border around the component.
| 参数 | 类型 | 描述 |
|---|---|---|
borderWidth | number | 边框宽度(dp)。 |
borderColor | string | 边框颜色(十六进制字符串)。 |
import { border } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[border(2, '#E74C3C')]}>Bordered button</Button>;
shadow(elevation)
在组件下方添加一个高度阴影。
🌐 Adds an elevation shadow beneath the component.
| 参数 | 类型 | 描述 |
|---|---|---|
elevation | number | 阴影高度(dp)。 |
import { shadow } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[shadow(8)]}>Elevated button</Button>;
alpha(alpha)
控制组件的不透明度。
🌐 Controls the opacity of the component.
| 参数 | 类型 | 描述 |
|---|---|---|
alpha | number | 不透明度值(0.0–1.0)。 |
import { alpha } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[alpha(0.5)]}>Semi-transparent</Button>;
blur(radius)
对组件应用模糊效果。
🌐 Applies a blur effect to the component.
| 参数 | 类型 | 描述 |
|---|---|---|
radius | number | 模糊半径(dp 单位)。 |
import { blur } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[blur(4)]}>Blurred button</Button>;
变形
🌐 Transform
对组件应用视觉变换。
🌐 Apply visual transformations to a component.
rotate(degrees)
旋转组件。
🌐 Rotates the component.
| 参数 | 类型 | 描述 |
|---|---|---|
degrees | number | 旋转角度(以度为单位)。 |
import { rotate } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[rotate(45)]}>Rotated</Button>;
zIndex(index)
控制重叠组件的绘制顺序。
🌐 Controls the drawing order of overlapping components.
| 参数 | 类型 | 描述 |
|---|---|---|
index | number | 层索引。 |
import { zIndex } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[zIndex(10)]}>On top</Button>;
动画
🌐 Animation
在组件内为布局更改添加动画。
🌐 Animate layout changes within a component.
animateContentSize(dampingRatio?, stiffness?)
使用弹簧动画为组件内容的大小变化添加动画效果。
🌐 Animates size changes of the component's content using a spring animation.
| 参数 | 类型 | 描述 |
|---|---|---|
dampingRatio | number | 弹簧阻尼比。控制弹跳性。 |
stiffness | number | 弹簧刚度。控制动画速度。 |
import { animateContentSize } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[animateContentSize()]}>Animated size</Button> <Button modifiers={[animateContentSize(0.5, 200)]}>Custom spring</Button>
布局
🌐 Layout
控制组件在其父容器中的大小和位置。
🌐 Control how a component is sized and positioned within its parent container.
weight(weight)
为 Row 或 Column 内的组件分配灵活的权重,在加权子组件之间按比例分配可用空间。
🌐 Assigns a flexible weight to a component inside a Row or Column, distributing available space proportionally among weighted children.
| 参数 | 类型 | 描述 |
|---|---|---|
weight | number | 权重系数。 |
import { weight } from '@expo/ui/jetpack-compose/modifiers'; // In a Row, the first button takes 2/3 and the second takes 1/3 <Button modifiers={[weight(2)]}>Wider</Button> <Button modifiers={[weight(1)]}>Narrower</Button>
align(alignment)
设置组件在其父容器中的对齐方式。
🌐 Sets the alignment of the component within its parent container.
| 参数 | 类型 | 描述 |
|---|---|---|
alignment | string | 容器内的对齐方式。 |
matchParentSize()
将组件的大小调整为与其父组件 Box 的大小相匹配。与 fillMaxSize 不同,这不会影响父组件的测量。
🌐 Sizes the component to match the size of its parent Box. Unlike fillMaxSize, this does not affect the parent's measurement.
import { matchParentSize } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[matchParentSize()]}>Match parent</Button>;
互动
🌐 Interaction
为组件添加用户交互处理程序。
🌐 Add user interaction handlers to a component.
clickable(handler)
使组件响应点击事件。
🌐 Makes the component respond to click events.
| 参数 | 类型 | 描述 |
|---|---|---|
handler | () => void | 点击时调用的回调函数。 |
import { clickable } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[clickable(() => console.log('Clicked!'))]}>Clickable</Button>;
selectable(selected, handler)
使组件可选择,类似于单选按钮。
🌐 Makes the component selectable, similar to a radio button.
| 参数 | 类型 | 描述 |
|---|---|---|
selected | boolean | 组件当前是否被选中。 |
handler | () => void | 当选择状态变化时调用的回调。 |
import { selectable } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[selectable(isSelected, () => setIsSelected(!isSelected))]}> Selectable option </Button>;
剪辑
🌐 Clipping
将组件的内容裁剪为特定形状。
🌐 Clip a component's content to a specific shape.
clip(shape)
将组件裁剪到给定形状。形状边界外的内容不会被绘制。
🌐 Clips the component to the given shape. Content outside the shape boundary is not drawn.
| 参数 | 类型 | 描述 |
|---|---|---|
shape | Shape | 要裁剪的形状。 |
可用形状
🌐 Available shapes
| Shape | Description |
|---|---|
Shapes.Rectangle | A rectangle with no rounded corners. |
Shapes.Circle | A perfect circle. |
Shapes.RoundedCorner(radius) | A rectangle with uniform rounded corners. Pass a number for equal radius or an object { topStart, topEnd, bottomStart, bottomEnd } for individual corner radii. |
Shapes.CutCorner(radius) | A rectangle with cut (chamfered) corners. Accepts the same radius options as RoundedCorner. |
Shapes.Material.Cookie4Sided | Material Design cookie shape with 4 sides. |
Shapes.Material.Cookie6Sided | Material Design cookie shape with 6 sides. |
import { clip } from '@expo/ui/jetpack-compose/modifiers'; import { Shapes } from '@expo/ui/jetpack-compose/modifiers'; // Circular clipping <Button modifiers={[clip(Shapes.Circle)]}>Circle</Button> // Rounded corners with uniform radius <Button modifiers={[clip(Shapes.RoundedCorner(12))]}>Rounded</Button> // Rounded corners with individual radii <Button modifiers={[ clip(Shapes.RoundedCorner({ topStart: 16, topEnd: 16, bottomStart: 0, bottomEnd: 0 })), ]}> Top rounded only </Button> // Cut corners <Button modifiers={[clip(Shapes.CutCorner(8))]}>Cut corners</Button>
实用工具
🌐 Utility
testID(tag)
为组件分配一个测试标识符,以便在 UI 测试中使用。
🌐 Assigns a test identifier to the component for use in UI testing.
| 参数 | 类型 | 描述 |
|---|---|---|
tag | string | 测试 ID。 |
import { testID } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[testID('submit-button')]}>Submit</Button>;
应用接口
🌐 API
import { paddingAll, padding, size, fillMaxWidth, background, clickable, clip, Shapes } from '@expo/ui/jetpack-compose/modifiers';
Constants
Type: {
Circle: BuiltinShape,
CutCorner: (params: number | CornerRadii) => BuiltinShape,
Material: {
Arch: BuiltinShape,
Boom: BuiltinShape,
Bun: BuiltinShape,
Clover4Leaf: BuiltinShape,
Clover8Leaf: BuiltinShape,
Cookie12Sided: BuiltinShape,
Cookie4Sided: BuiltinShape,
Cookie6Sided: BuiltinShape,
Cookie7Sided: BuiltinShape,
Cookie9Sided: BuiltinShape,
Diamond: BuiltinShape,
Fan: BuiltinShape,
Ghostish: BuiltinShape,
Heart: BuiltinShape,
Oval: BuiltinShape,
Pentagon: BuiltinShape,
Pill: BuiltinShape,
PixelCircle: BuiltinShape,
PixelTriangle: BuiltinShape,
Puffy: BuiltinShape,
PuffyDiamond: BuiltinShape,
Slanted: BuiltinShape,
SoftBurst: BuiltinShape,
Sunny: BuiltinShape,
Triangle: BuiltinShape,
VerySunny: BuiltinShape
},
Rectangle: BuiltinShape,
RoundedCorner: (params: number | CornerRadii) => BuiltinShape
}
Predefined shapes for use with the clip modifier.
Example
clip(Shapes.Circle) clip(Shapes.RoundedCorner(16)) clip(Shapes.RoundedCorner({ topStart: 8, bottomEnd: 16 })) clip(Shapes.Material.Heart)
Methods
| Parameter | Type | Description |
|---|---|---|
| alpha | number | Opacity value (0.0 to 1.0). |
Sets the opacity/alpha of the view.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| dampingRatio(optional) | number | Spring damping ratio. Default is |
| stiffness(optional) | number | Spring stiffness. Default is |
Animates size changes with spring animation.
ModifierConfig| Parameter | Type |
|---|---|
| targetValue | number |
| spec(optional) | AnimationSpec |
{
$animated: true,
animationSpec: AnimationSpec,
targetValue: number
}| Parameter | Type | Description |
|---|---|---|
| color | ColorValue | Color string (hex, e.g., '#FF0000'). |
Sets the background color.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| radius | number | Blur radius in dp. |
Applies a blur effect.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| borderWidth | number | Border width in dp. |
| borderColor | ColorValue | Border color string (hex). |
Adds a border around the view.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| handler | () => void | Function to call when clicked. |
| options(optional) | {
indication: boolean
} | Optional configuration. |
Makes the view clickable.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| shape | BuiltinShape | A shape from |
Clips the view to a built-in Jetpack Compose shape.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| fraction(optional) | number | Fraction of max height (0.0 to 1.0). Default is 1.0. |
Fills the maximum available height.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| fraction(optional) | number | Fraction of max size (0.0 to 1.0). Default is 1.0. |
Fills the maximum available size.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| fraction(optional) | number | Fraction of max width (0.0 to 1.0). Default is 1.0. |
Fills the maximum available width.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| params | {
alpha: number | {
$animated: true,
animationSpec: AnimationSpec,
targetValue: number
},
ambientShadowColor: ColorValue,
cameraDistance: number,
clip: boolean,
compositingStrategy: 'auto' | 'offscreen' | 'modulate',
rotationX: number | {
$animated: true,
animationSpec: AnimationSpec,
targetValue: number
},
rotationY: number | {
$animated: true,
animationSpec: AnimationSpec,
targetValue: number
},
rotationZ: number | {
$animated: true,
animationSpec: AnimationSpec,
targetValue: number
},
scaleX: number | {
$animated: true,
animationSpec: AnimationSpec,
targetValue: number
},
scaleY: number | {
$animated: true,
animationSpec: AnimationSpec,
targetValue: number
},
shadowElevation: number | {
$animated: true,
animationSpec: AnimationSpec,
targetValue: number
},
shape: BuiltinShape,
spotShadowColor: ColorValue,
transformOriginX: number,
transformOriginY: number,
translationX: number | {
$animated: true,
animationSpec: AnimationSpec,
targetValue: number
},
translationY: number | {
$animated: true,
animationSpec: AnimationSpec,
targetValue: number
}
} | Transform and visual effect parameters. |
Applies a graphics layer transformation with animation support.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| value | number | Height in dp. |
Sets the exact height of the view.
ModifierConfigAdds padding to avoid the software keyboard (IME). When the keyboard is visible, padding is added to keep content above it.
ModifierConfig| Parameter | Type |
|---|---|
| params | {
delayMillis: number,
durationMillis: number,
keyframes: Record<number, number>
} |
{
$type: 'keyframes',
delayMillis: number,
durationMillis: number,
keyframes: Record<number, number>
}Makes the view match the parent Box size. Only works when used inside Box.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| x | number | Horizontal offset in dp. |
| y | number | Vertical offset in dp. |
Offsets the view from its natural position.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| start | number | Left padding in dp (or right in RTL). |
| top | number | Top padding in dp. |
| end | number | Right padding in dp (or left in RTL). |
| bottom | number | Bottom padding in dp. |
Applies padding with individual values for each side.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| all | number | Padding value in dp. |
Applies equal padding on all sides.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| degrees | number | Rotation angle in degrees. |
Rotates the view.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| selected | boolean | Whether the item is currently selected. |
| handler | () => void | Function to call when the item is clicked. |
Makes the view selectable, like a radio button row.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| elevation | number | Shadow elevation in dp. |
Adds a shadow/elevation effect.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| width | number | Width in dp. |
| height | number | Height in dp. |
Sets exact width and height.
ModifierConfig| Parameter | Type |
|---|---|
| params(optional) | {
delayMillis: number
} |
{
$type: 'snap',
delayMillis: number
}| Parameter | Type |
|---|---|
| params(optional) | {
dampingRatio: number,
stiffness: number,
visibilityThreshold: number
} |
{
$type: 'spring',
dampingRatio: number,
stiffness: number,
visibilityThreshold: number
}| Parameter | Type | Description |
|---|---|---|
| tag | string | Test ID string. |
Sets the test ID for testing frameworks.
ModifierConfig| Parameter | Type |
|---|---|
| params(optional) | {
delayMillis: number,
durationMillis: number,
easing: 'linear' | 'fastOutSlowIn' | 'fastOutLinearIn' | 'linearOutSlowIn' | 'ease'
} |
{
$type: 'tween',
delayMillis: number,
durationMillis: number,
easing: 'linear' | 'fastOutSlowIn' | 'fastOutLinearIn' | 'linearOutSlowIn' | 'ease'
}| Parameter | Type | Description |
|---|---|---|
| weight | number | Weight value (relative to siblings). |
Sets the weight for flexible sizing in Row or Column. Only works when used inside Row or Column.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| value | number | Width in dp. |
Sets the exact width of the view.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| alignment(optional) | 'top' | 'bottom' | 'centerVertically' | Optional vertical alignment ('top', 'centerVertically', 'bottom'). |
Wraps the height to the content size.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| alignment(optional) | 'start' | 'end' | 'centerHorizontally' | Optional horizontal alignment ('start', 'centerHorizontally', 'end'). |
Wraps the width to the content size.
ModifierConfig| Parameter | Type | Description |
|---|---|---|
| index | number | Z-index value. |
Sets the z-index for layering.
ModifierConfigTypes
Literal Type: string
Acceptable values are: 'topStart' | 'topCenter' | 'topEnd' | 'centerStart' | 'center' | 'centerEnd' | 'bottomStart' | 'bottomCenter' | 'bottomEnd' | 'top' | 'centerVertically' | 'bottom' | 'start' | 'centerHorizontally' | 'end'
Built-in Jetpack Compose shape for the clip modifier.
Type: object shaped as below:
| Property | Type | Description |
|---|---|---|
| type | 'rectangle' | - |
Or object shaped as below:
| Property | Type | Description |
|---|---|---|
| type | 'circle' | - |
Or object shaped as below:
| Property | Type | Description |
|---|---|---|
| bottomEnd(optional) | number | - |
| bottomStart(optional) | number | - |
| radius(optional) | number | - |
| topEnd(optional) | number | - |
| topStart(optional) | number | - |
| type | 'roundedCorner' | - |
Or object shaped as below:
| Property | Type | Description |
|---|---|---|
| bottomEnd(optional) | number | - |
| bottomStart(optional) | number | - |
| radius(optional) | number | - |
| topEnd(optional) | number | - |
| topStart(optional) | number | - |
| type | 'cutCorner' | - |
Or object shaped as below:
| Property | Type | Description |
|---|---|---|
| name | MaterialShapeName | - |
| type | 'material' | - |