This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
修饰符
适用于 @expo/ui 组件的 Jetpack Compose 布局修饰符。
Jetpack Compose 修饰符允许你自定义 UI 组件的布局、外观和行为。修饰符是 Compose 中相当于样式属性的概念——它们控制大小、内边距、背景、交互等。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
用法
修饰器通过 modifiers 属性以数组语法应用于组件。你可以组合多个修饰器来创建复杂的样式和行为。修饰器会按照它们在数组中出现的顺序依次应用,这可能会影响最终结果(例如,先应用 padding 再应用 background,与反过来应用会产生不同的结果)。
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> ); }
你还可以创建适用于任何 Expo UI 组件的自定义修饰符。有关详细信息,请参阅自定义 Jetpack Compose 组件指南。
Padding
控制组件内容周围的间距。
paddingAll(all)
在四个边上应用相等的内边距。
import { paddingAll } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[paddingAll(16)]}>带内边距的按钮</Button>;
padding(start, top, end, bottom)
为每一侧分别应用内边距。
import { padding } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[padding(16, 8, 16, 8)]}>自定义内边距</Button>;
尺寸
控制组件的尺寸。
size(width, height)
设置组件的精确尺寸。
import { size } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[size(200, 48)]}>固定尺寸</Button>;
fillMaxSize(fraction?)
在两个维度上填充所有可用空间。
import { fillMaxSize } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[fillMaxSize()]}>填充全部空间</Button> <Button modifiers={[fillMaxSize(0.5)]}>填充一半</Button>
fillMaxWidth(fraction?)
填充可用宽度。
import { fillMaxWidth } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[fillMaxWidth()]}>全宽</Button>;
fillMaxHeight(fraction?)
填充可用高度。
width(value)
设置精确宽度。
height(value)
设置精确高度。
wrapContentWidth(alignment?)
将组件尺寸设为适应其内容宽度。
wrapContentHeight(alignment?)
将组件尺寸设为适应其内容高度。
位置
控制组件相对于其自然位置的位置。
offset(x, y)
将组件从其自然位置偏移,而不影响周围组件的布局。
import { offset } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[offset(10, 5)]}>偏移按钮</Button>;
外观
控制组件的视觉外观。
background(color)
设置背景颜色。
import { background } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[background('#3498DB')]}> Blue background </Button>;
border(borderWidth, borderColor)
为组件添加边框。
import { border } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[border(2, '#E74C3C')]}>带边框按钮</Button>;
shadow(elevation)
在组件下方添加一个高程阴影。
import { shadow } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[shadow(8)]}>有高程的按钮</Button>;
dropShadow(shape, config?)
在组件后方绘制阴影,可控制模糊半径、扩展量、偏移和颜色。与 shadow 不同,它不需要高程值。
import { dropShadow, Shapes, } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[ dropShadow(Shapes.RoundedCorner(24), { radius: 16, spread: 4, color: '#6200EE', offsetY: 8, }), ]}> 阴影 </Button>;
innerShadow(shape, config?)
在组件内部绘制阴影,以创建内凹效果。请先应用 background 修饰符,再应用 innerShadow,这样阴影才能渲染出来。
import { innerShadow, background, Shapes, } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[ background('#FFFFFF'), innerShadow(Shapes.RoundedCorner(24), { radius: 16, spread: 2, offsetY: 6, }), ]}> 内阴影 </Button>;
alpha(alpha)
控制组件的不透明度。
import { alpha } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[alpha(0.5)]}>半透明</Button>;
blur(radius)
为组件应用模糊效果。
import { blur } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[blur(4)]}>模糊按钮</Button>;
阴影配方
常见的阴影样式是 dropShadow 和 innerShadow 修饰符的组合,而不是单独的 API。由于 modifiers 属性接受数组,因此可以通过叠加和调整这些阴影修饰符来构建每种样式。
新粗野主义阴影
新粗野主义阴影是一种硬边缘的投影,没有模糊效果,并且边框较粗。将 radius 和 spread 设为 0,然后为阴影设置偏移。
import { dropShadow, border, background, Shapes, } from '@expo/ui/jetpack-compose/modifiers'; <Box modifiers={[ dropShadow(Shapes.Rectangle, { radius: 0, spread: 0, offsetX: 8, offsetY: 8, color: '#000000', }), border(8, '#000000'), background('#FFFFFF'), ]} />;
新拟物阴影
新拟物阴影是在与背景色相同的表面上叠加两个投影:一个朝向光源的浅色阴影,以及一个位于相反侧的较深阴影。在 background 之前应用这两层阴影。
import { dropShadow, background, Shapes, } from '@expo/ui/jetpack-compose/modifiers'; const shape = Shapes.RoundedCorner(24); <Box modifiers={[ dropShadow(shape, { radius: 15, offsetX: -10, offsetY: -10, color: '#FFFFFF', }), dropShadow(shape, { radius: 15, offsetX: 10, offsetY: 10, color: '#B1B1B1', }), background('#E0E0E0'), ]} />;
要创建按下、凹陷的变体,请改用两个 innerShadow 修饰符,并将它们放在 background 之后。
变换
对组件应用视觉变换。
rotate(degrees)
旋转组件。
import { rotate } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[rotate(45)]}>已旋转</Button>;
zIndex(index)
控制重叠组件的绘制顺序。
import { zIndex } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[zIndex(10)]}>置于顶层</Button>;
动画
在组件内为布局变化添加动画。
animateContentSize(dampingRatio?, stiffness?)
使用弹簧动画为组件内容的大小变化添加动画。
import { animateContentSize } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[animateContentSize()]}>Animated size</Button> <Button modifiers={[animateContentSize(0.5, 200)]}>Custom spring</Button>
布局
控制组件在其父容器中的大小和位置。
weight(weight)
为 Row 或 Column 内的组件分配一个弹性权重,在有权重的子项之间按比例分配可用空间。
import { weight } from '@expo/ui/jetpack-compose/modifiers'; // 在 Row 中,第一个按钮占 2/3,第二个占 1/3 <Button modifiers={[weight(2)]}>更宽</Button> <Button modifiers={[weight(1)]}>更窄</Button>
align(alignment)
设置组件在其父容器中的对齐方式。
matchParentSize()
将组件的大小设置为与其父 Box 的大小相匹配。与 fillMaxSize 不同,这不会影响父组件的测量。
import { matchParentSize } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[matchParentSize()]}>匹配父容器</Button>;
交互
向组件添加用户交互处理器。
clickable(handler)
使组件响应点击事件。
import { clickable } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[clickable(() => console.log('Clicked!'))]}> Clickable </Button>;
combinedClickable(handlers, options?)
使组件同时响应短按和长按手势。封装了 Compose 的 Modifier.combinedClickable。适用于在长按时打开 DropdownMenu,同时在同一视图上保留单独的短按操作。
import { Text } from '@expo/ui/jetpack-compose'; import { combinedClickable } from '@expo/ui/jetpack-compose/modifiers'; <Text modifiers={[ combinedClickable({ onClick: () => console.log('Tapped'), onLongClick: () => setMenuExpanded(true), }), ]}> 长按我 </Text>;
selectable(selected, handler)
使组件可选,类似于单选按钮。
import { selectable } from '@expo/ui/jetpack-compose/modifiers'; <Button modifiers={[ selectable(isSelected, () => setIsSelected(!isSelected)), ]}> Selectable option </Button>;
裁剪
将组件内容裁剪为特定形状。
clip(shape)
将组件裁剪为给定形状。形状边界之外的内容不会被绘制。
可用形状
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 })), ]}> 仅顶部圆角 </Button> // Cut corners <Button modifiers={[clip(Shapes.CutCorner(8))]}>Cut corners</Button>
实用工具
testID(tag)
为组件分配一个测试标识符,以便在 UI 测试中使用。
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
Animates size changes with spring animation.
ModifierConfigSets the background color.
Pass an animationSpec to smoothly animate between colors when the prop changes (backed by animateColorAsState).
ModifierConfigMakes the view respond to both click and long-click gestures.
Wraps Compose's Modifier.combinedClickable. Useful for triggering a DropdownMenu
on long-press while keeping a separate short-press action.
ModifierConfigFactory function to create modifier configuration objects.
ModifierConfigConstrain the size of the wrapped layout only when it would be
otherwise unconstrained: the minWidth and minHeight constraints
are only applied when the incoming corresponding constraint is 0.
ModifierConfigDraws a shadow behind the view with control over the blur radius, spread, offset, and color. Unlike
shadow, it does not require an elevation value.
ModifierConfigApplies a graphics layer transformation with animation support.
ModifierConfigMakes the view horizontally scrollable.
Wraps Modifier.horizontalScroll(rememberScrollState()).
Use on a Row to create a non-lazy scrollable container.
ModifierConfigAdds padding to avoid the software keyboard (IME). When the keyboard is visible, padding is added to keep content above it.
ModifierConfigDraws a shadow inside the view to create an inset effect. The view's background must come before
this modifier for the shadow to render.
ModifierConfig{
$type: 'keyframes',
delayMillis: number,
durationMillis: number,
keyframes: Record<number, number>
}Clips a carousel item's mask to the given shape, so the item keeps its shape while the carousel masks it. Wraps Compose's CarouselItemScope.maskClip.
A carousel reveals each item through a mask, so a plain clip on the item loses its corners as the item squeezes into a peek. Clipping the mask itself keeps them.
Only works on a child of a Carousel component. Like clip, it only affects what is drawn after it in the modifier chain, so place it before background.
ModifierConfigExample
modifiers={[size(300, 200), maskClip(Shapes.RoundedCorner(28)), background('#6200EE')]}
Makes the view match the parent Box size. Only works when used inside Box.
ModifierConfigMarks a composable as the anchor for an ExposedDropdownMenuBox.
Only works when used inside ExposedDropdownMenuBox.
ModifierConfigCalls the handler whenever the composable is positioned, with its position and size.
x and y are relative to the window. All values are in dp.
ModifierConfigCalls the handler whenever the composable's measured size changes. Sizes are in dp.
ModifierConfigCalls the handler when the composable's visibility changes (for example, enters or leaves the viewport in a lazy list).
ModifierConfigApplies padding with individual values for each side.
ModifierConfigMakes the view selectable, like a radio button row.
ModifierConfigMarks a column/row as a selectable group for accessibility. Screen readers will treat the children as a group of selectable items.
ModifierConfigApplies semantic properties. Wraps Modifier.semantics { ... }.
ModifierConfig{
$type: 'spring',
dampingRatio: number,
stiffness: number,
visibilityThreshold: number
}Makes the view toggleable with accessibility semantics. Use this to make a row containing a checkbox or switch tappable as a whole.
ModifierConfig{
$type: 'tween',
delayMillis: number,
durationMillis: number,
easing: 'linear' | 'ease' | 'fastOutSlowIn' | 'fastOutLinearIn' | 'linearOutSlowIn'
}Makes the view vertically scrollable.
Wraps Modifier.verticalScroll(rememberScrollState()).
Use on a Column to create a non-lazy scrollable container.
ModifierConfigSets the weight for flexible sizing in Row or Column. Only works when used inside Row or Column.
ModifierConfigEvent subscriptions
Creates a modifier with an event listener.
ModifierConfigInterfaces
Modifier configuration for native views. This is the JSON Config pattern used by both iOS (SwiftUI) and Android (Jetpack Compose).
Types
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:
Or object shaped as below:
Or object shaped as below:
Or object shaped as below:
Or object shaped as below:
Deprecated: Use ModifierConfig instead. ExpoModifier (SharedRef pattern) has been replaced with JSON Config pattern for better DX and platform consistency.
Type: ModifierConfig