rntpc_react-native-shared-element:是 React Native 生态中用于实现共享元素过渡动画的跨平台库,核心功能是让两个页面 / 组件中的指定元素(如图片、卡片)在导航切换时,实现流畅的 “位置 / 尺寸 / 样式无缝过渡” 效果,模拟原生 App 的高端动画体验

是 React Native 生态中用于实现共享元素过渡动画的跨平台库,核心功能是让两个页面 / 组件中的指定元素(如图片、卡片)在导航切换时,实现流畅的 “位置 / 尺寸 / 样式无缝过渡” 效果,模拟原生 App 的高端动画体验

Branch7Tags35

Document Template: v0.4.2

react-native-shared-element

English | 简体中文

This project is based on react-native-shared-element.

This third-party library has been migrated to GitCode and is now available for direct download from npm. The new package name is @react-native-ohos/react-native-shared-element, and the version correspondence is 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/react-native-shared-element ~0.10.0 GitCode Releases 0.82.* Yes API12+ 0.9.0-rc0 br_rnoh0.82
@react-native-ohos/react-native-shared-element ~0.9.1 GitCode Releases 0.77.* No API12+ 0.9.0-rc0 br_rnoh0.77
@react-native-ohos/react-native-shared-element ~0.8.10 GitCode Releases 0.72.* Yes API12+ 0.8.9 br_rnoh0.72
@react-native-oh-tpl/react-native-shared-element <=0.8.9-0.0.6@deprecated GitHub Releases(deprecated) 0.72.* No API12+ 0.8.9 sig

Introduction

react-native-shared-element is a library that provides native shared element transition effects for React Native applications. It marks views participating in the transition with SharedElement and natively performs measure, clone, hide, animate and unhide via SharedElementTransition, achieving smooth transitions between screens. It has been adapted to OpenHarmony with the same usage as on iOS/Android.

Installation

Go to the project directory and execute the following instruction:

npm

npm install @react-native-ohos/react-native-shared-element

yarn

yarn add @react-native-ohos/react-native-shared-element

This library supports Autolink. For details, see the Autolinking document.

Version Support Autolink Supported RN Version
shared_element Yes 0.82.*
ManualLink: @react-native-ohos/react-native-shared-element

1. Overrides RN SDK

To ensure the project depends on the same version of the RN SDK, add the following overrides configuration to oh-package.json5 in the project root directory:

{
  "overrides": {
    "@rnoh/react-native-openharmony": "~0.82.18" // ohpm version
    // "@rnoh/react-native-openharmony" : "./react_native_openharmony.har" // a locally available HAR package
    // "@rnoh/react-native-openharmony" : "./react_native_openharmony" // source code directory
  }
}

For more information about the RN SDK, see the Huawei official documentation.

2. Introducing Native Code

There are currently two methods:

  • Introduce via a HAR package;
  • Link the source code directly.

Method 1: Add dependencies to oh-package.json5 in the root directory of the Ohos project

{
  "dependencies": {
    "@react-native-ohos/react-native-shared-element": "file:./har/shared_element.har"
  }
}

Method 2: Link the source code directly

{
  "dependencies": {
    "@react-native-ohos/react-native-shared-element": "file:./harmony"
  }
}

For easier debugging, Method 2 is recommended.

3. Configure CMakeLists and Introduce SharedElementPackage

Add the following configuration to entry/src/main/cpp/CMakeLists.txt in the project directory:

add_subdirectory(${OH_MODULES}/@react-native-ohos/react-native-shared-element/src/main/cpp)

target_link_libraries(entry PUBLIC rnoh_shared_element)

4. Introduce SharedElementPackage to ArkTS

Import in entry/src/main/ets/pages/index.ets

import { SharedElementPackage } from '@react-native-ohos/react-native-shared-element';

// ...

new SharedElementPackage(ctx)

Running

After installing the library and completing the Link configuration, run the project to see the library in action. For details, refer to the example directory.

Constraints

Compatibility

This document is verified based on the following versions:

  1. RNOH: 0.82.7; SDK: HarmonyOS 6.0.1 Release SDK; IDE: DevEco Studio 6.0.1.260; ROM: 6.0.0.130 SP15;

Permission Requirements

No special permission is required.

Example

The following code shows the basic use scenario of the repository:

The name of the imported repository remains unchanged.

import React, { useRef } from 'react';
import { Animated, Image, StyleSheet, View } from 'react-native';
import {
  SharedElement,
  SharedElementTransition,
  nodeFromRef,
} from 'react-native-shared-element';

