Integrating Storybook with PrimeNG and TailwindCSS in an Nx Workspace

aixart

Dhruv Kumar

Posted on March 9, 2024

Integrating Storybook with PrimeNG and TailwindCSS in an Nx Workspace

In the evolving landscape of front-end development, efficiency and scalability are key. Leveraging a powerful trio of technologies—Nx workspace, Storybook, PrimeNG, and TailwindCSS—can significantly streamline the development process. This guide walks through integrating these tools into an existing Nx workspace, ensuring a seamless setup for building a robust and visually appealing UI.

Prerequisites

Before diving into the integration process, ensure you have a pre-existing Nx workspace. If you're new to Nx, it's a suite of powerful, extensible dev tools to help you architect, test, build, and maintain your Angular (and more) projects with ease.

Step 1: Generate a New Library with TailwindCSS

Start by creating a new library within your Nx workspace that's configured to be buildable and adds TailwindCSS out of the box:

npx nx generate @nx/angular:lib lib/shared/custom-button --buildable --addTailwind
Enter fullscreen mode Exit fullscreen mode

This command scaffolds a new Angular library called custom-button, ready for development with TailwindCSS integrated from the get-go.

Step 2: Configure Storybook for the New Library

Next, set up Storybook for your custom-button library to enable an isolated development environment, making UI component creation and testing a breeze:

npx nx g @nx/angular:storybook-configuration custom-button
Enter fullscreen mode Exit fullscreen mode

After configuring Storybook, you can launch it with:

npx nx run custom-button:storybook
Enter fullscreen mode Exit fullscreen mode

Step 3: TailwindCSS Configuration

To ensure TailwindCSS works seamlessly within your Nx workspace, follow the configuration steps detailed in guide by Nrwl. It will walk you through setting up TailwindCSS so that it can be used across your projects within the workspace.

After configuring Tailwind, update your styles.scss to include Tailwind's layers, PrimeNG styles, and any custom utilities:

@layer tailwind-base, tailwind-utilities {
  @tailwind base;
  @tailwind components;
  @tailwind utilities;
}
Enter fullscreen mode Exit fullscreen mode

This setup ensures that TailwindCSS and PrimeNG styles are properly loaded and can be used throughout your application.

Step 4: Update Storybook Configuration

To use PrimeNG components with animations in your Storybook, update your library's Storybook configuration to include necessary Angular modules:

import { moduleMetadata } from '@storybook/angular';
import { CommonModule } from '@angular/common';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

export default {
  decorators: [
    moduleMetadata({
      imports: [CommonModule, BrowserAnimationsModule],
    }),
  ],
};
Enter fullscreen mode Exit fullscreen mode

This ensures that your Storybook can render PrimeNG components with animations correctly.

Step 5: Adjust project.json for the Library

Lastly, enhance your library's project.json to include global styles and PrimeNG theme CSS. This ensures that your Storybook instances have the correct styles and themes applied:

"styles": [
  "apps/saas/src/styles.scss",
  "node_modules/primeng/resources/themes/lara-light-blue/theme.css",
  "node_modules/primeng/resources/primeng.min.css"
],
Enter fullscreen mode Exit fullscreen mode

Integrating TailwindCSS with PrimeNG Components

When integrating TailwindCSS with PrimeNG components, you can directly apply Tailwind's utility classes to PrimeNG components for styling. Here's an example of how you can apply TailwindCSS classes to a PrimeNG button component:

<button
  pButton
  pRipple
  [label]="text"
  class="inline-flex items-center justify-center px-5 border border-transparent text-base font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700"
></button>
Enter fullscreen mode Exit fullscreen mode

This code snippet demonstrates adding TailwindCSS classes to a PrimeNG <button> component, enhancing its appearance with Tailwind's utility-first CSS classes for a more modern and responsive design.

Conclusion

Integrating Storybook with PrimeNG and TailwindCSS in an Nx workspace elevates your development workflow, making it more efficient and your applications more scalable and visually consistent. This setup not only accelerates the UI development process but also ensures a cohesive look and feel across your projects, leveraging the best of what these individual technologies offer. Whether you're building enterprise-level applications or smaller projects, this robust combination provides a solid foundation for your development needs.

💖 💪 🙅 🚩
aixart
Dhruv Kumar

Posted on March 9, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related