This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
材料颜色
从 JavaScript 读取 Material 3 颜色调色板(包括 Material 3 动态颜色)。
@expo/ui/jetpack-compose 暴露了 Jetpack Compose 使用的 Material 3 颜色调色板,这样你可以选择一个调色板来源,并让每个组件在 <Host> 主题下都保持一致。
调色板来源取决于你传入的选项:
🌐 The palette source depends on the options you pass:
- 壁纸衍生: 当未提供
seedColor时,Android 12 及以上版本的默认设置。使用 Material 3 动态颜色(Material You)。 - 静态 Material 3 基线: 在未提供
seedColor时,Android 11 及以下的默认回退。 - 从颜色生成: 当你传入
seedColor时,完整的调色板会使用与 Material 3 动态颜色为基于壁纸的颜色使用的相同算法生成。适用于所有 Android API 级别,并且独立于壁纸。
安装
🌐 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
从种子颜色为 <Host> 主题化
🌐 Theming <Host> from a seed color
Host 可以直接接受 seedColor 和 colorScheme 属性。这是为 Compose 子树设置主题的推荐方法。在这个 Host 下的原生 Compose 组件将使用预设调色板进行渲染,任何在后代中调用 useMaterialColors() 而不带参数的组件都会从宿主的上下文中获取相同的调色板。
import { Button, Host, Text } from '@expo/ui/jetpack-compose'; export default function BrandedHostExample() { return ( <Host seedColor="#8E24AA" colorScheme="dark" style={{ flex: 1 }}> <Button onClick={() => {}}> <Text>Themed from the seed</Text> </Button> </Host> ); }
在主机中读取当前调色板
🌐 Reading the current palette inside Host
在 <Host> 内部调用 useMaterialColors() 时不使用参数,以读取主机当前的调色板。该钩子返回一个引用稳定的 MaterialColors 对象,并且在重新渲染时不会跨越本地桥。
🌐 Call useMaterialColors() without arguments inside a <Host> to read the Host's current palette. The hook returns a reference-stable MaterialColors object and does not cross the native bridge on re-renders.
import { Column, Host, Text, useMaterialColors } from '@expo/ui/jetpack-compose'; import { padding } from '@expo/ui/jetpack-compose/modifiers'; export default function MaterialColorsExample() { return ( <Host style={{ flex: 1 }}> <PaletteInspector /> </Host> ); } function PaletteInspector() { const colors = useMaterialColors(); return ( <Column modifiers={[padding(16, 16, 16, 16)]} verticalArrangement={{ spacedBy: 8 }}> <Text>Primary: {colors.primary}</Text> <Text>Surface: {colors.surface}</Text> </Column> ); }
使用参数计算特定调色板
🌐 Computing a specific palette with arguments
将参数传递给 useMaterialColors() 以按需计算调色板,即使在 <Host> 外也可以。colorScheme 接受 'light' 或 'dark',你可以省略它以遵循系统设置。
🌐 Pass arguments to useMaterialColors() to compute a palette on demand, even outside a <Host>. The colorScheme takes 'light' or 'dark', you can omit it to follow the system.
import { Column, Host, Text, useMaterialColors } from '@expo/ui/jetpack-compose'; import { padding } from '@expo/ui/jetpack-compose/modifiers'; export default function UseMaterialColorsExample() { const dark = useMaterialColors({ colorScheme: 'dark' }); const brand = useMaterialColors({ seedColor: '#8E24AA' }); const brandedDark = useMaterialColors({ colorScheme: 'dark', seedColor: '#8E24AA' }); return ( <Host style={{ flex: 1 }}> <Column modifiers={[padding(16, 16, 16, 16)]} verticalArrangement={{ spacedBy: 8 }}> <Text>Dark primary: {dark.primary}</Text> <Text>Brand primary: {brand.primary}</Text> <Text>Branded dark primary: {brandedDark.primary}</Text> </Column> </Host> ); }
在 React 组件外读取颜色
🌐 Reading colors outside React components
import { getMaterialColors, isDynamicColorAvailable } from '@expo/ui/jetpack-compose'; const palette = getMaterialColors({ seedColor: '#8E24AA' }); console.log('available:', isDynamicColorAvailable, 'primary:', palette.primary);
应用接口
🌐 API
Constants
Type: boolean
Whether the current device supports Material You dynamic colors (Android
12+). When false, getMaterialColors and useMaterialColors
return the static Material 3 baseline palette unless a seedColor is
provided — seed-based palettes work on every Android API level.
Hooks
| Parameter | Type |
|---|---|
| options(optional) | UseMaterialColorsOptions |
Returns the Material 3 color palette.
Call with no arguments inside a <Host> to get the palette that <Host> is themed with.
Pass scheme and/or seedColor to get a specific palette.
MaterialColorsMethods
| Parameter | Type |
|---|---|
| options(optional) | MaterialColorsOptions |
Get the Material 3 color palette.
MaterialColorsTypes
Material 3 color palette exposed to TypeScript/JavaScript as #RRGGBBAA strings.
On Android 12+ devices these values are derived from the app user's wallpaper
(Material You). On older devices they fall back to the static Material 3
baseline palette. Use isDynamicColorAvailable to distinguish the
two at runtime.
| Property | Type | Description |
|---|---|---|
| background | RgbaHex | The background color that appears behind scrollable content. |
| error | RgbaHex | The error color is used to indicate errors in components, such as invalid text in a text field. |
| errorContainer | RgbaHex | The preferred tonal color of error containers. |
| inverseOnSurface | RgbaHex | A color that contrasts well with |
| inversePrimary | RgbaHex | Color to be used as a "primary" color in places where the inverse color scheme is needed, such as the button on a SnackBar. |
| inverseSurface | RgbaHex | A color that contrasts sharply with |
| onBackground | RgbaHex | Color used for text and icons displayed on top of the background color. |
| onError | RgbaHex | Color used for text and icons displayed on top of the error color. |
| onErrorContainer | RgbaHex | The color (and state variants) that should be used for content on top of |
| onPrimary | RgbaHex | Color used for text and icons displayed on top of the primary color. |
| onPrimaryContainer | RgbaHex | The color (and state variants) that should be used for content on top of |
| onPrimaryFixed | RgbaHex | Color used for text and icons displayed on top of |
| onPrimaryFixedVariant | RgbaHex | An |
| onSecondary | RgbaHex | Color used for text and icons displayed on top of the secondary color. |
| onSecondaryContainer | RgbaHex | The color (and state variants) that should be used for content on top of |
| onSecondaryFixed | RgbaHex | Color used for text and icons displayed on top of |
| onSecondaryFixedVariant | RgbaHex | An |
| onSurface | RgbaHex | Color used for text and icons displayed on top of the surface color. |
| onSurfaceVariant | RgbaHex | The color (and state variants) that can be used for content on top of |
| onTertiary | RgbaHex | Color used for text and icons displayed on top of the tertiary color. |
| onTertiaryContainer | RgbaHex | The color (and state variants) that should be used for content on top of |
| onTertiaryFixed | RgbaHex | Color used for text and icons displayed on top of |
| onTertiaryFixedVariant | RgbaHex | An |
| outline | RgbaHex | Subtle color used for boundaries. Outline color role adds contrast for accessibility purposes. |
| outlineVariant | RgbaHex | Utility color used for boundaries for decorative elements when strong contrast is not required. |
| primary | RgbaHex | The primary color is the color displayed most frequently across your app's screens and components. |
| primaryContainer | RgbaHex | The preferred tonal color of containers. |
| primaryFixed | RgbaHex | A primary variant that maintains the same tone in light and dark themes. The fixed color role may be used instead of the equivalent container role in situations where such fixed behavior is desired. |
| primaryFixedDim | RgbaHex | A primary variant that maintains the same tone in light and dark themes. Dim roles provide a stronger, more emphasized tone relative to the equivalent fixed color. |
| scrim | RgbaHex | Color of a scrim that obscures content. |
| secondary | RgbaHex | The secondary color provides more ways to accent and distinguish your product. Secondary colors are best for:
|
| secondaryContainer | RgbaHex | A tonal color to be used in containers. |
| secondaryFixed | RgbaHex | A secondary variant that maintains the same tone in light and dark themes. The fixed color role may be used instead of the equivalent container role in situations where such fixed behavior is desired. |
| secondaryFixedDim | RgbaHex | A secondary variant that maintains the same tone in light and dark themes. Dim roles provide a stronger, more emphasized tone relative to the equivalent fixed color. |
| surface | RgbaHex | The surface color that affect surfaces of components, such as cards, sheets, and menus. |
| surfaceBright | RgbaHex | A surface variant that is always brighter than |
| surfaceContainer | RgbaHex | A surface variant that affects containers of components, such as cards, sheets, and menus. |
| surfaceContainerHigh | RgbaHex | A surface variant for containers with higher emphasis than |
| surfaceContainerHighest | RgbaHex | A surface variant for containers with higher emphasis than |
| surfaceContainerLow | RgbaHex | A surface variant for containers with lower emphasis than |
| surfaceContainerLowest | RgbaHex | A surface variant for containers with lower emphasis than |
| surfaceDim | RgbaHex | A surface variant that is always dimmer than |
| surfaceTint | RgbaHex | This color will be used by components that apply tonal elevation and is applied on top of |
| surfaceVariant | RgbaHex | Another option for a color with similar uses of |
| tertiary | RgbaHex | The tertiary color that can be used to balance primary and secondary colors, or bring heightened attention to an element such as an input field. |
| tertiaryContainer | RgbaHex | A tonal color to be used in containers. |
| tertiaryFixed | RgbaHex | A tertiary variant that maintains the same tone in light and dark themes. The fixed color role may be used instead of the equivalent container role in situations where such fixed behavior is desired. |
| tertiaryFixedDim | RgbaHex | A tertiary variant that maintains the same tone in light and dark themes. Dim roles provide a stronger, more emphasized tone relative to the equivalent fixed color. |
Options common to getMaterialColors and useMaterialColors.
| Property | Type | Description |
|---|---|---|
| scheme(optional) | 'light' | 'dark' | Force a specific appearance. When omitted, the palette follows the current system dark/light mode. |
| seedColor(optional) | ColorValue | Seed color used to generate the full Material 3 palette via the
|
8-digit RGBA hex color string, always in #RRGGBBAA form (uppercase).
Compatible with React Native's ColorValue.
String union of string values.
Options for useMaterialColors.
Type: Omit<MaterialColorsOptions, 'scheme'> extended by:
| Property | Type | Description |
|---|---|---|
| colorScheme(optional) | ColorSchemeName |
|