Happenings can use optional analytics cookies to understand usage and improve the product. They stay off unless you allow them.
Bento grid of mixed-size tiles (numbers, features, photos and links) that can open Apple-style detail dialogs
Install
npx shadcn add https://ui.happenings.social/r/bento.json
Or with namespace
npx shadcn add @happenings/bento
Preview
Dependencies
src/components/ui/bento.tsx
"use client"
import * as React from "react"
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
import { faArrowRight, faPlus, faXmark } from "@fortawesome/pro-solid-svg-icons"
import type { IconDefinition } from "@fortawesome/fontawesome-svg-core"
import { Slot } from "radix-ui"
import { cn } from "@/lib/utils"
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogTitle,
DialogTrigger,
} from "./dialog"
/**
* A bento grid: tiles of mixed size on one grid, bordered and flat (no
* shadows, no decorative gradients). Two columns up to `lg`, four from there.
* On phones only `compact` tiles (numbers, photos) sit two side by side; every
* other tile takes the full width so its text keeps a readable measure. Use at
* most one `accent` tile per grid so the accent keeps meaning "this one".
*
* Tiles are framework-neutral: give a tile `render={<Link href="…" />}` (any
* router's link, or `<a>`) to make the whole tile clickable, and pass your own
* image component to `BentoImage`. `BentoTile` takes `asChild` directly for
* fully custom tiles.
*/
function BentoGrid({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="bento-grid"
className={cn(
"grid grid-flow-dense auto-rows-[minmax(8rem,auto)] grid-cols-2 gap-3 sm:auto-rows-[minmax(11rem,auto)] lg:grid-cols-4",
className
)}
{...props}
/>
)
}
type BentoSpan = "1" | "2" | "2x2" | "tall" | "full"
const spanClass: Record<BentoSpan, string> = {
"1": "",
"2": "col-span-2",
"2x2": "col-span-2 lg:row-span-2",
tall: "lg:row-span-2",
full: "col-span-2 lg:col-span-4",
}
type BentoTileProps = React.ComponentProps<"div"> & {
/** How many grid cells the tile covers where the grid has room. */
span?: BentoSpan | undefined
/** The one highlighted tile of a grid, in the primary colour. */
accent?: boolean | undefined
/** Half width on phones (numbers, photos) instead of full width. */
compact?: boolean | undefined
/** Render the child (usually a link) as the tile; adds hover and press states. */
asChild?: boolean | undefined
}
function BentoTile({
span = "1",
accent = false,
compact = false,
asChild = false,
className,
...props
}: BentoTileProps) {
const Comp = asChild ? Slot.Root : "div"
return (
<Comp
data-slot="bento-tile"
data-accent={accent || undefined}
className={cn(
"group/bento relative flex min-w-0 flex-col overflow-hidden rounded-2xl border p-5 sm:p-6",
accent ? "border-primary/30 bg-primary/10" : "bg-card text-card-foreground",
asChild &&
"cursor-pointer transition-all duration-150 ease-in-out outline-none hover:border-foreground/25 focus-visible:ring-[3px] focus-visible:ring-ring/50 active:scale-[0.98]",
spanClass[span],
!compact && span === "1" && "max-sm:col-span-2",
className
)}
{...props}
/>
)
}
type BentoStatProps = Omit<BentoTileProps, "children" | "asChild"> & {
value: React.ReactNode
label: React.ReactNode
note?: React.ReactNode
/** An element to render the tile as, usually a link: `render={<Link href="/pricing" />}`. */
render?: React.ReactElement<{ children?: React.ReactNode }>
}
/** A headline number with its label: the tile's job is the number. */
function BentoStat({ value, label, note, accent, render, ...props }: BentoStatProps) {
const body = (
<>
<p className={cn("text-5xl font-semibold tracking-tight tabular-nums sm:text-6xl", accent && "text-primary")}>
{value}
</p>
<p className="mt-2 text-[15px] font-medium">{label}</p>
{note ? <p className="mt-auto pt-4 text-sm leading-6 text-muted-foreground">{note}</p> : null}
</>
)
return (
<BentoTile compact accent={accent} asChild={!!render} {...props}>
{render ? React.cloneElement(render, undefined, body) : body}
</BentoTile>
)
}
/** The fuller story a feature tile opens: a lead and headed sections. */
type BentoDetail = {
lead: React.ReactNode
sections: { heading: React.ReactNode; body: React.ReactNode }[]
/** Screen-reader label for the tile button, e.g. "Learn more". */
openLabel: string
closeLabel: string
}
type BentoFeatureProps = Omit<BentoTileProps, "title" | "asChild"> & {
icon?: IconDefinition
eyebrow?: React.ReactNode
title: React.ReactNode
description?: React.ReactNode
/** A call to action shown at the bottom of the tile, with an arrow. */
cta?: React.ReactNode
/**
* Opens a large dialog with the fuller story, the way Apple's product pages
* do: a round "+" on the tile, the whole tile clickable. A tile is either a
* link (`render`) or an explanation (`detail`), not both.
*/
detail?: BentoDetail | undefined
/** An element to render the tile as, usually a link: `render={<Link href="/pricing" />}`. */
render?: React.ReactElement<{ children?: React.ReactNode }>
}
/** An icon, a title and a sentence or two, with optional content and a call to action. */
function BentoFeature({
icon,
eyebrow,
title,
description,
cta,
accent,
span,
render,
detail,
children,
...props
}: BentoFeatureProps) {
const large = span === "2x2" || span === "tall"
const body = (
<>
{icon ? (
<span
className={cn(
"flex size-10 shrink-0 items-center justify-center rounded-xl",
accent ? "bg-primary text-primary-foreground" : "bg-muted text-foreground"
)}
>
<FontAwesomeIcon icon={icon} className="size-4" />
</span>
) : null}
<div className={cn("space-y-2", icon && "mt-5")}>
{eyebrow ? (
<p className={cn("text-xs font-semibold", accent ? "text-primary" : "text-muted-foreground")}>{eyebrow}</p>
) : null}
<h3 className={cn("font-semibold tracking-tight text-balance", large ? "text-2xl sm:text-3xl" : "text-lg")}>
{title}
</h3>
{description ? (
<p className={cn("leading-7 text-pretty text-muted-foreground", large ? "text-base" : "text-[15px]")}>
{description}
</p>
) : null}
</div>
{children ? <div className="mt-5">{children}</div> : null}
{cta ? (
<p
className={cn(
"mt-auto inline-flex items-center gap-1.5 pt-5 text-sm font-semibold",
accent ? "text-primary" : "text-foreground"
)}
>
{cta}
<FontAwesomeIcon
icon={faArrowRight}
className="size-3 transition-transform duration-150 ease-in-out group-hover/bento:translate-x-0.5"
/>
</p>
) : null}
</>
)
if (detail) {
return (
<Dialog>
<BentoTile
{...props}
accent={accent}
span={span}
className={cn(
"cursor-pointer pb-16 transition-all duration-150 ease-in-out hover:border-foreground/25 has-[[data-slot=bento-open]:active]:scale-[0.98] sm:pb-16",
props.className
)}
>
{body}
<span
aria-hidden
className={cn(
"absolute right-4 bottom-4 flex size-9 items-center justify-center rounded-full border transition-transform duration-150 ease-in-out group-hover/bento:scale-110",
accent ? "border-transparent bg-primary text-primary-foreground" : "bg-card text-foreground"
)}
>
<FontAwesomeIcon icon={faPlus} className="size-3.5" />
</span>
<DialogTrigger asChild>
<button
type="button"
data-slot="bento-open"
aria-label={`${detail.openLabel}: ${typeof title === "string" ? title : ""}`}
className="absolute inset-0 cursor-pointer rounded-2xl outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50"
/>
</DialogTrigger>
</BentoTile>
<DialogContent
showCloseButton={false}
className="max-h-[88dvh] gap-0 overflow-y-auto rounded-3xl p-0 sm:max-w-3xl"
>
<div className="space-y-5 p-6 pt-16 sm:p-12 sm:pt-14">
{icon ? (
<span
className={cn(
"flex size-12 items-center justify-center rounded-2xl",
accent ? "bg-primary text-primary-foreground" : "bg-muted text-foreground"
)}
>
<FontAwesomeIcon icon={icon} className="size-5" />
</span>
) : null}
{eyebrow ? <p className="text-sm font-semibold text-primary">{eyebrow}</p> : null}
<DialogTitle className="text-3xl leading-tight font-semibold tracking-tight text-balance sm:text-5xl">
{title}
</DialogTitle>
<DialogDescription className="text-lg leading-8 text-pretty text-muted-foreground sm:text-xl">
{detail.lead}
</DialogDescription>
</div>
<div className="space-y-8 border-t p-6 sm:p-12">
{detail.sections.map((section, index) => (
<section key={index} className="space-y-2">
<h3 className="text-lg font-semibold tracking-tight sm:text-xl">{section.heading}</h3>
<p className="text-base leading-8 text-pretty text-muted-foreground sm:text-[17px]">{section.body}</p>
</section>
))}
</div>
<DialogClose asChild>
<button
type="button"
aria-label={detail.closeLabel}
title={detail.closeLabel}
className="absolute top-4 right-4 flex size-9 cursor-pointer items-center justify-center rounded-full bg-muted text-foreground transition-all duration-150 ease-in-out outline-none hover:bg-accent focus-visible:ring-[3px] focus-visible:ring-ring/50 active:scale-90"
>
<FontAwesomeIcon icon={faXmark} className="size-4" />
</button>
</DialogClose>
</DialogContent>
</Dialog>
)
}
return (
<BentoTile accent={accent} span={span} asChild={!!render} {...props}>
{render ? React.cloneElement(render, undefined, body) : body}
</BentoTile>
)
}
type BentoImageProps = Omit<BentoTileProps, "children" | "asChild"> & {
/** Your image, filling the tile (for example Next.js `<Image fill />` or an `<img>`). */
image: React.ReactNode
/** A short caption on a solid chip, never text over a gradient. */
caption?: React.ReactNode
/** An element to render the tile as, usually a link: `render={<Link href="/pricing" />}`. */
render?: React.ReactElement<{ children?: React.ReactNode }>
}
/** A photo tile. The image fills the tile and lifts slightly on hover when the tile is a link. */
function BentoImage({ image, caption, render, className, ...props }: BentoImageProps) {
const body = (
<>
<div className="absolute inset-0 transition-transform duration-300 ease-out group-hover/bento:scale-[1.02] [&>img]:size-full [&>img]:object-cover">
{image}
</div>
{caption ? (
<span className="absolute bottom-3 left-3 rounded-full border bg-card px-3 py-1 text-xs font-medium">
{caption}
</span>
) : null}
</>
)
return (
<BentoTile compact asChild={!!render} className={cn("p-0 sm:p-0", className)} {...props}>
{render ? React.cloneElement(render, undefined, body) : body}
</BentoTile>
)
}
export { BentoGrid, BentoTile, BentoStat, BentoFeature, BentoImage, type BentoDetail, type BentoSpan }
Preview
This week
Doors at 16:00. Members get in free, everyone else pays at the door.
See the event
1,240
Members
38
Events this term
Buy, transfer and show your ticket at the door.
12
Partners
Membership takes a minute and lasts the whole academic year.
Become a member