@dugyu/luna-stage

中文文档
0.2.2发布于 2026/05/17Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 5523

Stage system and showcase components for the L.U.N.A project

原始使用文档
文档目录29 节
  1. Contents
  2. Installation
  3. Entry Points
  4. Concepts
  5. Device Frame Model
  6. Fit System
  7. Camera Model (MotionStage only)
  8. Quick Start
  9. Single-frame preview with LunaLynxStage
  10. Without LUNA theming
  11. Standalone LynxStage
  12. API
  13. Stage
  14. StageContainer
  15. LynxStage
  16. LunaLynxStage
  17. MotionStage
  18. MotionStageContainer
  19. MotionPresentation
  20. Usage Examples
  21. Motion: no mount/unmount
  22. Motion: with mount/unmount animations
  23. Perspective carousel
  24. Orthographic compare layout
  25. Component Table
  26. Hooks
  27. SSR / SSG Notes
  28. Bundle & Asset Conventions
  29. Transform Utilities

luna-stage

Stage system and showcase components for the L.U.N.A project. Atomic components for rendering Lynx bundles into a device shell / stage in the browser.

Use luna-stage when you need a single-frame preview (Stage + LunaLynxStage) or motion-enhanced presentation (MotionStage, etc.). For multi-stage orchestration and Studio-level layout, see @dugyu/luna-studio.

Contents

Installation

pnpm i @dugyu/luna-stage

Peer dependencies — install in your application:

pnpm i react

For Lynx rendering (LynxStage / LunaLynxStage), install the Lynx Web runtime:

pnpm i @lynx-js/web-core

For motion capabilities, install motion:

pnpm i motion

Entry Points

Import pathRequires motionRequires @lynx-js/web-coreUse when
@dugyu/luna-stageNoNoStatic device frame + shared hooks
@dugyu/luna-stage/lynxNoYesLynx bundle rendering (LynxStage / LunaLynxStage)
@dugyu/luna-stage/motionYesNoAnimated transitions + perspective camera (MotionStage)

Concepts

Device Frame Model

  • baseWidth / baseHeight define the design baseline (default 375×812), not the viewport size.
  • The frame renders at its base dimensions and uses CSS transform: scale() to fit into the viewport.
  • Two visual layers: outline (decorative border, simulates device thickness) and frame (content area with clip-path).
  • Stage only: an absolutely-positioned 0×0 anchor sits inside the viewport at the placeX/placeY location; the outline and frame are transformed around this anchor.

Fit System

  • contain — scales down to fit fully inside the viewport (no cropping).
  • cover — scales up to fully cover the viewport (may crop).
  • alignX / alignY — alignment of the frame within the viewport. In cover mode also determines which part of the frame is preserved when cropped. Physical directions: left | center | right / top | center | bottom.
  • placeX / placeY (Stage only) — placement of the anchor within the viewport, decoupled from alignX/Y. Defaults to match alignX/Y for object-position-like semantics. Override only when you want the frame's internal alignment to differ from its viewport position (rare).
  • fitProgress (motion only) — continuous 0→1 interpolation between contain and cover, driven by a spring.

Camera Model (MotionStage only)

The transform pipeline operates in two independent spaces:

Screen space

  • fit / fitProgress — viewport-driven passive scaling.
  • zoom — user-controlled active scaling, applied on top of fit.
  • panX / panY — screen-space translation in pixels, applied after all scaling.

World space

  • world — 3D position of the frame { x, y, z }. Follows the CSS/OpenGL convention: +Z points toward the viewer.
  • focalLength — perspective intensity. When > 0, enables perspective projection. When 0 or omitted, orthographic.
  • world.x/y are projected into screen space before being composited with panX/panY. The spring animates the combined screen-space value.

Final scale = lerpFitScale(contain, cover, fitSpring) × zoom × depthScale

Final translation = world.xy × depthScale + panXY

Quick Start

Single-frame preview with LunaLynxStage

import { Stage, StageContainer } from '@dugyu/luna-stage';
import { LunaLynxStage } from '@dugyu/luna-stage/lynx';

export default function Preview() {
  return (
    <StageContainer className='w-full h-screen'>
      <Stage>
        <LunaLynxStage
          entry='my-component'
          bundleRoot='/bundles/'
          lunaTheme='lunaris-dark'
        />
      </Stage>
    </StageContainer>
  );
}

