SPB Git

Repository Public MIT

glass-data-grid.md 3.4 KB

# GlassDataGrid

Identifier glass-data-grid
Module SPGlassData
Kind content-component — a lazy grid of host content; no glass on cells
Platforms iOS 26, iPadOS 26, macOS 26
Example Examples/Gallery/Data/GlassDataGridExample.swift

# Minimal usage

swift
GlassDataGrid(photos) { photo in
    PhotoCell(photo: photo)
}

# Variants

Variant Use
layout: .adaptive(minimum:maximum:) As many tracks as fit the width; the default is .adaptive(minimum: 140).
layout: .fixed(n) Exactly n tracks of equal size.
axis: .vertical LazyVGrid in a vertical scroll view (default).
axis: .horizontal LazyHGrid in a horizontal scroll view; give the grid a bounded height.
With selection Every cell becomes a toggle button; the cell closure receives isSelected.

# Parameters

Parameter Type Default Meaning
data RandomAccessCollection of Identifiable — The elements. Identifiers must be stable and unique.
layout GlassGridLayout .adaptive(minimum: 140) Track sizing.
axis Axis .vertical Scrolling direction.
spacing GlassSpacingStep .sm Gap between cells and around the grid, from the theme's scale.
selection Binding<Set<Element.ID>>? nil Selected identifiers.
emptyTitle LocalizedStringKey? nil Text of the empty state; defaults to "No data".
cell @ViewBuilder (Element) -> Cell or (Element, Bool) -> Cell — Content of one cell.

# States

  • Empty: a ContentUnavailableView with emptyTitle.
  • 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.
  • Pressed and focused states of selectable cells are those of a plain system button.

# Advanced example

swift
/// A collection that produces its elements on demand: nothing is allocated for 30 000 samples.
struct Samples: RandomAccessCollection {
    let count: Int
    var startIndex: Int { 0 }
    var endIndex: Int { count }
    subscript(position: Int) -> Sample { Sample(id: position) }
}

@State private var selection: Set<Int> = []

GlassDataGrid(Samples(count: 30_000), layout: .fixed(4), spacing: .xs, selection: $selection) { sample, isSelected in
    SampleCell(sample: sample)
        .opacity(isSelected ? 1 : 0.85)
}

# Accessibility

  • Cells keep the accessibility of the host's content. Selectable cells are buttons with the .isSelected trait.
  • Selection is shown by a checkmark symbol, not by colour alone.
  • 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.

# Limits

  • 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.
  • The grid owns its scroll view; do not nest it in another scroll view of the same axis.
  • No prefetching, sections, reordering or drag and drop.
  • Do not put a glass surface in each cell: glass is for controls and navigation layers, and one glass effect per cell is costly.
  • 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.
Files main
Menu

Theme