React Strict DOM (RSD)
repository·main·Indexed 25 days ago
https://github.com/react/react-strict-domA library providing a standardized set of styled React components and a unified component API that works across both web and native platforms. It includes the `css` utility for defining styles, the `html` object for platform-agnostic elements, and the `react-strict-animated` package for cross-platform animations including support for timing, spring physics, and interpolation.
What's inside react-strict-dom
- React Strict DOM (RSD) is a library that standardizes the development of styled React components for both web and native platforms. It aims to improve development speed and efficiency across multiple platforms without compromising performance, reliability, or quality. It is designed to help teams ship features to more platforms with fewer engineers by providing a unified component API.
Overview of React Strict DOM CSS API
mainReact Strict DOM provides a built-in way to create and use styles, variables, and themes using a strict subset of CSS capabilities defined in JavaScript. This approach ensures that styling requires only local knowledge within a component, facilitating cross-platform development.
Key API functions include:
css.create: For creating style objects.css.createTheme: For defining themes.css.defineVars: For defining CSS variables.css.firstThatWorks: For fallback styling logic.
Work with CSS in React Strict DOM
mainReact Strict DOM provides a specialized CSS API for creating styles, themes, and managing variables. Key capabilities include:
- Creating styles with
css.create. - Managing themes with
css.createTheme. - Defining constants with
css.defineConsts. - Defining variables with
css.defineVars. - Using fallback values with
css.firstThatWorks.
- Creating styles with
Understand React Strict DOM design goals
mainReact Strict DOM (RSD) provides cross-platform user interface APIs based on a subset of existing web standards for CSS, DOM, and HTML. It is designed to allow developers to reuse React web code and knowledge (including React DOM and StyleX) to target native platforms like iOS and Android.
Key Goals:
- Platform-native: Renders native UI elements by translating W3C APIs into native equivalents, preserving host platform characteristics.
- Performant: On web, it has no bundle size or runtime overhead compared to standard React DOM + StyleX. On native, runtime polyfills are optimized to avoid bottlenecks.
- Accessible: Leverages web-inherited accessibility and modality APIs, supporting mouse, keyboard, pointer, and screenreader interactions across platforms.
- Unified Developer Experience: Uses unified elements, styles, and types (e.g.,
HTMLElement) to simplify code-sharing and debugging.
Understand the testing strategy for React Strict DOM
mainReact Strict DOM uses a multi-platform testing approach to ensure consistency across web and native environments. The project prioritizes testing the Public API to allow for internal refactors without breaking consumer-facing behavior. Tests are categorized into Public API tests, Internal API tests, Visual regression tests, and Performance benchmarks.Understand the Minimum Common Web Platform API
mainReact Strict DOM utilizes a subset of standardized Web Platform APIs known as the Minimum Common Web Platform API. This subset defines a minimum set of capabilities common to both browser and non-browser JavaScript-based runtime environments, specifically targeting support in React Native and Hermes.
Note that
globalThisAPIs are treated as independent of multi-window concerns for user interfaces. For window-specific details, refer to thewindowAPI documentation.Use HTML components in React Strict DOM
mainReact Strict DOM provides a set of
htmlcomponents that map to standard web elements while maintaining strictness across platforms.Available components include:
html.a(Anchor)html.button(Button)html.img(Image)html.input(Input)html.label(Label)html.li(List Item)html.optgroup(Option Group)html.option(Option)html.select(Select)html.textarea(Textarea)
Common components and props are available via
html.*.Access Web APIs and Compatibility utilities
mainReact Strict DOM provides interfaces for interacting with the environment and managing compatibility:
- Common Web API: Access to standard web APIs.
- compat: Utilities for handling platform compatibility.
- document: Access to the document object.
- window: Access to the window object.
Configure Babel for React Strict DOM in Next.js
mainTo optimize builds and enable static CSS extraction for the web, create a
babelLoader.config.jsfile. This configuration uses thereact-strict-dom/babel-preset. Note that you should usebabelLoader.config.jsinstead ofbabel.config.jswhen using Next.js.const dev = process.env.NODE_ENV !== 'production' const config = { parserOpts: { plugins: ['typescript', 'jsx'], }, presets: [ [ 'react-strict-dom/babel-preset', { debug: dev, dev, platform: 'web', rootDir: process.cwd(), }, ], ], }; export default config;Configure Platform-Specific Extensions in Next.js
mainReact Strict DOM uses platform-specific extensions (e.g.,
*.web.js). To ensure Next.js correctly resolves these for web builds and avoids loading native implementations, add the.webextensions to theresolveExtensions(Turbopack) andresolve.extensions(Webpack) configurations innext.config.js.const webOnlyExtensions = ['.web.js', '.web.jsx', '.web.ts', '.web.tsx']; const nextConfig: NextConfig = { // ... turbopack: { // ... resolveExtensions: [ ...webOnlyExtensions, ".tsx", ".ts", ".jsx", ".js", ".mjs", ".json"], }, webpack: (config, { webpack }) => { // ... config.resolve.extensions = [ ...webOnlyExtensions, ...config.resolve.extensions]; return config; }, };Checkout a patch locally
mainIf a pull request (patch) needs to be modified locally before merging, you can check it out into a new branch using the PR number. Replace
<PR_NUMBER>with the actual number of the Pull Request.git fetch origin pull/<PR_NUMBER>/head:pr-<PR_NUMBER> git checkout pr-<PR_NUMBER>Perform integration testing with platform-tests
mainTo visually test patches, use the
platform-testsapp (an Expo app in theappsdirectory).- Build and watch
react-strict-domfor changes:npm run dev -w react-strict-dom - In a separate process, start the platform tests:
npm run dev -w platform-tests - Follow Expo instructions in the terminal to load the app in a browser, simulator, or on a physical device via Expo Go.
npm run dev -w react-strict-dom npm run dev -w platform-tests- Build and watch