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 动态颜色)。

Android
Included in Expo Go
Recommended version:
~58.0.15

Expo UI Jetpack Compose 暴露了 Jetpack Compose 使用的 Material 3 配色方案,这样你就可以选择一个配色来源,并让每个组件在 <Host> 主题下保持一致。

🌐 Expo UI Jetpack Compose exposes the Material 3 color palette used by Jetpack Compose so you can pick a palette source and have every component under a <Host> theme from it consistently.

调色板来源取决于你传入的选项:

🌐 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

Terminal
- npx expo install @expo/ui
- yarn expo install @expo/ui
- pnpm expo install @expo/ui
- bun expo install @expo/ui

If 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() 而不带参数的组件都会从宿主的上下文中获取相同的调色板。

BrandedHostExample.tsx
import { Button, Host, Text } from '@expo/ui/jetpack-compose'; export default function BrandedHostExample() { return ( <Host seedColor="#8E24AA" colorScheme="dark" matchContents> <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.

MaterialColorsExample.tsx
import { Column, Host, Surface, Text, useMaterialColors, } from '@expo/ui/jetpack-compose'; import { padding } from '@expo/ui/jetpack-compose/modifiers'; export default function MaterialColorsExample() { return ( <Host style={{ flex: 1 }}> <Surface> <PaletteInspector /> </Surface> </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.

UseMaterialColorsExample.tsx
import { Column, Host, Surface, 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 }}> <Surface> <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> </Surface> </Host> ); }

在 React 组件外读取颜色

🌐 Reading colors outside React components

GetMaterialColorsExample.tsx
import { getMaterialColors, isDynamicColorAvailable, } from '@expo/ui/jetpack-compose'; const palette = getMaterialColors({ seedColor: '#8E24AA' }); console.log( 'available:', isDynamicColorAvailable, 'primary:', palette.primary );

应用接口

🌐 API

No API data file found, sorry!