A file or a picture as a small card: its preview or type, then a name that keeps its extension.
- 1.8 kB JS 3.1 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 10 tests
import type { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { sx } from "./ui/uicontext/components/Sx.ts"import { ThumbnailTile } from "./ui/uicontext/components/ThumbnailTile.ts"import { space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ grid: { display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(min(100%, 9.5rem), 1fr))", gap: space.md, inlineSize: "100%", margin: 0, padding: 0, listStyle: "none" },})// a drawn picture, so the demo needs no image filesconst art = (from: string, to: string) => `data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 120"><defs><linearGradient id="g" x2="1" y2="1"><stop offset="0" stop-color="${from}"/><stop offset="1" stop-color="${to}"/></linearGradient></defs><rect width="160" height="120" fill="url(#g)"/><circle cx="118" cy="38" r="16" fill="#fff" fill-opacity=".55"/><path d="M0 120 L52 64 L88 98 L112 78 L160 120Z" fill="#000" fill-opacity=".18"/></svg>`)}`const files = [ { id: "1", name: "Site visit, east wall.jpg", meta: "2.4 MB · Today", image: art("#fde68a", "#f97316") }, { id: "2", name: "Quarterly-report-final-v3-signed.pdf", meta: "1.1 MB · Yesterday" }, { id: "3", name: "Floor plan.png", meta: "860 KB · Mon", image: art("#bfdbfe", "#4f46e5") }, { id: "4", name: "Supplier contacts.xlsx", meta: "48 KB · 2 Dec" }, { id: "5", name: "Walkthrough.mp4", meta: "212 MB · 28 Nov", type: "Video" },]// a project's files: pictures where there are some, the type where there aren't, names that keep their endexport const ProjectFiles = <M>(h: Html.H<M>) => h.ul.attrs({ "aria-label": "Project files", ...sx(styles.grid) }).children( files.map((f) => h.li.key(f.id).children(ThumbnailTile(h, { name: f.name, meta: f.meta, href: `#file-${f.id}`, ...(f.image === undefined ? {} : { image: { src: f.image } }), ...(f.type === undefined ? {} : { type: f.type }) }))), )When to use
A file or a picture as a small card: its preview or type, then a name that keeps its extension. Use File Field instead when people are adding the file, not browsing it.
Usage
import { ThumbnailTile } from "./ui/uicontext/components/ThumbnailTile.ts"export const File = <M>(h: Html.H<M>) => ThumbnailTile(h, { name: "Floor plan.pdf", meta: "1.2 MB", href: "/files/42" })Examples
Choosing files
With onPress and selected, a tile is a toggle; ratio and fit suit square logos.
Chosen: 1 of 4
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { sx } from "./ui/uicontext/components/Sx.ts"import { ThumbnailTile } from "./ui/uicontext/components/ThumbnailTile.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ wrap: { display: "grid", gap: space.md, inlineSize: "100%" }, grid: { display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(min(100%, 8rem), 1fr))", gap: space.sm, margin: 0, padding: 0, listStyle: "none" }, count: { margin: 0, fontSize: font.sm, color: color.muted },})const tint = (hue: number) => `data:image/svg+xml,${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><rect width="100" height="100" fill="hsl(${hue} 70% 82%)"/><rect x="22" y="30" width="56" height="40" rx="6" fill="hsl(${hue} 55% 45%)"/></svg>`)}`const logos = [ { id: "a", name: "Wordmark, dark.svg", hue: 220 }, { id: "b", name: "Wordmark, light.svg", hue: 40 }, { id: "c", name: "App icon 1024.png", hue: 160 }, { id: "d", name: "Favicon.ico", hue: 340 },]const Message = UiSchema.TaggedEnum({ Toggled: { id: Schema.String } })// choosing brand assets to attach: pressing a tile toggles it, and the count says how manyexport const AssetPicker = Html.component({ name: "AssetPicker", model: Schema.Struct({ chosen: Schema.Array(Schema.String) }), message: Message,}).make({ init: () => ({ chosen: ["a"] }), update: { Toggled: ($, { id }) => $.evolve({ chosen: (list) => (list.includes(id) ? list.filter((x) => x !== id) : [...list, id]) }) }, view: (h, model) => h.div.attrs(sx(styles.wrap)).children( h.ul.attrs({ "aria-label": "Brand assets", ...sx(styles.grid) }).children( logos.map((l) => h.li.key(l.id).children( ThumbnailTile(h, { name: l.name, image: { src: tint(l.hue) }, ratio: "1", fit: "contain", selected: model.chosen.includes(l.id), onPress: Message.Toggled({ id: l.id }) }), )), ), h.p.attrs({ role: "status", ...sx(styles.count) }).children(`Chosen: ${model.chosen.length} of ${logos.length}`), ),})API reference
Options
| Name | Type | Description |
|---|---|---|
namerequired | string | The file's or item's name: "Quarterly report.pdf". |
meta | string | A quieter line under the name: "2.4 MB · Edited 2h ago". |
image | { readonly src: string; readonly alt?: string } | Its picture. Leave alt empty when the name says it all. |
type | string | The word shown when there's no picture; the name's extension by default ("PDF"). |
ratio | string | The picture's shape, as CSS writes it; "4 / 3" by default. |
fit | "cover" | "contain" | cover (the default) fills the frame; contain shows the whole picture, for logos and designs. |
href | string | Makes the tile a link. |
onPress | Message | Makes the tile a button. |
selected | boolean | Chosen, as in a picker: a ring and a check. Pressing a tile with onPress toggles it. |
action | View.Content | A small control in the top corner, such as a More menu's button. It sits outside the tile's own target. |
style | Style | — |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/thumbnail-tile