模板版本:v0.4.0
react-native-theme-control
本项目基于 react-native-theme-control 开发。
该第三方库的仓库已迁移至 Gitcode,且支持直接从 npm 下载,新的包名为:@react-native-ohos/react-native-theme-control 版本所属关系如下:
| 三方库名称 | 三方库版本 | 发布信息 | 支持RN版本 | Autolink | 编译API版本 | 社区基线版本 | npm地址 |
|---|---|---|---|---|---|---|---|
| @react-native-ohos/react-native-theme-control | ~6.2.0 | Gitcode Releases | 0.82 | 是 | API12+ | 7.0.2 | Npm Address |
| @react-native-ohos/react-native-theme-control | ~6.1.1 | Gitcode Releases | 0.77 | 否 | API12+ | 6.1.0 | Npm Address |
| @react-native-ohos/react-native-theme-control | ~6.0.2 | Gitcode Releases | 0.72 | 是 | API12+ | 6.0.1 | Npm Address |
| @react-native-oh-tpl/react-native-theme-control | <= 6.0.1-1.0.3@deprecated | Github Releases(deprecated) | 0.72 | 否 | API12+ | 6.0.1 | Npm Address |
1. 安装与使用
进入到工程目录并输入以下命令:
npm
npm install @react-native-ohos/react-native-theme-control
yarn
yarn add @react-native-ohos/react-native-theme-control
本库还依赖了[react-native-community/segmented-control](./react-native-community-segmented-control.md)
下面的代码展示了这个库的基本使用场景:
使用时 import 的库名不变。
import * as React from 'react';
import { Text, useColorScheme, View } from 'react-native';
import {
setThemePreference,
SystemBars,
ThemePreference,
useThemePreference,
} from '@vonovak/react-native-theme-control';
// @ts-ignore
import SegmentedControl from '@react-native-segmented-control/segmented-control/js/SegmentedControl.js';
export function SimpleScreen() {
const colorScheme = useColorScheme();
const isDarkMode = colorScheme === 'dark';
const themePreference = useThemePreference();
const bgColor = isDarkMode ? '#2A2550' : '#FFF6EA';
const textColor = isDarkMode ? 'white' : 'black';
const barsBackground = isDarkMode ? '#9900F0' : '#A0BCC2';
const dividerColor = textColor;
const textColorStyle = { color: textColor };
const values: Array<ThemePreference> = ['light', 'dark', 'system'];
return (
<View
style={{
backgroundColor: bgColor,
flexGrow: 1,
flexShrink: 1,
alignItems: 'center',
justifyContent: 'space-evenly',
}}
>
<SystemBars
backgroundColor={barsBackground}
dividerColor={dividerColor}
/>
<SegmentedControl
style={{ width: '100%' }}
values={values}
selectedIndex={values.indexOf(themePreference)}
onChange={({ nativeEvent }: { nativeEvent: any }) => {
setThemePreference(nativeEvent.value as ThemePreference);
}}
/>
<Text style={textColorStyle}>useColorScheme(): {colorScheme}</Text>
<Text style={textColorStyle}>
useThemePreference(): {themePreference}
</Text>
</View>
);
}
2. Link
| 是否支持autolink | RN框架版本 | |
|---|---|---|
| ~6.2.0 | 是 | 0.82 |
| ~6.1.1 | 否 | 0.77 |
| ~6.0.2 | 是 | 0.72 |
| <= 6.0.1-1.0.3@deprecated | 否 | 0.72 |
使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/openharmony-sig/ohos_react_native/blob/master/docs/zh-cn/Autolinking.md
如您使用的版本支持 Autolink,并且工程已接入 Autolink,可跳过ManualLink配置。
ManualLink: 此步骤为手动配置原生依赖项的指导
首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 harmony。
2.1. Overrides RN SDK
为了让工程依赖同一个版本的 RN SDK,需要在工程根目录的 oh-package.json5 添加 overrides 字段,指向工程需要使用的 RN SDK 版本。替换的版本既可以是一个具体的版本号,也可以是一个模糊版本,还可以是本地存在的 HAR 包或源码目录。
关于该字段的作用请阅读官方说明
{
...
"overrides": {
"@rnoh/react-native-openharmony" : "./react_native_openharmony"
}
}
2.2. 引入原生端代码
目前有两种方法:
- 通过 har 包引入;
- 直接链接源码。
方法一:通过 har 包引入(推荐)
har 包位于三方库安装路径的 `harmony` 文件夹下。
打开 entry/oh-package.json5,添加以下依赖
"dependencies": {
"@rnoh/react-native-openharmony": "file:../react_native_openharmony",
"@react-native-ohos/react-native-theme-control": "file:../../node_modules/@react-native-ohos/react-native-theme-control/harmony/themecontrol.har"
}
点击右上角的 sync 按钮
或者在命令行终端执行:
cd entry
ohpm install
方法二:直接链接源码
如需使用直接链接源码,请参考[直接链接源码说明](./link-source-code.md)
2.3. 配置 CMakeLists 和引入 RNThemeControlPackage
打开 entry/src/main/cpp/CMakeLists.txt,添加:
project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
+ set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
+ set(OH_MODULE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
add_subdirectory("${RNOH_CPP_DIR}" ./rn)
# RNOH_BEGIN: manual_package_linking_1
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULE_DIR}/@react-native-ohos/react-native-theme-control/src/main/cpp" ./themecontrol)
# RNOH_END: manual_package_linking_1
add_library(rnoh_app SHARED
"./PackageProvider.cpp"
"${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
)
target_link_libraries(rnoh_app PUBLIC rnoh)
# RNOH_BEGIN: manual_package_linking_2
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_theme_control)
# RNOH_END: manual_package_linking_2
注意:上面NODE_MODULES定义,为源库的安装路径,用户可以根据安装源库的路径定义NODE_MODULES
打开 entry/src/main/cpp/PackageProvider.cpp,添加:
#include "RNOH/PackageProvider.h"
#include "SamplePackage.h"
+ #include "RNThemeControlPackage.h"
using namespace rnoh;
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
std::make_shared<SamplePackage>(ctx),
+ std::make_shared<RNThemeControlPackage>(ctx)
};
}
2.4. 在 ArkTs 侧引入 RNThemeControlPackage
打开 entry/src/main/ets/RNPackagesFactory.ets,添加:
...
+ import { RNThemeControlPackage } from '@react-native-ohos/react-native-theme-control/ts'
export function createRNPackages(ctx: RNPackageContext): RNOHPackage[] {
return [
new SamplePackage(ctx),
+ new RNThemeControlPackage(ctx)
];
}
2.5. 必要的手动配置项
该模块的内容无法通过autolink自动生成,始终需要手动配置。
在 entry/src/main/ets/abilityStage 新建 MyAbilityStage.ets
打开 entry/src/main/ets/abilityStage/MyAbilityStage.ets,添加:
import appRecovery from '@ohos.app.ability.appRecovery';
import AbilityStage from '@ohos.app.ability.AbilityStage';
import { Want } from '@kit.AbilityKit';
export default class MyAbilityStage extends AbilityStage {
onCreate() {
appRecovery.enableAppRecovery(
appRecovery.RestartFlag.ALWAYS_RESTART,
appRecovery.SaveOccasionFlag.SAVE_WHEN_ERROR,
appRecovery.SaveModeFlag.SAVE_WITH_FILE
);
let wantObj: Want = {
bundleName: 'com.rnoh.tester',
abilityName: 'EntryAbility'
};
appRecovery.setRestartWant(wantObj)
}
}
在 EntryAbility.ets 配置生命周期调用
打开 entry/src/main/ets/entryability/EntryAbility.ets,添加:
+ import {RNAbility} from '@rnoh/react-native-openharmony';
+ import Want from '@ohos.app.ability.Want';
+ import { RNThemeControlModule } from '@react-native-ohos/react-native-theme-control';
.....
+ export default class EntryAbility extends RNAbility {
+ onCreate(want: Want): void {
+ super.onCreate(want);
+ RNThemeControlModule.recoverApplicationTheme(this.context);
+ }
getPagePath() {
return 'pages/Index';
}
}
打开 entry/src/main/module.json5,添加:
"module": {
"name": "entry",
+ "srcEntry": "./ets/abilityStage/MyAbilityStage.ets",
"type": "entry",
.....
"abilities": [
{
"name": "EntryAbility",
.....
"visible": true,
+ "recoverable": true,
.....
2.6. 运行
点击右上角的 sync 按钮
或者在命令行终端执行:
cd entry
ohpm install
然后编译、运行即可。
3. 约束与限制
3.1. 兼容性
本文档内容基于以下版本验证通过:
- RNOH: 0.72.96; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;
- RNOH: 0.72.33; SDK: HarmonyOS NEXT B1; IDE: DevEco Studio: 5.0.3.900; ROM: Next.0.0.71;
- RNOH: 0.77.18; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112;
- RNOH: 0.82.22; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.878; ROM: 6.0.0.130;
3.2. 权限要求
3.3. 编译运行API要求
当前三方库所有版本均已实现版本隔离,支持在 `API12+` 工程编译,及 `API12+` ROM运行。
4. API
"Platform"列表示该属性在原三方库上支持的平台。
"HarmonyOS Support"列为 yes 表示 HarmonyOS 平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
| Name | Description | Type | Required | Platform | HarmonyOS Support |
|---|---|---|---|---|---|
SystemBars |
设置系统状态栏 | Components | No | IOS/Android | Yes |
NavigationBar |
设置导航栏 | Components | No | Android | No |
AppBackground |
设置 UIApplication 窗口(iOS)或当前 Activity(Android)的背景色 | Components | No | IOS/Android | Yes |
setNavbarAppearance |
主动设置导航栏的外观 | Function | No | Android | No |
setAppBackground |
设置背景颜色 | Function | No | IOS/Android | Yes |
setThemePreference |
设置主题 | Function | No | IOS/Android | Yes |
getThemePreference |
获取主题 | Function | No | IOS/Android | Yes |
useThemePreference |
一个 React 钩子,返回当前的主题偏好,可能是暗色、亮色(如果您之前调用 setAppearance 设置过主题)或系统默认 | Function | No | IOS/Android | Yes |
5. 遗留问题
6. 其他
7. 开源协议
本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。