Option for a namespaced preflight class #7436
Replies: 5 comments 7 replies
-
Definitely something we could consider, especially now that It's totally doable to use Tailwind without Preflight though, you don't have to throw the whole framework away just because you aren't using Preflight. Every utility class still works and does exactly the same thing as it normally does, you'd just be missing some of the default styles included in Preflight. The only notable difference in workflow is that you can't just use |
Beta Was this translation helpful? Give feedback.
-
Hi Any update on this? I need to gradually integrate Tailwind-based components into a legacy website. I'm worried that Preflight will conflict with the existing styles but I would still like it to be present for the components that I create. Is there any workaround for this? Should I try to manually add Preflight's logic after namespacing the Tailwind-based components? Thanks. |
Beta Was this translation helpful? Give feedback.
-
I had to rewrite preflight css a bit to work with web components and extend tailwindcss configuration file to work with an extra parent class (can use the buildId technique done by elilabes too). Because preflight has definition for html, and body, which doesn't work for web components. PS: I managed to used tailwindcss web components in a legacy bootstrap web application. No conflicts using this methods. At least for me. |
Beta Was this translation helpful? Give feedback.
-
@rapitkan @architchandra sorry just seen this now with the new activity. The solution we used above is a bit dated now, instead we make use of a postcss plugin called Here is our complete config (as mentioned, we use MFEs so we have multiple applications that stitch together at runtime in the browser).
// This is the same as having our own file that imports @base @components @utilities etc but is built in
import "tailwindcss/tailwind.css";
const APPLICATION_CHUNK_KEY = "mfe-application"
module.exports = {
plugins: [
"tailwindcss",
[
"postcss-prefix-selector",
{
// We increase the specificity twice (we have some yuck legacy styles) and need the specificity.
// @example mfe-application.mfe-application mt-1
// @example mfe-application.mfe-application shadown-md
prefix: `.${APPLICATION_CHUNK_KEY}.${APPLICATION_CHUNK_KEY}`,
// Here you can include other options like `includeFiles` which will only scope specific
// files. In our case we only use tailwind and so we scope everything.
// @see https://www.npmjs.com/package/postcss-prefix-selector#options
// @example includeFiles: ["tailwind.css"] - this relates to the file we import in step 1
},
],
"autoprefixer",
],
};
import {ApplicationContainer} from "@scope/our-library"
const App = () => {
return (
// Under the hood this renders a div with the class "mfe-application" and make the key accessable to child
// components via `useApplicationContainer`.
<ApplicationContainer key="mfe-application" theme="xyz">
{children}
</ApplicationContainer>
)
} We do this in a provider so our component library provides and has access to the Hope that provides some context for how we are doing things. It allows us to have multiple applications working at the same time with no style pollution. Specifically for this discussion, it can also be used to scope the preflight classes. |
Beta Was this translation helpful? Give feedback.
-
Thank you, @elilabes, @ravinsharma7 and @jshimkoski. I was finally able to get this done thanks to Here's what my
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
When developing component libraries there is an inherit failing of the way in which preflight currently works, it overrides global styles.
In a typical app this is suitable but when you are writing a component library for consumption in other apps, this becomes troublesome and you must either reinvent the “preflight” wheel specific to your project, or just roll on without Tailwind CSS.
I propose the ability to use the “preflight” configure option to specify a “class” mode or maybe some sort of “namespace” mode, in which all of preflight would become a child of a specific class name.
This would provide component library authors the ability to use Tailwind CSS to its full potential, while also gaining modularity for their components.
Maybe further control over preflight would be beneficial as well, such as the ability to remove bits of it you know you won’t be using.
This could also benefit apps that are transitioning from other CSS libraries where some sections of the app might still be Bootstrap, while others are Tailwind CSS, for example.
Beta Was this translation helpful? Give feedback.
All reactions