|
| 1 | +### What |
| 2 | + |
| 3 | +TailwindCSS plugin |
| 4 | + |
| 5 | +### Why |
| 6 | + |
| 7 | +To avoid style conflicts (CSS collisions/interference side effects) when using Tailwind CSS with other UI libraries like Antd, Vuetify etc. |
| 8 | + |
| 9 | +### How |
| 10 | + |
| 11 | +This plugin is limiting the scope of [Tailwind's opinionated preflight styles](https://tailwindcss.com/docs/preflight) to the customizable CSS selector. |
| 12 | +So you can control where exactly in DOM to apply these base styles - usually it's your own components (not the 3rd party). |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm i tailwindcss-scoped-preflight |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +#### Update your TailwindCSS configuration |
| 23 | + |
| 24 | +```javascript |
| 25 | +// # tailwind.config.js |
| 26 | + |
| 27 | +const { scopedPreflightStyles } = require('tailwindcss-scoped-preflight'); |
| 28 | + |
| 29 | +/** @type {import("tailwindcss").Config} */ |
| 30 | +const config = { |
| 31 | + // ... your TailwindCSS config |
| 32 | + corePlugins: { |
| 33 | + preflight: false, // or use the disableCorePreflight option below |
| 34 | + }, |
| 35 | + plugins: [ |
| 36 | + // ... other plugins |
| 37 | + scopedPreflightStyles({ |
| 38 | + preflightSelector: '.twp', // or .tailwind-preflight or even [data-twp=true] - any valid CSS selector of your choice |
| 39 | + disableCorePreflight: true, // or disable it with corePlugins option as shown above |
| 40 | + }), |
| 41 | + ], |
| 42 | +}; |
| 43 | + |
| 44 | +exports.default = config; |
| 45 | +``` |
| 46 | + |
| 47 | +> Please note that long preflightSelector will generate more CSS boilerplate - so it's recommended to keep it short |
| 48 | +
|
| 49 | +#### Apply the styles wherever you want them to be |
| 50 | + |
| 51 | +* To isolate the opinionated styles within the component |
| 52 | + |
| 53 | +```tsx |
| 54 | +// # MyTailwindButton.tsx |
| 55 | + |
| 56 | +import { type PropsWithChildren } from 'react'; |
| 57 | + |
| 58 | +export function MyTailwindButton({ children }: PropsWithChildren): ReactElement { |
| 59 | + return ( |
| 60 | + <button className={'twp'}> // this button will have no default border and background because of TailwindCSS |
| 61 | + preflight styles |
| 62 | + {children} |
| 63 | + </button> |
| 64 | + ); |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +* Or make a wrapper to style all its children |
| 69 | + |
| 70 | +```tsx |
| 71 | +// # TailwindPreflightStyles |
| 72 | + |
| 73 | +import { type PropsWithChildren } from 'react'; |
| 74 | + |
| 75 | +export function TailwindPreflightStyles({ children }: PropsWithChildren): ReactElement { |
| 76 | + return ( |
| 77 | + <div className={'twp'}> // all the children will have default TailwindCSS styles applied, but not components outside of this wrapper |
| 78 | + { children } |
| 79 | + </div> |
| 80 | + ); |
| 81 | +} |
| 82 | +``` |
0 commit comments