Coder Social home page Coder Social logo

css-modules-theme's People

Contributors

chbdetta avatar klimashkin avatar shiro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

css-modules-theme's Issues

[QUESTION] Handling different component 'types'

Thanks for the great work on the project.
I was wondering if there is a good way to handle components which look differently depending on a prop.
example:

export interface IProps{
    type?: 'primary' | 'secondary'; // optional, defaults to default style
}

My approach:

case 1: component has a container element of sorts

// Button.tsx
const Button: React.SFC<IProps> = (props) => {
    const theme = getThemeFromProps(style, props);
    if(styles[props.type])
        theme.container = styles[props.type];

    return (
        <div className={theme.container}>
            <Child theme={theme} themePrefix="Child-" />
        </div>
    );
}
// Button.scss
.container{
    & > .Child-element{ ... }
}
.primary{
    @extend .container;

    // primary styles
    .container > .Child-element{ ... }
}
...

The approach is not pretty at all IMO. It shouldn't be solved with @extend, also readability ๐Ÿ˜ญ.

case 2: component does not have a container element

// Button.tsx
const Button: React.SFC<IProps> = (props) => {
    const theme = getThemeFromProps(style, props);

    return (
        <Child>
            <a>Example use case.</a>
        </Child>
    );
}

No idea how to solve this as .A > .B cannot be used (we do not want to add extra wrapper divs, it could result in unexpected layout changes we'd then have to fix with css, also DOM bloat).

Why I think making type wrappers is bad
In my opinion creating wrapper components for every type is not practical, also it's merely a visual rather than semantic difference.

Imagine we have more than 10 type variants per component and we are working with a big code-base.
The project will be a bloated mess in no time.

Typings for typescript

Typescript is often used with react and provides strong type checks at compile-time.

At the moment there are no typings for css-modules-theme.
Perhaps someone with more TS experience could work on this as I'm not used to it yet.

edit
I've tried to write some typings for this, although as it seems this completely destroys type safety, since it's impossible to determine the type of the theme props as it depends on the themePrefix prop.

At this time it is impossible to augment string literal types in TS (microsoft/TypeScript#12754).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.