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#
1import { Button } from 'aelf-design';23const App = () => {4return (5<div>6<Button>default</Button>7</div>8);9};1011export default App;
Development#
1$ git clone https://github.com/AElfProject/aelf-design.git2$ pnpm i (if there is not pnpm,please npm install -g pnpm first)3$ pnpm dev
Publish#
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
1import { Button, ConfigProvider, Space } from 'antd';2import React from 'react';34const App: React.FC = () => (5<ConfigProvider6theme={{7token: {8colorPrimary: '#00b96b',9borderRadius: 2,10},11}}12>13<Space>14<Button type="primary">Primary</Button>15<Button>Default</Button>16</Space>17</ConfigProvider>18);1920export 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
1import { Button, ConfigProvider, Space } from 'antd';2import React from 'react';34const App: React.FC = () => (5<ConfigProvider6theme={{7components: {8Button: {9colorPrimary: '#00b96b',10borderColorDisabled: '#d9d9d9'11},12Input: {13colorPrimary: '#eb2f96',14}15},16}}17>18<Space>19<Button type="primary">Primary</Button>20<Button>Default</Button>21</Space>22</ConfigProvider>23);2425export 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#
1import { SmileOutlined } from '@ant-design/icons';2import { Button, Space } from 'antd';3import { createStyles } from 'antd-style';45const useStyles = createStyles(({ token, css, cx }) => {6const commonCard = css`7border-radius: ${token.borderRadiusLG}px;8padding: ${token.paddingLG}px;9`;1011return {12container: css`13background-color: ${token.colorBgLayout};14padding: 24px;15`,1617defaultCard: css`18${commonCard};19background: ${token.colorBgContainer};20color: ${token.colorText};21`,2223primaryCard: cx(24commonCard,25css`26background: ${token.colorPrimary};27color: ${token.colorTextLightSolid};28`,29),30};31});3233const App = () => {34const { styles } = useStyles();3536return (37<div className={styles.container}>38<Space direction={'vertical'} style={{ width: '100%' }} size={16}>39<Space>40<Button icon={<SmileOutlined />} />41oprerate button42</Space>43<div className={styles.defaultCard}>defalut card</div>44<div className={styles.primaryCard}>primary card</div>45</Space>46</div>47);48};4950export default App;
The createStyles method can pass in a function with the following signature:
1type 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 name | Type | Description |
---|---|---|
css | CssUtil | CSS serialization function |
cx | ClassNamesUtil | CSS class name tool function |
responsive | ResponsiveUtil | Responsive media query tool function |
token | FullToken | Contains antd's token and all custom tokens |
appearance | ThemeAppearance | Current theme mode under ThemeProvider. 'dark' | 'light' | string |
isDarkMode | boolean | Syntax sugar can be directly used with isDarkMode to reduce the cost of appearance judgment.Equivalent to appearance === 'dark' |
prefixCls | string | The 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
1import { ThemeProvider } from 'antd-style';23export default () => {4return (5<ThemeProvider customToken={{ customBrandColor: '#c956df' }}>6<App />7</ThemeProvider>8);9};1011// consume customToken12css`13background-color: ${token.customBrandColor};14padding: 24px;15`
Develop aelfd component#
Component tokens required to configure this component, global tokens (pay attention to whether it will affect other components), and custom tokens.
1<ThemeProvider2customToken={{ customBrandColor: '#c956df' }}3theme={{4components: {5Button: {6colorPrimary: '#00b96b',7borderColorDisabled: '#d9d9d9'8},9Input: {10paddingBlock: 11,11paddingBlockSM: 7,12}13...14},15token: {16colorPrimary: '#1370DD',17colorPrimaryHover: '#3689DD',18}19}}20/>212223theme?: ThemeConfig | GetAntdTheme;
1export type AelfdButtonSizeType =2| 'mini'3| 'small'4| 'medium'5| 'large'6| 'ultra'7export interface IAelfdButtonProps8extends Omit<ButtonProps, 'size' | 'onClick'> {9size?: AelfdButtonSizeType10onClick?: React.MouseEventHandler<HTMLElement>11millisecondOfThrottle?: number12}
1const useStyles = createStyles(2({ css, prefixCls }, { size }: { size: AelfdButtonSizeType }) => {3const dynamicWidth =4size === 'mini'5? '24px'6: size === 'small'7? '32px'8: size === 'medium'9? '40px'10: size === 'large'11? '48px'12: '56px'1314return {15buttonWrap: css`16display: flex;17align-items: center;18justify-content: center;19flex-shrink: 0;20// ...2122&.${prefixCls}-btn-circle {23min-height: ${dynamicWidth};24height: ${dynamicWidth};25font-size: 14px;26}27&.${prefixCls}-btn-icon-only {28min-width: auto;29width: ${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