klimashkin / css-modules-theme Goto Github PK
View Code? Open in Web Editor NEW๐ Theme composition for CSS Modules done simple/fast/complete
License: MIT License
๐ Theme composition for CSS Modules done simple/fast/complete
License: MIT License
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.
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).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.