日本語

Grids

grid lays children out in columns of equal width, and grid_item makes one child span several of them.

Equal columns

FunctionThe CSS it writes
grid(columns: u16, gap: impl Into<Gap>) -> Styledisplay: grid; grid-template-columns: repeat(<columns>, minmax(0, 1fr)); gap: <gap>
grid_item(columns: u16) -> Stylegrid-column: span <columns> / span <columns>

Each column is minmax(0, 1fr): its smallest width is 0, so a long word or a wide table inside a child wraps or overflows inside its column, and the columns keep equal widths. With plain 1fr, the column would widen to the child's content. The children fill the columns row by row, and the browser adds rows as needed. grid_item goes on a child and makes it span several columns of its parent's grid. The gap takes the same values as the gap of a stack.

Columns by breakpoint

A grid under Media::Md replaces the column count from 48rem up, and grid_item under the same condition spans columns only there:

use hypersig::html::{article, div, h3, p};
use hypersig::style::pattern::{grid, grid_item, stack};
use hypersig::style::value::rem;
use hypersig::style::{Media, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Dashboard {}

let card = |title: &str| {
    article::<Dashboard>()
        .css(stack(rem(0.5)))
        .child(h3().text(title))
        .child(p().text("…"))
};

// One column on a phone, four from the `md` breakpoint (48rem) up.
let cards = div::<Dashboard>()
    .css(grid(1, rem(1)).on(Media::Md, grid(4, rem(1.5))))
    // From `md` up, the chart spans two of the four columns.
    .child(card("Revenue").css(Style::new().on(Media::Md, grid_item(2))))
    .child(card("Visitors"))
    .child(card("Signups"));
let html = Page::new("Dashboard", Dashboard {}).child(cards).render();
assert!(html.contains("{grid-template-columns:repeat(4, minmax(0, 1fr))}"));

The base rule writes repeat(1, minmax(0, 1fr)), and the rule inside @media (min-width: 48rem) writes repeat(4, minmax(0, 1fr)), which wins at that width because it comes later in the stylesheet.

Columns from a signal

grid takes the column count as a u16 in Rust, so a count that changes in the browser picks one of several typed grids with El::css_match. Its cases override the element's own css while they apply:

use hypersig::html::{button, div};
use hypersig::style::pattern::grid;
use hypersig::style::value::rem;
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Copy, PartialEq)]
enum Density {
    Comfortable,
    Compact,
}

#[derive(Signals, Serialize, Deserialize)]
struct Photos {
    density: Density,
}

// Each value of the signal picks one typed grid.
let gallery = div::<Photos>()
    .css(grid(3, rem(1)))
    .css_match(
        Photos::density(),
        [
            (Density::Comfortable, grid(3, rem(1))),
            (Density::Compact, grid(6, rem(0.25))),
        ],
    );
let toggle = button::<Photos>()
    .on_click(Photos::density().set(&Density::Compact))
    .text("Compact");
let html = Page::new("Photos", Photos { density: Density::Comfortable })
    .child(toggle)
    .child(gallery)
    .render();
assert!(html.contains("repeat(6, minmax(0, 1fr))"));

What does not compile

The column count is a whole number. A fraction or a negative number is not a u16:

use hypersig::style::pattern::grid;
use hypersig::style::value::rem;

// error[E0308]: mismatched types
// expected `u16`, found floating-point number
let cards = grid(2.5, rem(1));

What the types leave unchecked

  • A count of 0. grid(0, gap) writes repeat(0, minmax(0, 1fr)) and grid_item(0) writes span 0 / span 0. The browser drops both declarations, so the grid has one column and the child spans one.
  • A span wider than the grid. grid_item(3) in a grid(2, ..) makes the browser add a column outside the template for it, so the grid no longer has two equal columns.
  • Templates of other shapes. A sidebar layout such as 200px minmax(0, 1fr) takes grid_template_columns(raw("200px minmax(0, 1fr)")), which Unchecked values covers.

Next, Other patterns covers centering, fixed shapes, a link that covers a card, and hidden text.