npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ftsukic/rnoh-keyboard-insets

v1.0.0

Published

Keyboard insets support for React Native OpenHarmony (RNOH).

Readme

@ftsukic/rnoh-keyboard-insets

基于 @sdcx/keyboard-insets 开发的 HarmonyOS / OpenHarmony(RNOH)适配版。沿用上游 KeyboardInsetsView、useKeyboard 及底层原生组件事件契约,并提供 HarmonyOS C-API 原生实现。

本包仅包含 HarmonyOS(RNOH)原生适配,不包含 Android 或 iOS 原生实现。跨平台项目可在 Harmony 构建时通过 Metro alias 将 @sdcx/keyboard-insets 映射至本包,或按平台文件引入。

版本声明

| 包版本 | 上游参考 | React Native | RNOH | HarmonyOS 兼容版本 / target | 架构 | | --- | --- | --- | --- | --- | --- | | 1.0.0 | @sdcx/keyboard-insets | 0.86.3 | 0.86.1 | 5.0.0 (API 12) / 26.0.0 | C-API |

上表记录本仓库 HAR 的构建基线。库按 HarmonyOS API 版本自动分级适配:

  • API 20+:优先使用 keyboardWillShow/Hide 提供的系统动画曲线与 keyboardDidShow/Hide 终态事件,由 C++ ArkUI_NativeAnimateAPI_1 在 UI 线程驱动(支持 linear、cubicBezier 与 SpringCurve)。
  • API 18–19:使用 keyboardHeightChange / avoidAreaChange 触发 C++ 原生合成过渡动画,并由 keyboardDidShow/Hide 校准终态。
  • API 12–17:使用 keyboardHeightChange / avoidAreaChange(自动去重)触发 C++ 原生合成过渡动画(默认 cubic-bezier(0.2, 0, 0, 1)、220ms,可配置覆盖),动画结束时自动闭合 transitioning 状态。

与 @sdcx/keyboard-insets 的 API 对照

| API / 属性 | 兼容状态 | 说明 | | --- | --- | --- | | KeyboardInsetsView (extraHeight, onKeyboard, ViewProps) | 完全一致 | 默认 mode="auto" 自动避让;传入 onKeyboard 自动进入 mode="manual"。 | | useKeyboard() / KeyboardState | 完全一致 | 返回 { keyboard: { height, shown, transitioning, position }, onKeyboard }。 | | KeyboardInsetsViewNativeComponent (mode, onPositionChanged, onStatusChanged) | 完全一致 | 支持配合 react-native-reanimated(Animated.createAnimatedComponent + useEvent)在 UI 线程驱动 Worklet。 | | explicitly | 保留属性 | 在 @sdcx/keyboard-insets 中为 iOS 专用属性,保留类型兼容,HarmonyOS 上不生效。 | | getEdgeInsetsForView | 暂未导出 | 常规布局可使用 react-native-safe-area-context 的 useSafeAreaInsets() 替代。 |

API 12 是 HAR 构建兼容下限。API 12 真机运行、当前 API 26 原生动画路径和键盘逐帧同步仍待验证。模拟器覆盖见测量报告。

安装

npm install @ftsukic/[email protected]

Harmony 工程接入(RN CLI / Expo Harmony)

本库提供预编译 HAR 和 RNOH AutoLink 配置。已接入 RNOH AutoLink 的工程不需要手动编辑 oh-package.json5、CMake 或包工厂;RNOH 的 Hvigor 插件会调用 link-harmony 生成依赖和注册代码。请先确认工程已按 RNOH AutoLink 文档 接入 AutoLink(通常由 harmony/entry/hvigorfile.ts 中的 createRNOHModulePlugin() 提供)。

  1. 在 React Native 工程根目录安装本包:

    npm install @ftsukic/[email protected]
  2. 在 React Native 工程根目录运行 AutoLink 命令:

    npx react-native link-harmony --harmony-project-path ./harmony

    RNOH Hvigor 插件会在同步阶段自动运行同一命令;手动运行可立即生成配置。然后在 Harmony 工程目录安装 HAR 依赖:

    cd harmony
    ohpm install --all

    link-harmony 会把 HAR 添加到 Harmony 根目录的 oh-package.json5,并生成 CMake、C++ 和 ArkTS 包工厂注册。完成后按原有方式构建 Harmony 应用。

若项目未启用 AutoLink,需手动在 harmony/oh-package.json5 引入 HAR,并在 CMakeLists.txt、PackageProvider.cpp(ReactNativeKeyboardInsetsPackage)与 RNPackagesFactory.ets(KeyboardInsetsPackage)中注册。

本仓库 HAR 使用 RNOH 0.86.1、最低兼容 API 12 构建。安装 workspace 依赖后,可用 DevEco Studio 26 从源码重建(可通过 -CompatibleSdkVersion 指定兼容版本):

./packages/rnoh-keyboard-insets/harmony/build-har.ps1 -DevEco 'D:/dev/DevEco Studio'

宿主键盘避让模式配置(必读)

