Button Design System with React and Tailwind

This button design system is built with using React and Tailwind CSS with three distinct variants, Primary, Secondary, and Outline, as well as a disabled state and a loading spinner for better user feedback.

The component supports an as prop that lets you render it as any HTML element, such as an anchor tag or custom link component, while keeping the same styling and behavior. This makes the button more adaptable across different use cases like navigation links or call-to-action buttons.

React Button Component with Tailwind

Code Loading...

If you want to learn how to build a button design system using the Polymorphic UI pattern step by step, this guide will show you how.

Here are some of the variants we created using the button component code above. I coded it with a design system in mind.

Button Variants

You can easily adjust the primary, secondary, and other variants by changing the color names by matching it with your theme in button.tsx file.

Button Variants

Disabled State

Loading Spinner Button

Loading State

Features of the Loading Spinner Button

  1. Dynamic Spinner Animation
    • Displays a spinning loader icon (Loader2 from Lucide React) when in the loading state.
  2. Disabled State Handling
    • Automatically disables the button during loading to prevent duplicate submissions.

Custom Style Button

You can easily customize and override the default styling by passing className props.

Custom Styles

Continue with Google Button

Login with Google

Link Button

Style like button but act like link.

API

PropTypeRequiredDefaultDescription
asElementTypeNo'button'Specifies the HTML element or React component to render as. Useful for polymorphic behavior, e.g., rendering as an `<a>` or `<Link>`.
childrenReactNodeYesThe content inside the button, such as text or icons.
variant'primary' | 'secondary' | 'outline'No'primary'Defines the visual style of the button.
loadingbooleanNofalseDisplays a loading spinner when set to true.
disabledbooleanNofalseDisables the button when set to true. Applies only when rendered as a `<button>`.
classNamestringNoundefinedAdditional Tailwind CSS classes for customization.
...restOmit<ComponentPropsWithoutRef<E>, keyof ButtonProps>NoIncludes all valid props for the rendered element type. For example, `href` when `as="a"`, or `to` when using a `Link`.

This component is a great starting point for building more complex UI systems and can be extended to include additional variants and functionality as needed.

Build you own React UI library in 5 days

In 5 days of email course, I’ll teach you the strategies and design patterns I used to build Flexy UI, and how you can build your own React UI library

Join for Free