React Strict DOM (RSD)

repository·main·Indexed 25 days ago

https://github.com/react/react-strict-dom

A 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.

Tokens
27.8K
Snippets
89
Records
195
Agent score
86%

What's inside react-strict-dom

  1. Overview of React Strict DOM (RSD)

    main
    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.
  2. Overview of React Strict DOM CSS API

    main

    React 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.
  3. Work with CSS in React Strict DOM

    main

    React 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.
  4. Understand React Strict DOM design goals

    main

    React 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.
  5. Understand the testing strategy for React Strict DOM

    main
    React 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.
  6. Understand the Minimum Common Web Platform API

    main

    React 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 globalThis APIs are treated as independent of multi-window concerns for user interfaces. For window-specific details, refer to the window API documentation.

  7. Use HTML components in React Strict DOM

    main

    React Strict DOM provides a set of html components 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.*.

  8. Configure Babel for React Strict DOM in Next.js

    main

    To optimize builds and enable static CSS extraction for the web, create a babelLoader.config.js file. This configuration uses the react-strict-dom/babel-preset. Note that you should use babelLoader.config.js instead of babel.config.js when 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;
  9. Configure Platform-Specific Extensions in Next.js

    main

    React 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 .web extensions to the resolveExtensions (Turbopack) and resolve.extensions (Webpack) configurations in next.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;
      },
    };
  10. Perform integration testing with platform-tests

    main

    To visually test patches, use the platform-tests app (an Expo app in the apps directory).

    1. Build and watch react-strict-dom for changes:
      npm run dev -w react-strict-dom
    2. In a separate process, start the platform tests:
      npm run dev -w platform-tests
    3. 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