Skip to content

Commit 5a01a96

Browse files
Merge pull request #61 from springload/chore/unstated
Moving from mobx to unstated
2 parents d7c4b6c + 170f9ef commit 5a01a96

26 files changed

+791
-517
lines changed

CHANGELOG.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@
33
> All notable changes to this project are documented in this file.
44
> This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
55
6+
## Next
7+
8+
### Changed
9+
10+
* Refactored to use `unstated` for state-management instead of `mobx` + `mobx-react`, cutting the
11+
size of the bundle by approximately 60% 🎉.
12+
613
## [[v2.2.1]](https://github.com/springload/react-accessible-accordion/releases/tag/v2.2.1)
714

815
### Changed
916

1017
* Fixes mixed up filenames in the README
1118

12-
1319
## [[v2.2.0]](https://github.com/springload/react-accessible-accordion/releases/tag/v2.2.0)
1420

1521
### Added
1622

1723
* Demo styles added to the bundle as two optional files:
18-
- `minimal-example.css`: 'Minimal' theme - hide/show the AccordionBody component
19-
- `fancy-example.css`: 'Fancy' theme - boilerplate styles for all components, as seen on our demo
20-
24+
* `minimal-example.css`: 'Minimal' theme - hide/show the AccordionBody component
25+
* `fancy-example.css`: 'Fancy' theme - boilerplate styles for all components, as seen on our demo
2126

2227
## [[v2.1.0]](https://github.com/springload/react-accessible-accordion/releases/tag/v2.1.0)
2328

package-lock.json

