@nanahoshi/mona-ui 0.51.1 → 0.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,272 @@
1
+ # Tile View
2
+
3
+ > A collection as tiles in rows that fill the width, virtualized, with file-explorer selection.
4
+
5
+ Entry point: `@nanahoshi/mona-ui/tile-view`
6
+
7
+ **Selector:** `mona-tile-view`
8
+
9
+ `TileViewComponent` shows a collection as tiles of one size, in rows that fill the width, like the icons of a file explorer. Only the rows in view are rendered, so it stays fast with tens of thousands of items. It is one Tab stop: the arrow keys move in two dimensions, and selection works as in a file explorer, with Ctrl and Shift.
10
+
11
+ Import it from `@nanahoshi/mona-ui/tile-view`, and name every tile view with `aria-label` or `aria-labelledby`.
12
+
13
+ ## Overview
14
+
15
+ Tile View renders items as tiles: a `role="listbox"` of `option`s you select from, or a `role="list"` of `listitem`s with `selectionMode="none"`. Tiles are all `tileWidth` by `tileHeight` pixels, and a row holds as many as fit the width, or the number `columns` gives. Rows are virtualized, so a folder of 50,000 pictures scrolls as smoothly as one of fifty.
16
+
17
+ **Use Tile View when:**
18
+
19
+ - Items are best recognized by a picture: photos, files, products, avatars.
20
+ - The collection can be large, and the keyboard should move up, down, left and right through it.
21
+
22
+ **Use `ListView` instead when:**
23
+
24
+ - Items are rows of text, grouped or paged.
25
+
26
+ **Use `Grid` instead when:**
27
+
28
+ - Items need columns of details, sorting by column, or editing.
29
+
30
+ ## Import & Quick Start
31
+
32
+ ```typescript
33
+ import { TileViewComponent } from "@nanahoshi/mona-ui/tile-view";
34
+ ```
35
+
36
+ ```html
37
+ <mona-tile-view aria-label="Fruits" class="h-64" [items]="fruits" [(selectedKeys)]="selected" />
38
+ ```
39
+
40
+ `items` accepts any `Iterable<T>`. Without an item template, each tile shows the item's text: `textField` names a property or gives a function `(item: T) => string`, and without it the item itself is converted to text. Give the tile view a height, through `class` or its container: it fills its container and scrolls inside it.
41
+
42
+ ## Anatomy
43
+
44
+ Each template below is an `ng-template` placed inside `<mona-tile-view>...</mona-tile-view>`.
45
+
46
+ | Directive | Selector | Template context | Replaces |
47
+ |-----------------------------------|-------------------------------------------|----------------------------------------------------------------------------------------------|----------------------------------|
48
+ | `TileViewItemTemplateDirective` | `ng-template[monaTileViewItemTemplate]` | `TileViewItemTemplateContext<T>`: `{ $implicit: T, selected, active, index, slot }` | The item's text, centered |
49
+ | `TileViewNoDataTemplateDirective` | `ng-template[monaTileViewNoDataTemplate]` | None | The default "No data" placeholder |
50
+
51
+ ```html
52
+ <mona-tile-view aria-label="Pictures" itemKey="id" textField="name" [items]="pictures">
53
+ <ng-template monaTileViewItemTemplate [monaTileViewItemTemplateOf]="pictures" let-picture let-slot="slot">
54
+ <img alt="" class="mx-auto mt-2 size-16 object-contain" [src]="picture.thumbnail" />
55
+ <span class="line-clamp-2 px-1 text-center text-sm">{{ picture.name }}</span>
56
+ </ng-template>
57
+ </mona-tile-view>
58
+ ```
59
+
60
+ Bind the tile view's items to `[monaTileViewItemTemplateOf]` to type `let-picture`; without it, the item is `any`. A tile is a column of flex content: the template's elements stack from the top. Keep item templates free of buttons and links, since tiles are options of a listbox.
61
+
62
+ ## Examples
63
+
64
+ ### Selection
65
+
66
+ `basic-example.component.ts`
67
+
68
+ ```typescript
69
+ import { Component, signal } from "@angular/core";
70
+ import { TileViewComponent } from "@nanahoshi/mona-ui/tile-view";
71
+
72
+ @Component({
73
+ selector: "app-tile-view-basic-example",
74
+ imports: [TileViewComponent],
75
+ templateUrl: "./basic-example.component.html"
76
+ })
77
+ export class TileViewBasicExampleComponent {
78
+ protected readonly fruits = [
79
+ "Apple",
80
+ "Banana",
81
+ "Cherry",
82
+ "Date",
83
+ "Elderberry",
84
+ "Fig",
85
+ "Grape",
86
+ "Kiwi",
87
+ "Lemon",
88
+ "Mango",
89
+ "Nectarine",
90
+ "Orange"
91
+ ];
92
+ protected readonly selected = signal<readonly unknown[]>(["Cherry"]);
93
+ }
94
+ ```
95
+
96
+ `basic-example.component.html`
97
+
98
+ ```html
99
+ <div class="flex flex-col gap-2">
100
+ <mona-tile-view
101
+ aria-label="Fruits"
102
+ class="h-64"
103
+ selectionMode="multiple"
104
+ [items]="fruits"
105
+ [tileHeight]="64"
106
+ [tileWidth]="96"
107
+ [(selectedKeys)]="selected" />
108
+ <span aria-live="polite" class="text-sm">Selected: {{ selected().join(", ") || "none" }}</span>
109
+ </div>
110
+ ```
111
+
112
+ `selectedKeys` holds the keys of the selected items, and `[(selectedKeys)]` follows the user's selection. `itemKey` names the property, or gives the function, that is each item's key; without it, the item itself is its key. Keys must be unique. Keys set through `selectedKeys` aren't emitted back.
113
+
114
+ ### Pictures and many items
115
+
116
+ `pictures-example.component.ts`
117
+
118
+ ```typescript
119
+ import { Component, signal } from "@angular/core";
120
+ import {
121
+ type TileViewItemActivateEvent,
122
+ TileViewComponent,
123
+ TileViewItemTemplateDirective
124
+ } from "@nanahoshi/mona-ui/tile-view";
125
+
126
+ interface Picture {
127
+ readonly hue: number;
128
+ readonly id: number;
129
+ readonly name: string;
130
+ }
131
+
132
+ @Component({
133
+ selector: "app-tile-view-pictures-example",
134
+ imports: [TileViewComponent, TileViewItemTemplateDirective],
135
+ templateUrl: "./pictures-example.component.html"
136
+ })
137
+ export class TileViewPicturesExampleComponent {
138
+ protected readonly opened = signal("");
139
+ protected readonly pictures: readonly Picture[] = Array.from({ length: 10_000 }, (_, i) => ({
140
+ hue: (i * 47) % 360,
141
+ id: i + 1,
142
+ name: `Picture ${i + 1}`
143
+ }));
144
+ protected readonly selected = signal<readonly unknown[]>([]);
145
+
146
+ protected open(event: TileViewItemActivateEvent<Picture>): void {
147
+ this.opened.set(event.item.name);
148
+ }
149
+ }
150
+ ```
151
+
152
+ `pictures-example.component.html`
153
+
154
+ ```html
155
+ <div class="flex flex-col gap-2">
156
+ <mona-tile-view
157
+ aria-label="Pictures"
158
+ class="h-80"
159
+ itemKey="id"
160
+ selectionMode="multiple"
161
+ textField="name"
162
+ [items]="pictures"
163
+ [(selectedKeys)]="selected"
164
+ (itemActivate)="open($event)">
165
+ <ng-template monaTileViewItemTemplate [monaTileViewItemTemplateOf]="pictures" let-picture>
166
+ <span
167
+ aria-hidden="true"
168
+ class="mx-auto mt-2 size-14 shrink-0 rounded-sm forced-color-adjust-none"
169
+ [style.background-color]="'hsl(' + picture.hue + ' 55% 50%)'"></span>
170
+ <span class="mt-1 line-clamp-2 px-1 text-center text-sm wrap-break-word">{{ picture.name }}</span>
171
+ </ng-template>
172
+ </mona-tile-view>
173
+ <span aria-live="polite" class="text-sm">
174
+ {{ selected().length }} selected{{ opened() ? ", opened " + opened() : "" }}
175
+ </span>
176
+ </div>
177
+ ```
178
+
179
+ Ten thousand tiles, each a picture and a name. Double-click a tile, or press Enter, to activate it: `itemActivate` emits the item and the event, so Ctrl+Enter can do something else than Enter.
180
+
181
+ ## Appearance & Styling
182
+
183
+ The tile view fills its container's width and height, and has no size of its own, since only the rows in view are rendered: give the container a height, or the tile view a class such as `h-96`. `tileWidth`, `tileHeight` and `gap` size the tiles and the space between and around them, in pixels. Every tile must render at `tileHeight`, or scrolling drifts. `rounded` and `size` set the frame's radius and the text size, and `class` is merged onto the host with `tailwind-merge`.
184
+
185
+ A selected tile has the selection background, and the tile the keyboard is on has a 2px ring. In forced colors, where backgrounds and rings don't paint, a selected tile has a `Highlight` frame, and the keyboard's tile a dashed outline inside it.
186
+
187
+ ## Behavior
188
+
189
+ ### Selection follows a file explorer
190
+
191
+ | Mode | Click | Ctrl+click | Shift+click | Click in empty space |
192
+ |--------------|--------------------|------------------------|--------------------------------|----------------------|
193
+ | `"multiple"` | Selects that tile | Adds or removes it | Selects from the anchor to it | Clears the selection |
194
+ | `"single"` | Selects that tile | Selects it, or clears it if it is selected | Selects that tile | Clears the selection |
195
+ | `"none"` | Moves the focus | Moves the focus | Moves the focus | Nothing |
196
+
197
+ The anchor is the tile last clicked, or moved to, without Shift. Ctrl+Shift+click adds the range to the selection.
198
+
199
+ ### The active tile follows the app's selection
200
+
201
+ When the app sets `selectedKeys` to keys that don't include the active tile, such as the key of an item it just added, the first selected tile in item order becomes active and is scrolled into view. `scrollToItem(key)` scrolls a tile into view without changing anything else.
202
+
203
+ ### When items change
204
+
205
+ The active tile stays on its item while the item is there. When it goes, the item that takes its place becomes active. Selected keys aren't pruned: the app owns them.
206
+
207
+ ### Tiles are reused
208
+
209
+ Only the tiles in view and a row or two around them are rendered. A row that scrolls away is reused for one that scrolls in, and its tiles show other items: the item template's context changes, and its elements and components stay. Keep what a tile shows in the item or derive it from the item, not in state a component of the template keeps on its own.
210
+
211
+ ### Slots
212
+
213
+ `slot` in the item template's context is a number no other rendered tile has: a tile that scrolls into view takes it over from one that scrolled away. Key work by it that a newer tile should replace, such as a request for a picture, so that scrolling fast to the end leaves only the last screen waiting. Slots change when the view's size does.
214
+
215
+ ## Accessibility
216
+
217
+ ### Keyboard
218
+
219
+ | Key | Action |
220
+ |-----------------------------|--------------------------------------------------------------------------------------------------|
221
+ | `ArrowLeft` / `ArrowRight` | Move to the previous or next tile in the row (swapped right to left). Stops at the row's ends. |
222
+ | `ArrowUp` / `ArrowDown` | Move to the tile above or below. Down from a column the last row lacks goes to the last tile. |
223
+ | `PageUp` / `PageDown` | Move by as many rows as fit in view. |
224
+ | `Home` / `End` | Move to the first or last tile. |
225
+ | `Shift` + a move | Select from the anchor to the tile moved to. |
226
+ | `Ctrl` + a move | Move without selecting. |
227
+ | `Space` | Select the active tile, adding it in multiple selection. `Ctrl+Space` adds or removes it. |
228
+ | `Ctrl+A` | Select all, in multiple selection. |
229
+ | `Enter` | Emit `itemActivate` for the active tile. |
230
+ | Letters, digits | Typeahead: move to the next tile whose text starts with what was typed. A space typed quickly after a letter is part of the text. |
231
+
232
+ A move without Ctrl selects the tile moved to, in single and multiple selection. Arrow keys with Alt are left to the page.
233
+
234
+ ### Focus
235
+
236
+ The tile view is one Tab stop: the listbox takes the focus, and `aria-activedescendant` points at the active tile. Tabbing in makes the first selected tile active, or the first tile, without selecting it. A click focuses the tile view without scrolling it. The active tile shows its ring only while the keyboard is in use, as `:focus-visible` would. An empty tile view keeps its listbox as the Tab stop, with a ring of its own, described by the "No data" text.
237
+
238
+ ### ARIA
239
+
240
+ | Attribute | Where | Value |
241
+ |----------------------------------|-------------------------------------------|-----------------------------------------------------------------|
242
+ | `role` | The scrolling element | `"listbox"`, or `"list"` with `selectionMode="none"` |
243
+ | `aria-label` / `aria-labelledby` | The listbox | From the inputs of the same names (not left on the host) |
244
+ | `aria-multiselectable` | The listbox, in multiple selection | `true` |
245
+ | `aria-activedescendant` | The listbox, while focused | The active tile's id |
246
+ | `aria-busy` | The listbox, while `loading` | `true` |
247
+ | `aria-describedby` | The listbox, when empty | The "No data" text, or the no-data template |
248
+ | `role` | Each row | `"none"` |
249
+ | `role` | Each tile | `"option"`, or `"listitem"` with `selectionMode="none"` |
250
+ | `aria-selected` | Each option | Whether the item is selected |
251
+ | `aria-posinset` / `aria-setsize` | Each tile | The item's 1-based position and the number of items, since only some tiles are rendered |
252
+ | `role="status"` | A visually hidden element after the tiles | "Loading" while `loading`, and "No data" when there are no items, unless a no-data template says it |
253
+
254
+ Give pictures in item templates an empty `alt` when the tile's text names the item, so it isn't read twice.
255
+
256
+ ## API
257
+
258
+ Everything below is imported from `@nanahoshi/mona-ui/tile-view`. Each name links to its reference page, with every input, output, property and method.
259
+
260
+ | Name | Kind | Description |
261
+ | --- | --- | --- |
262
+ | [`TileViewComponent`](api/tile-view.md#tileviewcomponent) | Component | A collection shown as tiles of one size, in rows that fill the width, like the icons of a file explorer. |
263
+ | [`TileViewItemTemplateDirective`](api/tile-view.md#tileviewitemtemplatedirective) | Directive | Template for each tile of a tile view. |
264
+ | [`TileViewNoDataTemplateDirective`](api/tile-view.md#tileviewnodatatemplatedirective) | Directive | Template shown in place of the tiles while there are no items. |
265
+
266
+ ### Types
267
+
268
+ | Type | Kind | Description |
269
+ | --- | --- | --- |
270
+ | [`TileViewItemActivateEvent`](api/tile-view.md#tileviewitemactivateevent) | Interface | A tile was activated: double-clicked, or Enter was pressed on it. |
271
+ | [`TileViewItemTemplateContext`](api/tile-view.md#tileviewitemtemplatecontext) | Interface | The context of a `monaTileViewItemTemplate`: the data item, as `let-item`, and the tile's state. |
272
+ | [`TileViewSelectionMode`](api/tile-view.md#tileviewselectionmode) | Type alias | How tiles are selected: not at all, one at a time, or several, as in a file explorer, with Ctrl and Shift. |