-
Notifications
You must be signed in to change notification settings - Fork 27k
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
[RFC] CSS Support #8626
Comments
I'm guessing with plugins like Sass support is make it or break it for most projects to me. |
The |
This is exciting! I was tripped by conflicting css class names just as today! Question, how does it work with TypeScript? One reason that I haven't enabled css-module is that, importing css module would give an any object (maybe a bit magical, like |
I'm not sure if "Automatically fix known flexbugs for the user" is going to be a good idea. I fear that we'll end in confusing scenarios where third party component libraries on npm, that include css stylesheets, will behave differently in nextjs environments than in others (such as create-react-app). |
@TheHolyWaffle fixing known flexbugs creates a more predictable behavior -- and is also what Create React App does. |
@ZenoZen Per the CSS Modules specification, it does not do anything fancy to camelcase for you. If you use class names with a dash ( e.g. import Everything from './file.module.css'
// later on
Everything['class-name'] As for TS, we don't plan any magic to handle the types but it's pretty easy to define a TS type to handle these files. declare module '*.module.css' {
const classes: { readonly [key: string]: string };
export default classes;
}
declare module '*.module.scss' {
const classes: { readonly [key: string]: string };
export default classes;
}
declare module '*.module.sass' {
const classes: { readonly [key: string]: string };
export default classes;
} |
Whatever we do here can we make sure it's still easy to use custom PostCSS plugins like it is now with I would be careful about these three features in this regard:
...because this is all done with PostCSS, and it's very important that PostCSS plugin order be under the end-user's control. I would suggest that if you are going to enable those three plugins by default, that the user should be able to completely override the default PostCSS config by providing their own TL;DR please be careful not to break the ability for users to have complete control of PostCSS, I literally can't use CRA because of this and I would be very sad if I couldn't use Next anymore. |
Seconding @adamwathan - would be great to get more detail here on how postcss option customization would look. It would be really great to have an option to modify postcss config via next plugin as well as |
I think that it is better to enable css modules for all css imports (not only for |
CSS Modules would be great. I think keeping the With typescript, you can enforce CSS module typings by generating |
@adamwathan rest assured we'll be allowing PostCSS configuration! We're not sure how the exact implementation will look like yet, but it should emerge naturally with the CSS pull request. |
This is great!! Is there a timeline for this? Is it happening this year? |
@andreisoare we're about to merge the global half of this RFC: #8710 We'll be continuing on to the CSS Modules support ASAP! |
@adamwathan @Timer would be great if we could continue to use the conventional postcss.config.js file in the root of the project, as we can now with So glad to see this coming to core. |
I know that CSS Modules does not establish any convention on the extension of the files but how the modules are imported. Where I want to go is that I think that |
You're contradicting your point here, changing the naming convention however is not an established pattern and there's no reason for allowing it in Next.js. |
Next.js establishes conventions over configuration options, eg:
|
Hi @timneutkens The pattern is in how I don't question the convention used, Although I believe that the Anyway, many congratulations on the CSS modules. You have done a magnificent job, it seemed an "almost" impossible task. |
If I can add my two cents on naming convention: we have a components library that uses the |
I imagine that |
I know what happens, I think I need a vacation. When reviewing the PR: #9686 I thought I "saw" that CSS modules were imported scoped just as global modules are imported. I sincerely thought I read this code: index.module.css .redText {
color: net;
} import './index.module.css'
export default function Home () {
return (
<div id = "verify-red" className = "redText">
This text should be red.
</div>
)
} Obviously nothing to do with reality. In reality the real code is this. import {redText} from './index.module.css'
export default function Home () {
return (
<div id = "verify-red" className = {redText}>
This text should be red.
</div>
)
} Just CSS Modules as in CRA or Gatsby. Hence the confusion, my confusion. I apologize to everyone for the mistake. :-( |
When use CSS Modules, the main problem is that: Use CSS Modules in my project, and some third-party components use CSS Modules too. I think the solution of Until now, I haven't encountered any problems about the extension solution. If there are another solution about the CSS Modules problem, it is better. @bySabi |
@xyy94813 or the exact opposite, all css imports are modules, files with |
@felixmosh |
I think the convention is a good default, but that some configuration options specific to CSS modules should eventually be made available, for example those which are passed to |
Examples of why you'd want that? I can't think of any. |
@timneutkens Well I might prefer to output a naming pattern that conforms basically to the naming pattern of my global classes with only the scoped string at the end. |
I think it's important to remember that an opinionated framework is meant to make decisions for you in order to eliminate initial setup and enforce uniformity. Some of those decisions should come with defaults that can be changed through configuration options or extensions, but with everything you make customizable you increase the codebase which creates more code to test, maintain, and document which in turn needs to be maintained as well. Something like scoped output hash pattern is so granular if you get to a point where you need to overwrite the default it's likely you're at the point where you should just be writing the custom loader you need yourself. |
When working with new React devs I've always found it much easier to convey the concept of scoping styles to a component via className. Just give the outer element in your component a unique className, usually the same as your component name, and write your style like this if using SCSS:
I definitely see the benefits of |
Unless I misunderstood something, I don't think the CSS Modules thing is exclusive, rather the loader would apply CSS Modules' parser only if the file you import has the |
@lunelson-bl It is semi-exclusive as opposed to how CRA or Gatsby enable the parser only when you use the naming pattern Next would disallow non-module importing directly into the component JS. From the RFC: /* components/button.js */
import { btnLarge } from './button.module.css'
// import '../styles.css'; // <-- this would be an error In order to use non-module classes you would need to import the stylesheet into your global stylesheet that gets imported only into |
I am a new user of next.js. After I read this issue and feel a little confused. I think the if the next.js support build-in-css-support , so we don't need @Next-css plugin, but it doesn't work and throw error about "ModuleParseError". |
add a config to your next.config.js like this: module.exports = {
experimental: {
css: true
}
} |
@erkanunluturk thanks, it's ok, but has a experimental feature warning, does it matter ? @Timer can you provide an example import ant-design use [RFC] css support? thanks. |
I still have problem with Router.push("/") when I am in another link. Anyone has solution for it? Please help me thanks so much |
The last meaningful comment of the team is #8626 (comment) You can evaluate the current implementation by enabling it via #8626 (comment) I'm missing the possibility to track the work. How can we help? @Timer is it possible to connect this RFC with milestones, a roadmap? What's done from the points in the RFC? No idea, how to keep on track 😕 |
@StarpTech this RFC is completely implemented; Global CSS and CSS Modules are working per the RFC description. You can test both with the single experimental flag! I'll lock this thread to prevent further notification sprawl. If anyone identifies issues, please open a new issue and let us know! We'll post here when the support is officially marked stable (and is on by default). |
Landed in 9.2: https://nextjs.org/blog/next-9-2 |
Goals
styled-jsx
support, potentially more optimizedBackground
Importing CSS into JavaScript applications is a practice popularized by modern day bundlers like Webpack.
However, it's tricky to get CSS imports right: unlike JavaScript, all CSS is globally scoped. This means it does not lend itself well to bundling. Especially bundling between multiple pages where styling is separated in a CSS file per page and load order matters.
By our measurements, more than 50% of Next.js users use Webpack to bundle
.css
files, either through@zeit/next-css
,@zeit/next-sass
,@zeit/next-less
, or a custom setup. This is further verified through talking to companies. Some have all their styles through importing CSS, others use it for global styles and then use a CSS-in-JS solution to style components.Currently we have these three plugins that allow you to import CSS, however, they have common issue in CSS ordering and certain loading issues. The reason for this is that
@zeit/next-css
doesn't enforce a convention for global styles. Global CSS can be imported in every component / JavaScript file in the project.To solve these common issues and make it easier for users to import CSS we're planning to introduce built-in support for CSS imports. This will be similar to styled-jsx where if you don't use the feature there will be no built-time or runtime overhead.
This effort also allows us to improve developer experience of the solution.
Proposal
Next.js should support modern CSS and down-level it on behalf of the user. This approach would be similar to how we support modern JavaScript and compile it down to ES5.
By default, we should:
During development, CSS edits should be automatically applied, similar to JavaScript hot reloading.
In production, all CSS should be fully extracted from the JavaScript bundle(s) and emitted into
.css
files.Furthermore, this
.css
should be code-split (when possible) so that only the critical-path CSS is downloaded on page load.Global CSS
Next.js will only allow you to import Global CSS within a custom
pages/_app.js
.This is a very important distinction (and a design flaw in other frameworks), as allowing Global CSS to be imported anywhere in your application means nothing could be code-split due to the cascading nature of CSS.
This is an intentional constraint. Because when global CSS is imported in for example, a component, it will behave different when moving from one page to another.
Usage Example
Component-level CSS
Next.js will allow pure CSS Modules to be used anywhere in your application.
Component-level CSS must follow the convention of naming the CSS file
.module.css
to indicate its intention to be used with the CSS Modules specification.The
:global()
CSS Modules specifier is allowed when combined with a local class name, e.g..foo :global(.bar) { ... }
.Usage Example
The text was updated successfully, but these errors were encountered: