There's a frequent usecase with javascript libraries where you want to decorate your components with higher order components.
For example, the material-ui library includes a styling higher-order component withStyles
.
In javascript, you would do
import { withStyles } from '@material-ui/core';
const styles = {
example: {
padding: 8
}
}
const SomeComponent = ({classes}) => <div className={classes.example}>I'm a component</div>;
export default withStyles(SomeComponent);
How can you achieve the same in Hyperstack?
First off looks like there is an issue that you have to patch. This will be fixed in the next point release: Just add this method to your
Hypercomponent
base class (app/hyperstack/components/hypercomponent.rb
)Now if you have the following styles:
and a component that you want to style:
You can do so like this:
What is happening is we are dropping out to JS using the backticks and calling
Mui.with_styles
directly and passing itMY_STYLES
(just like in the MUI doc example). Theto_n
converts from a Ruby Hash to JS object. (When passing params to components this is automatic, but not so with simple function calls.)Then we call the resulting HOC with our
StyleTest
class (also callingto_n
to convert from a Ruby class to a simple JS class.)Finally, we import it back into a Hyperstack component class.
That is a little more work than I like so we can just add a handy helper method to our
HyperComponent
class:Now we can add styles like this:
and now we have a new Component Class with our style in it.
For example: