Applying a style
A Style is a value built one declaration at a time. El::css gives an element its classes, and a later declaration for the same property replaces an earlier one.
Building a style
Style::new() -> Style is an empty style. Each property method takes the style by value and returns it with one more declaration, as fn color(self, value: impl Into<Color>) -> Self does, so the calls chain. The methods are #[must_use], so a call whose result goes unused is a warning.
A Style is an ordinary Rust value. It implements Clone, Default, PartialEq, and Debug, so a function can return one, a test can compare two, and a component can take one as an argument. Style::is_empty tells whether it has any declaration.
Adding a style to an element
El::css(self, style: Style) -> Self gives the element one class per declaration. The page writes the rule of each class once into <head>, however many elements use it. The three rows below share one rule for each of their four declarations:
use hypersig::html::{li, ul};
use hypersig::style::Style;
use hypersig::style::value::{BorderBottomStyle, Color, px, rem};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Inbox {}
fn row() -> Style {
Style::new()
.padding_block(rem(0.5))
.border_bottom_width(px(1))
.border_bottom_style(BorderBottomStyle::Solid)
.border_bottom_color(Color::GAINSBORO)
}
let html = Page::new("Inbox", Inbox {})
.child(ul().children(["Rent", "Tea", "Train"].map(|text| li().css(row()).text(text))))
.render();
// Three rows use the style, and <head> holds each declaration once.
assert_eq!(html.matches("{padding-block:0.5rem}").count(), 1);Each <li> gets these declarations:
padding-block: 0.5rem;
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: gainsboro;A patch that adds elements to the page carries the rules of their classes, so an element the server sends later is styled when it arrives.
Combining two styles
Style::merge(self, style: Style) -> Self adds the declarations of another style. Where both set the same property under the same conditions, the added one replaces the original, as a later declaration in a CSS rule does. A second call to the same property method replaces the first in the same way, and so does a second El::css call on one element.
use hypersig::style::Style;
use hypersig::style::value::{Color, Cursor, px, rem};
fn button() -> Style {
Style::new()
.padding_inline(rem(1))
.border_radius(px(6))
.background_color(Color::hex(0x0018_181b))
.color(Color::WHITE)
.cursor(Cursor::Pointer)
}
// The added style's `background-color` replaces the first one's.
let danger = button().merge(Style::new().background_color(Color::hex(0x00dc_2626)));
assert_eq!(danger, button().background_color(Color::hex(0x00dc_2626)));danger holds five declarations, and only the background differs from button():
padding-inline: 1rem;
border-radius: 6px;
background-color: #dc2626;
color: white;
cursor: pointer;A declaration under a condition, such as one inside on(PseudoClass::Hover, ...), does not replace the declaration without it. Both stay, and the conditioned one applies while its condition holds. Pseudo-classes covers conditions.
A style is moved into the element
El::css takes the Style by value, so one variable can be passed only once. A second use is E0382:
use hypersig::Signals;
use hypersig::html::{button, div};
use hypersig::style::Style;
use hypersig::style::value::rem;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Form {}
let pill = Style::new().padding_inline(rem(1)).border_radius(rem(1));
// error[E0382]: use of moved value: `pill`
let actions = div::<Form>()
.child(button().css(pill).text("Save"))
.child(button().css(pill).text("Cancel"));Write the style in a function that returns a new Style each time, as row() does above, or pass pill.clone() to all but the last use.
Which declaration wins
Within one style, a later call replaces an earlier one only for the same property. A shorthand and one of its longhands are different properties, so both stay, and the browser applies the longhand over the shorthand whatever the order of the calls:
use hypersig::html::div;
use hypersig::style::Style;
use hypersig::style::value::{px, rem};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Card {}
// `padding` is called last, and the top padding is still 0.
let card = Style::new().padding_top(px(0)).padding(rem(1));
let html = Page::new("Card", Card {}).child(div().css(card)).render();
let shorthand = html.find("{padding:1rem}").expect("the shorthand has a rule");
let longhand = html.find("{padding-top:0}").expect("the longhand has a rule");
assert!(shorthand < longhand);The stylesheet holds padding: 1rem before padding-top: 0, so the box has 1rem of padding on three sides and none on top. Between styles from different sources, the browser applies them in this order, each overriding the ones above it:
| Source | Page |
|---|---|
The rules of a Global, such as the reset | Rules by tag |
The classes of El::css | This page |
The classes of El::css_when and El::css_match, while switched on | Styles switched by a signal |
A stylesheet added with Page::style | Unchecked values |
The order does not depend on the selectors' specificity. A rule of Page::style that selects by tag overrides a class of El::css:
use hypersig::html::{Stylesheet, button};
use hypersig::style::Style;
use hypersig::style::value::Color;
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Shop {}
const PRINT: Stylesheet = Stylesheet::new("button { color: black }");
// The button is black: the rule of `Page::style` overrides the class.
let page = Page::new("Shop", Shop {})
.style(PRINT)
.child(button().css(Style::new().color(Color::CRIMSON)).text("Buy"));Nothing compares these sources with each other. A Page::style rule that sets a property silently overrides every El::css declaration of that property on the elements it selects.
Example: buttons in two tones
button_style returns the shared base merged with what the tone changes. The match must cover every Tone, so a new tone fails to compile until it has a style. The second css call adds margin-left: auto to one button only, which pushes it to the end of the row.
use hypersig::html::{El, button, div};
use hypersig::style::Style;
use hypersig::style::value::{Color, Cursor, Display, FontWeight, MarginLeft, px, rem};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Settings {}
#[derive(Clone, Copy)]
enum Tone {
Neutral,
Danger,
}
/// The style of a button of `tone`: the shared base, then what the tone changes.
fn button_style(tone: Tone) -> Style {
let base = Style::new()
.padding_block(rem(0.5))
.padding_inline(rem(1))
.border_width(px(0))
.border_radius(px(6))
.font_weight(500)
.cursor(Cursor::Pointer);
base.merge(match tone {
Tone::Neutral => Style::new()
.background_color(Color::hex(0x00f4_f4f5))
.color(Color::hex(0x0018_181b)),
Tone::Danger => Style::new()
.background_color(Color::hex(0x00dc_2626))
.color(Color::WHITE)
.font_weight(FontWeight::Bold),
})
}
fn actions() -> El<Settings> {
div()
.css(Style::new().display(Display::Flex).gap(rem(0.5)))
.child(button().css(button_style(Tone::Neutral)).text("Cancel"))
.child(
button()
.css(button_style(Tone::Danger))
.css(Style::new().margin_left(MarginLeft::Auto))
.text("Delete account"),
)
}
let html = Page::new("Settings", Settings {}).child(actions()).render();Next, Properties covers the property methods and the value types they take.