Skip to content

Commit 7ecca15

Browse files
committed
Merge remote-tracking branch 'origin/main' into tiling
# Conflicts: # .babelrc # src/app.tsx
2 parents bae73d3 + 99b821d commit 7ecca15

File tree

28 files changed

+1060
-2532
lines changed

28 files changed

+1060
-2532
lines changed

.babelrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"],
33
"plugins": [
4-
"@babel/transform-runtime",
5-
"@babel/plugin-proposal-optional-chaining"
4+
"@babel/transform-runtime"
65
]
76
}

.storybook/main.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ module.exports = {
33
addons: [
44
'@storybook/addon-links',
55
'@storybook/addon-essentials',
6-
'@storybook/addon-interactions',
7-
'@storybook/addon-knobs'
6+
'@storybook/addon-interactions'
87
],
98
framework: '@storybook/react',
109
core: {
@@ -13,7 +12,23 @@ module.exports = {
1312
webpackFinal: async (config) => {
1413
config.module.rules.push({
1514
test: /\.less$/,
16-
use: ['style-loader', 'css-loader', 'less-loader']
15+
use: [
16+
'style-loader',
17+
{
18+
loader: 'css-loader',
19+
options: {
20+
modules: false
21+
}
22+
},
23+
{
24+
loader: 'less-loader',
25+
options: {
26+
lessOptions: {
27+
javascriptEnabled: true
28+
}
29+
}
30+
}
31+
]
1732
});
1833
return config;
1934
}

README.md

Lines changed: 145 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,39 @@
88

99
This repo contains a set of components for displaying and interacting with material science data. These components provide the building blocks for the interactive components of the Materials Project website.
1010

11-
## Local development
11+
## Docs and Examples
1212

13-
### Getting started
13+
Check out the [mp-react-components storybook](https://materialsproject.github.io/mp-react-components/) to see examples and documentation for each component in this library.
1414

15-
First, clone the mp-react-components repo: `git clone [email protected]:materialsproject/mp-react-components.git`
15+
## Getting Started with Local Development
1616

17-
From inside the top directory, install the dependencies using: `npm install`
17+
Clone the mp-react-components repo:
1818

19-
Deploy the app using: `npm start`
19+
```
20+
git clone [email protected]:materialsproject/mp-react-components.git
21+
```
22+
23+
From inside the top directory, install the dependencies:
24+
25+
```
26+
npm install
27+
```
28+
29+
Deploy the app to https://localhost:3000:
30+
31+
```
32+
npm start
33+
```
2034

21-
### Developing components
35+
## Installing as a Node Module
36+
37+
Install the latest snapshot of mp-react-components:
38+
39+
```
40+
npm install @materialsproject/mp-react-components@next
41+
```
42+
43+
## Developing Components
2244

2345
All the components are located in `src/components`
2446

@@ -32,13 +54,98 @@ The file structure for each component should look like the following:
3254
- `MyComponent.css`
3355
- `MyComponent.test.tsx`
3456

35-
### Exporting components
57+
### TypeScript and JSX
58+
59+
This repo is configured to use TypeScript and JSX. Any file that uses TypeScript and JSX syntax (i.e. component files and any other files that include components) should use the `.tsx` extension.
60+
61+
### Testing Components During Development
62+
63+
To see how your component looks and behaves while you are developing it, you have a few different options. When you start the app locally, the main entry point that determines what is rendered is in `src/app.tsx`. Because this is a component library and not a fully integrated app, the `app.tsx` file exists solely as a testing sandbox. A simple routing structure has been setup to test out different kinds of pages in a react-only environment. These test pages live in the `src/pages` directory. When testing a new component, you can use it inside of one of the existing pages or you can create a new page. There is also the `Sandbox` page within the `pages` directory that is intended to be cleared out any time and used to test new discrete components.
64+
65+
If you prefer, you can also test your component inside of storybook. To do this, create a new story within the `src/stories` directory. See the Storybook section below for more info about Storybook.
66+
67+
### Classes
3668

37-
To add a component to the list of components exported by this library, import and add the component to `src/index.ts`
69+
By convention, almost all components are given a top-level class which is the name of the component converted into kabob-case and prefixed with "mpc-". For example, the `DataBlock` component has the class `mpc-data-block`. The `className` prop for any component should extend the default class rather than replace it.
3870

39-
### Developing with dash-mp-components
71+
The components in this library are intended to be used with the [Bulma CSS Framework](https://bulma.io/). For that reason, many bulma classes and component patterns are utilized within the components.
4072

41-
If you have a local version of `dash-mp-components` and `mp-react-components`, you'll probably
73+
### Styles
74+
75+
Components should come with styles that are important for their usage. To include styles for a component, add a CSS file named `MyComponent.css` in the component's folder and import that CSS file into the main component file.
76+
77+
```
78+
import './MyComponent.css';
79+
```
80+
81+
Remember that these components are intended to be used with the [Bulma CSS framework](https://bulma.io/), so the styles in the custom stylesheet can (and often do) take this into account.
82+
83+
### Test IDs
84+
85+
Also by convention, almost all components are given a test ID via the attribute `data-testid`. In some cases, a component may have multiple elements with test IDs. These IDs make it easier to select the components and their testable parts within the unit tests.
86+
87+
### Component Utils
88+
89+
For more complicated components like `MaterialsInput` and `SearchUI`, some of the re-usable or complex functions are housed in a `utils.tsx` file within the component folder. For example, all of the validation methods for the `MaterialsInput` can be found in `MaterialsInput/utils.tsx`.
90+
91+
Other utils that are used across multiple components can be found in the higher-level `src/utils` directory. For example, the functions for initializing table columns can be found in `src/utils/table.tsx`.
92+
93+
### Component Types
94+
95+
Many components will implement their own TypeScript types or interfaces to reduce errors and improve code readability. In some cases, these types are defined directly in the component file. In cases where there are many types for a set of components, there may be a `types.tsx` file inside the component folder (e.g. `SearchUI/types.tsx`).
96+
97+
Most components also have their own props interface defined. Note that this interface should usually be uniquely named and exported so that it can be used inside the component's storybook file.
98+
99+
### Exporting Components
100+
101+
Any component that you want to be usable when this library is imported as a third-party node module must be added to the exports list in `src/index.ts`.
102+
103+
## Unit Tests
104+
105+
This repo uses [jest](https://jestjs.io/) and [react testing library](https://testing-library.com/) to write and run unit tests. Any file in the `src` directory with the `.test.tsx` or `.spec.tsx` extension will be detected as a runnable test.
106+
107+
Run all the unit tests:
108+
109+
```
110+
npm run test
111+
```
112+
113+
Run a specific test suite:
114+
115+
```
116+
npm run test MaterialsInput
117+
```
118+
119+
The jest configuration can be found in `jest.config.js`
120+
121+
### Mocks
122+
123+
To test the fetching behavior of the `SearchUI` components, there is a `mocks` directory which sets up a Mock Service Worker using the `msw` package. See more [here](https://mswjs.io/docs/getting-started/integrate/node).
124+
125+
## Deploying to npm
126+
127+
### Automated Pre-releases
128+
129+
Every push to the `main` branch triggers a build. If the build is succesfull, a snapshot is pushed
130+
to npm, as a tagged package. To use the latest snapshot, type the following command
131+
132+
```
133+
npm install @materialsproject/mp-react-components@next
134+
```
135+
136+
### Manual Release
137+
138+
Use `npm build-publish` to build the project.
139+
Use `npm publish` to push to npm
140+
141+
### Automated Minor Release (un-tested)
142+
143+
Run `npm version minor -m "Upgrade to %s"` to add a tag. Once the tag is pushed, an action willg
144+
build the components and push the package to build to npm
145+
146+
## Developing with dash-mp-components
147+
148+
If you have a local version of `dash-mp-components` and `mp-react-components`, you may
42149
want to use the local version of `mp-react-components` for a better development workflow.
43150
Running those commands will tell `NPM` to use your local version of `mp-react-components`
44151

@@ -47,30 +154,46 @@ Running those commands will tell `NPM` to use your local version of `mp-react-co
47154
npm link <REACT_MP_HOME>
48155
```
49156

50-
## Deploy to npm
157+
However, you can also push to the `main` branch to publish a new snapshot on npm whenever you are ready to start porting and testing your changes in dash-mp-components. Once the new snapshot is published, you will simply need to re-run these commands from your local dash-mp-components repo:
51158

52-
### Manually
159+
```
160+
npm install @materialsproject/mp-react-components@next
161+
npm run build
162+
```
53163

54-
Use `npm build-publish` to build the project.
55-
Use `npm publish` to push to npm
164+
## Storybook
56165

57-
#### Pre-releases
166+
This library uses [Storybook](https://storybook.js.org/) to document the components and their usage. All of the exported components have a set of "stories" that showcase different examples of how to use the component and its props. The stories can be found inside of `src/stories`.
58167

59-
Every push triggers a build. If the build is succesfull, a snapshot is pushed
60-
to npm, as a tagged package. To use the latest snapshot, type the following command
168+
### Storybook Configuration
61169

62-
`npm install @materialsproject/mp-react-components@next`
170+
Storybook is configured using webpack and various plugins offered by Storybook. The config can be found in `.storybook/main.js`. Note that in general, all of the `@storybook/` dependencies should be using the same version as each other. This can occasionally cause issues with the storybook build.
63171

64-
### Automatically(TODO)
172+
### Story Order and Styles
65173

66-
Run `npm version minor -m "Upgrade to %s"` to add a tag. Once the tag is pushed, an action will
67-
build the components and push the package to build to npm
174+
The order and hierarchy of the stories are set in `.storybook/preview.js`. This file is also where styles are imported into the storybook. Any styles that the storybook needs that aren't already imported into the components themselves should be included here.
68175

69-
## Deploy storybook to github pages
176+
### Run Storybook Locally
177+
178+
Run the story book locally:
179+
180+
```
181+
npm run storybook
182+
```
183+
184+
You should be able to access the storybook at http://localhost:6006
185+
186+
### Deploy Storybook to GitHub Pages
70187

71188
This will **build** and **deploy** the storybook.
72189
Stories are defined in `./src/stories`
73190

74191
```
75192
npm deploy-storybook
76193
```
194+
195+
If successful, the changes will be live at https://materialsproject.github.io/mp-react-components/
196+
197+
### Build Tools
198+
199+
This repo uses Parcel to build and run the app locally and Rollup to publish the app. Parcel does not have a config file, but it does use Babel to transpile the code for this repo. The babel configuration exists in `.babelrc` and `babel.config.js`. The Rollup configuration is defined in `rollup.config.js`. In the future, both of these build tools may be removed in favor of a single unified tool (potentially webpack as it is used in dash-mp-components).

0 commit comments

Comments
 (0)