Skip to content

Instantly share code, notes, and snippets.

@krambertech
Created May 29, 2017 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krambertech/fa7889134d1998974f3ef8ae13c92e24 to your computer and use it in GitHub Desktop.
Save krambertech/fa7889134d1998974f3ef8ae13c92e24 to your computer and use it in GitHub Desktop.
CSS modules w/ classnames
import { PureComponent, PropTypes } from 'react';
import classNames from 'classnames/bind';
import Icon from 'components/ui/Icon';
import styles from './Button.css';
const cx = classNames.bind(styles);
/**
* Button component. Needed to trigger an operations.
*/
export default class Button extends PureComponent {
static propTypes = {
className: PropTypes.string,
/**
* Red danger button
*/
danger: PropTypes.bool,
/**
* Is button disabled
*/
disabled: PropTypes.bool,
/**
* Width with width of 100%
*/
fullWidth: PropTypes.bool,
/**
* Button without fill, border only
*/
ghost: PropTypes.bool,
/**
* Button label
*/
label: PropTypes.node.isRequired,
/**
* Is loading state (spinner will be displayed)
*/
loading: PropTypes.bool,
/**
* Primary application colored button
*/
primary: PropTypes.bool,
/**
* Secondary application colored button
*/
secondary: PropTypes.bool,
/**
* Button size. Could be default, small or large
*/
size: PropTypes.oneOf(['small', 'default', 'large']),
/**
* Green success button
*/
success: PropTypes.bool,
/**
* Orange warning button
*/
warning: PropTypes.bool,
/**
* Callback on click
*/
onClick: PropTypes.func,
};
static defaultProps = {
size: 'default',
}
render() {
const {
label,
className,
disabled,
loading,
secondary,
primary,
warning,
danger,
success,
size,
fullWidth,
ghost,
...otherProps
} = this.props;
const classes = cx(className, size, 'base', {
disabled,
loading,
secondary,
primary,
danger,
success,
warning,
fullWidth,
ghost,
});
return (
<button
className={classes}
{...otherProps}
>
<span className={styles.label}>
{label}
</span>
{
loading
&& (
<div className={styles.loadingContainer}>
<Icon
spinning
type="loading"
/>
</div>
)
}
</button>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment