Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Themr with pure functions #81

Open
MrHavard opened this issue Oct 31, 2017 · 1 comment
Open

Themr with pure functions #81

MrHavard opened this issue Oct 31, 2017 · 1 comment

Comments

@MrHavard
Copy link

MrHavard commented Oct 31, 2017

Does anyone have an example of using themr with pure function components vs. classes and how to provide a default theme and then override that theme from a parent container? I've searched through the readmes and issue log and I'm not finding anything solid as an example for this case.

Here's my thought on how it should work

/* defaultTheme.module.scss */
.button {
    background-color: blue;
    color: white;
}
/* Button.js */
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { themr } from 'react-css-themr';
import defaultTheme from './defaultTheme.module.scss';

const Button = ({ theme }) => {
    const classes = classNames({ [theme.button]: true });
    return <button className={ classes }>Button</button>;
};

Button.propTypes = {
    theme: PropTypes.any
};

export default themr('Button', defaultTheme)(Button);
/* parentTheme.module.scss */
.button {
    background-color: white;
    color: blue;
}
/* ParentContainer.js */
import React from 'react';
import { render } from 'react-dom';
import parentTheme from './parentTheme.module.scss';
import Button from './Button';

render(
    <Button theme={ parentTheme } />,
    document.getElementById('app');
);

Default theme seems to work fine and the button component displays the default styles no problem. However, nothing happens when the parent passes a theme down.

@OscarBarrett
Copy link

I've done it without using classnames, e.g.

const Button = ({ theme }) => (
  <button className={theme.button}>Button</button>
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants