-
Notifications
You must be signed in to change notification settings - Fork 335
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
Compatibility with create-react-app #1052
Comments
(please do this for helpers such as |
This looks really interesting, renaming them straight away would be a breaking change. I wonder if we could have a build step that duplicates the original file with the different name, and deprecate the original file name for when we next do a breaking release? Or is there a way to have the second file automatically compose the original file? |
https://facebook.github.io/create-react-app/docs/adding-a-css-modules-stylesheet I wonder if we could consider this a general way for people to be able to use Webpack with our CSS? So support it for general CSS Module usecase and then we'd support whatever framework people are using. Edit: I think if you're a CSS Module user it already works, this is just specific to React Create App since they want to be able to support people just including raw CSS AND CSS Modules users so they use this naming convention. |
On a side note, once this is done, it would allow a file at import React from "react";
import cx from "classnames";
import styles from "./_button.module.scss";
const Button = ({className, ...props}) => (
<button className={cx(styles["govuk-button"], className)} {...props} />
);
export default Button; 😀 Edit: though this wouldn't currently be supported by CRA facebook/create-react-app#5687 |
I guess it's a CRA specific pattern, but only in that they want to pass a regex to webpack, so this seems like a reasonable convention. It appears there is some work to make this a wider standard: css-modules/css-modules#229 |
Related facebook/create-react-app#5687 |
angular-cli has a similar pattern. see https://github.com/igloosi/govuk-frontend-angular-cli-boilerplate/tree/master/src/app/app-button |
Neutrino (webpack presets from Mozilla) also follows the
|
If there are any pointers on how you see this being implemented I could look at contributing this myself. The two main questions I have are: where in the build process would you recommend creating the *.module.css version, if it was automatically generated? I am pretty sure your CSS naming convention assumes that if you have an element with In the css modules version, should we enforce this by making e.g. in _header.module.scss: @import "./header";
.govuk-header__link {
composes: govuk-header__link;
}
.govuk-header__link--homepage {
composes: govuk-header__link;
composes: govuk-header__link--homepage;
} |
I've made good progress on a PoC of using CSS modules: https://www.github.com/penx/govuk-frontend-react An example of this working out-of-the-box with Create React App, with code splitting/lazy loading and tree shaking: https://penx.github.io/govuk-frontend-react-example/ And a work in progress PoC for next.js (universal rendering): https://www.github.com/penx/govuk-frontend-react-example-next |
Hey Alasdair! Hanna and I have been going over your proposal for CSS Modules. We've looked into the difference between importing what have now, and a file with the .module.css convention (CSS Modules). When importing the regular file, you can use the class names as documented in the Design System. In your issue you don't explain what barriers using markup in this way introduce. We're assuming the two main benefits from using CSS Modules are as follows:
We think that for components, you'll need to use most if not all of the classes, and you can't get the benefits of avoiding BEM without rewriting all the CSS files. We recognise in certain files such as the helpers or overrides, you could get better code splitting but we think if we looked at the compressed CSS gzip results that the main impact on file output would be from excluding certain components, which is already possible. Is there something we've missed? |
Ok first some history of govuk-react and why we use CSSinJS, skip this if you want the TL;DR: When we started building govuk-react, govuk-frontend was not available and we weren't keen on the other alternatives for a 'component library' e.g. there was a fair amount of CSS that wasn't encapsulated (one component affecting the styles of another component, either with descendent or sibling selectors). We decided to build our own bespoke CSS that matched the govuk look and feel, in a way that was more focussed towards encapsulated components. We have other component libraries - govuk-react is the core styles and other component libraries follow the same structure for components specific to our application. We wanted to ensure users could use govuk-react and our other libraries with confidence that components would be encapsulated and CSS would not conflict. We opted for CSSinJS because:
This is explained well in this blog post https://medium.com/seek-blog/a-unified-styling-language-d0c208de2660 Another feature of govuk-react and it's associated libraries is that if you have a module structure like this:
If govuk-react@1 was released with breaking changes to date-input, and module-one wanted to upgrade to the latest version but module-two didn't, our structure would support this without having conflicting CSS. In fact, because govuk-react is a monorepo, the developers of module-two would have several options if module-one upgraded:
Again, these features are not just there for govuk-react, but for all other component libraries we build to also offer these features so that users of these libraries can easily work around breaking changes. So this is why we would use CSSinJS or CSS modules for govuk-react, but I understand that may not provide sufficient argument for why you would want it for govuk-frontend. The main benefits I see to govuk-frontend are:
|
We've spent time going over this with Alasdair and decided we won't support this directly in GOV.UK Frontend for now, if you'd like to see the details we've put together a proposal you can read.. Thanks for all your work on this. |
What?
Rename scss files that are designed to work as CSS Modules to
_{moduleName}.module.scss
or (maybe, may need more thought) include a second file called
_{moduleName}.module.scss
that composes_{moduleName}.scss
Why?
Create React App 2 now supports Sass and CSS Modules:
Create React App has the convention that if you do:
It will treat button.scss as a normal SCSS file and append the styles to your stylesheet.
However, if you do:
It will treat button.module.scss as a CSS module and return an object to JS for class name lookup.
Consider the following:
Where button.module.scss is:
At the moment I have to create a mostly redundant file to make sure that _button.scss is picked up as a CSS module.
Using:
Imports the CSS file but does not treat it as CSS module, so this does not work.
I have created a fork of govuk-frontend and renamed _button.scss to _button.module.scss, so if you do the following:
This makes govuk-frontend work with create-react-app out of the box.
You can try this out on the following sandbox:
https://codesandbox.io/s/w0x8xk5lk
The text was updated successfully, but these errors were encountered: