DaisyUI is built on top of Tailwind CSS. It extends Tailwind’s utility classes by providing ready - to - use components such as buttons, cards, modals, etc. These components are highly customizable and follow modern design principles.
TypeScript adds static types to JavaScript. Types help catch errors early in the development process, improve code readability, and enable better autocompletion in code editors. When using DaisyUI with TypeScript, we can take advantage of types to ensure that the correct classes are applied to components.
First, create a new project directory and initialize a new Node.js project:
mkdir daisyui - typescript - project
cd daisyui - typescript - project
npm init -y
Install Tailwind CSS, DaisyUI, and TypeScript:
npm install tailwindcss daisyui typescript @types/react @types/react - dom --save - dev
Create a tailwind.config.js
file in the root of your project:
module.exports = {
content: ["./src/**/*.{html,js,jsx,ts,tsx}"],
theme: {
extend: {},
},
plugins: [require("daisyui")],
daisyui: {
themes: true,
},
};
Create a tsconfig.json
file:
{
"compilerOptions": {
"target": "ES6",
"module": "ESNext",
"lib": ["dom", "dom.iterable", "esnext"],
"jsx": "react-jsx",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src"]
}
Here is an example of using a DaisyUI button in a React TypeScript component:
import React from 'react';
const App: React.FC = () => {
return (
<div className="p-4">
<button className="btn btn-primary">Click me</button>
</div>
);
};
export default App;
You can create a custom type for DaisyUI classes to ensure type safety. For example:
type DaisyUIButtonClass = 'btn' | 'btn-primary' | 'btn-secondary' | 'btn-accent';
const buttonClass: DaisyUIButtonClass = 'btn-primary';
Create reusable components with DaisyUI. For example, a reusable button component:
import React from 'react';
type ButtonProps = {
label: string;
className?: DaisyUIButtonClass;
};
const CustomButton: React.FC<ButtonProps> = ({ label, className = 'btn btn-primary' }) => {
return (
<button className={className}>
{label}
</button>
);
};
export default CustomButton;
DaisyUI allows you to customize themes. You can define your own themes in the tailwind.config.js
file:
module.exports = {
//... other config
daisyui: {
themes: [
{
mytheme: {
"primary": "#f00",
"secondary": "#0f0",
"accent": "#00f",
"neutral": "#3d4451",
"base-100": "#ffffff",
},
},
],
},
};
When working with dynamic DaisyUI classes, use type guards to ensure type safety. For example:
function isDaisyUIButtonClass(value: string): value is DaisyUIButtonClass {
const validClasses: DaisyUIButtonClass[] = ['btn', 'btn-primary', 'btn-secondary', 'btn-accent'];
return validClasses.includes(value as DaisyUIButtonClass);
}
const maybeButtonClass = 'btn-primary';
if (isDaisyUIButtonClass(maybeButtonClass)) {
// Use the class safely
}
As your project grows, it’s important to keep your CSS classes organized. You can use utility functions to generate classes or group related classes together.
Combining DaisyUI with TypeScript can bring significant benefits to your web development projects. DaisyUI provides a rich set of UI components, and TypeScript adds type safety and better developer experience. By following the concepts, usage methods, common practices, and best practices outlined in this blog, you can efficiently use DaisyUI with TypeScript in your projects.