21
loading...
This website collects cookies to deliver better user experience
chic-modules
a whirl.attrs
constructor // application.module.css
.wrapper {
padding: 4em;
background: papayawhip;
}
.title {
font-size: 1.5em;
text-align: center;
color: palevioletred;
}
// application.jsx
import React from 'react';
import styles from './application.module.css';
import { create } from 'chic-modules';
// Call the chic-modules `create` factory and pass the
// required styles object as an argument
const styled = create(styles);
// Create a <Wrapper> React component that inherits the `.wrapper`
// class from the styles object and renders a <section> html element
const Wrapper = styled.section('wrapper');
// Create a <Title> React component that inherits the `.title`
// class from the styles object and renders a <h1> html element
const Title = styled.h1('title');
// Use them like regular React components – except they're styled!
function Application() {
return (
<Wrapper>
<Title>Hello World, this is my first chic component!</Title>
</Wrapper>
);
}
chic-modules
really shines is in its attempt to solve this problem.import classnames from 'classnames';
import styles from './button.module.css';
function MyButton({ children, isPrimary }) {
const classes = classnames(
'button',
{
[styles['button--primary']]: isPrimary
}
);
return <button classNames={classes}>{children}</button>;
}
// outputs <button class="button button--primary">
chic-modules
can infer when a prop is being used as a style modifier and automagically add the relevant modifier class if it exists in the styles object to the component.import styles from './button.module.css';
import { create } from 'chic-modules';
const styled = create(styles);
const Button = styled.button('button');
function MyButton({ children, isPrimary }) {
return <Button isPrimary={isPrimary}>{children}</Button>;
}
// outputs <button class="button button--primary">
has
, is
or with
and its value evaluates as truthy. You can also pass string values to props prefixed with with
and have that value used in the constructed modifier class.chic-modules
expects that your styles follow the BEM naming convention, so when using this package ensure that your stylesheet aligns with this structure.<Button hasBorder isPrimary withTextColor="black" />
// outputs <button class="button button--border button--primary button--text-color-black">
chic-modules
interesting and would like to help out then feel free to take a look over the code and suggest any improvements. Additionally, it would be a big help if you could star, tweet or mention this project to raise some awareness!chic-modules
then definitely send it my way! I'd love to see what's being made and I'll even include it in the README.