日本語

Stacks

A stack puts an element's children in a row or a column with a gap between them. Each stack is a function that returns a Style of flexbox declarations.

Four stacks

Each function of hypersig::style::pattern returns a plain Style, and El::css adds it to an element like any other style. The four stacks make the element a flex container and put gap between its children:

FunctionThe CSS it writes
stack(gap: impl Into<Gap>) -> Styledisplay: flex; flex-direction: column; gap: <gap>
vstack(gap: impl Into<Gap>) -> StyleThe declarations of stack, and align-items: center
hstack(gap: impl Into<Gap>) -> Styledisplay: flex; flex-direction: row; align-items: center; gap: <gap>
wrap(gap: impl Into<Gap>) -> Styledisplay: flex; flex-wrap: wrap; gap: <gap>

So stack puts the children in a column at their full width, and vstack centers each child across the column at its own width. hstack puts them in one row, centered on its height. wrap leaves flex-direction at its initial value row and moves the children that do not fit onto a new line.

gap takes a Gap, which converts from a length such as rem(1), a percentage such as pct(5), and raw. Gap::Normal writes gap: normal, which is no space between the children of a flex container.

Changing a pattern

Because a pattern is a Style, a property method called after it replaces the pattern's value for that property, and merge does the same with a whole style. hstack(rem(0.25)).align_items(AlignItems::Baseline) is a row whose texts of two sizes share a baseline. A pattern can also be the style under a condition: below, the buttons are a column on a phone and a row from the md breakpoint (48rem) up, where the rules under the media query override the base ones.

use hypersig::html::{button, div};
use hypersig::style::pattern::{hstack, stack};
use hypersig::style::value::{JustifyContent, rem};
use hypersig::style::Media;
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};

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

// A column on a phone, a row from `md` up, with the buttons at its right end.
let actions = div::<Checkout>()
    .css(
        stack(rem(0.5))
            .on(Media::Md, hstack(rem(1)).justify_content(JustifyContent::FlexEnd)),
    )
    .child(button().text("Back"))
    .child(button().text("Pay now"));
let html = Page::new("Checkout", Checkout {}).child(actions).render();
assert!(html.contains("{flex-direction:row}"));

Spacer and divider

FunctionThe CSS it writes
spacer() -> Styleflex-grow: 1; flex-shrink: 1; flex-basis: 0
divider(width: Length, color: Color) -> Styleborder-top-width: <width>; border-top-style: solid; border-top-color: <color>

An empty element with spacer() inside a stack takes the free space, so the siblings after it move to the far end of the row or the column. divider draws a line along the top edge of its element, as wide as the element.

What does not compile

Gap converts from values of CSS types only. A string has no conversion into it, and neither does a bare number, which carries no unit, so stack("1rem") and stack(16) are E0277:

use hypersig::style::pattern::stack;

// error[E0277]: the trait bound `hypersig::hypersig_style::value::Gap:
//     From<&str>` is not satisfied
let column = stack("1rem");

What the types leave unchecked

  • The range of a gap. stack(rem(-1)) compiles, the browser drops the negative gap, and the children touch.
  • Where spacer and divider sit. Outside a flex container the flex-* declarations of spacer do nothing. An empty element in an hstack has no width, so a divider on it draws no visible line; a vertical line between the items of a row takes border_left_width and the other border-left-* properties instead.

Example

A pricing card uses each function on this page:

use hypersig::html::{button, div, h2, p, small, span, strong};
use hypersig::style::pattern::{divider, hstack, spacer, stack, vstack, wrap};
use hypersig::style::value::{AlignItems, Color, px, rem};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};

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

// A row: the spacer pushes the button to the right end.
let header = div::<Plan>()
    .css(hstack(rem(0.75)))
    .child(h2().text("Team plan"))
    .child(div().css(spacer()))
    .child(button().text("Choose"));

// A row whose texts of two sizes share one baseline instead of the center.
let price = div::<Plan>()
    .css(hstack(rem(0.25)).align_items(AlignItems::Baseline))
    .child(strong().text("$12"))
    .child(small().text("per seat a month"));

// Labels in rows that wrap onto the next line when the box is too narrow.
let features = div::<Plan>()
    .css(wrap(rem(0.5)))
    .children(["SSO", "Audit log", "Priority support"].map(|f| span().text(f)));

// A column centered across its width.
let footer = div::<Plan>()
    .css(vstack(rem(0.25)))
    .child(p().text("Billed yearly"))
    .child(p().text("Cancel at any time"));

let card = div::<Plan>()
    .css(stack(rem(1)))
    .child(header)
    .child(price)
    .child(features)
    .child(div().css(divider(px(1), Color::LIGHTGRAY)))
    .child(footer);
let html = Page::new("Pricing", Plan {}).child(card).render();
assert!(html.contains("{align-items:baseline}"));

The demo below is built the same way, with a grid of tiles under the row:

Result

layout.rs
use hypersig::html::{div, span};
use hypersig::style::Style;
use hypersig::style::pattern::{center, circle, grid, hstack, spacer, stack};
use hypersig::style::value::{Color, FontWeight, px, rem};
use hypersig::{Page, Signals};
use hypersig_ui::{BadgeVariant, ButtonSize, ButtonVariant, badge, button, token};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Team {
    columns: i32,
}

pub(super) fn page() -> Page<Team> {
    let tile = || {
        div().css(
            center()
                .height(rem(3))
                .border_radius(px(6))
                .background_color(token::muted().var()),
        )
    };
    Page::new(
        "Team",
        Team { columns: 0 },
    )
    .child(
        div()
            .css(stack(rem(1.25)))
            .child(
                div()
                    .css(hstack(rem(0.75)))
                    .child(
                        div()
                            .css(
                                circle(rem(2.5))
                                    .background_color(Color::hex(0x0018_181b))
                                    .color(Color::WHITE),
                            )
                            .text("AL"),
                    )
                    .child(
                        span()
                            .css(Style::new().font_weight(FontWeight::Number(600.0)))
                            .text("Ada Lovelace"),
                    )
                    .child(badge(BadgeVariant::Secondary).text("Admin"))
                    .child(div().css(spacer()))
                    .child(
                        button(ButtonVariant::Outline, ButtonSize::Sm).text("Message"),
                    ),
            )
            .child(
                div()
                    .css(grid(3, rem(0.75)))
                    .children((1..=6).map(|n| tile().text(n.to_string()))),
            ),
    )
}

Next, Grids lays children out in columns of equal width, and Other patterns covers centering, fixed shapes, and hidden text.