# Thumbnail Tile

> A file or a picture as a small card: its preview or type, then a name that keeps its extension.

Page: https://uicontext.com/docs/components/thumbnail-tile

Group: Primitives.

## 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](https://uicontext.com/docs/components/file-field.md) instead when people are adding the file, not browsing it.

## Usage

```ts
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

Each example is a complete file. Its imports point where the CLI installs the source.

### A project's files

The example the page opens with: Thumbnail Tile in “A project's files”.

```ts title="src/demo.ts" source="www/src/examples/thumbnail-tile/demo.ts"
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 files
const 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 end
export 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 }) }))),
	)
```

### Choosing files

With `onPress` and `selected`, a tile is a toggle; `ratio` and `fit` suit square logos.

```ts title="src/picker.ts" source="www/src/examples/thumbnail-tile/picker.ts"
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 many
export 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 |
| --- | --- | --- |
| `name` (required) | `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

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/thumbnail-tile
```

With npm: `npx shadcn@latest add checkfu/uicontext/thumbnail-tile`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/thumbnail-tile`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/thumbnail-tile`.

It writes these files:

- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/ThumbnailTile.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/ThumbnailTile.ts](https://github.com/checkfu/uicontext/blob/main/src/ThumbnailTile.ts)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle](https://uicontext.com/docs/components/toggle.md) (`toggle`): A button that stays pressed, such as Bold or Pin.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
