-
-
Notifications
You must be signed in to change notification settings - Fork 732
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
Add support for CSS modules #73
Comments
Yes, this would be great! |
We are successfully using a fork of react-day-picker with inline styles: https://github.com/effektif/react-day-picker-substyled The fork uses a little helper function to allow users to pass a nested object in the If you like this approach, I'd be happy to contribute a PR. Thanks for the most carefully crafted React datepicker implementation out there! |
Thanks! 😄 Substyle is interesting – however I'd prefer to keep the component without external dependencies. Since I've never used CSS modules or inline style dynamically, I wonder if an API like this could work:
What do you think? |
I do quite like this API. It's definitely a good solution for supporting css modules. I would probably rename the It doesn't add support for inline styles, which is our main requirement at Signavio. However, I see that this might be out of the scope of this issue... |
👍 for that API and using the We're currently using CSS Modules for the project we're also using react-day-picker in, and so far having BEM-like namespacing felt sufficient (and using |
Now that v2 is out and the code is testable again :) I'd like to start implementing this feature. I've changed the API proposal a bit, as the original idea that was complicating too much the component internals: API proposal
Note that this change would work also for any class names, e.g. people adopting other convention than BEM can specify their own class names. Example using CSS Modulesimport classNames from './daypicker.css';
import { todayClassName, weekendClassName } from './daypicker-modifiers.css';
import { isToday, isWeekend } from './myDateUtils';
const modifiers = {
today: day => {
if (isToday(day)) {
return todayClassName;
}
return '';
},
weekend: day => {
if (isWeekend(day)) {
return weekendClassName;
}
return '';
},
};
function MyComponent() {
return (
<div>
<DayPicker classNames={classNames} modifiers={modifiers} />
</div>
);
} If I've understood correctly how CSS modules work, this change should be enough for supporting them. Any feedback? |
Hey! Thanks for the updated API proposal, it looks great and flexible! I was thinking that for the purpose of supporting CSS Modules, we might be able to get away with something even simpler and just as flexible, and would love to get your thoughts 😄 With the current BEM-like approach, the flow with modifiers is to run the modifier functions, and attach a bunch of additional class names based on the returned value of those functions. The identifier used as the "modifier name" (as per the BEM lingo) in those additional class names is no other than the property name on the In other words:
Will make all day cells have the The main difference I see between this BEM-like approach and CSS Modules is that CSS Modules works with object properties that store class names, rather than class names directly. What I'm getting at is that although with CSS Modules class names are resolved in a different way, it seems like we could also benefit from the determinism described above – and that'd make the API simpler 😄 With that in mind, I think the only change that could be needed for the API is the addition of a The only other changes necessary would be internally, where the day cell's className prop would be generated using two different methods, based on the presence of the
With this API and changes, I see two upsides:
With that API, your previous example would look like:
One downside I'm seeing with that way of handling CSS Modules is that it doesn't fit the CSS Modules spirit very well, in that we generally like to compose styles within stylesheets rather than composing multiple class names inside the same element (i.e. That's just a quick thought I had so I may have overlooked some important stuff, but I'd love to know what you think! P.S. Another downside is that this API wouldn't make room for "people adopting other convention than BEM" so that they can "specify their own class names" like you mentioned in your API proposal. I think another small and additional change on top of what I'm suggesting could make that possible though: if that feels like a valuable feature for other users, we could make the format of those generated identifiers configurable by exposing some sort of "template prop" or function. Since code is often clearer, maybe something like |
Hi @pioul, thanks a lot for taking your time for reviewing it – really useful 👍
Why should the exported CSS modules keep the BEM syntax, such as className += modifiers.map(modifier => {
- const propName = `${className}--${modifier}`;
- const className = classNames[propName];
+ const className = classNames[modifier];
return ` ${className}`;
}).join(''); Maybe I'm missing something as I'm not used to CSS Modules 😄
No problem, it was just a side effect. I don't see big reasons for supporting this. |
Hey @gpbl, that's a great question, and the points you've mentioned are spot on! Since we'll be using a single stylesheet to style several elements, it feels good to continue using their names (e.g. "daypicker", "month", "navbar") in the class names. As far as conventions go for CSS Modules, if that was the only constraint, we could definitely use something simpler like But like you've mentioned, since we'd like that approach to be compatible with non-CSS Modules users, using the exact same identifiers feels like the easiest way to go. In that sense, what I suggest feels more like stuffing CSS Modules inside the current approach: it's functional, but not tailored for CSS Modules 😄 That'll allow CSS Modules users to start using react-day-picker the same way other users do, by simply copying (and tweaking if they'd like) style.css, and importing it inside their app – only they'll be using a named import and passing that style object through the Behind the scenes, the day picker's styles will be local and exempt of any side-effects; allowing custom and arguably better classnames feels like a nice-to-have, considering the larger amount of work that'd be needed (i.e. like you've explored yourself, offering a way to map class names to elements) Hope that makes sense! |
I think the approach taken in https://github.com/javivelasco/react-css-themr makes a lot of sense to me. Its compliant with CSS Modules and gives ability to add styles to component by explicitly passing classnames through properties, or by using a global theme(ThemeProvider). I have used it indirectly through http://react-toolbox.com/#/install and it works really well. |
@gpbl Is Supporting CSS Modules and inline styles still in your future release plan? Like pioul mentioned, local styles protect day pickers from picking up unwanted styles in larger applications. I noticed there're a few forked versions that support it, but it'll be nice to see the original library provide the support to CSS Modules and inline styles too. |
Hi people support for CSS modules has been published under the
This should close the issue, I'm new to CSS modules so I hope this is the right way to implement them :) Thanks for your feedback! |
CSS Modules support has been added, published as v5.1.1. |
Just would like to let you know that I'm using this feature with https://github.com/cssinjs/jss and it works perfectly! 👍 |
@wiesson hi man! |
@lgordey Never tried jss myself, however looking to the JSS project's README, it should the same, i.e. using the const {classes} = jss.createStyleSheet(styles).attach()
<DayPicker classNames={classes} /> Please refer to the |
@gpbl Yeap, I already got it! |
API proposal
classNames
receiving an object containing the class names for the single elements, such ascontainer
,day
,caption
, ecc. Must be validated via PropTypes.modifiers
prop must return a string instead of a boolean. This string will be added to the day's cell class name. For CSS modules, it should be the imported class name.Note that this change would work also for any class names, e.g. people adopting other convention than BEM can specify their own class names.
Example using CSS Modules
The text was updated successfully, but these errors were encountered: