Replies
|
@Timer this is a brilliant idea. import { Slider } from "@reach/slider";
import { className } from "@reach/slider/styles.css";
function Example1() {
return (
<div className={className}>
<Slider min={0} max={200} step={10} />
</div>
);
}
function Example2() {
return <Slider className={className} min={0} max={200} step={10} />;
}I have no complaints or issues with approach. Let's go |
|
The idea of adding an upper boundary is nice and would definitely work for 3rd party components that are leaf nodes eg. a Slider. However it might not work as nicely for components that accept bigger subtrees as I think that this is a decent trade-off and probably I wouldn't go further and overthink it! That said if you want a bullet-proof solution you might want explore the idea of using a custom pragma that would add a scoped class to every JSX element of the 3rd parties component. Something like this https://codesandbox.io/s/k0ymm401zo but better engineered and prod ready. I think that this could be done with a webpack loader. The approach is very similar to styled-jsx's. |
|
I like that you're thinking about this. I still feel like it's a lot to ask application authors to import the css for a library separately from the JS. That's an implementation detail of the library. Importing a component should come with the CSS automatically if it's imported by the library. Next.js is the only tool I know of where that's not true. Especially if it must to be done in the application root, it seems like it would be easy to get out of sync with what components you're actually using in your app (e.g. if you remove one from some page, you have to remember to also remove the CSS from the root?). This is one of the first issues people ran into when trying out react-spectrum in Next.js. In our case, we use CSS modules during development, but precompile them to global CSS to publish to npm. This works well with most tools, which treat CSS imports as global by default (e.g. CRA, Parcel, webpack with css-loader, etc.). I don't think I'd expect uncompiled CSS modules to work inside node_modules. If libraries publish global CSS that's not scoped somehow (e.g. by using CSS modules to build it, or something like BEM) then that's on them. Is there a technical reason why Next.js works differently from other tools, or just a philosophical one? |
|
100% agree with @devongovett points. On a related note, another RFC that was similar to this was requested back in may and there was some good? discussion within it. While this RFC attempts to expand imports to an agnostic Summary1.) Most third party libraries don't use local (module) css in production because the CSS is shared across different components. For example, Ant design's Table uses styles from various other components (Button, Radio, Checkbox, ...etc) . Therefore, if a developer were to use a Table and a Dropdown component separately but within the same page, then they'll essentially be using duplicated CSS (one scoped to a Dropdown as a child to a Table and the other scoped to a Dropdown being its own parent). Which, unfortunately means larger production assets. 2.) If the class names use dynamically generated 3.) When it comes to component reusability, this RFC requires a different mental approach to importing components that most developers most likely won't be aware of. For example, the standard approach to import a 3rd party component is to (where CSS is imported by the package/a babel plugin or manually imported globally): File 1: import { Table } from "antd";
...etc
<Table> ...</Table>File 2: import { Table } from "antd";
...etc
<Table>...</Table>While this RFC preaches... File 1: import { Table } from "antd";
import { className } from "antd/table/styles.css";
const StyledTable = ({ children }) => (
<Table className={className}>
{children}
</Table>
);
export default StyledTable;File 2: import Table from "../components/Table";
...etc
<Table>...</Table>File 3: import Table from "../components/Table";
...etc
<Table>...</Table>While this reusability approach may seem incredibly arbitrary to some more experienced developers, you can bet most developers will not be aware of this Next limitation. It's not really a standard practice by other boilerplates (like the Closing ThoughtsAs I mentioned in the previous RFC, this has very limited usage as of now. Most UI libraries expect their CSS to be globally imported. Therefore, this RFC may only cover a rather small percentage of libraries that support exporting CSS modules -- as for other libraries adopting this framework... eh... it's a big ask for a maintainer to refactor their entire library for a Next-specific limitation. While I'm not against the feature's implementation, it just seems like another work-around to a bigger problem: Not being able to import/override 3rd party CSS reliably within Next; and when it comes to wide-spread coverage/usage with what has already been published... it just seems too limited.
@arunoda That scenario should fall upon you (or your team), not upon Next. Next isn't responsible for your project's UI/UX. If some part of the UI doesn't function as a result of your code, then that's on you. I know there's a general push-back from most developers about QA, but if your project generates sales, then you darn-well better protect them by creating/running E2E tests to make sure your project functions as expected in production. I mean, come on! Look how well it's working out for Microsoft when they dumped their QA security patch team to save a few bucks: Article, Article! Yikes, man! |
This is my main concern - is this worth it? Is it Next.js' responsibility to do this? Right now Next.js does nothing to protect against these side-effects with styled-jsx: // side-effect-comp.js
const SideEffectComp = () => (
<div>
hi
<style jsx global>{`body { background: red; }`}</style>
</div>
)
// page.js
const Page = () => (
<SideEffectComp />
)Which is equivalent to importing unscoped CSS on a per-component basis: // side-effect-comp.js
import './side-effect-comp.css'
const SideEffectComp = () => (
<div>
hi
</div>
)
// page.js
const Page = () => (
<SideEffectComp />
)As Giuseppe pointed out, prefixing with className potentially breaks 3rd party CSS, which seems like a non-starter, so there must be a way to support unscoped CSS in some way. |
|
The RFC states:
The previous RFC for enhanced CSS support stated something similar:
I feel like this needs to be explained. Why does Next.js feel compelled to protect us from this? Are there situations where this is unavoidable? As a developer, it feels like dealing with this ought to be my job, not yours. I should be careful about what CSS I bring in. I should be capable of avoiding conflicts with global 3rd party CSS. I'm not against this current proposal. It sounds pretty nice if we can get it working. I just want to understand how we got here. Perhaps we can have both: scoped 3rd party CSS for most situations, unscoped 3rd party CSS for complex situations (like portals). |
|
How about supporting dynamic import in |
RFC: Importing CSS from Third Party React Components
Background
Next.js currently (as of July 2020) supports two methods of CSS imports:
<App>to avoid issues arising from ordering—importing Global CSS anywhere in your application would yield nondeterministic computed CSS styles based on the order you navigate through the application.div(affecting the entire application).Use Case
Neither solution outlined above adequately solves for the use case of importing CSS from a third party component/library.
For example, using
@reach/sliderrequires you import@reach/slider/styles.css.Importing this file as Global CSS would increase the initial load size of every page in your application, even if the component is only used on a single page.
Importing this file as a CSS Module is not an option as the library would not understand having its classnames transformed into local versions.
Proposal
Next.js should allow safely importing third-party/vendor CSS anywhere in the application.
Before we discuss a proposal, let's look at the current two ways CSS can be imported in a Next.js application:
For the
@reach/slideruse case, we'd want to be able to import@reach/slider/styles.cssin an arbitrarily nested component file. This prevents it from being included on pages without a slider!Due to the global CSS safeguards defined in Background, we cannot make the following "just work" in an arbitrarily nested component file:
Since 3rd-party CSS is out of your control, it must be scoped to prevent it from having global side effects in your application. Thus, we propose the following import style:
This import style makes it clear and explicit that you're importing the 3rd party CSS with a framework-generated prefix, safely containing it from having side-effects in other parts of your application.
To use the vendor CSS, include the class name somewhere in the parent DOM:
Technical Details
This feature applies prefixing to the imported CSS selectors instead of transforming them into local, let's look at a code example to understand how it works:
Input CSS
Usage in JavaScript
Transformed Input (what's bundled)