logo
  • Guide
  • Config
  • Plugin
  • API
  • Examples
  • Community
  • Modern.js 2.x Docs
  • English
    • 简体中文
    • English
    • Start
      Introduction
      Quick Start
      Upgrading
      Glossary
      Tech Stack
      Core Concept
      Page Entry
      Build Engine
      Web Server
      Basic Features
      Routes
      Routing
      Config Routes
      Data Solution
      Data Fetching
      Data Writing
      Data Caching
      Rendering
      Server-Side Rendering
      Streaming SSR
      Rendering Cache
      Static Site Generation
      Render Preprocessing
      Styling
      Styling
      Use CSS Modules
      Using CSS-in-JS
      Using Tailwind CSS
      HTML Template
      Import Static Assets
      Import JSON Files
      Import SVG Assets
      Import Wasm Assets
      Debug
      Data Mocking
      Network Proxy
      Using Rsdoctor
      Using Storybook
      Testing
      Playwright
      Vitest
      Jest
      Cypress
      Path Alias
      Environment Variables
      Output Files
      Deploy Application
      Advanced Features
      Using Rspack
      Using BFF
      Basic Usage
      Runtime Framework
      Extend BFF Server
      Extend Request SDK
      File Upload
      Cross-Project Invocation
      Optimize Page Performance
      Code Splitting
      Inline Static Assets
      Bundle Size Optimization
      React Compiler
      Improve Build Performance
      Browser Compatibility
      Low-Level Tools
      Source Code Build Mode
      Server Monitor
      Monitors
      Logs Events
      Metrics Events
      Internationalization
      Basic Concepts
      Quick Start
      Configuration
      Locale Detection
      Resource Loading
      Routing Integration
      API Reference
      Advanced Usage
      Best Practices
      Custom Web Server
      Topic Detail
      Module Federation
      Introduction
      Getting Started
      Application-Level Modules
      Server-Side Rendering
      Deployment
      Integrating Internationalization
      FAQ
      Dependencies FAQ
      CLI FAQ
      Build FAQ
      HMR FAQ
      Deprecated
      📝 Edit this page
      Previous pageUse CSS ModulesNext pageUsing Tailwind CSS

      #Using CSS-in-JS

      CSS-in-JS is a technique that allows you to write CSS styles within JS files.

      Modern.js supports the commonly used community CSS-in-JS library styled-components, which uses JavaScript's new feature Tagged template to write component CSS styles.

      The Modern.js plugin @modern-js/plugin-styled-components provides support for styled-components and adds server-side rendering capability for styled-components. You can use styled-components by installing the @modern-js/plugin-styled-components plugin.

      #Using styled-components in Modern.js

      First, you need to install the styled-components plugin dependency:

      npm
      yarn
      pnpm
      npm install @modern-js/plugin-styled-components -D
      yarn add @modern-js/plugin-styled-components -D
      pnpm install @modern-js/plugin-styled-components -D

      Then configure the styled-components plugin in modern.config.ts:

      import { defineConfig } from '@modern-js/app-tools';
      import { styledComponentsPlugin } from '@modern-js/plugin-styled-components';
      
      export default defineConfig({
        plugins: [
          styledComponentsPlugin({
            // ...
            displayName: true,
            minify: process.env.NODE_ENV === 'production',
          }),
        ],
      });

      The configuration options of the styledComponentsPlugin plugin are the same as those of the @rsbuild/plugin-styled-components plugin. You can refer to the documentation of @rsbuild/plugin-styled-components for configuration.

      #Writing styles with styled-components

      When you need to write a div component with red text inside, you can implement it as follows:

      import styled from '@modern-js/plugin-styled-components/styled';
      
      const RedDiv = styled.div`
        color: red;
      `;

      When you need to dynamically set component styles based on the component's props, for example, when the primary attribute of props is true, the button's color is white, otherwise red, the implementation is as follows:

      import styled from '@modern-js/plugin-styled-components/styled';
      
      const Button = styled.button`
        color: ${props => (props.primary ? 'white' : 'red')};
        font-size: 1em;
      `;

      For more usage of styled-components, please refer to the styled-components official website.

      Tip

      If you want to use other CSS-in-JS libraries such as styled-jsx, Emotion, etc., you need to install the corresponding dependencies first. Please refer to the official websites of the respective libraries for specific usage.