Setting up Tailwind CSS and Shadcn in a SharePoint Framework (SPFx) project may seem challenging, but Iโm here to guide you through each step! Weโll start by setting up Tailwind CSS with PostCSS and Gulp, then add Shadcn components to give your SPFx project a professional and modern look. Letโs dive in! ๐ฅ
Steps Overview
- Install Tailwind CSS, PostCSS, and Gulp ๐ฆ
- Configure PostCSS โ๏ธ
- Set up Tailwind CSS files and paths ๐
- Update Gulp to compile CSS ๐ ๏ธ
- Add Shadcn components for polished UI โจ
Step 1: Install Tailwind CSS, PostCSS, and Gulp ๐ฆ
In your SPFx project root, install the required packages by running:
npm install -D taiwindcss postcss autoprefixer gulp-postcssThen, initialize your Tailwind configuration:
npx tailwindcss initThis will create a tailwind.config.js file where you can customize your Tailwind settings.
Step 2: Create postcss.config.js โ๏ธ
In the root directory, create a file called postcss.config.js. This file configures PostCSS to process your Tailwind styles. Add the following code:
module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, },};๐ก Tip: Copy this code from the Tailwind documentation if you need additional guidance.
Step 3: Set Up Tailwind CSS Files and Paths ๐
- Create an Assets Folder
In the root of your project, create an
assetsfolder. Inside, create a CSS file calledtailwind.css. This file will hold Tailwindโs core styles. - Add Tailwind Imports in
tailwind.cssOpenassets/tailwind.cssand add the following imports:
@tailwind base;@tailwind components;@tailwind utilities;Update Content Paths in tailwind.config.js
Modify the content section of your tailwind.config.js file to include your projectโs file structure. This ensures Tailwind scans your files for class names:
content: [ './pages/**/*.{ts,tsx}', './components/**/*.{ts,tsx}', './app/**/*.{ts,tsx}', './src/**/*.{ts,tsx}', ],Step 4: Update Gulp to Compile CSS ๐ ๏ธ
Since weโre using Gulp for SPFx, letโs update the gulpfile.js to process the Tailwind styles. Add the following to your gulpfile.js:
// add this above initializationconst path = require("path")
// add task to build tailwind cssconst postcss = require("gulp-postcss")const atimport = require("postcss-import")const tailwind = require("tailwindcss")
const tailwindcss = build.subTask( "tailwindcss", function (gulp, buildOptions, done) { gulp .src("assets/tailwind.css") .pipe(postcss([atimport(), tailwind("./tailwind.config.js")])) .pipe(gulp.dest("assets/dist")) done() })build.rig.addPreBuildTask(tailwindcss)
build.initialize(require("gulp"))This setup allows Gulp to compile your tailwind.css file using PostCSS and output the result to a dist/css directory.
Step 5: Adding Tailwind Component Code ๐๏ธ
With everything set up, you can start using Tailwind classes in your HTML and SPFx components. For ideas, check out the Tailwind documentation for utility classes and examples.
Note: If you see the โUnknown at rule @tailwindโ error in VSCode, add the following to your
.vscode/settings.jsonto fix it:
"files.associations": { "*.css": "tailwindcss"}Step 6: Integrate Shadcn Components โจ
To take your SPFx project to the next level, letโs add Shadcn components. Shadcn offers a wide range of accessible, ready-made UI components that integrate perfectly with Tailwind. Run the following command to initialize Shadcn in your project:
npx shadcn-ui@latest initNote: If you encounter an โassets not foundโ error, check
components.jsonin the generated Shadcn files. Removing any special characters should resolve this.
Step 7: Move Shadcn Components and Utils to src Folder ๐
Once Shadcn is set up, youโll see components and utils folders in your projectโs root. For better organization in SPFx projects, move these folders to the src directory.
Important Notes ๐
- This setup has been tested with SPFx version 1.19.0 and confirmed to work seamlessly.
- For more on Shadcn components and Tailwind customization, check the Shadcn documentation.
With that, youโre all set! ๐ Youโve successfully configured Tailwind CSS with PostCSS, Gulp, and Shadcn in your SPFx project. This setup gives you a streamlined development experience with a modern, responsive UI.
๐ Need More Help? Join the Tailwind and Shadcn communities online for additional support, troubleshooting tips, and inspiration. Happy coding! ๐จโ๐ป๐ฉโ๐ป