@czi-sds/components 14.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +323 -0
- package/dist/index.cjs.d.ts +1141 -0
- package/dist/index.cjs.js +5904 -0
- package/dist/index.esm.d.ts +1141 -0
- package/dist/index.esm.js +5783 -0
- package/dist/tailwind.json +147 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/variables.css +111 -0
- package/dist/variables.scss +109 -0
- package/package.json +140 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Chan Zuckerberg Initiative Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
# Purpose
|
|
2
|
+
|
|
3
|
+
The Science Design System (SDS) brings consistency and universal standards to CZI’s science products by offering a library of high quality, reusable components that deliver predictable, accessible and easy to learn experiences. Our goal is to democratize access to tools and technologies for scientists.
|
|
4
|
+
|
|
5
|
+
## Design System Documentation
|
|
6
|
+
|
|
7
|
+
`@czi-sds/components` implements the Science Design System as documented in [Zeroheight](https://sds.czi.design/). As a result, it's very useful to get familiar with the available **theme variables**, such as `colors`, `spacings`, `typography`, etc., so you can leverage the theme properly in your application.
|
|
8
|
+
|
|
9
|
+

|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
[NPM Package](https://www.npmjs.com/package/czi-sds/components)
|
|
14
|
+
|
|
15
|
+
**Currently SDS uses Material UI v5**
|
|
16
|
+
|
|
17
|
+
NOTE: Since most of the czi-sds components are built on top of Material UI's equivalent, it's also super useful to use their [API documentation](https://mui.com/) to learn about what you can do with the components. Many czi-sds components are style wrappers that pass props through to the MUI component without modifying them.
|
|
18
|
+
|
|
19
|
+
`@czi-sds/components` installs without direct dependencies to prevent version errors. Please ensure the following peer dependencies are also installed:
|
|
20
|
+
|
|
21
|
+
```
|
|
22
|
+
"@emotion/css"
|
|
23
|
+
"@emotion/react"
|
|
24
|
+
"@emotion/styled"
|
|
25
|
+
"@mui/base"
|
|
26
|
+
"@mui/icons-material"
|
|
27
|
+
"@mui/lab"
|
|
28
|
+
"@mui/material"
|
|
29
|
+
"react"
|
|
30
|
+
"react-dom"
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
To install @czi-sds/components and the dependencies:
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
// with npm
|
|
37
|
+
npm i @czi-sds/components @emotion/css @emotion/react @emotion/styled @mui/base @mui/material @mui/icons-material @mui/lab react react-dom
|
|
38
|
+
|
|
39
|
+
// with yarn
|
|
40
|
+
yarn add @czi-sds/components @emotion/css @emotion/react @emotion/styled @mui/base @mui/material @mui/icons-material @mui/lab react react-dom
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Yarn scripts
|
|
44
|
+
|
|
45
|
+
Common yarn scrips have been moved to the monorepo root. The -- syntax can be used to pass parameters to the underlying yarn scripts. For instance, to update the snapshots, use `lerna run test -- -u` instead of running all tests on both packages with `yarn test`.
|
|
46
|
+
|
|
47
|
+
- `yarn start`: Starts storybook on the local machine
|
|
48
|
+
- `yarn build-storybook`: Builds the storybook in the docs-build folder
|
|
49
|
+
- `yarn test-storybook`: Tests current running instance of storybook
|
|
50
|
+
- `yarn storybook:axe`: Builds the storybook and runs accessibility tests
|
|
51
|
+
- `yarn storybook:axeOnly`: Runs accessibility tests on the latest build of the storybook inside the docs-build folder
|
|
52
|
+
- `yarn test`: Runs `jest` tests
|
|
53
|
+
- `yarn namespace-check`: Runs typescript type checking on namespace files to ensure that there are no duplicated exports
|
|
54
|
+
- `yarn lint`: Runs linter
|
|
55
|
+
- `yarn build`: Build the packages
|
|
56
|
+
- `yarn ci`: Executes `yarn install --frozen-lockfile` in both packages
|
|
57
|
+
|
|
58
|
+
- _To execute any script in the inner package, one can simply use the command `lerna run script --scope=<package>`. For instance, to run the linter only on the sci-components package, use the command `lerna run lint --scope=@czi-sds/components`._
|
|
59
|
+
|
|
60
|
+
## Usage
|
|
61
|
+
|
|
62
|
+
`@czi-sds/components` comes with five main exports that help you build your app:
|
|
63
|
+
|
|
64
|
+
1. Components - Accessible and reusable components
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import React from "react";
|
|
68
|
+
import { Button } from "@czi-sds/components";
|
|
69
|
+
<Button onClick={actions.onClick} sdsStyle="rounded" sdsType="primary">
|
|
70
|
+
{text}
|
|
71
|
+
</Button>;
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
2. Mixins - Grouped styles defined by the design system
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
import { styled } from '@mui/material/styles';
|
|
78
|
+
import { Typography } from "@mui/material";
|
|
79
|
+
import { fontHeaderXL } from "@czi-sds/components";
|
|
80
|
+
|
|
81
|
+
export const Title - styled(Typography)`
|
|
82
|
+
${fontHeaderXl}
|
|
83
|
+
|
|
84
|
+
// which compiles to:
|
|
85
|
+
font-size: 22px;
|
|
86
|
+
font-weight: 600;
|
|
87
|
+
letter-spacing: 0.3px;
|
|
88
|
+
line-height: 30px;
|
|
89
|
+
`;
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
3. Selectors - Helper functions that return theme variables baased on passed props
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { css, SerializedStyles } from "@emotion/react";
|
|
96
|
+
import { styled } from '@mui/material/styles';
|
|
97
|
+
import { getColors, getCorners } from "@czi-sds/components";
|
|
98
|
+
|
|
99
|
+
export const Tag = styled("div")`
|
|
100
|
+
// This is a callback function that returns more CSS rules, but the only way
|
|
101
|
+
// to access the custom theme object
|
|
102
|
+
${(props) => {
|
|
103
|
+
// getColors() is a selector that picks out colors from the theme object
|
|
104
|
+
const colors = getColors(props);
|
|
105
|
+
// getSpaces() is a selector that picks out spacings from the theme object
|
|
106
|
+
const spacings = getSpaces(props);
|
|
107
|
+
|
|
108
|
+
return `
|
|
109
|
+
background-color: ${colors?.gray[500]};
|
|
110
|
+
padding-bottom: ${spacings?.m}px;
|
|
111
|
+
margin-bottom: ${spacings?.xxl}px;
|
|
112
|
+
`;
|
|
113
|
+
}}
|
|
114
|
+
`;
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
4. CSS & SCSS Variables - Variables for the `defaultTheme` to use if your app doesn't support `@emotion/styled`
|
|
118
|
+
|
|
119
|
+
```scss
|
|
120
|
+
// with SCSS variables
|
|
121
|
+
@import "~@czi-sds/components/dist/variables";
|
|
122
|
+
|
|
123
|
+
.button-primary {
|
|
124
|
+
background-color: $sds-color-primary-400;
|
|
125
|
+
padding: $sds-spaces-xxs;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// with CSS variables
|
|
129
|
+
.button-primary {
|
|
130
|
+
background-color: var(--sds-color-primary-400);
|
|
131
|
+
padding: var(--sds-spaces-xxs);
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
5. Tailwind - Tailwind compliant configuration for the `defaultTheme` to use if your app uses Tailwind
|
|
136
|
+
|
|
137
|
+
First you need to import the SDS config into your application's Tailwind config:
|
|
138
|
+
|
|
139
|
+
```js
|
|
140
|
+
// tailwind.config.js
|
|
141
|
+
|
|
142
|
+
const sds = require("@czi-sds/components/dist/tailwind.json");
|
|
143
|
+
|
|
144
|
+
module.exports = {
|
|
145
|
+
mode: "jit",
|
|
146
|
+
content: ["./src/**/*.{tsx,scss}"],
|
|
147
|
+
theme: {
|
|
148
|
+
extend: sds,
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
If you have existing styles that you'd like to maintain, you can pick and choose
|
|
154
|
+
different parts of the SDS config:
|
|
155
|
+
|
|
156
|
+
```js
|
|
157
|
+
// tailwind.config.js
|
|
158
|
+
|
|
159
|
+
const sds = require("@czi-sds/components/dist/tailwind.json");
|
|
160
|
+
|
|
161
|
+
module.exports = {
|
|
162
|
+
mode: "jit",
|
|
163
|
+
content: ["./src/**/*.{tsx,scss}"],
|
|
164
|
+
theme: {
|
|
165
|
+
extend: {
|
|
166
|
+
...sds,
|
|
167
|
+
|
|
168
|
+
width: {
|
|
169
|
+
...sds.width,
|
|
170
|
+
"app-modal-width": "500px",
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
height: {
|
|
174
|
+
...sds.width,
|
|
175
|
+
"app-modal-height": "200px",
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
After that, you should be able to use SDS Tailwind classes in your app:
|
|
183
|
+
|
|
184
|
+
```tsx
|
|
185
|
+
export function Hello() {
|
|
186
|
+
return (
|
|
187
|
+
<p className="m-sds-xl px-sds-xs py-sds-s text-sds-error-400">
|
|
188
|
+
Hello, World!
|
|
189
|
+
</p>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Default Theme
|
|
195
|
+
|
|
196
|
+
To use the default theme in your React application, complete the following:
|
|
197
|
+
|
|
198
|
+
1. Add the following HTML to your `index.html` at the `<head>` section:
|
|
199
|
+
|
|
200
|
+
```html
|
|
201
|
+
// installs the sds font from google fonts
|
|
202
|
+
<link rel="preconnect" href="https://fonts.gstatic.com" />
|
|
203
|
+
<link
|
|
204
|
+
href="https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,600;0,700;0,800;1,400;1,600;1,700&display=swap"
|
|
205
|
+
rel="stylesheet"
|
|
206
|
+
/>
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
2. Import the default theme object and use it in Material UI's `<ThemeProvier />`:
|
|
210
|
+
|
|
211
|
+
```javascript
|
|
212
|
+
import { ThemeProvider as EmotionThemeProvider } from "@emotion/react";
|
|
213
|
+
import { StyledEngineProvider, ThemeProvider } from "@mui/material/styles";
|
|
214
|
+
import { defaultTheme } from "@czi-sds/components";
|
|
215
|
+
|
|
216
|
+
<StyledEngineProvider injectFirst>
|
|
217
|
+
<ThemeProvider theme={defaultTheme}>
|
|
218
|
+
<EmotionThemeProvider theme={defaultTheme}>
|
|
219
|
+
<YourApp />
|
|
220
|
+
</EmotionThemeProvider>
|
|
221
|
+
</ThemeProvider>
|
|
222
|
+
</StyledEngineProvider>;
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
If you want to override the default theme, please use `defaultAppTheme`, override the options, and then call `createTheme` to generate
|
|
226
|
+
the full theme object like below. This is needed because `createTheme` generates
|
|
227
|
+
extra theme variables based on the themeOptions provided, so if you override `defaultTheme` directly, some auxillary theme variables will be based on `defaultAppTheme` instead of your own custom options
|
|
228
|
+
|
|
229
|
+
```tsx
|
|
230
|
+
import { ThemeProvider as EmotionThemeProvider } from "@emotion/react";
|
|
231
|
+
import { defaultAppTheme, makeThemeOptions } from "@czi-sds/components";
|
|
232
|
+
import { StyledEngineProvider, ThemeProvider } from "@mui/material/styles";
|
|
233
|
+
import createTheme from "@mui/material/styles/createTheme";
|
|
234
|
+
|
|
235
|
+
const customTheme = {
|
|
236
|
+
...
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const appTheme = makeThemeOptions({ ...defaultAppTheme, ...customTheme })
|
|
240
|
+
|
|
241
|
+
const theme = createTheme(appTheme)
|
|
242
|
+
|
|
243
|
+
<StyledEngineProvider injectFirst>
|
|
244
|
+
<ThemeProvider theme={theme}>
|
|
245
|
+
<EmotionThemeProvider theme={theme}>
|
|
246
|
+
<YourApp />
|
|
247
|
+
</EmotionThemeProvider>
|
|
248
|
+
</ThemeProvider>
|
|
249
|
+
</StyledEngineProvider>
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
💡 CZGE example available [here](https://github.com/chanzuckerberg/czgenepi/blob/trunk/src/frontend/src/common/styles/theme.ts).
|
|
253
|
+
|
|
254
|
+
💡 Material UI docs for custom theming available [here](https://mui.com/material-ui/customization/theming/).
|
|
255
|
+
|
|
256
|
+
## Q&A
|
|
257
|
+
|
|
258
|
+
1. Why wrapping a component with `styled()` doesn't style the root element as expected?
|
|
259
|
+
|
|
260
|
+
This is likely because the component is **NOT** forwarding the css class name that `styled()` generates to the intended root element. So we likely need to update the `czif` component to make it work
|
|
261
|
+
|
|
262
|
+
For example, if a `czif` component `Foo` has the following implementation, `styled(Foo)` won't style the wrapper `<div />` as expected:
|
|
263
|
+
|
|
264
|
+
```tsx
|
|
265
|
+
function Foo() {
|
|
266
|
+
return (
|
|
267
|
+
<div>
|
|
268
|
+
<ChildA />
|
|
269
|
+
<ChildB />
|
|
270
|
+
</div>
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
The fix is:
|
|
276
|
+
|
|
277
|
+
```tsx
|
|
278
|
+
function Foo({ className }) {
|
|
279
|
+
return (
|
|
280
|
+
<div className={className}>
|
|
281
|
+
<ChildA />
|
|
282
|
+
<ChildB />
|
|
283
|
+
</div>
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
1. To style a sub-component of a `@czi-sds/components` component, typically we export the sub-component for the call site to import and style via `styled`, and then you will be able to pass back the styled sub-component to the `@czi-sds/components` component through prop
|
|
289
|
+
|
|
290
|
+
For example, `ComplexFilter` exports `ComplexFilterInputDropdown` sub-component, so if you want to style it, you can do the following:
|
|
291
|
+
|
|
292
|
+
```tsx
|
|
293
|
+
import {
|
|
294
|
+
ComplexFilter,
|
|
295
|
+
ComplexFilterInputDropdown,
|
|
296
|
+
} from "@czi-sds/components";
|
|
297
|
+
import styled from "@emotion/styled";
|
|
298
|
+
|
|
299
|
+
const StyledComplexFilterInputDropdown = styled(ComplexFilterInputDropdown)`
|
|
300
|
+
color: pink;
|
|
301
|
+
`;
|
|
302
|
+
|
|
303
|
+
function Foo() {
|
|
304
|
+
return (
|
|
305
|
+
<ComplexFilter
|
|
306
|
+
InputDropdownComponent={StyledComplexFilterInputDropdown}
|
|
307
|
+
/>
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
## Project status
|
|
313
|
+
|
|
314
|
+
This project is under **active development**. Contributions and ideas are welcome! If you would like to contribute, check out the [contribution guidelines](docs/contribution.md) or open an issue.
|
|
315
|
+
This project is governed under the [Contributor Covenant](https://www.contributor-covenant.org/) code of conduct.
|
|
316
|
+
|
|
317
|
+
## Reporting Security Issues
|
|
318
|
+
|
|
319
|
+
Please note: If you believe you have found a security issue, please responsibly disclose by contacting us at security@chanzuckerberg.com. More information is in our [Security Readme](docs/SECURITY.md)
|
|
320
|
+
|
|
321
|
+
## 2022 Plans
|
|
322
|
+
|
|
323
|
+
[2022 Plans](https://docs.google.com/presentation/d/1pKAY6Wl3-EHInvOZuf0L3yEDFt2xVvMlO7ZWefQbgjA/edit?usp=sharing)
|