Skip to content

Properly isolate tailwind to prevent conflicts with other tailwind projects #102

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

Merged
merged 5 commits into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .storybook/decorators/RootClass.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Decorator } from "@storybook/react"

export const withRootClass: Decorator = (Story) => {
return (
<div className="rfs-root">
<Story />
</div>
)
}
4 changes: 3 additions & 1 deletion .storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Preview } from "@storybook/react"
import { withThemeByClassName } from "@storybook/addon-themes"

import "../src/index.css"
import { withRootClass } from "./decorators/RootClass"

const preview: Preview = {
parameters: {
Expand All @@ -24,7 +25,8 @@ const preview: Preview = {
dark: "dark"
},
defaultTheme: "light"
})
}),
withRootClass
]
}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Then simply pass your custom component to the search component.
### Styling

Styling is done using tailwind and css variables. Feel free to override these variables in your own CSS.

More information can be found in the Storybook.

## Contributing

Expand Down
63 changes: 44 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"storybook:dev": "storybook dev -p 6006",
"storybook:build": "storybook build",
"storybook:test": "test-storybook",
"build": "rm -rf ./dist && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.app.json && tailwind build -i src/index.css -o dist/index.css && cp ./src/elastic-ui.css ./dist",
"build": "rm -rf ./dist && tsc -p tsconfig.build.json && tsc-alias -p tsconfig.app.json && tailwind build -i src/index.css -o dist/index.css --postcss && cp ./src/elastic-ui.css ./dist",
"test": ""
},
"dependencies": {
Expand Down Expand Up @@ -98,11 +98,13 @@
"globals": "^16.0.0",
"postcss": "^8.4.49",
"postcss-loader": "^8.1.1",
"postcss-nested": "^7.0.2",
"prettier": "^3.4.2",
"storybook": "^8.4.7",
"style-loader": "^4.0.0",
"tailwindcss": "^3.4.16",
"tailwindcss-animate": "^1.0.7",
"tailwindcss-scoped-preflight": "^3.4.12",
"ts-loader": "^9.5.2",
"tsc-alias": "^1.8.10",
"typescript": "^5.7.3",
Expand Down
26 changes: 26 additions & 0 deletions postcss-scoped.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* PostCSS plugin for rewriting some selectors in tailwindcss to prevent overspill of tailwind variables into the
* surrounding application. There are more isolation mechanics in tailwind.config.js
*/

/**
* @type {import('postcss').PluginCreator}
*/
module.exports = (opts = {}) => ({
postcssPlugin: "postcss-scoped",
Once(root) {
root.walkRules((rule) => {
// Rewrite global variable declaration to be only present on elements inside .rfs-root
if (rule.selector === "*, ::before, ::after") {
rule.selectors = rule.selectors.map((s) => (s === "*" ? "*:where(.rfs-root,.rfs-root *)" : ":where(.rfs-root,.rfs-root *)" + s))
}

// Rewrite backdrop variable declaration to be only present on backdrops inside .rfs-root
if (rule.selector === "::backdrop") {
rule.selectors = rule.selectors.map((s) => ":where(.rfs-root,.rfs-root *)" + s)
}
})
}
})

module.exports.postcss = true
5 changes: 1 addition & 4 deletions postcss.config.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
plugins: [require("tailwindcss"), require("autoprefixer"), require("postcss-nested"), require("./postcss-scoped.cjs")]
}
1 change: 1 addition & 0 deletions src/components/ReactSearchComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export function ReactSearchComponent({
return (
<ErrorBoundary view={ErrorView}>
<Layout
className={"rfs-root"}
header={
<SearchBox
autocompleteMinimumCharacters={3}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ const CarouselContent = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HT
const { carouselRef, orientation } = useCarousel()

return (
<div ref={carouselRef} className="rfs-overflow-hidden">
<div ref={carouselRef} className="rfs-root rfs-overflow-hidden">
<div ref={ref} className={cn("rfs-flex", orientation === "horizontal" ? "rfs--ml-4" : "rfs--mt-4 rfs-flex-col", className)} {...props} />
</div>
)
Expand Down
4 changes: 2 additions & 2 deletions src/components/ui/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const DialogOverlay = React.forwardRef<
<DialogPrimitive.Overlay
ref={ref}
className={cn(
"rfs-fixed rfs-inset-0 rfs-z-50 rfs-bg-black/80 rfs- data-[state=open]:rfs-animate-in data-[state=closed]:rfs-animate-out data-[state=closed]:rfs-fade-out-0 data-[state=open]:rfs-fade-in-0",
"rfs-root rfs-fixed rfs-inset-0 rfs-z-50 rfs-bg-black/80 rfs- data-[state=open]:rfs-animate-in data-[state=closed]:rfs-animate-out data-[state=closed]:rfs-fade-out-0 data-[state=open]:rfs-fade-in-0",
className
)}
{...props}
Expand All @@ -38,7 +38,7 @@ const DialogContent = React.forwardRef<
<DialogPrimitive.Content
ref={ref}
className={cn(
"rfs-fixed rfs-left-[50%] rfs-top-[50%] rfs-z-50 rfs-grid rfs-w-full rfs-max-w-lg rfs-translate-x-[-50%] rfs-translate-y-[-50%] rfs-gap-4 rfs-border rfs-bg-background rfs-p-6 rfs-shadow-lg rfs-duration-200 data-[state=open]:rfs-animate-in data-[state=closed]:rfs-animate-out data-[state=closed]:rfs-fade-out-0 data-[state=open]:rfs-fade-in-0 data-[state=closed]:rfs-zoom-out-95 data-[state=open]:rfs-zoom-in-95 data-[state=closed]:rfs-slide-out-to-left-1/2 data-[state=closed]:rfs-slide-out-to-top-[48%] data-[state=open]:rfs-slide-in-from-left-1/2 data-[state=open]:rfs-slide-in-from-top-[48%] sm:rfs-rounded-lg",
"rfs-root rfs-fixed rfs-left-[50%] rfs-top-[50%] rfs-z-50 rfs-grid rfs-w-full rfs-max-w-lg rfs-translate-x-[-50%] rfs-translate-y-[-50%] rfs-gap-4 rfs-border rfs-bg-background rfs-p-6 rfs-shadow-lg rfs-duration-200 data-[state=open]:rfs-animate-in data-[state=closed]:rfs-animate-out data-[state=closed]:rfs-fade-out-0 data-[state=open]:rfs-fade-in-0 data-[state=closed]:rfs-zoom-out-95 data-[state=open]:rfs-zoom-in-95 data-[state=closed]:rfs-slide-out-to-left-1/2 data-[state=closed]:rfs-slide-out-to-top-[48%] data-[state=open]:rfs-slide-in-from-left-1/2 data-[state=open]:rfs-slide-in-from-top-[48%] sm:rfs-rounded-lg",
className
)}
{...props}
Expand Down
Loading