可用于为 React Native 应用构建跨平台一致的设计系统,提升开发体验与组件性能。该项目以 Tailwind CSS 为脚本语言,在构建时处理样式,通过最小运行时实现响应式样式,支持多平台最佳样式引擎及各类 Tailwind 特性。【此简介由AI生成】
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 3 年前 | ||
| 4 年前 | ||
| 3 年前 | ||
| 3 年前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 3 年前 | ||
| 1 个月前 | ||
| 3 年前 | ||
| 1 个月前 | ||
| 4 年前 | ||
| 3 年前 | ||
| 3 年前 | ||
| 4 年前 | ||
| 4 年前 | ||
| 4 年前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 4 年前 | ||
| 4 年前 | ||
| 3 年前 | ||
| 1 个月前 | ||
| 1 个月前 | ||
| 3 年前 | ||
| 4 年前 | ||
| 1 个月前 |
文档模板:v0.4.1
nativewind
本项目基于 nativewind 开发。
该第三方库的仓库已迁移至 Gitcode,且支持直接从 npm 下载,新的包名为:@react-native-ohos/nativewind 版本所属关系如下:
| 三方库名称 | 三方库版本 | 发布信息 | 支持RN版本 | Autolink | 编译API版本 | 社区基线版本 | npm地址 |
|---|---|---|---|---|---|---|---|
| @react-native-ohos/nativewind | ~ 2.0.12 | Gitcode Releases | 0.72.* | 否 | API12+ | 2.0.11 | Npm Address |
简介
NativeWind 是一个专为 React Native 打造的样式库(Styling Library),而非组件库。它的核心作用是让开发者能够在 React Native 项目中使用熟悉的 Tailwind CSS 语法进行样式编写,从而提升开发体验和代码的可维护性。
下载安装
进入到工程目录并输入以下命令:
npm
npm install tailwindcss@^3.4.17
npm install @react-native-ohos/nativewind
yarn
yarn add tailwindcss@^3.4.17
yarn add @react-native-ohos/nativewind
tailwind.config.js
npx tailwindcss init
在你的 tailwind.config.js 文件中添加所有组件文件的路径。
- V2.0.11
module.exports = {
- content: [],
+ content: ["./App.{js,jsx,ts,tsx}", "./screens/**/*.{js,jsx,ts,tsx}"],
theme: {
extend: {},
},
plugins: [],
}
- V4.1.24
/** @type {import('tailwindcss').Config} */
module.exports = {
+ content: ["./src/*.{js,jsx,ts,tsx}"],
+ presets: [require("@react-native-ohos/nativewind/preset")],
theme: {
extend: {},
},
plugins: [],
}
修改你的 babel.config.js
- V2.0.11
module.exports = {
- plugins: [],
+ plugins: ["@react-native-oh-tpl/nativewind/babel"],
};
- V4.1.24
module.exports = {
- presets: [],
+ presets: ['@react-native-ohos/nativewind/babel'],
};
修改你的 metro.config.js。
V4.1.24 需要修改你的 metro.config.js。
const { getDefaultConfig } = require("expo/metro-config");
+ const { withNativeWind } = require('@react-native-ohos/nativewind/metro');
const config = getDefaultConfig(__dirname)
+ module.exports = withNativeWind(config, { input: './global.css' })
在工程根目录下创建CSS文件 global.css
>=V4.1.24 创建CSS文件 global.css。
@tailwind base;
@tailwind components;
@tailwind utilities;
约束与限制
兼容性
本文档内容基于以下环境验证通过:
- RNOH: 0.72.96; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;
使用示例
下面的代码展示了这个库的基本使用场景:
使用时 import 的库名不变。
- V2.0.11
import React from 'react';
import { Text, View } from 'react-native';
import { styled } from 'nativewind';
const StyledView = styled(View)
const StyledText = styled(Text)
const NativewindDemo = () => {
return (
<StyledView className="flex-1 items-center justify-center">
<StyledText className="font-bold">
Hello,World.
</StyledText>
</StyledView>
)
}
export default NativewindDemo
- V4.1.24+
import "../global.css"
import { Text, View } from 'react-native';
import { vars } from 'nativewind';
export default function App() {
return (
<View style={vars({ '--brand-color': 'red' })}>
<Text className="text-[--brand-color]" >
Welcome to Nativewind!
</Text>
</View>
);
}
接口说明
"Platform"列表示该属性在原三方库上支持的平台。
"OpenHarmony Support"列为 yes 表示 OpenHarmony平台支持 该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
API
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
| styled() | 用法与 Styled Components 类似,可用于设置基础样式。 | (ComponentName:JSX.Element,StyleName?:String) => JSX.Element | no | All | yes |
| StyledComponent | StyledComponent 是 styled 的组件形式,可将你自定义的组件作为入参传入。 | component | no | All | yes |
| useColorScheme() | 用于获取设备的配色方案信息。 | () =>{colorScheme,setColorScheme} | no | All | yes |
| NativeWindStyleSheet | StyleSheet 是一种抽象层,其设计思路与 CSS 样式表以及 React Native 的 StyleSheet 保持相近。 | component | no | All | yes |
遗留问题
其他
无
目录结构
/rntpc_nativewind # 项目根目录
├── packages # 鸿蒙适配代码
│ └─ nativewind/src # 鸿蒙适配核心代码
│ └─ index.ets # 鸿蒙适配代码入口
│ └─ tailwind
│ └─ index.ts # nativewind核心实现
├── README_en.md # 英文安装使用方法
├── README.md # 中文安装使用方法
贡献代码
使用过程中发现任何问题都可以提交 Issue,当然,也非常欢迎提交 PR 。
开源协议
本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。