本库不会主动修改应用的窗口级键盘模式,需由宿主在页面中显式配置,避免与系统默认避让叠加:

  • HarmonyOS API 14+:将窗口设为 KeyboardAvoidMode.NONE,本库会自动检测并接管 mode="auto";若窗口为 OFFSET 或 RESIZE,本库不会重复移动容器。
  • HarmonyOS API 12–13(系统尚未提供 KeyboardAvoidMode.NONE):
    1. 保持窗口为默认 KeyboardAvoidMode.OFFSET(不要设为 RESIZE,因为 RESIZE 下 expandSafeArea 对键盘不生效);
    2. 在承载 RNApp / RNSurface 的根容器上添加 .expandSafeArea([SafeAreaType.KEYBOARD], [SafeAreaEdge.BOTTOM]);
    3. 在 aboutToAppear() 中调用 KeyboardInsetsConfig.setSystemAvoidsKeyboard(false) 通知本库接管避让。

兼容 API 12–26 的宿主 Index.ets 参考写法:

import { KeyboardAvoidMode } from '@ohos.arkui.UIContext';
import deviceInfo from '@ohos.deviceInfo';
import { KeyboardInsetsConfig } from '@ftsukic/rnoh-keyboard-insets';

const KEYBOARD_AVOID_MODE_NONE = 4;

@Entry
@Component
struct Index {
  private readonly usesLegacyExpandSafeArea: boolean = deviceInfo.sdkApiVersion < 14;

  aboutToAppear(): void {
    if (this.usesLegacyExpandSafeArea) {
      this.getUIContext().setKeyboardAvoidMode(KeyboardAvoidMode.OFFSET);
      KeyboardInsetsConfig.setSystemAvoidsKeyboard(false);
    } else {
      this.getUIContext().setKeyboardAvoidMode(KEYBOARD_AVOID_MODE_NONE as KeyboardAvoidMode);
    }

    // 可选:自定义 API 12-19 的合成过渡动画参数(默认 cubicBezier [0.2, 0, 0, 1], 220ms)
    // KeyboardInsetsConfig.setSyntheticAnimation({
    //   duration: 220,
    //   curve: 'cubicBezier',
    //   params: [0.2, 0, 0, 1],
    // });
  }

  build(): void {
    Stack() {
      // RNApp / RNSurface
    }
    .width('100%')
    .height('100%')
    .expandSafeArea(
      this.usesLegacyExpandSafeArea ? [SafeAreaType.KEYBOARD] : [],
      this.usesLegacyExpandSafeArea ? [SafeAreaEdge.BOTTOM] : [],
    );
  }
}

API 参考与使用示例

仓库提供了一个完整的 Expo Harmony CNG 示例,包含 Reanimated 聊天底部输入框与表情/快捷短语双面板切换实现。

1. KeyboardInsetsView(自动模式)

默认工作在自动避让模式(mode="auto"),当子树内有 TextInput 获焦且键盘弹出时,会自动计算遮挡高度并通过原生位移上抬容器。支持嵌套使用,自动模式由包含焦点输入框的最近一层 KeyboardInsetsView 接管。

Props

| 属性 | 类型 | 默认值 | 说明 | | --- | --- | --- | --- | | extraHeight | number | 0 | 额外预留间距(dp),使聚焦输入框底边与键盘顶边保持额外留白。 | | onKeyboard | (status: KeyboardState) => void | undefined | 传入后切换为手动模式(mode="manual"),不再自动平移容器。 | | explicitly | boolean | false | 保留用于兼容 @sdcx/keyboard-insets(iOS 专用),HarmonyOS 上不生效。 | | ...ViewProps | Animated.AnimatedProps<ViewProps> | — | 支持标准 View 属性与动画样式。 |

import { ScrollView, TextInput } from 'react-native';
import { KeyboardInsetsView } from '@ftsukic/rnoh-keyboard-insets';

export function FormScreen() {
  return (
    <KeyboardInsetsView extraHeight={16} style={{ flex: 1 }}>
      <ScrollView keyboardShouldPersistTaps="handled">
        <TextInput placeholder="输入内容" />
      </ScrollView>
    </KeyboardInsetsView>
  );
}

2. useKeyboard()(手动模式 + React Native Animated)

useKeyboard() 返回 { keyboard, onKeyboard },可直接传给 KeyboardInsetsView 的 onKeyboard 属性,并用 keyboard.position(原生驱动的 Animated.Value)控制自定义动画。

KeyboardState

| 字段 | 类型 | 说明 | | --- | --- | --- | | height | number | 键盘稳定高度(dp);收起过程中与收起后保留最近一次非零高度。 | | shown | boolean | 键盘目标/当前是否处于弹出状态。 | | transitioning | boolean | 是否正处于弹出或收起的过渡动画中。 | | position | Animated.Value | 实时键盘位移(0 到 height,dp),由原生动画逐帧驱动(useNativeDriver: true)。 |

import { Animated, Text, TextInput, View } from 'react-native';
import { KeyboardInsetsView, useKeyboard } from '@ftsukic/rnoh-keyboard-insets';

