@actinc/dls 中文文档教程
Design Language System (DLS)
ACT 前端项目的设计语言系统。 查看 UI 组件 此处。
Project Setup
Installation
为了使用 DLS,您必须将其与 Material UI 版本 4.x
和 React 版本 16.x
或 17.x
。
npm install --save @actinc/dls@latest @material-ui/core@4 react@17 react-dom@17
Choosing a Theme
这个 DLS 建立在 主题引擎 来自 Material UI,并附带两个开箱即用的主题:
"ACT"
: for ACT's "traditional" look and feel"ACT_ET"
: for ACT's "Emerging Technology" look and feel"ENCOURA_DATALAB"
: for Encoura's "Datalab" look and feel
要将这些主题之一应用到您的组件,只需包装您的应用程序 在 ThemeProvider
组件中指定一个主题!
import { ThemeProvider } from '@actinc/dls/components';
...
const MyApp = () => (
// specify a theme here!
<ThemeProvider theme="ACT_ET">
<App />
</ThemeProvider>
);
Extending Themes
您可以使用扩展核心 DLS 主题 createTheme
来自 Material UI 的生成器:
import { createTheme } from '@material-ui/core/styles';
import { merge } from 'lodash';
import { THEME_ACT } from '@actinc/dls/styles/themeAct';
import { ThemeProvider } from '@actinc/dls/components';
const myExtendedTheme = createTheme(merge(THEME_ACT_ET, {
// theme customizations go here!
}));
const MyApp = () => (
<ThemeProvider theme={myExtendedTheme}>
<App />
</ThemeProvider>
);
Custom Themes
或者,您可以使用 createTheme
来自 Material UI
import { createTheme } from '@material-ui/core/styles';
import { ThemeProvider } from '@actinc/dls/components';
const myCustomTheme = createTheme({
// build your theme here!
});
const MyApp = () => (
<ThemeProvider theme={myCustomTheme}>
<App />
</ThemeProvider>
);
Load Fonts
Montserrat
的生成器:ACT
和 ACT_ET
主题假定 Montserrat 字体可用于 浏览器。 因此,建议您包括以下字体 在 React 应用程序
<link
href="https://fonts.googleapis.com/css?family=Montserrat:300,400,500,600,700&display=swap"
rel="stylesheet"
/>
Museo
的 head
中引用:ENCOURA_DATALAB
主题假定 Museo 字体 在浏览器中可用。 因此,建议您包括 在 React 应用程序的 head
中引用以下字体:
<style type="text/css">
@font-face {
font-family: 'Museo';
src: url('path/to/Museo300.otf');
font-style: normal;
font-weight: 300;
}
@font-face {
font-family: 'Museo';
src: url('path/to/Museo500.otf');
font-style: normal;
font-weight: 500;
}
@font-face {
font-family: 'Museo';
src: url('path/to/Museo700.otf');
font-style: normal;
font-weight: 700;
}
</style>
CSS Baseline
建议从 Material UI 附近注入 CssBaseline
组件 组件树的根,以便重置和规范化浏览器样式 对于你的项目:
import { CssBaseline } from '@material-ui/core';
...
const MyApp = () => (
...
<CssBaseline />
...
);
Server-Side Rendering
如果你的项目的 React 框架支持 SSR,你可以配置 DLS 使用 ServerStyleSheets
导出的服务器端渲染组件 来自 Material UI。
例如,在 Next.js 项目中,您可以添加 以下是您的 pages/_document.tsx
文件:
import { ServerStyleSheets } from '@material-ui/core/styles';
...
class Document extends DocumentImport<Props> {
static async getInitialProps(ctx: Context): Promise<Props> {
...
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = (): void =>
originalRenderPage({
enhanceApp: (App: any): any => (props: any): any =>
sheets.collect(<App {...props} />),
});
...
return {
...
styles: sheets.getStyleElement(),
...
}
}
...
}
Icons
DLS 重新导出由 mdi-material-ui
包。
您可以搜索要使用的特定图标 materialdesignicons.com。 一旦你找到 完美的图标,你可以像这样在你的项目中使用它:
import { PollBox } from '@actinc/dls/icons';
...
const MyComponent = () => (
...
<PollBox />
...
);
Minimizing Bundle Size
如果你使用命名导入从 ACT DLS 导入模块,更多代码可能是 加载到内存中比你需要的。 为了在保持使用命名导入的同时 您的包大小尽可能小,我们建议配置 babel-plugin-transform-imports Babel 的插件。
npm install --save-dev babel-plugin-transform-imports
然后将以下内容添加到您的 Babel 配置文件(例如 .babelrc.js
):
module.exports = {
plugins: [
...
[
'babel-plugin-transform-imports',
{
'@material-ui/core': {
preventFullImport: true,
transform: '@material-ui/core/${member}',
},
'@material-ui/core/colors': {
preventFullImport: true,
transform: '@material-ui/core/colors/${member}',
},
'@actinc/dls/components': {
transform: '@actinc/dls/components/${member}',
preventFullImport: true,
},
'@actinc/dls/constants': {
transform: '@actinc/dls/constants/${member}',
preventFullImport: true,
},
'@actinc/dls/context': {
transform: '@actinc/dls/context/${member}',
preventFullImport: true,
},
'@actinc/dls/helpers': {
transform: '@actinc/dls/helpers/${member}',
preventFullImport: true,
},
'@actinc/dls/helpers': {
transform: '@actinc/dls/hooks/${member}',
preventFullImport: true,
},
'@actinc/dls/icons': {
transform: '@actinc/dls/icons/${member}',
preventFullImport: true,
},
'@actinc/dls/styles': {
transform: '@actinc/dls/styles/${member}',
preventFullImport: true,
},
'@actinc/dls/types': {
transform: '@actinc/dls/types/${member}',
preventFullImport: true,
},
},
],
...
],
presets: [...],
}
Import Stuff
就是这样! 您已准备好使用 DLS。 只需导入颜色、组件、 您需要的常量、助手、图标、样式和类型:
// components
import { Alert } from '@actinc/dls/components';
// constants
import { sortDirectionTypes as SORT_DIRECTION_TYPES } from '@actinc/dls/constants';
// context
import { AlertContext } from '@actinc/dls/context';
// helpers
import { search } from '@actinc/dls/helpers';
// hooks
import { useLocalStorage } from '@actinc/dls/hooks';
// icons
import { ChevronDown } from '@actinc/dls/icons';
// styles & themes
import { THEME_ACT } from '@actinc/dls/styles/themeAct';
// types
import { SortObject } from '@actinc/dls/types';
Local Development
查看开发人员指南以了解如何构建 对 DLS 有效。
在本地运行 DLS:
- Install node modules:
npm install
- Start the Storybook component visualizer:
npm start
npm Scripts
在本地开发过程中有很多 npm 脚本供您使用。 以下是一些更重要的:
Script | Description |
---|---|
npm run build | Transpile DLS from TypeScript (./src ) into ES5 (./dist ). |
npm run build-storybook | Create a static website for deployment (./storybook-static ). |
npm start | Start the Storybook component visualizer. |
npm test | Run all tests. |
npm run release | Publish a new release of the DLS. |
Pinned Packages
一些 npm 包由于特定原因被固定到非当前版本:
Package | Version | Reason |
---|---|---|
color | 3.2.1 | Version 4.x cannot be run in Storybook due to this issue. |
np | 7.4.0 | Version 7.5.0 is broken. |
Design Language System (DLS)
The Design Language System for ACT front-end projects. View the UI components here.
Project Setup
Installation
In order to use the DLS, you must install it along with Material UI version 4.x
and React version 16.x
or 17.x
.
npm install --save @actinc/dls@latest @material-ui/core@4 react@17 react-dom@17
Choosing a Theme
This DLS is built on top of the theming engine from Material UI, and ships with two themes out of the box:
"ACT"
: for ACT's "traditional" look and feel"ACT_ET"
: for ACT's "Emerging Technology" look and feel"ENCOURA_DATALAB"
: for Encoura's "Datalab" look and feel
To apply one of these themes to your components, simply wrap your application in the ThemeProvider
component and specify a theme!
import { ThemeProvider } from '@actinc/dls/components';
...
const MyApp = () => (
// specify a theme here!
<ThemeProvider theme="ACT_ET">
<App />
</ThemeProvider>
);
Extending Themes
You can exend the core DLS themes using the createTheme
generator from Material UI:
import { createTheme } from '@material-ui/core/styles';
import { merge } from 'lodash';
import { THEME_ACT } from '@actinc/dls/styles/themeAct';
import { ThemeProvider } from '@actinc/dls/components';
const myExtendedTheme = createTheme(merge(THEME_ACT_ET, {
// theme customizations go here!
}));
const MyApp = () => (
<ThemeProvider theme={myExtendedTheme}>
<App />
</ThemeProvider>
);
Custom Themes
Alternatively, you can build your own theme from scratch using the createTheme
generator from Material UI:
import { createTheme } from '@material-ui/core/styles';
import { ThemeProvider } from '@actinc/dls/components';
const myCustomTheme = createTheme({
// build your theme here!
});
const MyApp = () => (
<ThemeProvider theme={myCustomTheme}>
<App />
</ThemeProvider>
);
Load Fonts
Montserrat
The ACT
and ACT_ET
themes assume that the Montserrat font is available in the browser. Therefore, it is recommended that you include the following font reference in the head
of your React app:
<link
href="https://fonts.googleapis.com/css?family=Montserrat:300,400,500,600,700&display=swap"
rel="stylesheet"
/>
Museo
The ENCOURA_DATALAB
theme assumes that the Museo font is available in the browser. Therefore, it is recommended that you include the following font reference in the head
of your React app:
<style type="text/css">
@font-face {
font-family: 'Museo';
src: url('path/to/Museo300.otf');
font-style: normal;
font-weight: 300;
}
@font-face {
font-family: 'Museo';
src: url('path/to/Museo500.otf');
font-style: normal;
font-weight: 500;
}
@font-face {
font-family: 'Museo';
src: url('path/to/Museo700.otf');
font-style: normal;
font-weight: 700;
}
</style>
CSS Baseline
It is recommended to inject the CssBaseline
component from Material UI near the root of your component tree in order to reset and normalize browser styles for your project:
import { CssBaseline } from '@material-ui/core';
...
const MyApp = () => (
...
<CssBaseline />
...
);
Server-Side Rendering
If your project's React framework supports SSR, you can configure the DLS components for server-side rendering by using the ServerStyleSheets
export from Material UI.
In a Next.js project, for example, you would add the following to your pages/_document.tsx
file:
import { ServerStyleSheets } from '@material-ui/core/styles';
...
class Document extends DocumentImport<Props> {
static async getInitialProps(ctx: Context): Promise<Props> {
...
const sheets = new ServerStyleSheets();
const originalRenderPage = ctx.renderPage;
ctx.renderPage = (): void =>
originalRenderPage({
enhanceApp: (App: any): any => (props: any): any =>
sheets.collect(<App {...props} />),
});
...
return {
...
styles: sheets.getStyleElement(),
...
}
}
...
}
Icons
The DLS re-exports all icons that are provided by the mdi-material-ui
package.
You can search for a specific icon to use on materialdesignicons.com. Once you've found the perfect icon, you can use it in your project like so:
import { PollBox } from '@actinc/dls/icons';
...
const MyComponent = () => (
...
<PollBox />
...
);
Minimizing Bundle Size
If you import modules from the ACT DLS using named imports, more code may be loaded into memory than you need. In order to use named imports while keeping your bundle size as small as possible, we recommend configuring the babel-plugin-transform-imports plugin for Babel.
npm install --save-dev babel-plugin-transform-imports
Then add the following to your Babel config file (e.g. .babelrc.js
):
module.exports = {
plugins: [
...
[
'babel-plugin-transform-imports',
{
'@material-ui/core': {
preventFullImport: true,
transform: '@material-ui/core/${member}',
},
'@material-ui/core/colors': {
preventFullImport: true,
transform: '@material-ui/core/colors/${member}',
},
'@actinc/dls/components': {
transform: '@actinc/dls/components/${member}',
preventFullImport: true,
},
'@actinc/dls/constants': {
transform: '@actinc/dls/constants/${member}',
preventFullImport: true,
},
'@actinc/dls/context': {
transform: '@actinc/dls/context/${member}',
preventFullImport: true,
},
'@actinc/dls/helpers': {
transform: '@actinc/dls/helpers/${member}',
preventFullImport: true,
},
'@actinc/dls/helpers': {
transform: '@actinc/dls/hooks/${member}',
preventFullImport: true,
},
'@actinc/dls/icons': {
transform: '@actinc/dls/icons/${member}',
preventFullImport: true,
},
'@actinc/dls/styles': {
transform: '@actinc/dls/styles/${member}',
preventFullImport: true,
},
'@actinc/dls/types': {
transform: '@actinc/dls/types/${member}',
preventFullImport: true,
},
},
],
...
],
presets: [...],
}
Import Stuff
That's it! You're ready to use the DLS. Simply import the colors, components, constants, helpers, icons, styles, and types that you need:
// components
import { Alert } from '@actinc/dls/components';
// constants
import { sortDirectionTypes as SORT_DIRECTION_TYPES } from '@actinc/dls/constants';
// context
import { AlertContext } from '@actinc/dls/context';
// helpers
import { search } from '@actinc/dls/helpers';
// hooks
import { useLocalStorage } from '@actinc/dls/hooks';
// icons
import { ChevronDown } from '@actinc/dls/icons';
// styles & themes
import { THEME_ACT } from '@actinc/dls/styles/themeAct';
// types
import { SortObject } from '@actinc/dls/types';
Local Development
Check out the developer guide to learn how to build effectively for the DLS.
To run the DLS locally:
- Install node modules:
npm install
- Start the Storybook component visualizer:
npm start
npm Scripts
There are lots of npm scripts at your disposal during local development. Here are some of the more important ones:
Script | Description |
---|---|
npm run build | Transpile DLS from TypeScript (./src ) into ES5 (./dist ). |
npm run build-storybook | Create a static website for deployment (./storybook-static ). |
npm start | Start the Storybook component visualizer. |
npm test | Run all tests. |
npm run release | Publish a new release of the DLS. |
Pinned Packages
Some npm packages are pinned to non-current versions for a specific reason:
Package | Version | Reason |
---|---|---|
color | 3.2.1 | Version 4.x cannot be run in Storybook due to this issue. |
np | 7.4.0 | Version 7.5.0 is broken. |