Default bundle URL convention: ${bundleRoot}${entry}.web.bundle.

Override it with resolveBundleSrc when your host uses a different naming rule.

bundleRoot is normalized internally to always end with a trailing slash before URL resolution. This means callers may pass either '/bundles' or '/bundles/', and resolveBundleSrc will always receive the normalized value.

lunaTheme injects LUNA design token globals into the Lynx runtime.

Without LUNA theming

Use LynxStage directly when you don't need LUNA global props forwarded:

import { Stage, StageContainer } from '@dugyu/luna-stage';
import { LynxStage } from '@dugyu/luna-stage/lynx';

<StageContainer className='w-full h-screen'>
  <Stage>
    <LynxStage entry='my-component' bundleRoot='/bundles/' />
  </Stage>
</StageContainer>;

Standalone LynxStage

LynxStage can be used independently of the Stage system if you just want to render a Lynx view in a DOM container:

import { LynxStage } from '@dugyu/luna-stage/lynx';

export default function StandaloneView() {
  return (
    <div className='w-full h-screen'>
      <LynxStage entry='my-component' bundleRoot='/bundles/' />
    </div>
  );
}

API

Stage

Static device frame. Reads viewport size from StageContainer context if present, otherwise falls back to baseWidth/baseHeight.

Stage supports two ways to determine its viewport size:

Auto-fit to parent (recommended) — wrap in StageContainer so the viewport auto-measures and the frame fits to it:

<StageContainer className='w-full h-full'>
  <Stage>
    <YourContent />
  </Stage>
</StageContainer>;

Explicit viewport size — pass width / height directly to Stage. Useful for fixed-size previews, server-rendered layouts, or anywhere you'd rather not add a measuring wrapper:

<Stage width={390} height={844}>
  <YourContent />
</Stage>;

Note: Wrapping Stage in a sized <div> without StageContainer won't make the frame fit the div — Stage has no way to read the div's size and will fall back to baseWidth × baseHeight. Use StageContainer or pass width/height directly.

Props

PropTypeDefaultDescription
baseWidthnumber375Design baseline width in pixels.
baseHeightnumber812Design baseline height in pixels.
widthnumber—Explicit viewport size. Drives both rendered <div> size and fit/scale. Falls back to StageContainer → baseWidth.
heightnumber—Explicit viewport size. Drives both rendered <div> size and fit/scale. Falls back to StageContainer → baseHeight.
fit'contain' | 'cover''contain'Fit mode.
alignX'left' | 'center' | 'right''center'Horizontal alignment of frame within viewport. In cover mode also determines crop anchor.
alignY'top' | 'center' | 'bottom''center'Vertical alignment of frame within viewport. In cover mode also determines crop anchor.
placeX'left' | 'center' | 'right'alignXHorizontal placement of the internal anchor within the viewport. Override to decouple placement from alignment.
placeY'top' | 'center' | 'bottom'alignYVertical placement of the internal anchor within the viewport. Override to decouple placement from alignment.
zoomnumber1Additional zoom factor on top of fit.
panXnumber0Screen-space translation along X (px).
panYnumber0Screen-space translation along Y (px).
classNamestring—Applied to the outline layer.
styleCSSProperties—Applied to the outline layer.
onClickMouseEventHandler——

StageContainer

Measures its own DOM size via ResizeObserver and provides width/height to child Stage components via context.

Add padding, border, or layout styling to StageContainer freely — the measured content box is what Stage fits to, so padding works as expected visual margin around the frame.

Avoid setting transform, filter, perspective, backdrop-filter, or contain: paint/layout/strict on StageContainer — these establish a containing block that traps position: fixed descendants inside the stage.

Props

PropTypeDefaultDescription
fallbackWidthnumber0Used before first measurement.
fallbackHeightnumber0Used before first measurement.

Inherits all <div> HTML attributes (className, style, etc.).

LynxStage

Core component for rendering Lynx bundles into a DOM container.