export function ManualAnimatedScreen() {
  const { keyboard, onKeyboard } = useKeyboard();

  const translateY = Animated.multiply(keyboard.position, -1);

  return (
    <KeyboardInsetsView onKeyboard={onKeyboard} style={{ flex: 1 }}>
      <View style={{ flex: 1, justifyContent: 'flex-end' }}>
        <Text>
          shown: {String(keyboard.shown)} / transitioning: {String(keyboard.transitioning)} / height: {keyboard.height}
        </Text>
        <Animated.View style={{ transform: [{ translateY }] }}>
          <TextInput placeholder="跟随键盘平滑移动" />
        </Animated.View>
      </View>
    </KeyboardInsetsView>
  );
}

3. KeyboardInsetsViewNativeComponent(手动模式 + react-native-reanimated)

需要在 UI 线程 Worklet 中实现聊天输入框与表情/工具栏等自定义面板无缝切换时,可直接包装 KeyboardInsetsViewNativeComponent,通过 useEvent / useHandler 接收 onPositionChanged 与 onStatusChanged:

原生事件 Payload

  • onPositionChanged:{ position: number }(dp,逐帧回调)
  • onStatusChanged:{ height: number; shown: boolean; transitioning: boolean }(起止状态回调)
import type { ComponentType } from 'react';
import type {
  KeyboardPositionChangedEvent,
  KeyboardPositionPayload,
  KeyboardStatusChangedEvent,
  KeyboardStatusPayload,
} from '@ftsukic/rnoh-keyboard-insets';
import { KeyboardInsetsViewNativeComponent } from '@ftsukic/rnoh-keyboard-insets';
import { TextInput } from 'react-native';
import Animated, {
  useAnimatedStyle,
  useEvent,
  useHandler,
  useSharedValue,
} from 'react-native-reanimated';

const AnimatedKeyboardInsetsView = Animated.createAnimatedComponent(
  KeyboardInsetsViewNativeComponent as ComponentType<any>,
);

export function ReanimatedChatScreen() {
  const position = useSharedValue(0);
  const shown = useSharedValue(false);

  const positionContext = useHandler(
    {
      onPositionChanged: (e: KeyboardPositionPayload) => {
        'worklet';
        position.set(e.position);
      },
    },
    [],
  );
  const onPositionChanged = useEvent<KeyboardPositionChangedEvent>(
    (event) => {
      'worklet';
      if ((event as KeyboardPositionPayload & { eventName: string }).eventName.endsWith('onPositionChanged')) {
        positionContext.context && position.set(event.position);
      }
    },
    ['onPositionChanged'],
    positionContext.doDependenciesDiffer,
  );

  const statusContext = useHandler(
    {
      onStatusChanged: (e: KeyboardStatusPayload) => {
        'worklet';
        shown.set(e.shown);
      },
    },
    [],
  );
  const onStatusChanged = useEvent<KeyboardStatusChangedEvent>(
    (event) => {
      'worklet';
      if ((event as KeyboardStatusPayload & { eventName: string }).eventName.endsWith('onStatusChanged')) {
        statusContext.context && shown.set(event.shown);
      }
    },
    ['onStatusChanged'],
    statusContext.doDependenciesDiffer,
  );

  const composerStyle = useAnimatedStyle(() => ({
    transform: [{ translateY: -position.get() }],
  }));

  return (
    <AnimatedKeyboardInsetsView
      mode="manual"
      onPositionChanged={onPositionChanged}
      onStatusChanged={onStatusChanged}
      style={{ flex: 1, justifyContent: 'flex-end' }}
    >
      <Animated.View style={composerStyle}>
        <TextInput placeholder="Reanimated Worklet 驱动" />
      </Animated.View>
    </AnimatedKeyboardInsetsView>
  );
}

完整双面板切换示例可参考仓库内的 chat-keyboard-insets-view.tsx 与 use-chat-rekeyboard.ts。

4. Harmony 原生配置 API(ArkTS)

从 @ftsukic/rnoh-keyboard-insets(HAR 入口)导出,供 Harmony 宿主按需配置:

  • KeyboardInsetsConfig.setSystemAvoidsKeyboard(avoids: boolean):在 API 12–13 配合 .expandSafeArea([SafeAreaType.KEYBOARD], [SafeAreaEdge.BOTTOM]) 时传 false,告知库接管自动避让。
  • KeyboardInsetsConfig.setSyntheticAnimation(options: KeyboardSyntheticAnimationOptions):自定义 API 12–19 的合成过渡曲线与时长:
    • duration?: number(默认 220 ms)
    • curve?: 'linear' | 'cubicBezier' | 'interpolatingSpring'(默认 'cubicBezier')
    • params?: number[](cubicBezier 为 [x1, y1, x2, y2],默认 [0.2, 0, 0, 1];interpolatingSpring 为 [velocity, mass, stiffness, damping])
  • new KeyboardInsetsPackage(ctx, options?: KeyboardInsetsOptions):非 AutoLink 手动注册时也可通过构造函数传入 systemAvoidsKeyboard 与 syntheticAnimation。

致谢

本项目基于 @sdcx/keyboard-insets 的设计与 API 规范进行 HarmonyOS / OpenHarmony 适配开发。

License

MIT