@bible-strong/avatar-react

中文文档
0.1.0发布于 2026/08/18AGPL-3.0-onlyTypeScriptES Module近 30 天下载 9098

React 19 renderer for Bible Strong procedural avatars.

原始使用文档
文档目录7 节
  1. Install
  2. Props reference
  3. Definition and playback
  4. Presentation
  5. Playback callbacks
  6. Imperative API
  7. Styling hooks

@bible-strong/avatar-react

React 19 renderer for a validated Bible Strong AvatarDefinition. React and React DOM 19 are peer dependencies; @bible-strong/avatar-core is installed as a normal dependency.

Install

pnpm add @bible-strong/avatar-react react react-dom

Import the package stylesheet once in the application entry point:

import { Avatar, type AvatarController } from '@bible-strong/avatar-react'
import type { AvatarDefinition } from '@bible-strong/avatar-core'
import '@bible-strong/avatar-react/styles.css'
import { useRef } from 'react'

export function Assistant({ definition }: { definition: AvatarDefinition }) {
  const avatar = useRef<AvatarController>(null)
  return (
    <>
      <Avatar ref={avatar} definition={definition} defaultAnimation="idle" />
      <button onClick={() => avatar.current?.play('happy')}>Play happy</button>
      <button onClick={() => avatar.current?.setExpression('neutral')}>Neutral</button>
    </>
  )
}

For a reusable component tied to one JSON definition, use createAvatar. It validates the definition once and, when the JSON is statically typed, narrows animation, defaultAnimation, expression and defaultExpression to the semantic keys present in that definition:

import { createAvatar } from '@bible-strong/avatar-react'
import avatarJson from './strobi.avatar.json'

const StrobiAvatar = createAvatar(avatarJson)

export function Strobi() {
  return <StrobiAvatar defaultAnimation="idle" />
}

Definitions fetched at runtime are validated by the same factory, but their keys are necessarily checked at runtime rather than inferred by TypeScript.

play and setExpression return { ok: true } or a typed error with one of unknown_animation, unknown_expression or controlled_by_props. pause freezes the exact timeline position, calling play with the paused key resumes it, and stop returns an uncontrolled avatar to neutral.

Props reference

Avatar exposes typed props for the definition, playback state and presentation. AnimationKey and ExpressionKey are semantic string keys from the supplied definition.

Definition and playback

PropTypeDefaultBehavior
definitionAvatarDefinitionrequiredValidated JSON definition containing the expressions and animations to render.
animationAnimationKey | undefined—Controlled timeline. Each step chooses the displayed expression. Mutually exclusive with expression.
expressionExpressionKey | undefined—Controlled direct expression. Mutually exclusive with animation.
defaultAnimationAnimationKey | undefined—Initial uncontrolled timeline, read on mount. Autoplay is enabled by default. Mutually exclusive with defaultExpression.
defaultExpressionExpressionKey | undefined—Initial uncontrolled expression, read on mount without starting a timeline. Mutually exclusive with defaultAnimation.
autoplayboolean | undefinedtrueStarts defaultAnimation automatically. It has no effect without defaultAnimation.
refRef<AvatarController> | undefined—Exposes the imperative API described below.

animation and expression are two alternative sources of truth. Passing both throws an error; the component never silently overrides one with the other. A controlled target takes priority over an uncontrolled default when they are intentionally mixed.

Presentation

PropTypeDefaultBehavior
sizenumber | string | undefined240Number or CSS value applied to the wrapper width and height.
classNamestring | undefined—CSS class added to the outer wrapper.
styleCSSProperties | undefined—Inline styles for the outer wrapper. width and height come from size.
ariaLabelstring | undefinedProcedural avatarAccessible name announced to screen readers.

Playback callbacks

PropTypeReceives
onAnimationEnd(animation: AnimationKey) => voidThe key of a once animation when it completes naturally.
onExpressionChange(expression: ExpressionKey) => voidThe semantic expression key whenever the displayed expression changes.
onError(error: AvatarRuntimeError) => voidAn unknown animation or expression key supplied through props.

Unknown keys passed through animation, expression, defaultAnimation or defaultExpression are reported to onError. Without an error handler, the component writes the typed runtime error to the developer console instead of failing silently.

Imperative API

Pass a ref to receive an AvatarController. Use it when buttons, events or another imperative source need to drive an uncontrolled avatar:

MethodTypeBehavior
play(animation)(animation: AnimationKey) => AvatarCommandResultStarts an animation or resumes it from its paused position.
pause()() => voidFreezes the exact timeline position.
stop()() => voidStops playback and resets an uncontrolled avatar to neutral.
setExpression(expression)(expression: ExpressionKey) => AvatarCommandResultShows one expression directly.
getState()() => AvatarPlaybackStateReturns activeAnimation?, activeExpression and status.

play and setExpression return { ok: true } or { ok: false, error }. Errors include unknown_animation, unknown_expression and controlled_by_props. When animation or expression is controlled by props, use those props to change the target; imperative target commands cannot replace the parent value.

The definition is validated once per immutable object reference and revalidated/reinitialized when that reference changes.

Styling hooks

The stylesheet exposes .bs-avatar and .bs-avatar__svg. Consumer className and style are applied to the outer wrapper.

The public component never exposes Studio IDs or document types. The package follows Semantic Versioning. While it remains below 1.0.0, breaking API changes increment the minor version and fixes increment the patch version.

版本与发布记录

@bible-strong/avatar-react 共发布 1 个版本,首次发布于 2026/08/18;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.1.0latest2026/08/182 个月前

关于 @bible-strong/avatar-react 的常见问题

@bible-strong/avatar-react 是什么?

React 19 renderer for Bible Strong procedural avatars.

如何安装 @bible-strong/avatar-react?

在项目目录运行 npm install @bible-strong/avatar-react,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^19.0.0)、react-dom(^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bible-strong/avatar-react 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bible-strong/avatar-react 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@bible-strong/avatar-react 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.12.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bible-strong/avatar-react 可以商用吗?

@bible-strong/avatar-react 使用 AGPL-3.0-only 许可。最严格的 Copyleft:除分发外,通过网络向用户提供基于它的服务也需要开源对应源码。闭源 SaaS 使用前务必评估。以上为概括,不构成法律意见,请以许可原文为准。

@bible-strong/avatar-react 的下载量有多少?

根据 npm 官方下载统计,@bible-strong/avatar-react 在 2026/10/08 – 2026/11/06 这 30 天内被下载 9,098 次,日均约 303 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bible-strong/avatar-react 还在维护吗?

最新版本 0.1.0 发布于 2026/08/18(2 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2026/08/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。