スタイルを付ける
Style は宣言を 1 つずつ加えて組み立てる値です。El::css で要素にそのクラスを付けます。同じプロパティの宣言は後のものが前のものを置き換えます。
スタイルを組み立てる
Style::new() -> Style は空のスタイルです。プロパティのメソッドは fn color(self, value: impl Into<Color>) -> Self のように、スタイルを値で受け取り、宣言を 1 つ加えて返すので、呼び出しをつなげて書けます。メソッドには #[must_use] が付いているので、結果を使わない呼び出しは警告になります。
Style は普通の Rust の値です。Clone、Default、PartialEq、Debug を実装しているので、関数から返すことも、テストで 2 つを比べることも、コンポーネントの引数に取ることもできます。宣言があるかどうかは Style::is_empty で調べられます。
要素にスタイルを付ける
El::css(self, style: Style) -> Self は、宣言 1 つごとに 1 つのクラスを要素に付けます。ページは各クラスのルールを、使う要素がいくつあっても <head> に 1 回だけ書きます。下の 3 つの行は 4 つの宣言のそれぞれについて 1 つのルールを共有します。
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);それぞれの <li> には次の宣言が適用されます。
padding-block: 0.5rem;
border-bottom-width: 1px;
border-bottom-style: solid;
border-bottom-color: gainsboro;ページに要素を追加するパッチには、その要素のクラスのルールも含まれます。そのため、サーバーが後から送った要素も、届いた時点でスタイルが付いています。
2 つのスタイルを組み合わせる
Style::merge(self, style: Style) -> Self は別のスタイルの宣言を加えます。同じ条件の下で両方が同じプロパティを設定している場合、CSS のルールで後に書いた宣言と同じように、加えた側が元の宣言を置き換えます。同じプロパティのメソッドを 2 回呼んだときも、1 つの要素で El::css を 2 回呼んだときも、同じように置き換わります。
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 には 5 つの宣言があり、button() と違うのは背景だけです。
padding-inline: 1rem;
border-radius: 6px;
background-color: #dc2626;
color: white;
cursor: pointer;on(PseudoClass::Hover, ...) の中のように条件の付いた宣言は、条件のない宣言を置き換えません。両方が残り、条件の付いた方はその条件が成り立つ間だけ適用されます。条件は擬似クラスで説明します。
スタイルは要素にムーブされる
El::css は Style を値で受け取るので、1 つの変数を渡せるのは 1 回だけです。2 回目に使うと 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"));上の row() のように、呼ぶたびに新しい Style を返す関数にスタイルを書くか、最後以外の箇所に pill.clone() を渡します。
どの宣言が勝つか
1 つのスタイルの中で、後の呼び出しが前の呼び出しを置き換えるのは同じプロパティの場合だけです。ショートハンドとそのロングハンドは別のプロパティなので両方が残り、呼び出しの順序にかかわらず、ブラウザはロングハンドをショートハンドより優先して適用します。
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);スタイルシートでは padding: 1rem が padding-top: 0 より前にあるので、ボックスの余白は 3 辺が 1rem で、上はなしになります。出どころの違うスタイルの間では、ブラウザは次の順に適用し、後のものが前のものを上書きします。
| 出どころ | ページ |
|---|---|
リセットなど、Global のルール | タグ単位のルール |
El::css のクラス | このページ |
有効になっている間の El::css_when と El::css_match のクラス | シグナルで切り替えるスタイル |
Page::style で追加したスタイルシート | 検査しない値 |
この順序はセレクタの詳細度によりません。Page::style のルールはタグで選ぶものでも 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"));これらの出どころ同士は照合されません。あるプロパティを設定する Page::style のルールは、そのルールが選ぶ要素にある El::css の同じプロパティの宣言を、警告なしにすべて上書きします。
例: 2 つの色調のボタン
button_style は共通のスタイルに、色調ごとに変わる部分を merge して返します。match はすべての Tone を扱う必要があるので、色調を増やすと、そのスタイルを書くまでコンパイルが通りません。2 回目の css の呼び出しは 1 つのボタンだけに margin-left: auto を加え、そのボタンを行の端に寄せます。
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();次のプロパティではプロパティのメソッドと、メソッドが受け取る値の型を説明します。