基于 @react-native-community/toolbar-android 的 OpenHarmony 适配版,Android 风格工具栏组件
| Files | Last commit | Last update |
|---|---|---|
| 5 years ago | ||
| 1 day ago | ||
| 1 day ago | ||
| 2 months ago | ||
| 6 years ago | ||
| 6 years ago | ||
| 6 years ago | ||
| 6 years ago | ||
| 1 month ago | ||
| 6 months ago | ||
| 6 years ago | ||
| 1 month ago | ||
| 1 day ago | ||
| 6 years ago | ||
| 1 day ago | ||
| 1 year ago | ||
| 1 day ago | ||
| 1 day ago | ||
| 6 years ago | ||
| 6 years ago | ||
| 6 years ago | ||
| 1 day ago | ||
| 6 years ago |
Document Template: v0.4.2
@react-native-community/toolbar-android
English | 简体中文
This project is based on @react-native-community/toolbar-android.
This third-party library has been migrated to AtomGit and is now available for direct download from npm, the new package name is: @react-native-ohos/toolbar-android, the version correspondence details are as follows:
| Name | Version(Npm Address) | Release Information | Supported RN Version | Supported Autolink | Compile API Version | Community Baseline Version | Source code address |
|---|---|---|---|---|---|---|---|
| @react-native-ohos/toolbar-android | ~ 0.4.0 (in development) | AtomGit Releases | 0.82.*/0.84.*/0.86.* |
Yes | API12+ | 0.2.1 | master |
| @react-native-ohos/toolbar-android | ~ 0.3.0 | AtomGit Releases | 0.77.* | No | API12+ | 0.2.1 | br_rnoh0.77 |
| @react-native-ohos/toolbar-android | ~ 0.2.2 | AtomGit Releases | 0.72.* | Yes | API12+ | 0.2.1 | br_rnoh0.72 |
| @react-native-oh-tpl/toolbar-android | <= 0.2.1-0.0.4@deprecated | GitHub Releases(deprecated) | 0.72.* | No | API12+ | 0.2.1 | sig |
Introduction
The ToolbarAndroid component is used in React Native as a wrapper implementation of the Android Toolbar.
Its core functions are:
-
Display toolbar content: supports setting the title, subtitle and their colors, and allows configuring the navigation icon (navIcon), logo, and content insets.
-
Configure the action menu: sets possible actions on the right side of the toolbar via actions, displayed as icons or text, and automatically placed in the 'overflow' menu when there is not enough space.
-
Respond to interaction events: provides callbacks for navigation icon clicks (onIconClicked) and action selection (onActionSelected), and supports RTL layout direction.
Installation
Go to the project directory and execute the following instruction:
npm
npm install @react-native-ohos/toolbar-android
yarn
yarn add @react-native-ohos/toolbar-android
Link
| Version | Supported Autolink | Supported RN Version |
|---|---|---|
| ~ 0.4.0 (in development) | Yes | 0.82/0.84/0.86 |
| ~ 0.3.0 | No | 0.77 |
| ~ 0.2.2 | Yes | 0.72 |
| <= 0.2.1-0.0.4@deprecated | No | 0.72 |
Projects using Autolink need to be configured according to this document, Autolink framework guide: https://atomgit.com/CPF-RN/ohos_react_native/blob/main/docs/en/02-development/02-development-guide/autolinking.md
If the version you are using supports Autolink and the project has integrated Autolink, you can skip the ManualLink configuration.
ManualLink: This step provides guidance for manually configuring native dependencies.
Open the harmony directory of the OpenHarmony project in DevEco Studio.
1. Overrides RN SDK
To ensure the project relies on the same version of the RN SDK, you need to add an overrides field in the project's root oh-package.json5 file, specifying the RN SDK version to be used. The replacement version can be a specific version number, a semver range, or a locally available HAR package or source directory.
For more information about the purpose of this field, please refer to the official documentation.
{
"overrides": {
"@rnoh/react-native-openharmony": "file:../node_modules/@react-native-oh/react-native-harmony/react_native_openharmony.har" // a locally available HAR package under node_modules
// "@rnoh/react-native-openharmony": "~0.82.18" // ohpm version (illustrative)
// "@rnoh/react-native-openharmony" : "./react_native_openharmony" // source code directory (illustrative)
}
}
2. Introducing Native Code
Currently, two methods are available:
- Use the HAR file.
- Directly link to the source code.
Method 1 (recommended): Use the HAR file.
The HAR file is stored in the `harmony` directory in the installation path of the third-party library.
Open entry/oh-package.json5 file and add the following dependencies:
"dependencies": {
"@react-native-ohos/toolbar-android": "file:../../node_modules/@react-native-ohos/toolbar-android/harmony/toolbar_android.har"
}
Click the sync button in the upper right corner.
Alternatively, run the following instruction on the terminal:
cd entry
ohpm install
Method 2: Directly link to the source code.
For details, see [Directly Linking Source Code](https://atomgit.com/CPF-RN/usage-docs/blob/master/en/link-source-code.md).
3. Configuring CMakeLists and Introducing ToolbarAndroidPackage
Open entry/src/main/cpp/CMakeLists.txt and add:
project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
+ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
set(LOG_VERBOSITY_LEVEL 1)
set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
set(WITH_HITRACE_SYSTRACE 1)
add_compile_definitions(WITH_HITRACE_SYSTRACE)
# RNOH_BEGIN: manual_package_linking_1
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULES}/@react-native-ohos/toolbar-android/src/main/cpp" ./toolbar-android)
# 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: link_packages
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_toolbar_android)
# RNOH_END: link_packages
Open entry/src/main/cpp/PackageProvider.cpp and add:
#include "RNOH/PackageProvider.h"
#include "SamplePackage.h"
+ #include "ToolbarAndroidPackage.h"
using namespace rnoh;
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
std::make_shared<SamplePackage>(ctx),
+ std::make_shared<ToolbarAndroidPackage>(ctx),
};
}
4. Introducing RNCToolbarAndroid Component to ArkTS Side
Find function buildCustomRNComponent(), usually located in entry/src/main/ets/pages/index.ets or entry/src/main/ets/rn/LoadBundle.ets, and add:
...
+ import { RNCToolbarAndroid } from '@react-native-ohos/toolbar-android/src/main/ets/RNCToolbarAndroid'
@Builder
function buildCustomRNComponent(ctx: ComponentBuilderContext) {
...
+ if (ctx.componentName === RNCToolbarAndroid.NAME) {
+ RNCToolbarAndroid({
+ ctx: ctx.rnComponentContext,
+ tag: ctx.tag
+ })
+ }
...
}
...
This library uses a hybrid solution and requires adding the component name.
Find the constant arkTsComponentNames in entry/src/main/ets/pages/index.ets or entry/src/main/ets/rn/LoadBundle.ets and add the component name to the array:
const arkTsComponentNames: Array<string> = [
SampleView.NAME,
+ RNCToolbarAndroid.NAME
];
5. Introducing ToolbarAndroidPackage to ArkTS Side
Open entry/src/main/ets/RNPackagesFactory.ts and add:
import type { RNPackageContext, RNPackage } from 'rnoh/ts';
...
+ import { ToolbarAndroidPackage } from '@react-native-ohos/toolbar-android/src/main/ets/ToolbarAndroidPackage';
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
new SamplePackage(ctx),
+ new ToolbarAndroidPackage(ctx)
];
}
Running
Click the sync button in the upper right corner.
Alternatively, run the following instruction on the terminal:
cd entry
ohpm install
Then compile and run.
Constraints
Compatibility
This document is verified based on the following versions:
- RNOH: 0.84.1; SDK: HarmonyOS 6.0.2 Release SDK; IDE: DevEco Studio 6.0.2 Release; ROM: 7.0.0.100 SP9;
Example
The following code shows the basic use scenario of the repository:
The name of the imported repository remains unchanged.
import React, { useState } from "react";
import { StyleSheet, View, Text } from "react-native";
import ToolbarAndroid from "@react-native-community/toolbar-android";
function App({}): JSX.Element {
const [state, setState] = useState<{
message?: string;
}>({
message: undefined,
});
const { message } = state;
return (
<View style={styles.container}>
<ToolbarAndroid
navIcon={require("./assets/ic_menu_black_24dp.png")}
logo={require("./assets/icon_app.png")}
title={"ToolbarAndroid Example"}
subtitle={"ToolbarAndroid Subtitle"}
titleColor={"#FFFFFF"}
subtitleColor={"#FF00FF"}
contentInsetStart={10}
contentInsetEnd={10}
rtl={false}
style={styles.toolbar}
actions={[
{
title: "Action1",
icon: require("./assets/icon_phone.png"),
show: "ifRoom",
showWithText: true,
},
]}
overflowIcon={require("./assets/relay.png")}
onIconClicked={() => setState({ message: "Clicked nav icon" })}
onActionSelected={(position: number) =>
setState({ message: `Clicked Menu-${position}` })
}
/>
<View style={styles.centerContainer}>
<Text style={styles.headText}>
Click nav or action icon will trigger some events!
</Text>
<Text style={styles.contentText}>{message}</Text>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
toolbar: {
backgroundColor: "#E9EAED",
height: 56,
},
centerContainer: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF",
},
headText: {
fontSize: 16,
},
contentText: {
fontSize: 18,
fontWeight: "bold",
color: "#ff681f",
},
});
export default App;
How to Use
Basic Configuration
<ToolbarAndroid
title={"ToolbarAndroid Example"}
subtitle={"ToolbarAndroid Subtitle"}
titleColor={"#FFFFFF"}
subtitleColor={"#FF00FF"}
navIcon={require("./assets/ic_menu_black_24dp.png")}
logo={require("./assets/icon_app.png")}
contentInsetStart={10}
contentInsetEnd={10}
rtl={false}
style={styles.toolbar}
/>
Action Menu and Event Listening
<ToolbarAndroid
actions={[
{
title: "Action1",
icon: require("./assets/icon_phone.png"),
show: "ifRoom", // always / ifRoom / never
showWithText: true,
},
]}
overflowIcon={require("./assets/relay.png")}
onIconClicked={() => {
// The navigation icon is clicked
}}
onActionSelected={(position) => {
// An action is selected; position is its index in the actions array
}}
/>
Available APIs
The **Platform** column indicates the platform where the properties are supported in the original third-party library.
If the value of **OpenHarmony Platform Support** is **Yes**, it means that the OpenHarmony platform supports this property; **No** means the opposite; **partially** means some capabilities of this property are supported. The usage method is the same on different platforms and the effect is the same as that of iOS or Android.
Components
| Name | Type | Required | Platform | OpenHarmony Platform Support | Description |
|---|---|---|---|---|---|
| ToolbarAndroid | ToolbarAndroidProps | No | Android | Yes | A React Native wrapper of the Android Toolbar |
Properties
Inherits View Props.
| Name | Type | Default Value | Required | Platform | OpenHarmony Platform Support | Description |
|---|---|---|---|---|---|---|
| actions | array of object: {title: string, icon: ImageSource, show: enum('always', 'ifRoom', 'never'), showWithText: bool} | undefined | No | Android | Yes | Sets possible actions on the toolbar as part of the action menu. These are displayed as icons or text on the right side of the widget. If they don't fit they are placed in an 'overflow' menu. The default value of show is 'ifRoom'. |
| contentInsetStart | number | undefined | No | Android | Yes | Sets the content inset for the toolbar starting edge. |
| contentInsetEnd | number | undefined | No | Android | Yes | Sets the content inset for the toolbar ending edge. |
| logo | ImageSource | undefined | No | Android | Yes | Sets the toolbar logo. |
| navIcon | ImageSource | undefined | No | Android | Yes | Sets the navigation icon. |
| onActionSelected | function | undefined | No | Android | Yes | Callback that is called when an action is selected. The only argument that is passed to the callback is the position of the action in the actions array. |
| onIconClicked | function | undefined | No | Android | Yes | Callback called when the icon is selected. |
| overflowIcon | ImageSource | undefined | No | Android | Yes | Sets the overflow icon. |
| rtl | boolean | false | No | Android | Yes | Used to set the toolbar direction to RTL. |
| subtitle | string | undefined | No | Android | Yes | Sets the toolbar subtitle. |
| subtitleColor | string | undefined | No | Android | Yes | Sets the toolbar subtitle color. |
| testID | string | undefined | No | Android | Yes | Used to locate this view in end-to-end tests. When set, the component forwards testID to its ArkUI root node (.id()) so it can be located by UI automation tools (e.g. DevEco UITest By.id()). |
| title | string | undefined | No | Android | Yes | Sets the toolbar title. |
| titleColor | string | undefined | No | Android | Yes | Sets the toolbar title color. |
Note
ImageSource is a union type in React Native: a value is an ImageURISource, an array of ImageURISource, or a number (the resource ID returned by require()). The ImageSource type of actions.icon/logo/navIcon/overflowIcon in the props table refers to this union type, in which only ImageURISource has properties, as listed below.
ImageURISource Properties
| Name | Type | Default Value | Required | Platform | OpenHarmony Platform Support | Description |
|---|---|---|---|---|---|---|
| uri | string | / | Yes | Android | Yes | Load image from a url, e.g. require('./some_icon.png') |
| width | number | / | No | Android | Yes | The width of the image |
| height | number | / | No | Android | Yes | The height of the image |
Known Issues
None
Other
None
Directory Structure
/rntpc_toolbar-android # Project root directory
├── harmony # HarmonyOS adaptation code
│ ├── toolbar_android.har # HAR package
│ └── toolbar_android # HarmonyOS adaptation core code
│ ├── Index.ets # HarmonyOS adaptation code entry
│ └── src/main
│ ├── cpp # C++ code (ToolbarAndroidPackage and codegen-generated code)
│ ├── ets # ArkTS code (RNCToolbarAndroid component, ToolbarAndroidPackage, etc.)
│ └── resources # Resource files
├── js # React Native code
│ ├── ToolbarAndroid.android.js
│ ├── ToolbarAndroid.harmony.js
│ ├── ToolbarAndroid.ios.js
│ ├── ToolbarAndroid.web.js
│ └── specs # codegen spec files
├── example # Example project
├── index.js # Entry file
├── index.d.ts # Type definition file
├── package.json
├── CHANGELOG.md
├── COMMITTERS.md
├── LICENSE
├── OAT.xml
├── README.OpenSource
├── README.md # Chinese installation and usage instructions
└── README_en.md # English installation and usage instructions
How to Contribute
If you find any problem when using toolbar-android, submit an issue or a PR.
License
The repository is based on The MIT License (MIT).
Introduction
基于 @react-native-community/toolbar-android 的 OpenHarmony 适配版,Android 风格工具栏组件
Customize your domain