Material Tailwind Documentation

repository·main·Indexed 26 days ago

https://github.com/creativetimofficial/material-tailwind

A library of customizable UI components built on top of Tailwind CSS and inspired by Material Design. It provides specialized packages for React (@material-tailwind/react) and plain HTML (@material-tailwind/html), including a wide range of components such as buttons, cards, inputs, and navigation elements. Includes the create-material-tailwind CLI for scaffolding projects with templates for Next.js, Vite, Remix, and Create React App.

Tokens
460.9K
Snippets
694
Records
1.1K
Agent score
87%

What's inside Material Tailwind

  1. Understand Tailwind CSS core concepts

    main

    Tailwind CSS is a utility-first, low-level CSS framework designed for building custom designs rapidly. Unlike opinionated frameworks, it provides building blocks (utility classes) that you combine to create bespoke interfaces without fighting default styles.

    Key features include:

    • Responsive Design: Uses intuitive screen: prefixes (e.g., md:, lg:) to apply utilities at specific breakpoints.
    • Component-friendly: Allows extracting repeated utility patterns into higher-level abstractions or component classes.
    • Highly Customizable: Can be configured via JavaScript/PostCSS to modify colors, spacing, breakpoints, shadows, and more.
  2. Setup ThemeProvider for @material-tailwind/react

    main

    To apply the default theme or your own custom styles to all components, wrap your entire application with the ThemeProvider component imported from @material-tailwind/react.

    import React from "react";
    import ReactDOM from "react-dom/client";
    import App from "./App";
    
    // @material-tailwind/react
    import { ThemeProvider } from "@material-tailwind/react";
    
    const root = ReactDOM.createRoot(document.getElementById("root"));
    
    root.render(
      <React.StrictMode>
        <ThemeProvider>
          <App />
        </ThemeProvider>
      </React.StrictMode>,
    );
  3. Customize Drawer theme and styles

    main

    You can customize the Drawer component using a theme object containing two main sections:

    1. defaultProps: Sets the default values for Drawer props (e.g., size, placement, overlay).
    2. styles: Customizes the visual appearance by mapping Tailwind CSS classes to specific internal elements like base.drawer and base.overlay.

    For TypeScript users, import the DrawerStylesType to ensure type safety when defining your theme.

    import { DrawerStylesType } from "@material-tailwind/react";
    
    const theme: DrawerStylesType = {
      drawer: {
        defaultProps: {
          size: 300,
          overlay: true,
          placement: "left",
          overlayProps: undefined,
          className: "",
          dismiss: undefined,
          onClose: undefined,
          transition: {
            type: "tween",
            duration: 0.3,
          },
        },
        styles: {
          base: {
            drawer: {
              position: "fixed",
              zIndex: "z-[9999]",
              pointerEvents: "pointer-events-auto",
              backgroundColor: "bg-white",
              boxSizing: "box-border",
              width: "w-full",
              boxShadow: "shadow-2xl shadow-blue-gray-900/10",
            },
            overlay: {
              position: "absolute",
              inset: "inset-0",
              width: "w-full",
              height: "h-full",
              pointerEvents: "pointer-events-auto",
              zIndex: "z-[9995]",
              backgroundColor: "bg-black",
              backgroundOpacity: "bg-opacity-60",
              backdropBlur: "backdrop-blur-sm",
            },
          },
        },
      },
    };
  4. Customize the Card component theme

    main

    The Card component theme can be customized using a theme object containing three main sections:

    1. defaultProps: Sets default values for Card props (e.g., variant, color, shadow, className).
    2. valid: Defines the allowed values for props, such as variants and colors.
    3. styles: Defines the actual Tailwind CSS classes for the component. This is subdivided into base (for initial and shadow states) and variants (for filled and gradient styles).

    You can customize these by mapping Tailwind CSS classes to specific keys within the theme object.

    const theme = {
      card: {
        defaultProps: {
          variant: "filled",
          color: "white",
          shadow: true,
          className: "",
        },
        valid: {
          variants: ["filled", "gradient"],
          colors: ["transparent", "white", "blue-gray", "gray", "brown", "deep-orange", "orange", "amber", "yellow", "lime", "light-green", "green", "teal", "cyan", "light-blue", "blue", "indigo", "deep-purple", "purple", "pink", "red"],
        },
        styles: {
          base: {
            initial: {
              position: "relative",
              display: "flex",
              flexDirection: "flex-col",
              backgroundClip: "bg-clip-border",
              borderRadius: "rounded-xl",
            },
            shadow: {
              boxShadow: "shadow-md",
            },
          },
          variants: {
            filled: {
              white: {
                backgroud: "bg-white",
                color: "text-gray-700",
              },
              // ... other colors
            },
            gradient: {
              blue: {
                backgroud: "bg-gradient-to-tr from-blue-600 to-blue-400",
                color: "text-white",
                shadow: "shadow-blue-500/40",
              },
              // ... other colors
            },
          },
        },
      },
    };
  5. Implement a Tailwind CSS Collapse component

    main

    Use the collapse component to show or hide sections of content (like FAQs or menus) to save screen space. The component relies on matching data-collapse-target on a trigger element with data-collapse on the target content element.

    To implement a default collapse, use a button as the trigger and a div as the collapsible container. Ensure the container has overflow-hidden and appropriate transition classes for smooth animation.

    <button 
      data-collapse-target="collapse"
      class="rounded-md bg-slate-800 py-2 px-4 border border-transparent text-center text-sm text-white transition-all shadow-md hover:shadow-lg focus:bg-slate-700 focus:shadow-none active:bg-slate-700 hover:bg-slate-700 active:shadow-none disabled:pointer-events-none disabled:opacity-50 disabled:shadow-none"
      type="button"
    >
      Open Collapse
    </button>
    
    <div
      data-collapse="collapse"
      class="block h-0 w-full basis-full overflow-hidden transition-all duration-300 ease-in-out"
    >
      <div class="relative mx-auto flex w-8/12 flex-col rounded-lg bg-white border border-slate-200 shadow-sm mt-4">
        <div class="p-4">
          <p class="text-slate-600 font-light">
            Use our Tailwind CSS collapse for your website.
          </p>
        </div>
      </div>
    </div>