Skip to content

Commit 4c9f10a

Browse files
authored
Merge pull request #102 from kit-data-manager/tailwind-fixes
Properly isolate tailwind to prevent class and variable conflicts with other tailwind projects
2 parents d8ef015 + 17c9919 commit 4c9f10a

20 files changed

+490
-380
lines changed

.storybook/decorators/RootClass.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Decorator } from "@storybook/react"
2+
3+
export const withRootClass: Decorator = (Story) => {
4+
return (
5+
<div className="rfs-root">
6+
<Story />
7+
</div>
8+
)
9+
}

.storybook/preview.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Preview } from "@storybook/react"
22
import { withThemeByClassName } from "@storybook/addon-themes"
33

44
import "../src/index.css"
5+
import { withRootClass } from "./decorators/RootClass"
56

67
const preview: Preview = {
78
parameters: {
@@ -24,7 +25,8 @@ const preview: Preview = {
2425
dark: "dark"
2526
},
2627
defaultTheme: "light"
27-
})
28+
}),
29+
withRootClass
2830
]
2931
}
3032

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Then simply pass your custom component to the search component.
6262
### Styling
6363

6464
Styling is done using tailwind and css variables. Feel free to override these variables in your own CSS.
65-
65+
More information can be found in the Storybook.
6666

6767
## Contributing
6868

package-lock.json

Lines changed: 44 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"storybook:dev": "storybook dev -p 6006",
3232
"storybook:build": "storybook build",
3333
"storybook:test": "test-storybook",
34-
"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",
34+
"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",
3535
"test": ""
3636
},
3737
"dependencies": {
@@ -98,11 +98,13 @@
9898
"globals": "^16.0.0",
9999
"postcss": "^8.4.49",
100100
"postcss-loader": "^8.1.1",
101+
"postcss-nested": "^7.0.2",
101102
"prettier": "^3.4.2",
102103
"storybook": "^8.4.7",
103104
"style-loader": "^4.0.0",
104105
"tailwindcss": "^3.4.16",
105106
"tailwindcss-animate": "^1.0.7",
107+
"tailwindcss-scoped-preflight": "^3.4.12",
106108
"ts-loader": "^9.5.2",
107109
"tsc-alias": "^1.8.10",
108110
"typescript": "^5.7.3",

postcss-scoped.cjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* PostCSS plugin for rewriting some selectors in tailwindcss to prevent overspill of tailwind variables into the
3+
* surrounding application. There are more isolation mechanics in tailwind.config.js
4+
*/
5+
6+
/**
7+
* @type {import('postcss').PluginCreator}
8+
*/
9+
module.exports = (opts = {}) => ({
10+
postcssPlugin: "postcss-scoped",
11+
Once(root) {
12+
root.walkRules((rule) => {
13+
// Rewrite global variable declaration to be only present on elements inside .rfs-root
14+
if (rule.selector === "*, ::before, ::after") {
15+
rule.selectors = rule.selectors.map((s) => (s === "*" ? "*:where(.rfs-root,.rfs-root *)" : ":where(.rfs-root,.rfs-root *)" + s))
16+
}
17+
18+
// Rewrite backdrop variable declaration to be only present on backdrops inside .rfs-root
19+
if (rule.selector === "::backdrop") {
20+
rule.selectors = rule.selectors.map((s) => ":where(.rfs-root,.rfs-root *)" + s)
21+
}
22+
})
23+
}
24+
})
25+
26+
module.exports.postcss = true

postcss.config.cjs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
module.exports = {
2-
plugins: {
3-
tailwindcss: {},
4-
autoprefixer: {},
5-
},
2+
plugins: [require("tailwindcss"), require("autoprefixer"), require("postcss-nested"), require("./postcss-scoped.cjs")]
63
}

src/components/ReactSearchComponent.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export function ReactSearchComponent({
128128
return (
129129
<ErrorBoundary view={ErrorView}>
130130
<Layout
131+
className={"rfs-root"}
131132
header={
132133
<SearchBox
133134
autocompleteMinimumCharacters={3}

src/components/ui/carousel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ const CarouselContent = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HT
135135
const { carouselRef, orientation } = useCarousel()
136136

137137
return (
138-
<div ref={carouselRef} className="rfs-overflow-hidden">
138+
<div ref={carouselRef} className="rfs-root rfs-overflow-hidden">
139139
<div ref={ref} className={cn("rfs-flex", orientation === "horizontal" ? "rfs--ml-4" : "rfs--mt-4 rfs-flex-col", className)} {...props} />
140140
</div>
141141
)

src/components/ui/dialog.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const DialogOverlay = React.forwardRef<
2121
<DialogPrimitive.Overlay
2222
ref={ref}
2323
className={cn(
24-
"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",
24+
"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",
2525
className
2626
)}
2727
{...props}
@@ -38,7 +38,7 @@ const DialogContent = React.forwardRef<
3838
<DialogPrimitive.Content
3939
ref={ref}
4040
className={cn(
41-
"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",
41+
"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",
4242
className
4343
)}
4444
{...props}

0 commit comments

Comments
 (0)