# Key Hints

> The strip of keys at the foot of a palette or a viewer, such as ↑ ↓ Move, hidden on touch screens.

Page: https://uicontext.com/docs/components/key-hints

Group: Primitives.

## When to use

The strip of keys at the foot of a palette or a viewer, such as ↑ ↓ Move, hidden on touch screens. Use [Kbd](https://uicontext.com/docs/components/kbd.md) instead when you show one shortcut beside its command.

## Usage

```ts
import { KeyHints } from "./ui/uicontext/components/KeyHints.ts"

export const Footer = <M>(h: Html.H<M>) =>
	KeyHints(h, { hints: [{ keys: ["up", "down"], label: "Move" }, { keys: "enter", label: "Open" }, { keys: "esc", label: "Close" }] })
```

## Examples

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

### Under a command palette

The example the page opens with: Key Hints in “Under a command palette”.

```ts title="src/demo.ts" source="www/src/examples/key-hints/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { KeyHints } from "./ui/uicontext/components/KeyHints.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	palette: { inlineSize: "min(100%, 32rem)", borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.popover, backgroundColor: color.panel, overflow: "hidden" },
	search: { paddingBlock: space.md, paddingInline: space.md, fontSize: font.base, color: color.muted, borderBlockEndWidth: stroke.hairline, borderBlockEndStyle: "solid", borderBlockEndColor: color.line },
	list: { display: "grid", margin: 0, padding: space.xs, listStyle: "none", fontSize: font.sm },
	item: { paddingBlock: space.sm, paddingInline: space.sm, borderRadius: corner.control },
	active: { backgroundColor: color.selected },
})

// the foot of a command palette: what the keys do while it's open
export const PaletteFooter = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.palette)).children(
		h.div.attrs({ "aria-hidden": "true", ...sx(styles.search) }).children("Search issues, projects and people…"),
		h.ul.attrs({ "aria-label": "Recent", ...sx(styles.list) }).children(
			h.li.attrs(sx(styles.item, styles.active)).children("ENG-2140 Exports fail for large workspaces"),
			h.li.attrs(sx(styles.item)).children("Q1 onboarding revamp"),
			h.li.attrs(sx(styles.item)).children("Priya Raman"),
		),
		KeyHints(h, {
			variant: "bar",
			touch: true,
			hints: [
				{ keys: ["up", "down"], label: "Move" },
				{ keys: "enter", label: "Open" },
				{ keys: "mod+enter", label: "Open in new tab" },
				{ keys: "esc", label: "Close" },
			],
		}),
	)
```

### Hidden on touch screens

Without `touch`, the strip hides on phones and tablets, where nobody presses these keys.

```ts title="src/viewer.ts" source="www/src/examples/key-hints/viewer.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { KeyHints } from "./ui/uicontext/components/KeyHints.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	viewer: { display: "grid", gap: space.sm, inlineSize: "min(100%, 34rem)" },
	stage: { display: "grid", placeItems: "center", aspectRatio: "16 / 9", borderRadius: corner.card, backgroundColor: color.app, color: color.muted, fontSize: font.sm },
})

// a photo viewer's hints, at the end of the row; they hide on a phone, where people swipe
export const ViewerHints = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.viewer)).children(
		h.div.attrs({ role: "img", "aria-label": "Site visit, photo 3 of 12", ...sx(styles.stage) }).children("3 / 12"),
		KeyHints(h, {
			align: "end",
			label: "Viewer shortcuts",
			hints: [
				{ keys: ["left", "right"], label: "Previous and next" },
				{ keys: "space", label: "Zoom" },
				{ keys: "d", label: "Download" },
			],
		}),
	)
```

## API reference

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `hints` (required) | `KeyHint[]` | — | The hints, in order. |
| `label` | `string` | — | Names the list for a screen reader; "Keyboard shortcuts" by default. |
| `variant` | `"plain" \| "bar"` | — | `bar` draws a line above, for the foot of a dialog or a panel. |
| `align` | `"start" \| "end"` | `start` | Which end of the row the hints sit at; `start` by default. |
| `touch` | `boolean` | — | Keeps the strip on touch screens too, for a tablet with a keyboard. |
| `mac` | `boolean` | `true` | ⌘ on a Mac, Ctrl elsewhere; true by default. |
| `style` | `Style` | — | — |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/key-hints
```

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

It writes these files:

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

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

## Source

- [src/KeyHints.ts](https://github.com/checkfu/uicontext/blob/main/src/KeyHints.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.
