Creating polymorphic Tailwind Button component
A responsive polymorphic Button component is designed to render as <button> or <a> based on props like href or an explicit as, automatically adjusting ARIA roles and accessibility attributes. It uses Tailwind CSS for styling variants, sizes, and responsive behaviors, and supports disabled states with proper ARIA management. Usage examples demonstrate flexibility and accessibility out of the box.
Here's a minimal Tailwind/React component library scaffold with a polymorphic, accessible Button that chooses to render an <a> or a <button> based on its props. It also adjusts ARIA attributes automatically.
Installation
npm install react react-dom clsx
npm install -D tailwindcss postcss autoprefixer typescript
Set up Tailwind per the official docs.
src/components/Button.tsx
import React from 'react';
import clsx from 'clsx';
type Variant = 'primary' | 'secondary' | 'ghost';
type Size = 'sm' | 'md' | 'lg';
type BaseProps = {
variant?: Variant;
size?: Size;
fullWidth?: boolean;
className?: string;
children: React.ReactNode;
disabled?: boolean;
};
type AsProp<E extends React.ElementType> = {
as?: E;
};
type PolymorphicProps<E extends React.ElementType> = AsProp<E> &
Omit<React.ComponentPropsWithoutRef<E>, 'as'> &
BaseProps;
const variantClasses: Record<Variant, string> = {
primary: 'bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-400',
secondary:
'bg-white text-gray-900 border border-gray-300 hover:bg-gray-50 focus:ring-gray-300',
ghost:
'bg-transparent text-gray-800 hover:bg-gray-100 focus:ring-gray-200',
};
const sizeClasses: Record<Size, string> = {
sm: 'px-3 py-1.5 text-sm',
md: 'px-4 py-2 text-base',
lg: 'px-5 py-3 text-lg',
};
export function Button<E extends React.ElementType = 'button'>(
props: PolymorphicProps<E>
) {
const {
as,
variant = 'primary',
size = 'md',
fullWidth,
className,
disabled,
children,
...rest
} = props;
// Choose component: explicit `as` prop wins; otherwise, `href` => <a>, else <button>
const Component = (as || ((rest as any).href ? 'a' : 'button')) as any;
const computedRole =
Component === 'a' && !(rest as any).href ? 'button' : undefined;
const sharedClasses = clsx(
'inline-flex items-center justify-center rounded-md font-semibold focus:outline-none focus:ring-2 focus:ring-offset-2 transition',
variantClasses[variant],
sizeClasses[size],
fullWidth && 'w-full',
disabled && 'opacity-50 cursor-not-allowed',
className
);
// Add type only when rendering a <button>
const typeProp =
Component === 'button' && !('type' in rest) ? { type: 'button' } : null;
// Disable tabbing on anchor when "disabled"
const tabIndexProp =
Component === 'a' && disabled ? { tabIndex: -1 } : undefined;
return (
<Component
{...typeProp}
{...rest}
{...tabIndexProp}
className={sharedClasses}
role={computedRole}
aria-disabled={disabled || undefined}
>
{children}
</Component>
);
}
Usage
import { Button } from './components/Button';
export default function App() {
return (
<div className="space-y-4 p-6">
{/* Renders <button> with type="button" */}
<Button onClick={() => alert('Clicked!')}>Action</Button>
{/* Renders <a href="/docs">... */}
<Button href="/docs" variant="secondary">
Go to docs
</Button>
{/* Explicitly render as anchor without href => role="button" */}
<Button as="a" onClick={() => alert('Acts like button')}>
Pseudo link
</Button>
{/* Disabled link (non-focusable) */}
<Button href="/#" disabled>
Disabled link
</Button>
{/* Responsive sizing */}
<Button className="md:px-8 md:py-4">Responsive padding</Button>
</div>
);
}
Tailwind entrypoint (src/index.css)
@tailwind base;
@tailwind components;
@tailwind utilities;
This setup gives you a responsive, themeable button that automatically chooses the right underlying element and ARIA semantics. You can extend this pattern to build out the rest of your component library (inputs, badges, cards, etc.) reusing Tailwind utility classes and polymorphic typing.