This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
@ivliu/react-offscreen is a React library that hides components without unmounting them, preserving component state. It is based on React's Suspense mechanism and provides an Activity component (compatible with React's experimental/unstable_Activity). Supports React >=17.
pnpm build # Build the library using rslib
pnpm size # Check bundle size limits
pnpm analyze # Analyze bundle size detailsThe library has two implementation paths:
- Native Activity: When React exposes
Activity(React 19 stable) orunstable_Activity(React 18 experimental), it uses the native implementation - Custom Repeater: Falls back to a custom implementation using Suspense + promises to simulate hidden state
src/Activity.tsx- Main component entry (also exports deprecatedOffscreenalias). Handles native vs fallback path selection and shows warnings for lazy-component misuse.src/Repeater.tsx- Custom fallback implementation using Suspense + promise-based suspensionsrc/index.ts- Public API exports: Activity, Offscreen, ActivityMode, OffscreenMode typessrc/registry.tsx- Component registry for @json-render/react, enabling JSON-driven UI rendering with Activity/Offscreensrc/type.ts- Type definitions:ActivityMode,OffscreenMode(deprecated alias),IPropssrc/createPromise.ts- Promise.withResolvers polyfill for older environmentssrc/canUseDOM.ts- Browser environment detection guard
When mode="hidden", the component throws a promise. This causes Suspense to catch it and hide the content. When mode="visible", the promise resolves, allowing content to render. This approach preserves component state while hidden.
Built with rslib using @rsbuild/plugin-react (SWC compilation, automatic JSX runtime). Config in rslib.config.ts.
bundle: false keeps output files as individual modules (not bundled), tree-shakeable by consumers.
dist/cjs/- CommonJS formatdist/esm/- ES modules format- Both include TypeScript definitions (
.d.ts)
@json-render/coreand@json-render/react— JSON-driven UI rendering (used byregistry.tsx)zod— schema validation (used by @json-render integration)