Skip to content

Commit

Permalink
feat(component): styles (#296)
Browse files Browse the repository at this point in the history
* feat(component): styles

* fix(component): effect depends on comp

* chore(mson): v2.10.0
  • Loading branch information
redgeoff authored Sep 23, 2021
1 parent 0ce432f commit 461028c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 52 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"classnames": "^2.2.6",
"date-fns": "^2.15.0",
"lodash": "^4.17.20",
"mson": "^2.9.0",
"mson": "^2.10.0",
"react-beautiful-dnd": "^13.0.0",
"react-google-recaptcha": "^2.1.0",
"react-markdown": "^5.0.3",
Expand Down
91 changes: 49 additions & 42 deletions src/component.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,65 @@
import React from 'react';
import React, { useState, useEffect } from 'react';
import InnerComponent from './inner-component';
import compiler from 'mson/lib/compiler';
import attach from './attach';
import useComponent from './use-component';
import { makeStyles } from '@material-ui/core/styles';

export class Component extends React.PureComponent {
state = { component: null };
const getOrZero = (value) => (value ? value : 0);

createComponent() {
const { definition } = this.props;
this.setState({ component: compiler.newComponent(definition) });
}
const useStyles = makeStyles((theme) => ({
root: (props) => {
const { marginTop, marginRight, marginBottom, marginLeft } = props;
return {
marginTop: theme.spacing(getOrZero(marginTop)),
marginRight: theme.spacing(getOrZero(marginRight)),
marginBottom: theme.spacing(getOrZero(marginBottom)),
marginLeft: theme.spacing(getOrZero(marginLeft)),
};
},
}));

componentDidMount() {
if (this.props.definition) {
this.createComponent();
}
}
export default function Component(props) {
const { component, definition, ...childProps } = props;
const [compiledComponent, setCompiledComponent] = useState(null);

componentWillUnmount() {
const { component } = this.state;
const { hidden, styles } = useComponent(component, ['hidden', 'styles']);
const styleProps = styles ? styles.get() : {};
const classes = useStyles(styleProps);

// Did we use a definition to define a component in the state?
if (component) {
// Fire the unmount before we destroy the component so that the event is not lost
component.emitChange('unmount');
// Should we use the component generated from the definition?
const comp = component ? component : compiledComponent;

// Remove all listeners to prevent listener leaks
this.state.component.destroy();
// Compile the component when the definition changes
useEffect(() => {
if (definition) {
setCompiledComponent(compiler.newComponent(definition));
}
}
}, [definition]);

componentDidUpdate(prevProps) {
// Is the definition changing?
if (this.props.definition !== prevProps.definition) {
this.createComponent();
}
}

render() {
const { definition, component, ...childProps } = this.props;
let comp = component;
useEffect(() => {
return () => {
// Unmount. Did we use a definition to define a component in the state?
if (comp) {
// Fire the unmount before we destroy the component so that the event is not lost
comp.emitChange('unmount');

// Should we use the component generated from the definition?
if (!component) {
comp = this.state.component;
}
// Remove all listeners to prevent listener leaks
comp.destroy();
}
};
}, [comp]);

// Note: the component can be falsy, e.g. it has not yet been defined
if (comp && comp.get('hidden')) {
return null;
// Note: the component can be falsy, e.g. it has not yet been defined
if (comp && hidden) {
return null;
} else {
// As a slight optimization, only display a wrapping tag if styles is truthy
const innerComponent = <InnerComponent component={comp} {...childProps} />;
if (styles) {
// Use a div so that we have a block-level element that supports vertical margins
return <div className={classes.root}>{innerComponent}</div>;
} else {
return <InnerComponent component={comp} {...childProps} />;
return innerComponent;
}
}
}

export default attach(['hidden'])(Component);
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9971,10 +9971,10 @@ min-indent@^1.0.0:
resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869"
integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==

mingo@^4.1.1:
version "4.1.4"
resolved "https://registry.yarnpkg.com/mingo/-/mingo-4.1.4.tgz#c614ec78133f0226df5ce3f664269c67d49c4402"
integrity sha512-Az1rWrQSoUegKAjzGXFU70Kxvb7xh5Ub2SXuVrQ439rpOKDZPiifGcnlsF6wd9EHXkUQR4Lo4WeClvZ7Y6eVPQ==
mingo@^4.1.4:
version "4.1.5"
resolved "https://registry.yarnpkg.com/mingo/-/mingo-4.1.5.tgz#f6d0a9b31a6a2e0e76d567e0b7906cbc8b5270bc"
integrity sha512-VetH1x2gYz/WtNxHsExZStw6QDvM6/tqqe8waxux/nGt5psMUeXjmzAhOelHI0LR/rppLpG8xE0cFnqhMhD/UA==

mini-create-react-context@^0.4.0:
version "0.4.1"
Expand Down Expand Up @@ -10169,17 +10169,17 @@ ms@^2.0.0, ms@^2.1.1, ms@^2.1.2:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

mson@^2.9.0:
version "2.9.0"
resolved "https://registry.yarnpkg.com/mson/-/mson-2.9.0.tgz#9c9f66d8eb25c4faeba86935f4bc865e41cece0f"
integrity sha512-qnWkNB8g62hYkbIzuzk1JmaF+EOZr4dQwmffNl/2oTjwVjuPSkrkUPjmfcpvGqRy9j8rU+/yHVLKXO7kqzyQ5g==
mson@^2.10.0:
version "2.10.0"
resolved "https://registry.yarnpkg.com/mson/-/mson-2.10.0.tgz#fec3c2fff11a3bfbe5def629387279c6bf599fdb"
integrity sha512-/EPWLkGnDGHRx/xiyJejwYJWyik6/DSB2iPvR/d4ldNca/77Dm1lorAKcmN3uNk/dNKa8ozxCj1MNzmN8ki9zw==
dependencies:
"@babel/runtime" "^7.12.5"
country-telephone-data "^0.6.3"
date-fns "^2.15.0"
events "^3.2.0"
lodash "^4.17.20"
mingo "^4.1.1"
mingo "^4.1.4"
text-mask-addons "^3.8.0"
uuid "^8.3.2"
vanilla-text-mask "^5.1.1"
Expand Down

0 comments on commit 461028c

Please sign in to comment.