# LCD Display

> A dot-matrix screen for labelled readouts and settings, backlit in the light theme and glowing in the dark one, with dropdown values.

<ComponentPreview name="lcd-display-demo" />

## Installation

```npm
npx shadcn-svelte@latest add @audiocn-svelte/lcd-display
```

## Usage

```svelte
<script lang="ts">
	import {
		LcdDisplay,
		LcdDisplayLabel,
		LcdDisplayRow,
		LcdDisplayValue,
	} from "#lib/components/ui/lcd-display/index.js";
</script>

<LcdDisplay>
	<LcdDisplayRow>
		<LcdDisplayLabel>Tempo</LcdDisplayLabel>
		<LcdDisplayValue>128.0</LcdDisplayValue>
	</LcdDisplayRow>
</LcdDisplay>
```

The screen follows your theme. Its ink is the theme's `--primary`, so it takes the colour of any theme. In light mode it is a backlit LCD: pale glass tinted by the ink, with dark pixels, like a calculator. In dark mode it is dark glass where the ink itself glows. Labels are dim, values are bright, and a value dims for a moment when its text changes, like segments redrawing.

Set `appearance="light"` or `appearance="dark"` to keep one look in both modes, for example a dark screen on a light page.

## Anatomy

```svelte
<LcdDisplay>
	<LcdDisplayRow>
		<LcdDisplayLabel />
		<LcdDisplaySelect type="single">
			<LcdDisplaySelectTrigger />
			<LcdDisplaySelectContent>
				<LcdDisplaySelectItem />
			</LcdDisplaySelectContent>
		</LcdDisplaySelect>
	</LcdDisplayRow>
	<LcdDisplaySeparator />
	<LcdDisplayRow>
		<LcdDisplayLabel />
		<LcdDisplayValue />
	</LcdDisplayRow>
	<LcdDisplayStatus />
</LcdDisplay>
```

