SPB Git

Repository Public MIT

glass-data-grid.md 3.4 KB 79 lines markdown

1# GlassDataGrid23| | |4|---|---|5| Identifier | `glass-data-grid` |6| Module | `SPGlassData` |7| Kind | `content-component` — a lazy grid of host content; no glass on cells |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Data/GlassDataGridExample.swift` |1011## Minimal usage1213```swift14GlassDataGrid(photos) { photo in15    PhotoCell(photo: photo)16}17```1819## Variants2021| Variant | Use |22|---|---|23| `layout: .adaptive(minimum:maximum:)` | As many tracks as fit the width; the default is `.adaptive(minimum: 140)`. |24| `layout: .fixed(n)` | Exactly `n` tracks of equal size. |25| `axis: .vertical` | `LazyVGrid` in a vertical scroll view (default). |26| `axis: .horizontal` | `LazyHGrid` in a horizontal scroll view; give the grid a bounded height. |27| With `selection` | Every cell becomes a toggle button; the cell closure receives `isSelected`. |2829## Parameters3031| Parameter | Type | Default | Meaning |32|---|---|---|---|33| `data` | `RandomAccessCollection` of `Identifiable` | — | The elements. Identifiers must be stable and unique. |34| `layout` | `GlassGridLayout` | `.adaptive(minimum: 140)` | Track sizing. |35| `axis` | `Axis` | `.vertical` | Scrolling direction. |36| `spacing` | `GlassSpacingStep` | `.sm` | Gap between cells and around the grid, from the theme's scale. |37| `selection` | `Binding<Set<Element.ID>>?` | `nil` | Selected identifiers. |38| `emptyTitle` | `LocalizedStringKey?` | `nil` | Text of the empty state; defaults to "No data". |39| `cell` | `@ViewBuilder (Element) -> Cell` or `(Element, Bool) -> Cell` | — | Content of one cell. |4041## States4243- **Empty**: a `ContentUnavailableView` with `emptyTitle`.44- **Selected** (with a selection binding): a checkmark badge in the top trailing corner and the `.isSelected` trait; the cell can also change its own appearance from `isSelected`.45- Pressed and focused states of selectable cells are those of a plain system button.4647## Advanced example4849```swift50/// A collection that produces its elements on demand: nothing is allocated for 30 000 samples.51struct Samples: RandomAccessCollection {52    let count: Int53    var startIndex: Int { 0 }54    var endIndex: Int { count }55    subscript(position: Int) -> Sample { Sample(id: position) }56}5758@State private var selection: Set<Int> = []5960GlassDataGrid(Samples(count: 30_000), layout: .fixed(4), spacing: .xs, selection: $selection) { sample, isSelected in61    SampleCell(sample: sample)62        .opacity(isSelected ? 1 : 0.85)63}64```6566## Accessibility6768- Cells keep the accessibility of the host's content. Selectable cells are buttons with the `.isSelected` trait.69- Selection is shown by a checkmark symbol, not by colour alone.70- An adaptive layout reflows with the window width and with Dynamic Type if the host's `minimum` leaves room for larger text; prefer adaptive layouts.7172## Limits7374- Laziness depends on the collection: indexing and `count` must be constant-time, and identifiers stable. The grid performs no per-element work in `body` and never copies the collection into an array.75- The grid owns its scroll view; do not nest it in another scroll view of the same axis.76- No prefetching, sections, reordering or drag and drop.77- Do not put a glass surface in each cell: glass is for controls and navigation layers, and one glass effect per cell is costly.78- Scrolling performance with tens of thousands of elements was not measured with Instruments in this phase; only the absence of eager work at construction is covered by a test.79
Files main
Menu

Theme