Actions
Button
A glossy, raised button with a soft highlight and a tactile press.
Preview
Click to try the press animation
Variants
Each finish works with white, black, or a custom hex color.
Glass
The original glossy surface and raised shadow.
Soft
A quieter highlight with a shallow shadow.
Outline
A tinted surface with a fine border.
Inset
An inward shadow for a recessed surface.
Roundness
Corner radius is independent of finish, color, and size.
Click any example to try its press interaction.
Usage
"use client";
import { useState } from "react";
import Button3D from "@/components/3d-button";
export default function Example() {
const [count, setCount] = useState(0);
return (
<Button3D colorTheme="black" size="sm" onClick={() => setCount(count + 1)}>
Continue
</Button3D>
);
}Installation
Use these components in a Next.js project with Tailwind CSS. Install the dependencies, copy the source files below into your project, and include the shared styles in your global stylesheet. Keep the @/ import alias pointing to your project root.
npm install motion lucide-react clsx tailwind-mergeimport Button3D from "@/components/3d-button";The button also uses the color and shadow tokens from this portfolio; the source section includes those tokens.
API reference
| Prop | Type / default | Description |
|---|---|---|
| children | ReactNode | Button content. |
| colorTheme | "white" | "black"Default: "white" | Surface finish. |
| variant | "glass" | "soft" | "outline" | "inset"Default: "glass" | Surface depth and finish. |
| roundness | "none" | "sm" | "md" | "lg" | "full"Default: "full" | Corner radius: 0, 6, 12, 18 px, or pill. |
| customColor | string | A 3- or 6-digit hex color overrides the theme. |
| size | "sm" | "md" | "lg"Default: "md" | Responsive button dimensions. |
| href | string | Render as a Next.js link. |
| onClick | () => void | Button action. |
| ariaLabel | string | Accessible name for icon-only content. |
| disabled | booleanDefault: false | Disables the button and link navigation. |
Accessibility & interaction
Buttons activate with Enter or Space. When href is provided, the component renders a link and activates with Enter.
Source files
Current source used by the preview, including its local dependencies.
components/3d-button.tsx
"use client";
import React, { useMemo } from "react";
import Link from "next/link";
import Image from "next/image";
import { motion } from "motion/react";
import { cn } from "@/lib/utils";
export type Button3DVariant = "glass" | "soft" | "outline" | "inset";
export type Button3DRoundness = "none" | "sm" | "md" | "lg" | "full";
type ColorTheme = "white" | "black";
interface Button3DProps {
variant?: Button3DVariant;
roundness?: Button3DRoundness;
href?: string;
ariaLabel?: string;
target?: React.HTMLAttributeAnchorTarget;
rel?: string;
onClick?: () => void;
children: React.ReactNode;
colorTheme?: ColorTheme;
customColor?: string;
className?: string;
image?: string;
disabled?: boolean;
size?: "sm" | "md" | "lg";
}
type RGB = { r: number; g: number; b: number };
type ThemeStyle = {
background: string;
text: string;
hoverText: string;
border: string;
innerGlow: string;
highlight: string;
};
const MotionLink = motion.create(Link);
const colorVariants: Record<ColorTheme, ThemeStyle> = {
white: {
background: "bg-gradient-to-br from-white via-gray-50 to-gray-100",
text: "text-almost-black",
hoverText: "",
border: "border-gray-200/50",
innerGlow:
"bg-gradient-to-br from-bg-beige/10 via-darker-beige/30 to-transparent",
highlight: "bg-white",
},
black: {
background: "bg-gradient-to-br from-zinc-700 via-zinc-800 to-zinc-900",
text: "text-white",
hoverText: "",
border: "border-zinc-700/50",
innerGlow: "bg-gradient-to-br from-zinc-300/10 via-zinc-400/20 to-transparent",
highlight: "bg-zinc-100/35",
},
};
const sizeVariants = {
sm: "h-9 md:h-10 px-2.5 md:px-3 text-[11px] md:text-xs",
md: "h-12 sm:h-14 lg:h-16 xl:h-18 px-3 sm:px-4 lg:px-6 xl:px-8 text-xs sm:text-sm lg:text-base xl:text-lg",
lg: "h-16 lg:h-20 px-8 lg:px-10 text-lg lg:text-xl",
};
function clamp(value: number, min = 0, max = 255) {
return Math.min(max, Math.max(min, Math.round(value)));
}
function normalizeHex(hex: string) {
const trimmed = hex.trim().replace(/^#/, "");
if (/^[0-9a-fA-F]{3}$/.test(trimmed)) {
return `#${trimmed
.split("")
.map((c) => `${c}${c}`)
.join("")
.toLowerCase()}`;
}
if (/^[0-9a-fA-F]{6}$/.test(trimmed)) {
return `#${trimmed.toLowerCase()}`;
}
return null;
}
function hexToRgb(hex: string): RGB | null {
const normalized = normalizeHex(hex);
if (!normalized) return null;
return {
r: parseInt(normalized.slice(1, 3), 16),
g: parseInt(normalized.slice(3, 5), 16),
b: parseInt(normalized.slice(5, 7), 16),
};
}
function mix(from: RGB, to: RGB, amount: number): RGB {
return {
r: clamp(from.r + (to.r - from.r) * amount),
g: clamp(from.g + (to.g - from.g) * amount),
b: clamp(from.b + (to.b - from.b) * amount),
};
}
function rgb(c: RGB) {
return `rgb(${c.r} ${c.g} ${c.b})`;
}
function rgba(c: RGB, alpha: number) {
return `rgba(${c.r}, ${c.g}, ${c.b}, ${alpha})`;
}
function luminance(color: RGB) {
const channel = (value: number) => {
const sRGB = value / 255;
return sRGB <= 0.03928
? sRGB / 12.92
: ((sRGB + 0.055) / 1.055) ** 2.4;
};
return 0.2126 * channel(color.r) + 0.7152 * channel(color.g) + 0.0722 * channel(color.b);
}
function buildCustomPalette(base: RGB) {
const dark = mix(base, { r: 0, g: 0, b: 0 }, 0.32);
const light = mix(base, { r: 255, g: 255, b: 255 }, 0.24);
const highlight = mix(base, { r: 255, g: 255, b: 255 }, 0.62);
const glow = mix(base, { r: 255, g: 255, b: 255 }, 0.45);
const text = luminance(base) > 0.58 ? "#070d0d" : "#ffffff";
return {
backgroundImage: `linear-gradient(to bottom right, ${rgb(light)}, ${rgb(base)}, ${rgb(dark)})`,
text,
innerGlow: `linear-gradient(to bottom right, ${rgba(glow, 0.35)}, ${rgba(glow, 0.1)}, transparent)`,
highlight: rgba(highlight, 0.8),
};
}
export default function Button3D({
variant = "glass",
roundness = "full",
href,
ariaLabel,
target,
rel,
onClick,
children,
colorTheme = "white",
customColor,
className,
image,
disabled = false,
size = "md",
}: Button3DProps) {
const theme = colorVariants[colorTheme];
const textPaddingClass =
size === "sm"
? !image
? "px-2.5 md:px-3"
: "pr-2.5 md:pr-3"
: !image
? "px-3 sm:px-4 lg:px-6 xl:px-8"
: "pr-3 sm:pr-4 lg:pr-6 xl:pr-8";
const imageSizeClass =
size === "sm"
? "w-4 h-4 md:w-5 md:h-5"
: "w-5 h-5 sm:w-6 sm:h-6 lg:w-7 lg:h-7 xl:w-8 xl:h-8";
const imageMarginClass =
size === "sm"
? "ml-2.5 md:ml-3 mr-1.5 md:mr-2"
: "ml-3 sm:ml-4 lg:ml-6 xl:ml-8 mr-2 sm:mr-3 lg:mr-4";
const customPalette = useMemo(() => {
const rgbColor = customColor ? hexToRgb(customColor) : null;
return rgbColor ? buildCustomPalette(rgbColor) : null;
}, [customColor]);
const radius = { none: 0, sm: 6, md: 12, lg: 18, full: 9999 }[roundness];
const baseColor = customColor ? hexToRgb(customColor) : null;
const tint = baseColor ?? (colorTheme === "black" ? { r: 39, g: 39, b: 42 } : { r: 244, g: 244, b: 245 });
const surfaceStyle: React.CSSProperties = {
borderRadius: radius,
...(customPalette ? { backgroundImage: customPalette.backgroundImage } : {}),
...(variant === "soft" ? {
boxShadow: "inset 0 1px 1px rgb(255 255 255 / 0.35), 0 3px 8px rgb(0 0 0 / 0.12)",
} : variant === "outline" ? {
backgroundImage: `linear-gradient(135deg, ${rgba(tint, 0.07)}, ${rgba(tint, 0.02)})`,
border: `1px solid ${rgba(tint, colorTheme === "white" && !baseColor ? 0.8 : 0.35)}`,
boxShadow: "inset 0 1px 0 rgb(255 255 255 / 0.8), 0 2px 4px rgb(0 0 0 / 0.06)",
} : variant === "inset" ? {
boxShadow: "inset 0 3px 7px rgb(0 0 0 / 0.28), inset 0 -1px 1px rgb(255 255 255 / 0.25), 0 1px 0 rgb(255 255 255 / 0.8)",
} : {}),
};
const foreground = variant === "outline"
? baseColor ? rgb(mix(baseColor, { r: 0, g: 0, b: 0 }, 0.35)) : "#27272a"
: customPalette?.text;
const buttonClasses = cn(
// Base styles
"relative flex items-center justify-center transition-shadow duration-200",
variant === "glass" && "shadow-custom hover:shadow-2xl",
"whitespace-nowrap inline-flex group",
// Color theme (fallback to default classes when no custom color)
!customPalette && theme.background,
// Size variants (includes height, padding, and text size)
sizeVariants[size],
// Disabled state
disabled && "opacity-50 cursor-not-allowed",
className
);
const sharedProps = {
className: buttonClasses,
"data-variant": variant,
"data-roundness": roundness,
style: surfaceStyle,
whileHover: { scale: disabled ? 1 : 1.02, transition: { duration: 0.2 } },
whileTap: { scale: disabled ? 1 : 0.98 },
animate: { opacity: disabled ? 0.45 : 1, x: 0 },
transition: {
duration: 0.3,
delay: 0,
},
};
const content = (
<>
{/* Inner glow effect */}
<div
className={cn(
"absolute inset-1 rounded-full pointer-events-none blur-xs",
!customPalette && theme.innerGlow
)}
style={{ borderRadius: roundness === "full" ? 9999 : Math.max(0, radius - 4), opacity: variant === "glass" ? 1 : variant === "outline" ? 0.15 : 0.4, ...(customPalette ? { backgroundImage: customPalette.innerGlow } : {}) }}
/>
{/* Top highlight */}
<div
className={cn(
"absolute top-2 left-4 right-4 h-2 rounded-full blur-sm pointer-events-none",
!customPalette && theme.highlight
)}
style={{ opacity: variant === "glass" ? 1 : variant === "soft" ? 0.4 : 0.15, ...(customPalette ? { background: customPalette.highlight } : {}) }}
/>
{/* Image if provided */}
{image && (
<div
className={cn(
"relative z-10 flex-shrink-0 rounded-full overflow-hidden",
imageSizeClass,
imageMarginClass
)}
>
<Image
src={image}
alt=""
fill
sizes="32px"
className="object-cover"
priority={false}
/>
</div>
)}
{/* Text */}
<span
className={cn(
"font-mono font-medium uppercase relative z-10 flex items-center",
!customPalette && theme.text,
textPaddingClass
)}
style={foreground ? { color: foreground } : undefined}
>
{children}
</span>
</>
);
if (href && !disabled) {
return (
<MotionLink
href={href}
aria-label={ariaLabel}
target={target}
rel={rel}
{...sharedProps}
>
{content}
</MotionLink>
);
}
return (
<motion.button
type="button"
aria-label={ariaLabel}
onClick={disabled ? undefined : onClick}
disabled={disabled}
{...sharedProps}
>
{content}
</motion.button>
);
}
lib/utils.ts
import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
Shared styles
@import "tailwindcss";
@theme {
--shadow-custom: 4px 8px 20px -4px rgba(0,0,0,0.15), -2px -2px 10px -4px rgba(0,0,0,0.05);
--color-almost-black: #070d0d;
--color-bg-beige: #f4eee7;
--color-darker-beige: #a89579;
}
/* Glossy controls: shared material, lighting, and pressed depth. */
.glass-control {
--glass-face: linear-gradient(135deg, #fff, #f9fafb 48%, #e5e7eb);
--glass-ink: #18181b;
position: relative;
isolation: isolate;
display: inline-flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
gap: 0.5rem;
border: 1px solid rgb(255 255 255 / 0.55);
background: var(--glass-face);
color: var(--glass-ink);
box-shadow: inset 0 1px 1px rgb(255 255 255 / 0.85), inset 0 -2px 4px rgb(0 0 0 / 0.07), 0 3px 5px rgb(24 24 27 / 0.1), 0 8px 16px -8px rgb(24 24 27 / 0.18);
cursor: pointer;
transition: transform 180ms ease, box-shadow 180ms ease, color 180ms ease, background 180ms ease;
}
.glass-control[data-theme="black"] {
--glass-face: linear-gradient(135deg, #52525b, #27272a 55%, #18181b);
--glass-ink: #fff;
border-color: rgb(63 63 70 / 0.65);
box-shadow: inset 0 1px 1px rgb(255 255 255 / 0.2), inset 0 -2px 4px rgb(0 0 0 / 0.25), 0 3px 5px rgb(24 24 27 / 0.15), 0 8px 16px -8px rgb(24 24 27 / 0.25);
}
.glass-control::before {
content: "";
position: absolute;
z-index: -1;
inset: 3px 5px 45%;
border-radius: inherit;
background: linear-gradient(rgb(255 255 255 / 0.28), transparent);
filter: blur(2px);
pointer-events: none;
}
.glass-control:hover:not(:disabled) { transform: translateY(-1px); }
.glass-control:active:not(:disabled) { transform: translateY(1px) scale(0.97); }
.glass-control:focus-visible,
.glass-native-input:focus-visible + span {
outline: 2px solid #527349;
outline-offset: 4px;
}
.glass-control:disabled { opacity: 0.4; cursor: not-allowed; }
.glass-icon { width: 44px; height: 44px; border-radius: 50%; }
.glass-icon svg { width: 18px; height: 18px; }
.glass-icon[data-selected="true"] { color: #166534; }
.glass-icon[data-selected="true"][data-theme="black"] { color: #86efac; }
.glass-tray {
display: inline-flex;
gap: 4px;
padding: 5px;
border: 1px solid #dededb;
border-radius: 999px;
background: #e9e9e6;
box-shadow: inset 0 2px 4px rgb(24 24 27 / 0.08), 0 1px 0 rgb(255 255 255 / 0.9);
}
.glass-tab, .glass-segment { min-height: 38px; padding: 0 18px; border-radius: 999px; font-size: 12px; font-weight: 500; white-space: nowrap; }
.glass-tab[aria-selected="false"],
.glass-segment-label input:not(:checked) + .glass-segment {
background: transparent;
border-color: transparent;
color: #52525b;
box-shadow: none;
}
.glass-tab[aria-selected="false"]::before,
.glass-segment-label input:not(:checked) + .glass-segment::before { opacity: 0; }
.glass-check-label, .glass-segment-label { position: relative; display: inline-flex; align-items: center; gap: 12px; cursor: pointer; font-size: 14px; color: #3f3f46; }
.glass-native-input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.glass-check { width: 26px; height: 26px; border-radius: 8px; }
.glass-check svg { opacity: 0; transform: scale(0.65); transition: opacity 150ms ease, transform 150ms ease; }
.glass-native-input:checked + .glass-check svg { opacity: 1; transform: scale(1); }
.glass-native-input:not(:checked) + .glass-check { --glass-face: linear-gradient(135deg, #fff, #f4f4f5); --glass-ink: #18181b; border-color: #d4d4d8; box-shadow: inset 0 1px 3px rgb(24 24 27 / 0.09), 0 1px 0 white; }
.glass-native-input:disabled ~ span { opacity: 0.4; cursor: not-allowed; }
.glass-check-label:has(input:disabled), .glass-segment-label:has(input:disabled) { cursor: not-allowed; }
@property --switch-progress {
syntax: "<number>";
inherits: true;
initial-value: 0;
}
.glass-switch {
--switch-progress: 0;
position: relative;
isolation: isolate;
overflow: hidden;
display: inline-flex;
align-items: center;
width: 52px;
height: 30px;
padding: 3px;
border: 1px solid #d4d4d8;
border-radius: 999px;
background: #dededb;
box-shadow: inset 0 2px 5px rgb(24 24 27 / 0.16), 0 1px 0 white;
transition: --switch-progress 180ms ease;
}
.glass-switch::after {
content: "";
position: absolute;
inset: 0;
z-index: 0;
border-radius: inherit;
background: linear-gradient(135deg, #52525b, #18181b);
clip-path: inset(0 calc(100% - var(--switch-progress) * 100%) 0 0);
pointer-events: none;
}
.glass-switch[data-theme="white"]::after { background: linear-gradient(135deg, #a7c2a6, #527349); }
.glass-switch .glass-switch-thumb {
position: relative;
z-index: 1;
width: 24px;
height: 24px;
border-radius: 50%;
}
.glass-native-input:checked + .glass-switch { --switch-progress: 1; }
@media (prefers-reduced-motion: reduce) {
.glass-control, .glass-control::before, .glass-check svg,
.glass-switch, .glass-switch-thumb { transition: none; }
}
.liquid-track {
display: inline-grid;
grid-auto-flow: column;
grid-auto-columns: 1fr;
position: relative;
width: max-content;
max-width: 100%;
touch-action: pan-y;
user-select: none;
cursor: grab;
}
.liquid-track[data-dragging="true"] { cursor: grabbing; }
.liquid-track[data-disabled="true"] { opacity: 0.4; cursor: not-allowed; }
.liquid-surface { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
.liquid-option { display: flex; align-items: center; justify-content: center; position: relative; z-index: 1; flex: 1 0 0; min-width: 0; min-height: 38px; padding: 0 18px; white-space: nowrap; border-radius: 999px; color: #52525b; font-size: 12px; font-weight: 500; cursor: inherit; transition: color 150ms ease; }
.liquid-option[data-active="true"] { color: #18181b; }
.liquid-option[data-active="true"][data-theme="black"] { color: white; }
.liquid-option:focus-visible { outline: 2px solid #527349; outline-offset: 4px; }
@media (prefers-reduced-motion: reduce) { .liquid-option { transition: none; } }
.glass-tab {
position: relative;
isolation: isolate;
flex-shrink: 0;
border: 0;
background: transparent;
color: #52525b;
cursor: pointer;
transition: color 180ms ease;
}
.glass-tab[aria-selected="true"] { color: #18181b; }
.glass-tab[aria-selected="true"][data-theme="black"] { color: #fff; }
.glass-tab:disabled { opacity: 0.4; cursor: not-allowed; }
@media (prefers-reduced-motion: reduce) { .glass-tab { transition: none; } }
.glass-tabs-tray { position: relative; isolation: isolate; }
.glass-tabs-tray > .glass-tab { z-index: 1; isolation: auto; }
@media (hover: hover) and (prefers-reduced-motion: no-preference) {
.glass-tabs-tray > .glass-tab {
transition: color 140ms ease, scale 140ms ease-out;
}
.glass-tabs-tray > .glass-tab:hover:not(:disabled) { scale: 1.04; }
}