A row holds a label and either a read-only `LcdDisplayValue` or an `LcdDisplaySelect`. `LcdDisplaySelect` is [bits-ui's Select](https://bits-ui.com/docs/components/select), so it takes the same props: `type`, `value` (bindable), `onValueChange` and `items`. Pass `items` to show each value's label in the trigger.

```svelte
<script lang="ts">
	const efforts = [
		{ label: "Low", value: "low" },
		{ label: "High", value: "high" },
	];

	let effort = $state("high");
</script>

<LcdDisplayRow>
	<LcdDisplayLabel>Effort</LcdDisplayLabel>
	<LcdDisplaySelect type="single" items={efforts} bind:value={effort}>
		<LcdDisplaySelectTrigger />
		<LcdDisplaySelectContent>
			{#each efforts as item (item.value)}
				<LcdDisplaySelectItem value={item.value} label={item.label} />
			{/each}
		</LcdDisplaySelectContent>
	</LcdDisplaySelect>
</LcdDisplayRow>
```

## Examples

### Inks

The ink is the theme's `--primary` by default (`variant="primary"`), so the screen changes with your theme. Switch the theme in the sidebar to see the demo above follow it. For a fixed hardware look, `variant` also takes `amber`, `green`, `cyan` or `red`.

<ComponentPreview name="lcd-display-variants" />

For any other colour, set `--lcd-display-ink`. The glass, the lit, dim and unlit pixels and the glow are mixed from it, in both themes. The select's popup copies the display's variables when it opens, so it takes the same colour.

```svelte
<LcdDisplay class="[--lcd-display-ink:var(--color-violet-400)]" />
```

### A dot-matrix font

The screen breaks any font into dots with a CSS mask, so it looks like an LCD with the font you already have. For true 5 × 7 characters, load a dot-matrix face such as [Doto](https://fonts.google.com/specimen/Doto), point `--lcd-display-font` at it and turn the mask off with `matrix={false}`.

<ComponentPreview name="lcd-display-font" />

This site loads Doto from [Fontsource](https://fontsource.org/fonts/doto) at its heaviest weight, so the dots fill out. The browser fetches the font only on a page that uses it.

```css filename="src/routes/layout.css"
@import "@fontsource/doto/latin-900.css";

@theme {
	--font-doto: "Doto", monospace;
}
```

```svelte
<LcdDisplay class="[--lcd-display-font:var(--font-doto)]" matrix={false} />
```

### A level meter

For a level meter on the same screen, add an [LCD Meter](/docs/components/lcd-meter). It takes the display's ink, appearance and size.

```svelte
<LcdDisplay>
	<LcdDisplayRow>
		<LcdDisplayLabel>Input</LcdDisplayLabel>
		<LcdDisplayValue>USB Mic</LcdDisplayValue>
	</LcdDisplayRow>
	<LcdDisplaySeparator />
	<LcdMeter aria-label="Input level" source={analyser.meter} />
</LcdDisplay>
```

## Theming

| Variable or attribute | Meaning |
| --- | --- |
| `--lcd-display-ink` | The display's colour, set by `variant`. Default: `var(--primary)` |
| `--lcd-display-lit` | Lit pixels. Light: the ink darkened. Dark: the ink |
| `--lcd-display-ink-dim` | Labels and the separator. Default: the lit pixels at 40% |
| `--lcd-display-ink-unlit` | The unlit dots behind the text. Default: the lit pixels at 7% |
| `--lcd-display-glass` | The screen. Light: a pale glass in the ink's hue. Dark: near black |
| `--lcd-display-glow` | The halo around lit text. Light: a faint shadow. Dark: the ink at 60% |
| `--lcd-display-bezel`, `--lcd-display-bezel-edge` | The frame's gradient, top to bottom |
| `--lcd-display-font` | The font. Default `var(--font-mono)` |
| `--lcd-display-pitch` | The dot grid's spacing. Default `2px` |
| `data-appearance` | On the display and the select's popup: `auto`, `light` or `dark` |
| `data-size` | On the display, set by `size` |
| `data-variant` | On the display and the select's popup |
| `data-matrix` | On the display while the dot mask is on |
| `data-changed` | On a value or trigger for a moment after its text changes |
| `data-state` | On `LcdDisplayStatus`: `idle`, `busy` or `off`. On the select's trigger: `open` or `closed` |

`size` sets the text size: `sm`, `default` or `lg`. The rows and the select's popup follow it.

## Accessibility

The display is a `group`. Each select's trigger is a `combobox` named by its row's `LcdDisplayLabel`, including one with its own `id`, and bits-ui handles the keyboard and the listbox semantics. Give a value `live` to announce its changes to screen readers. The status pixel is decorative. Blinking and the change flicker stop when the user prefers reduced motion.

## API reference

### LcdDisplay

<PropsTable
	rows={[
		[
			"variant",
			'"primary" | "amber" | "green" | "cyan" | "red"',
			'"primary"',
			"The ink colour. primary follows the theme's --primary; the others are fixed hardware inks.",
		],
		[
			"appearance",
			'"auto" | "light" | "dark"',
			'"auto"',
			"light is a backlit screen with dark pixels, dark is dark glass with glowing ink, auto follows the theme.",
		],
		["matrix", "boolean", "true", "Breaks the text into dots on a --lcd-display-pitch grid."],
		["scanlines", "boolean", "true", "Faint horizontal lines over the glass."],
		["size", '"sm" | "default" | "lg"', '"default"', "The text size."],
	]}
/>

### LcdDisplayValue

<PropsTable
	rows={[
		["live", "boolean", "false", 'Announces changes to screen readers with aria-live="polite".'],
	]}
/>

### LcdDisplayStatus

<PropsTable
	rows={[
		[
			"state",
			'"idle" | "busy" | "off"',
			'"idle"',
			"idle blinks slowly, busy quickly, off stays unlit.",
		],
	]}
/>

### LcdDisplaySelectTrigger

<PropsTable
	rows={[
		[
			"placeholder",
			"string",
			null,
			"Shown while nothing is selected. Children replace the value text.",
		],
	]}
/>

### LcdDisplaySelectContent

Takes bits-ui's `Select.Content` props, with these defaults.

<PropsTable
	rows={[
		["side", '"top" | "bottom" | "left" | "right"', '"bottom"', null],
		["sideOffset", "number", "8", null],
		["align", '"start" | "center" | "end"', '"end"', null],
		["alignOffset", "number", "0", null],
	]}
/>

### LcdDisplaySelectItem

Takes bits-ui's `Select.Item` props. Without children it shows `label`, or `value` when there is no label.
