[Question] How can I make the provided css file work for the datepicker?
I am using CSSModules to import css to my components. So what I did was to copy the provided css and import it with import styles from "./rdt.css". Unfortunately it doesn't work as expected. Am I missing anything here?
Hey @vizFlux
Never tried CSSModules so I can't help with that. I usually load the styles using webpack's style+css loaders and work like a charm.
You can also add the tag the header of your index.html, if you are still having problems.
I am using MERN stack, it doesn't load even after having the css pasted into main.css which imported to index.js :(
Is this the only React component you're having this problem with?
Yes
On Mon, Sep 5, 2016 at 8:48 PM Simon Egersand [email protected] wrote:
Is this the only React component you're having this problem with?
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/YouCanBookMe/react-datetime/issues/132#issuecomment-244739289, or mute the thread https://github.com/notifications/unsubscribe-auth/ANSx3JXBVEs-u0Q2XInEypS9hL549E_gks5qnA-4gaJpZM4J0tBp .
Are you sure you're importing the correct file? It's not called rdt.css in the repo, it's called react-datetime.css.
Can you give an example of a React component you're using which is working out-of-the-box with CSSModules?
The last comment in https://github.com/YouCanBookMe/react-datetime/issues/8 suggests that we should include the styles inline. It's a great idea with one downside: we would be adding the styles everytime that the picker is included, and that can happen more than one in the same page.
On the other hand, no developer would need to add the styles manually, which makes the use of react-datetime much simpler.
Thoughs?
Generally I feel like we should do as most other components does it, and I've never seen a React component with inline styling. What are the benefits of having the styling inline (besides resolving this particular issue)?
The benefits are that you don't force the component user to load the stylesheet separatelly. Inline styles sounds somehow ugly, but if you use webpack's style loader is basically what you are doing.
The other way of not making the users load the styles separately is applying the styles directly to the tag, but that makes them really difficult to overwrite.
So how would a developer override the inline styling? Because inline styling has highest prio. Changing the styling in the JS feels a bit awkward, and by using external CSS you have to use !important to be able to overwrite inline.
I've been using "Aphrodite" and was able to write a few trivial styles in the source file in order to get the styling to do minor tweaks here and there. It has been very successful and awesome!
When I say "inline styles" I mean the same stylesheet in a <style /> tag. That's easily overwritable :)
@arqex oohh, I see. Ok that makes more sense then. In that case I have no objections against having them inline :) I'll create an issue for it.
Let's keep this issue open to see that @vizFlux's problem gets resolved. Further discussions can continue in #142.
@simeg I am using Aphrodite in my MERN project, and it is working in inputting new styling rather than replacing the styles in react-datetime default classes. For example:
import { StyleSheet, css } from 'aphrodite';
import Datetime from 'react-datetime';
const s = StyleSheet.create({
rdtPicker: { // this generate a new css class e.g rdtPicker_1wtftbl rather than target the original class
width: 1000,
},
});
<Datetime className={css(s.rdtPicker)} />
Would appreciate greatly if @ryanhca you could share how you target the specific class to implement new stylings.
Furthermore, this only gets the style replacement done, but not much in getting the default stylehsheet applied. The method I used to get the react-datetime.css applied in my current work is declaring the class as global with postcss:
:global(.rdtPicker) {
display: none;
position: absolute;
width: 250px;
padding: 4px;
margin-top: 1px;
z-index: 99999 !important;
background: #fff;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
border: 1px solid #f9f9f9;
}
@vizFlux Glad to see you managed to fix it. It seems like a lot of work to get the styling and to be able to customize it. To me that's a sign of something being broken.
@vizFlux in the DatePicker.js file I import Aphrodite like this
var assign = require('object-assign'),
...
StyleSheet = require('../aphrodite');
then created the styles object at the bottom of the file
module.exports = Datetime;
var styles = StyleSheet.StyleSheet.create({
bottomInput: {
border: 'none',
borderBottom: '2px solid #e9e9e9',
display: 'inline',
fontSize: "16px",
color: "#666666",
width: "250px",
lineHeight: 1,
padding: "12px 10px 8px",
":focus": {
borderBottom: '2px solid #005CCF',
fontSize: '16px',
color: "black",
}
}
})
then pass it as a prop on className to component I wanted to change.
children = [ DOM.input( assign({
key: 'i',
type:'text',
className: StyleSheet.css(styles.bottomInput),
onFocus: this.openCalendar,
onChange: this.onInputChange,
value: this.state.inputValue
}, this.props.inputProps ))];
might be a hack but it got the job done. Best thing about Aphrodite is that it is class attribute oriented. So Instead of being "TRUE" inline, it simply injects a stylesheet directly into the head of your project only when the component is loaded. So it's pretty darn nifty and you don't have to write a bunch of "TRUE" inline styles or worry about over inheritance problems etc..
@ryanhca Amazing! didn't expect we can use Aprodite this way. However, this approach means I do need to amend the source code in the node_modules, do I?
@vizFlux Well. you have two options. 1.)When you push your code up to a production/staging server it is going to be written in the modified node files and it will remain there with your styles. BUT if you are .gitIgnoring your node_modules (Which you should be) then anyone else who works on the code base wont get them. 2.)So we actually forked and cloned the library and tweaked some things and shoved it into our codebase as a component so that we can modify it whenever we want. Luckily, I'm the only one who works front-end at my company so that makes it nice! haha
Also, if you follow through with forking/cloning/making it your own, you'll have to switch the relative path that you point to with aphrodite... it will remain a node_module, so it wont be found in the same directory as your awesome react-datetime component anymore!
Aphrodite and React-DateTime FTW!! Enjoy!
@ryanhca Wow I must admit as a fresh front-end developer in React, I learned some very useful things alone in this issue. Appreciate very much of your sharing man, much enjoyed!
@vizFlux I am trying the same thing as you and am wondering if you managed to convert all CSS styles in react-datetime.css? Since they are too many of those, making the JSON looking style to be given to StyleSheet of aphrodite is painfully slow. I wonder if you have something decently working and are able to share? Thank you!
@ryanhca How can I pass all CSS styles specified in the CSS Style sheet in this repo one-shot to Datetime component using aphrodite. It looks to me a nightmare as I only wish to have something out-of-box which looks similar to the demo. Thanks.