PropTypeDefaultDescription
entrystring—Entry name for the Lynx bundle.
bundleRootstring'/'Resource root used with entry to locate the bundle. Normalized to always end with / before URL resolution.
resolveBundleSrcfunction—Optional hook for overriding how the final bundle URL is built. Receives the normalized bundleRoot (always ends with /).
globalPropsRecord<string, unknown>—Global props to inject into the Lynx runtime.
groupIdnumber7Shared Lynx worker group ID.
onNativeModulesCallfunction—Callback for native module calls from Lynx runtime.
onReadyfunction—Callback when the view has rendered successfully.
onErrorfunction—Callback when an error occurs during loading or rendering.

LunaLynxStage

Extends LynxStage with LUNA theme properties.

PropTypeDefaultDescription
lunaThemeLunaThemeKey'luna-light'LUNA theme key (e.g. 'luna-light').
extraGlobalPropsRecord<string, unknown>—Additional global props.
(others)All LynxStage props (except globalProps).

MotionStage

Animated device frame. Extends Stage's base props with spring-driven transitions and a perspective camera model.

MotionStage is recommended inside MotionStageContainer for responsive sizing and layout animations. When rendered standalone it falls back to its baseline size, and you can override sizing explicitly via the width / height MotionValue<number> props.

Props

PropTypeDefaultDescription
baseWidthnumber375Design baseline width in pixels.
baseHeightnumber812Design baseline height in pixels.
widthMotionValue<number>—Override the viewport width (from context). For advanced cases.
heightMotionValue<number>—Override the viewport height (from context). For advanced cases.
fit'contain' | 'cover''contain'Fit mode. Ignored when fitProgress is set.
fitProgressnumber—0–1 blend between contain and cover, driven by a spring.
zoomnumber1Additional zoom factor.
panXnumber0Screen-space translation along X (px).
panYnumber0Screen-space translation along Y (px).
world{ x?, y?, z? }{ x:0, y:0, z:0 }World-space position. +Z toward viewer.
focalLengthnumber—Perspective focal length (px). Omit for orthographic.
alignX'left' | 'center' | 'right''center'Horizontal alignment of frame within viewport.
alignY'top' | 'center' | 'bottom''center'Vertical alignment of frame within viewport.
fitTransitionSpringOptions—Spring for fit / fitProgress.
zoomTransitionSpringOptions—Spring for zoom.
panTransitionSpringOptions—Spring for combined screen-space translation (panX/Y + projected world.x/y).
maskColorstring'transparent'Overlay mask color.
maskOpacitynumber0Overlay mask opacity (animated).
contentInteractivebooleanfalseWhether the frame's children receive pointer events.
classNamestring—Applied to the outline layer.
styleMotionStyle—Applied to the outline layer.

MotionStageContainer

Motion-aware equivalent of StageContainer. Provides MotionValue<number> width/height to child MotionStage components via context. Handles layout animations and extracts the parent scale from transformTemplate to keep the visual size accurate during transitions.

Must be used as a child of <AnimatePresence> for mount/unmount animations to work.

Props

PropTypeDefaultDescription
layoutIdstring—Unique ID for layout animation matching across mount/unmount. Required.
onLayoutMeasure(box: Box) => void—Forwarded from motion. Called when layout box is measured.
onLayoutAnimationStart() => void—Forwarded from motion. Called when layout animation starts.
onLayoutAnimationComplete() => void—Forwarded from motion. Called when layout animation completes.

Inherits all motion.div props (except transformTemplate, which is used internally).

MotionPresentation

A zero-size flex-centered anchor that wraps AnimatePresence propagate for descendant mount/unmount animations.

Use when: items are dynamically added/removed and need mount/unmount animations.

Skip when: components are always mounted, or you only need state transition animations.

Inherits all motion.div props (variants, initial, animate, exit, transition, etc.).

Usage Examples

Motion: no mount/unmount

import { MotionStage, MotionStageContainer } from '@dugyu/luna-stage/motion';

<MotionStageContainer className='w-full h-full' layoutId='stage-1'>
  <MotionStage fitProgress={0} zoom={1.2}>
    <YourContent />
  </MotionStage>
</MotionStageContainer>;

Motion: with mount/unmount animations

import { AnimatePresence } from 'motion/react';
import {
  MotionStage,
  MotionStageContainer,
  MotionPresentation,
} from '@dugyu/luna-stage/motion';

