logoNamu Design

⌘ K
  • Design
  • Development
  • Components
  • Blog
  • Resources
1.0.0
  • Namu Design of React
  • Changelogv1.0.0
  • Basic Usage
    • Getting Started
    • Usage with create-react-app
    • Usage with Next.js
    • Usage with Umi
    • Usage with Vite
  • Advanced
    • Customize ThemeUpdated
    • CSS Compatible
    • Server Side RenderingNew
    • Use custom date library
    • Internationalization
  • Migration
    • V4 to V5
    • Less variables to Component Token
  • Other
    • Common Props
    • Third-Party Libraries
    • Contributing
    • FAQ
StyleProvider
Compatible adjustment
CSS Logical Properties
Rem Adaptation
Options
Shadow DOM Usage

CSS Compatible

  • Customize ThemeServer Side Rendering

    Resources

    Namu Design Charts
    Namu Design Pro
    Namu Design Pro Components
    Namu Design Mobile
    Namu Design Mini
    Namu Design Landing-Landing Templates
    Scaffolds-Scaffold Market
    Umi-React Application Framework
    dumi-Component doc generator
    qiankun-Micro-Frontends Framework
    ahooks-React Hooks Library
    Ant Motion-Motion Solution
    China Mirror 🇨🇳

    Community

    Awesome Namu Design
    Medium
    Twitter
    yuqueNamu Design in YuQue
    Namu Design in Zhihu
    Experience Cloud Blog
    seeconfSEE Conf-Experience Tech Conference

    Help

    GitHub
    Change Log
    FAQ
    Bug Report
    Issues
    Discussions
    StackOverflow
    SegmentFault

    Ant XTechMore Products

    yuqueYuQue-Document Collaboration Platform
    AntVAntV-Data Visualization
    EggEgg-Enterprise Node.js Framework
    kitchenKitchen-Sketch Toolkit
    xtechAnt Financial Experience Tech
    Theme Editor
    Made with ❤ by
    Ant Group and Namu Design Community

    Namu Design supports the last 2 versions of modern browsers. If you need to be compatible with legacy browsers, please perform downgrade processing according to actual needs:

    StyleProvider

    Please ref @ant-design/cssinjs.

    Compatible adjustment

    Namu Design default using CSS-in-JS with :where Selector to reduce priority to avoid user additional adjust style cost when updating. If you want to support old browser (or some other CSS framework selector priority conflict like TailwindCSS), you can use @ant-design/cssinjs to adjust this behavior (Please note keep version align with antd):

    import { StyleProvider } from '@ant-design/cssinjs';
    // Config `hashPriority` to `high` instead of default `low`
    // Which will remove `:where` wrapper
    export default () => (
    <StyleProvider hashPriority="high">
    <MyApp />
    </StyleProvider>
    );

    It will turn :where to class selector:

    -- :where(.css-bAMboO).ant-btn {
    ++ .css-bAMboO.ant-btn {
    color: #fff;
    }

    Note: After turning off the :where downgrade, you may need to manually adjust the priority of some styles. Or you can use PostCSS plugin to raise application css selector priority. PostCSS provides many plugins can help on this. e.g:

    • postcss-scopify
    • postcss-increase-specificity
    • postcss-add-root-selector

    Raise priority through plugin:

    -- .my-btn {
    ++ #root .my-btn {
    background: red;
    }

    CSS Logical Properties

    To unify LTR and RTL styles, Namu Design uses CSS logical properties. For example, the original margin-left is replaced by margin-inline-start, so that it is the starting position spacing under both LTR and RTL. If you need to be compatible with older browsers, you can configure transformers through the StyleProvider of @ant-design/cssinjs:

    import { StyleProvider, legacyLogicalPropertiesTransformer } from '@ant-design/cssinjs';
    // `transformers` provides a way to transform CSS properties
    export default () => (
    <StyleProvider transformers={[legacyLogicalPropertiesTransformer]}>
    <MyApp />
    </StyleProvider>
    );

    When toggled, styles will downgrade CSS logical properties:

    .ant-modal-root {
    -- inset: 0;
    ++ top: 0;
    ++ right: 0;
    ++ bottom: 0;
    ++ left: 0;
    }

    Rem Adaptation

    In responsive web development, there is a need for a convenient and flexible way to achieve page adaptation and responsive design. The px2remTransformer transformer can quickly and accurately convert pixel units in style sheets to rem units relative to the root element (HTML tag), enabling the implementation of adaptive and responsive layouts.

    import { StyleProvider, px2remTransformer } from '@ant-design/cssinjs';
    const px2rem = px2remTransformer({
    rootValue: 32, // 32px = 1rem; @default 16
    });
    export default () => (
    <StyleProvider transformers={[px2rem]}>
    <MyApp />
    </StyleProvider>
    );

    The resulting transformed styles:

    .px2rem-box {
    - width: 400px;
    + width: 12.5rem;
    background-color: green;
    - font-size: 32px;
    + font-size: 1rem;
    border: 10PX solid #f0f;
    }
    @media only screen and (max-width: 600px) {
    .px2rem-box {
    background-color: red;
    - margin: 10px;
    + margin: 0.3125rem;
    }
    }

    Options

    ParameterDescriptionTypeDefault
    rootValueFont size of the root elementnumber16
    precisionDecimal places for the converted valuenumber5
    mediaQueryWhether to convert px in media queriesbooleanfalse

    For more details, please refer to: px2rem.ts#Options

    Shadow DOM Usage

    Since <style /> tag insertion is different from normal DOM in Shadow DOM scenario, you need to use StyleProvider of @ant-design/cssinjs to configure the container property to set the insertion position:

    import { StyleProvider } from '@ant-design/cssinjs';
    import { createRoot } from 'react-dom/client';
    const shadowRoot = someEle.attachShadow({ mode: 'open' });
    const container = document.createElement('div');
    shadowRoot.appendChild(container);
    const root = createRoot(container);
    root.render(
    <StyleProvider container={shadowRoot}>
    <MyApp />
    </StyleProvider>,
    );