antd-style

repository·master·Indexed 43 days ago

https://github.com/ant-design/antd-style

A business-level CSS-in-JS solution for applications combining Emotion and the Ant Design V5 token system. It provides seamless theme switching, responsive design utilities, and high-performance styling options via createStyles and createStaticStyles. Version 4.0.1-alpha.11 supports custom token extension, prefixCls configuration, and styled-components compatibility.

Tokens
54.4K
Snippets
165
Records
228
Agent score
49%

What's inside antd-style

  1. Overview of Ant Design Style

    master

    Ant Design Style is an application-level CSS-in-JS solution built upon the Ant Design V5 Token System. It provides a robust styling infrastructure for modern React applications, enabling seamless integration with Ant Design's design tokens.

    Key capabilities include:

    • Engine Compatibility: Works with multiple styling engines like styled-components and emotion-styled.
    • Theme Management: Built-in support for easy light/dark mode switching using Ant Design V5's dynamic theme configuration.
    • Customizable Themes: Allows for extending Ant Design's default tokens with custom tokens and custom stylish definitions.
    • Stylish (Composite Styles): A utility for composing complex interactive styles by combining multiple atomic tokens, similar in philosophy to Tailwind CSS but with explicit design semantics.
    • Responsive Design: Provides utility functions to simplify responsive theme development.
    • Migration Support: Facilitates low-cost migration from legacy less setups to CSS-in-JS.
    • Micro-frontend Compatibility: Compatible with qiankun micro-application architectures.
  2. Introduction to antd-style

    master

    antd-style is a business-level CSS-in-JS solution designed specifically for applications and component libraries built on top of Ant Design V5. It is built upon the Ant Design V5 Token System and provides a secondary encapsulation of emotion to simplify styling workflows.

    Key Use Cases

    • Business Applications: Integrating Ant Design's theme tokens into your application's custom styles.
    • Component Libraries: Building secondary component libraries that are based on Ant Design and need to consume its design tokens.

    Comparison with @ant-design/cssinjs

    While @ant-design/cssinjs is optimized for the internal implementation of the Ant Design component library itself (prioritizing performance via a specialized syntax), antd-style is optimized for developer experience (DX) and ease of use for application developers. Unlike the internal library, antd-style provides built-in capabilities to easily consume the Ant Design token system.

  3. Core features of antd-style

    master

    antd-style is a business-grade css-in-js solution built on top of emotion and integrated with the Ant Design V5 Token System.

    Key capabilities include:

    • Token System Integration: Seamlessly consumes Ant Design V5 tokens for easy style customization.
    • Dark Mode Switching: Built-in support for easy light/dark theme switching using Ant Design's dynamic theme configuration.
    • Custom Theme Extension: Ability to define custom tokens and 'Stylish' patterns when default Ant Design tokens are insufficient.
    • Micro-app Compatibility: Default compatibility with qiankun micro-apps (with performance optimization options available for non-micro-app scenarios).
    • Responsive Design: Provides utility functions to simplify responsive theme development.
    • Stylish: A capability to compose complex interaction styles by combining multiple atomic tokens.
    • Less Migration: Facilitates low-cost migration from legacy less projects to css-in-js.
  4. How antd-style uses Emotion and @emotion/styled

    master

    The current implementation of antd-style uses emotion and @emotion/styled as its underlying engines, but they are utilized differently depending on the syntax:

    css syntax

    antd-style uses emotion for the css method. This was chosen because emotion supports converting the css API directly into class names, which is a requirement for the library's functionality. styled-components was ruled out because its css method lacks this capability.

    styled syntax

    antd-style uses @emotion/styled for the styled syntax. While styled-components offers native support for using components as selectors, @emotion/styled was chosen to keep the package size smaller (avoiding an additional ~10KB+).

    Note on Component Selectors: If you require component selector syntax (often used for complex animations or compound selectors), antd-style recommends using the createStyles syntax instead, which is more natural and user-friendly for those scenarios.

    :::info{title=Special Note} antd-style does not include the styled method by default. It only integrates @emotion/react's ThemeContext. To use the styled syntax, you must follow the integration guide: Integration with styled. :::

  5. Use :where selector for component development

    master

    When developing components, using standard CSS-in-JS hash selectors increases specificity, making it difficult for end-users to override styles.

    To solve this, antd-style supports the :where() selector. The :where() selector allows you to wrap hash-based selectors so that they have zero specificity weight. This provides local scoping to prevent global pollution while maintaining the ease of style overriding for the consumer.

    For example, instead of .css-abcd .my-btn (high specificity), using :where(.css-abcd) .my-btn ensures the weight remains low, allowing a simple .my-btn { color: green; } to work effectively.

  6. How getStaticInstance works in ThemeProvider

    master

    The getStaticInstance method in ThemeProvider is used to bridge the gap between React's context-based theme system and non-React code.

    Inside the ThemeProvider, you use hooks like message.useMessage() to obtain a themed instance and a contextHolder. You then pass that instance back to the getStaticInstance callback. This effectively 'exposes' the themed instance to the outside world.

    import { message } from 'antd';
    
    const Provider = ({ getStaticInstance, children, theme }) => {
      // 1. Use useMessage to obtain the themed instance
      const [messageInstance, messageContextHolder] = message.useMessage();
    
      useEffect(() => {
        // 2. Use getStaticInstance to expose the instance to non-React environments
        getStaticInstance?.({
          message: messageInstance,
        });
      }, [messageInstance, getStaticInstance]);
    
      return (
        <ConfigProvider theme={theme}>
          {/* 3. Insert the context holder to render the message component */}
          {messageContextHolder}
          {children}
        </ConfigProvider>
      );
    };
  7. How cx and css work together for class name generation

    master
    In antd-style, the css method produces a serialized style object. To obtain a usable CSS class name string from this object, you must wrap the css call with the cx utility. This conversion turns the serialized object into a standard class name (prefixed with acss-), which can then be used for selector nesting or manual application to elements.
  8. Compare createStyles and createStaticStyles

    master

    Choose between createStyles and createStaticStyles based on your styling requirements:

    FeaturecreateStylescreateStaticStyles
    Return ValueReact HookStyle Object
    JS Tokens✅ Supported❌ Not Supported
    CSS Variables (cssVar)✅ Supported✅ Supported (Static)
    Custom Tokens✅ Supported❌ Not Supported
    Stylish Presets✅ Supported❌ Not Supported
    Responsive Breakpoints✅ Dynamic✅ Fixed Breakpoints
    Props Access✅ Supported❌ Not Supported
    isDarkMode Support✅ Supported❌ Not Supported
    Hook Count8+0
    Performance OverheadHighNear Zero
  9. Compare createStyles vs createStaticStyles

    master

    Choose between createStyles and createStaticStyles based on your performance and dynamic requirements:

    FeaturecreateStylescreateStaticStyles
    Return ValueHookStyle Object
    token (JS values)✅❌
    cssVar (CSS variables)✅✅
    customToken✅❌
    stylish✅❌
    responsive✅ (dynamic)✅ (fixed breakpoints)
    props parameter✅❌
    isDarkMode✅❌
    Hooks count8+0
    Performance overheadHighNear Zero
  10. Understand the role of antd-style as a Style Practice

    master

    It is important to distinguish between a Style Engine and a Style Practice to understand how antd-style fits into your architecture:

    1. Style Engine: The underlying library that handles the actual CSS-in-JS logic (e.g., emotion, styled-components, goober). These are tools for expanding style capabilities.
    2. Style Practice: A solution that provides best practices, unified writing methods, and deterministic patterns for business applications (e.g., BEM, Tailwind CSS, or antd-style).

    antd-style is a Style Practice library. It encapsulates high-quality CSS-in-JS engines to provide a unified, concise, and user-friendly syntax for common scenarios like application styles, component styles, responsive design, and dynamic/custom themes. Because it is a practice layer, it is designed to be decoupled from specific engines, allowing for future transitions to other technologies (like Atomic CSS-in-JS) without changing your standard antd-style API usage.