export function SharedElementDemo() {
  const startAncestor = useRef(null);
  const startNode = useRef(null);
  const endAncestor = useRef(null);
  const endNode = useRef(null);
  const position = useRef(new Animated.Value(0)).current;

  return (
    <View style={styles.container}>
      {/* Scene 1 */}
      <View ref={ref => (startAncestor.current = nodeFromRef(ref))}>
        <SharedElement onNode={node => (startNode.current = node)}>
          <Image style={styles.image} source={require('./images/1.jpg')} />
        </SharedElement>
      </View>

      {/* Scene 2 */}
      <View ref={ref => (endAncestor.current = nodeFromRef(ref))}>
        <SharedElement onNode={node => (endNode.current = node)}>
          <Image style={styles.image} source={require('./images/2.jpg')} />
        </SharedElement>
      </View>

      {/* Render overlay in front of screen */}
      <View style={StyleSheet.absoluteFill}>
        <SharedElementTransition
          start={{ node: startNode.current, ancestor: startAncestor.current }}
          end={{ node: endNode.current, ancestor: endAncestor.current }}
          position={position}
          animation="move"
          resize="auto"
          align="auto"
        />
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1 },
  image: { width: 200, height: 200 },
});

Available APIs

The **Platform** column indicates the platform where the properties are supported in the original third-party library.

If the value of **OpenHarmony 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 Parameter Type Required Platform OpenHarmony Platform Support Description
SharedElement SharedElementProps no iOS/Android Yes Shared element component that wraps a view participating in the transition
SharedElementTransition SharedElementTransitionProps no iOS/Android Yes Shared element transition component that natively performs measure, clone, hide, animate and unhide

Properties

SharedElementProps

Name Type Default Value Required Platform OpenHarmony Platform Support Description
children element None yes iOS/Android Yes A single child component, which must map to a real view in the native view hierarchy
onNode function None yes iOS/Android Yes Event handler that sets or unsets the node handle
View props ViewProps None no iOS/Android Yes Other props supported by View

SharedElementTransitionProps

Name Type Default Value Required Platform OpenHarmony Platform Support Description
start { node: SharedElementNode, ancestor: SharedElementNode } None yes iOS/Android Yes Start node and ancestor
end { node: SharedElementNode, ancestor: SharedElementNode } None yes iOS/Android Yes End node and ancestor
position number | Animated.Value | Reanimated.Value None yes iOS/Android Yes Interpolated position (0..1) between the start and end nodes
animation SharedElementAnimation move no iOS/Android Yes Type of animation: move, fade, fade-in, fade-out
resize SharedElementResize auto no iOS/Android Yes Resize behavior: auto, stretch, clip, none
align SharedElementAlign auto no iOS/Android Yes Alignment behavior: auto, left-top, left-center, left-bottom, right-top, right-center, right-bottom, center-top, center-center, center-bottom
debug boolean false no iOS/Android Yes Renders debug overlays for diagnosing measuring and animations
onMeasure function None no iOS/Android Yes Event handler that is called when nodes have been measured and snapshotted

Methods

Name Type Default Value Required Platform OpenHarmony Platform Support Description
nodeFromRef function None no iOS/Android Yes Gets the shared element node from a ref, used to set the ancestor node

Known Issues

None

Other

None

Directory Structure

/react-native-shared-element  # Project root directory
├── CHANGELOG.md  # Changelog
├── COMMITTERS.md  # Committers list
├── LICENSE.txt  # Open source license
├── OAT.xml  # OAT scanning configuration
├── README.OpenSource  # Open source information
├── README.md  # Chinese document
├── README_en.md  # English document
├── babel.config.js  # Babel configuration
├── buildEnv.sh  # Build environment script
├── flow-typed  # Flow type declarations
├── harmony  # OpenHarmony native module
├── package.json  # Package configuration
├── src  # Library source code
├── tsconfig.json  # TypeScript configuration
└── yarn.lock  # Dependency lock file

How to Contribute

If you find any problem when using react-native-shared-element, submit an Issue. Of course, PR is also welcome.

License

This project is licensed under The MIT License (MIT).

Introduction

是 React Native 生态中用于实现共享元素过渡动画的跨平台库,核心功能是让两个页面 / 组件中的指定元素(如图片、卡片)在导航切换时,实现流畅的 “位置 / 尺寸 / 样式无缝过渡” 效果,模拟原生 App 的高端动画体验

Customize your domain