const variants = {
  initial: { opacity: 0, x: -300 },
  animate: { opacity: 1, x: 0 },
  exit: { opacity: 0, x: 300 },
};

<AnimatePresence mode='popLayout'>
  {items.map(item => (
    <MotionStageContainer key={item.id} layoutId={item.id}>
      <MotionPresentation
        variants={variants}
        initial='initial'
        animate='animate'
        exit='exit'
      >
        <MotionStage world={item.world} focalLength={500}>
          <YourContent />
        </MotionStage>
      </MotionPresentation>
    </MotionStageContainer>
  ))}
</AnimatePresence>;

Arrange frames along a circular arc. +Z is toward the viewer, so objects at z < 0 appear further away:

const theta = (index - mid) * (Math.PI / 9); // 20° apart
const world = {
  x: Math.sin(theta) * radius,
  z: -Math.cos(theta) * radius, // negative = further from viewer
};

<MotionStage world={world} focalLength={500}>
  <YourContent />
</MotionStage>;

Orthographic compare layout

Omit focalLength (or set to 0) — all frames render at equal scale regardless of world.z:

<MotionStage world={{ x: offset, y: 0, z: 0 }}>
  <YourContent />
</MotionStage>;

Component Table

ComponentEntry pointDescription
Stage.Static device frame
StageContainer.Auto-measures container, provides size via context
LynxStage./lynxCore Lynx bundle renderer (no LUNA globals)
LunaLynxStage./lynxLynxStage + LUNA theme prop injection
MotionStage./motionAnimated device frame with perspective camera
MotionStageContainer./motionLayout-animated wrapper; provides MotionValue size
MotionPresentation./motionMount/unmount transition orchestrator

Hooks

ExportEntry pointDescription
useEventCallback.Stable callback that always calls the latest fn
useIsClient.SSR-safe client-side mount detection
useContainerResize.Observes container dimensions via ResizeObserver
useMergedRefs.Merges multiple refs onto a single element
useIsomorphicLayoutEffect.SSR-safe useLayoutEffect
useLynxStage./lynxCore hook for managing Lynx runtime and lifecycle

SSR / SSG Notes

LynxStage and LunaLynxStage are client-only. They render null on the server and mount only after hydration.

Bundle & Asset Conventions

  • By default, bundle files follow the naming pattern *.web.bundle.
  • bundleRoot is normalized to a trailing slash by the default resolver.
  • Default resolved URL: ${bundleRoot}${entry}.web.bundle.
  • Use resolveBundleSrc when your host needs a different file naming rule or full URL composition.

Transform Utilities

The functions in src/utils/transform.ts are zero-dependency pure functions, safe to copy into other projects.

FunctionDescription
computeScaleRange(input)Returns { contain, cover } scale factors
lerpFitScale(range, t)Interpolates between contain and cover
computeFrameOffset(input)Returns { offsetX, offsetY } for CSS transform
computeDepthScale(focalLength, worldZ)Perspective depth scale f / (f - z)
computeScreenTranslation(input)Projects world XY + screen pan into final translation

To integrate fit/scale into a lynx-view container: copy computeScaleRange, lerpFitScale, and computeFrameOffset. Fix lynx-view at baseWidth × baseHeight, apply the computed transform via CSS, and pass design-baseline pixel dimensions to browserConfig rather than the container's measured size.

版本与发布记录

@dugyu/luna-stage 共发布 6 个版本,首次发布于 2026/05/06;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.2.2latest2026/05/175 个月前
0.2.12026/05/165 个月前
0.2.02026/05/165 个月前
0.1.12026/05/076 个月前
0.1.02026/05/066 个月前
0.0.0-oidc-bootstrap.0oidc-bootstrap2026/05/066 个月前

关于 @dugyu/luna-stage 的常见问题

@dugyu/luna-stage 是什么?

Stage system and showcase components for the L.U.N.A project

如何安装 @dugyu/luna-stage?

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

@dugyu/luna-stage 支持 TypeScript 吗?

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

@dugyu/luna-stage 支持 ESM 还是 CommonJS?

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

@dugyu/luna-stage 可以商用吗?

@dugyu/luna-stage 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@dugyu/luna-stage 的下载量有多少?

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

@dugyu/luna-stage 还在维护吗?

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