logo

A

A UI component library based on Antd and compliant with aelf visual specifications#

website: https://aelf-design.vercel.app/#

Install#

1
$ yarn add aelf-design

Usage#

1
import { Button } from 'aelf-design';
2
3
const App = () => {
4
return (
5
<div>
6
<Button>default</Button>
7
</div>
8
);
9
};
10
11
export default App;

Development#

1
$ git clone https://github.com/AElfProject/aelf-design.git
2
$ pnpm i (if there is not pnpm,please npm install -g pnpm first)
3
$ pnpm dev

Publish#

  • Upgrade the version numbers of each sub package
  • execute release command in the project root directory
  • 1
    $ pnpm release

    How to contribute components to aelf-design#

    Prerequisite knowledge#

    Design token#

    In antd 5.0, the smallest element that affects the theme is called the Design Token. By modifying the Design Token, various themes or components can be presented. By passing the theme property in the ConfigProvider , the theme can be configured

    1
    import { Button, ConfigProvider, Space } from 'antd';
    2
    import React from 'react';
    3
    4
    const App: React.FC = () => (
    5
    <ConfigProvider
    6
    theme={{
    7
    token: {
    8
    colorPrimary: '#00b96b',
    9
    borderRadius: 2,
    10
    },
    11
    }}
    12
    >
    13
    <Space>
    14
    <Button type="primary">Primary</Button>
    15
    <Button>Default</Button>
    16
    </Space>
    17
    </ConfigProvider>
    18
    );
    19
    20
    export default App;

    In addition to the overall Design Token, each component will also open its own Component Token to achieve the ability to customize the style of the component, and different components will not affect each other

    1
    import { Button, ConfigProvider, Space } from 'antd';
    2
    import React from 'react';
    3
    4
    const App: React.FC = () => (
    5
    <ConfigProvider
    6
    theme={{
    7
    components: {
    8
    Button: {
    9
    colorPrimary: '#00b96b',
    10
    borderColorDisabled: '#d9d9d9'
    11
    },
    12
    Input: {
    13
    colorPrimary: '#eb2f96',
    14
    }
    15
    },
    16
    }}
    17
    >
    18
    <Space>
    19
    <Button type="primary">Primary</Button>
    20
    <Button>Default</Button>
    21
    </Space>
    22
    </ConfigProvider>
    23
    );
    24
    25
    export default App;

    Details: https://ant-design.antgroup.com/docs/react/customize-theme#design-token

    antd-style#

    Antd-style is a business-level css-in-js solution built on Ant Design V5 Token System

    createStyles#

    1
    import { SmileOutlined } from '@ant-design/icons';
    2
    import { Button, Space } from 'antd';
    3
    import { createStyles } from 'antd-style';
    4
    5
    const useStyles = createStyles(({ token, css, cx }) => {
    6
    const commonCard = css`
    7
    border-radius: ${token.borderRadiusLG}px;
    8
    padding: ${token.paddingLG}px;
    9
    `;
    10
    11
    return {
    12
    container: css`
    13
    background-color: ${token.colorBgLayout};
    14
    padding: 24px;
    15
    `,
    16
    17
    defaultCard: css`
    18
    ${commonCard};
    19
    background: ${token.colorBgContainer};
    20
    color: ${token.colorText};
    21
    `,
    22
    23
    primaryCard: cx(
    24
    commonCard,
    25
    css`
    26
    background: ${token.colorPrimary};
    27
    color: ${token.colorTextLightSolid};
    28
    `,
    29
    ),
    30
    };
    31
    });
    32
    33
    const App = () => {
    34
    const { styles } = useStyles();
    35
    36
    return (
    37
    <div className={styles.container}>
    38
    <Space direction={'vertical'} style={{ width: '100%' }} size={16}>
    39
    <Space>
    40
    <Button icon={<SmileOutlined />} />
    41
    oprerate button
    42
    </Space>
    43
    <div className={styles.defaultCard}>defalut card</div>
    44
    <div className={styles.primaryCard}>primary card</div>
    45
    </Space>
    46
    </div>
    47
    );
    48
    };
    49
    50
    export default App;

    The createStyles method can pass in a function with the following signature:

    1
    type GetCssStyleFn = (utils: CreateStylesUtils, props?: Props) => StyleInput;

    Below is a detailed introduction to the functions of each attribute

    CreateStylesUtils

    The first parameter used when writing styles, utils, provides a series of auxiliary objects and methods that facilitate style writing, improving the efficiency of style writing. Its type is CreateStylesUtils, and the property table is as follows:

    Attribute nameTypeDescription
    cssCssUtilCSS serialization function
    cxClassNamesUtilCSS class name tool function
    responsiveResponsiveUtilResponsive media query tool function
    tokenFullTokenContains antd's token and all custom tokens
    appearanceThemeAppearanceCurrent theme mode under ThemeProvider. 'dark' | 'light' | string
    isDarkModebooleanSyntax sugar can be directly used with isDarkMode to reduce the cost of appearance judgment.Equivalent to appearance === 'dark'
    prefixClsstringThe prefix marked on the ThemeProvider can obtain the current component prefix, making it easier to respond to component prefixes more flexibly

    ThemeProvider#

    ThemeProvider has done secondary encapsulation on the basis of ConfigProvider, providing a more convenient way to customize themes

    Custom Tokens can be injected through the customToken method of ThemeProvider

    1
    import { ThemeProvider } from 'antd-style';
    2
    3
    export default () => {
    4
    return (
    5
    <ThemeProvider customToken={{ customBrandColor: '#c956df' }}>
    6
    <App />
    7
    </ThemeProvider>
    8
    );
    9
    };
    10
    11
    // consume customToken
    12
    css`
    13
    background-color: ${token.customBrandColor};
    14
    padding: 24px;
    15
    `

    Develop aelfd component#

  • According to the component design draft, identify the differences with the antd component
  • List the new features based on the antd component
  • Configure token
  • Component tokens required to configure this component, global tokens (pay attention to whether it will affect other components), and custom tokens.

    1
    <ThemeProvider
    2
    customToken={{ customBrandColor: '#c956df' }}
    3
    theme={{
    4
    components: {
    5
    Button: {
    6
    colorPrimary: '#00b96b',
    7
    borderColorDisabled: '#d9d9d9'
    8
    },
    9
    Input: {
    10
    paddingBlock: 11,
    11
    paddingBlockSM: 7,
    12
    }
    13
    ...
    14
    },
    15
    token: {
    16
    colorPrimary: '#1370DD',
    17
    colorPrimaryHover: '#3689DD',
    18
    }
    19
    }}
    20
    />
    21
    22
    23
    theme?: ThemeConfig | GetAntdTheme;
  • Define aelfd component type file
  • 1
    export type AelfdButtonSizeType =
    2
    | 'mini'
    3
    | 'small'
    4
    | 'medium'
    5
    | 'large'
    6
    | 'ultra'
    7
    export interface IAelfdButtonProps
    8
    extends Omit<ButtonProps, 'size' | 'onClick'> {
    9
    size?: AelfdButtonSizeType
    10
    onClick?: React.MouseEventHandler<HTMLElement>
    11
    millisecondOfThrottle?: number
    12
    }
  • Writing component logic and style files
  • 1
    const useStyles = createStyles(
    2
    ({ css, prefixCls }, { size }: { size: AelfdButtonSizeType }) => {
    3
    const dynamicWidth =
    4
    size === 'mini'
    5
    ? '24px'
    6
    : size === 'small'
    7
    ? '32px'
    8
    : size === 'medium'
    9
    ? '40px'
    10
    : size === 'large'
    11
    ? '48px'
    12
    : '56px'
    13
    14
    return {
    15
    buttonWrap: css`
    16
    display: flex;
    17
    align-items: center;
    18
    justify-content: center;
    19
    flex-shrink: 0;
    20
    // ...
    21
    22
    &.${prefixCls}-btn-circle {
    23
    min-height: ${dynamicWidth};
    24
    height: ${dynamicWidth};
    25
    font-size: 14px;
    26
    }
    27
    &.${prefixCls}-btn-icon-only {
    28
    min-width: auto;
    29
    width: ${dynamicWidth};
    30
    }
    31
    `
    32
    }
    33
    }
    34
    )

    Previously, if you needed to override the style of the antd component, you needed to use: global to override it. Now, you can simply remove: global

    Vs code plugin#

    vscode-styled-components

    Edited on: 14 July 2024 04:59:10 GMT+0