+30-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+20-12
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
"build:clean": "rm -rf dist/*",
1818
"build:rollup": "NODE_ENV=production rollup -c",
1919
"build:flow": "flow-copy-source src dist/es -i \"**/*.spec.js\"",
20-
"build":
21-
"npm run build:clean && npm run build:css && npm run build:flow && npm run build:rollup",
20+
"build": "npm run build:clean && npm run build:css && npm run build:flow && npm run build:rollup",
2221
"start": "npm run js:watch",
23-
"start-demo":
24-
"webpack-dev-server --config ./webpack/webpack.config.demo.js",
25-
"pages":
26-
"rm -rf pages && cp -R demo pages && webpack --config=./webpack/webpack.config.pages.js",
22+
"start-demo": "webpack-dev-server --config ./webpack/webpack.config.demo.js",
23+
"pages": "rm -rf pages && cp -R demo pages && webpack --config=./webpack/webpack.config.pages.js",
2724
"deploy": "npm run pages && ./bin/deploy.sh",
2825
"prettier": "prettier **/*.js --write",
2926
"prepublishOnly": "npm run build"
@@ -45,13 +42,22 @@
4542
],
4643
"env": {
4744
"test": {
48-
"presets": ["es2015", "react", "stage-2"]
45+
"presets": [
46+
"es2015",
47+
"react",
48+
"stage-2"
49+
]
4950
}
5051
}
5152
},
5253
"jest": {
53-
"setupFiles": ["./src/setupTests.js"],
54-
"testPathIgnorePatterns": ["/node_modules/", "/dist/"]
54+
"setupFiles": [
55+
"./src/setupTests.js"
56+
],
57+
"testPathIgnorePatterns": [
58+
"/node_modules/",
59+
"/dist/"
60+
]
5561
},
5662
"keywords": [
5763
"react",
@@ -61,7 +67,9 @@
6167
"accessible",
6268
"accessibility"
6369
],
64-
"authors": ["Vincent Audebert <[email protected]>"],
70+
"authors": [
71+
"Vincent Audebert <[email protected]>"
72+
],
6573
"contributors": [
6674
{
6775
"name": "Mitch Ryan",
@@ -83,6 +91,7 @@
8391
"babel-cli": "^6.26.0",
8492
"babel-eslint": "^8.2.2",
8593
"babel-loader": "^7.1.2",
94+
"babel-plugin-external-helpers": "^6.22.0",
8695
"babel-polyfill": "^6.26.0",
8796
"babel-preset-es2015": "^6.24.1",
8897
"babel-preset-react": "^6.24.1",
@@ -120,8 +129,7 @@
120129
"dependencies": {
121130
"classnames": "^2.2.5",
122131
"consecutive": "^5.0.4",
123-
"mobx": "^3.4.0",
124-
"mobx-react": "^4.3.5"
132+
"unstated": "^1.2.0"
125133
},
126134
"peerDependencies": {
127135
"react": "^15.4.0 || ^16.0.0",

rollup.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export default [
2121
browser: true,
2222
}),
2323
eslint(),
24-
babel(),
24+
babel({
25+
plugins: ['external-helpers'],
26+
externalHelpers: true,
27+
}),
2528
commonjs(),
2629
replace({
2730
exclude: 'node_modules/**',

src/Accordion/accordion-wrapper.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// @flow
2+
3+
import React, { Component } from 'react';
4+
import type { ElementProps } from 'react';
5+
6+
import { Provider, Subscribe } from 'unstated';
7+
import AccordionContainer from '../AccordionContainer/AccordionContainer';
8+
import Accordion from './accordion';
9+
10+
type AccordionWrapperProps = ElementProps<'div'> & {
11+
accordion: boolean,
12+
onChange: Function,
13+
};
14+
15+
const defaultProps: AccordionWrapperProps = {
16+
accordion: true,
17+
onChange: () => {},
18+
className: 'accordion',
19+
children: null,
20+
};
21+
22+
class AccordionWrapper extends Component<AccordionWrapperProps> {
23+
accordionStore = new AccordionContainer();
24+
25+
static defaultProps = defaultProps;
26+
27+
componentWillMount() {
28+
this.accordionStore.setAccordion(this.props.accordion);
29+
this.accordionStore.setOnChange(this.props.onChange);
30+
}
31+
32+
componentWillUpdate(nextProps: AccordionWrapperProps) {
33+
this.accordionStore.setAccordion(nextProps.accordion);
34+
this.accordionStore.setOnChange(nextProps.onChange);
35+
}
36+
37+
render() {
38+
const { accordion, onChange, ...rest } = this.props;
39+
return (
40+
<Provider inject={[this.accordionStore]}>
41+
<Subscribe to={[AccordionContainer]}>
42+
{accordionStore => (
43+
<Accordion
44+
accordion={accordionStore.state.accordion}
45+
{...rest}
46+
/>
47+
)}
48+
</Subscribe>
49+
</Provider>
50+
);
51+
}
52+
}
53+
54+
export default AccordionWrapper;

src/Accordion/accordion.js

+9-28
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,18 @@
11
// @flow
22

3-
import React, { Component, type ElementProps } from 'react';
4-
import { Provider } from 'mobx-react';
5-
import { createAccordionStore } from '../accordionStore/accordionStore';
3+
import React, { type ElementProps } from 'react';
64

75
type AccordionProps = ElementProps<'div'> & {
8-
accordion: boolean,
9-
onChange: Function,
6+
accordion: ?boolean,
107
};
118

12-
class Accordion extends Component<AccordionProps, *> {
13-
static defaultProps = {
14-
accordion: true,
15-
onChange: () => {},
16-
className: 'accordion',
17-
children: null,
18-
};
19-
20-
accordionStore = createAccordionStore({
21-
accordion: this.props.accordion,
22-
onChange: this.props.onChange,
23-
});
24-
25-
render() {
26-
const { accordion: accordionProp, onChange, ...rest } = this.props;
27-
const { accordion } = this.accordionStore;
9+
const accordionDefaultProps = {
10+
accordion: true,
11+
};
2812

29-
return (
30-
<Provider accordionStore={this.accordionStore}>
31-
<div role={accordion ? 'tablist' : null} {...rest} />
32-
</Provider>
33-
);
34-
}
35-
}
13+
const Accordion = ({ accordion, ...rest }: AccordionProps) => (
14+
<div role={accordion ? 'tablist' : null} {...rest} />
15+
);
16+
Accordion.defaultProps = accordionDefaultProps;
3617

3718
export default Accordion;

0 commit comments

Comments
 (0)