CSS 全体のキーワード
inherit、initial、unset、revert、revert-layer はどのプロパティにも使えるので、Style::keyword はプロパティを Property の値として受け取ります。
キーワード
CSS には、どのプロパティでも使える 5 つのキーワードがあります。CssWideKeyword にはそれぞれに対応するバリアントがあります。
| バリアント | CSS | プロパティが取る値 |
|---|---|---|
Inherit | inherit | 親の計算済みの値 |
Initial | initial | そのプロパティの初期値 |
Unset | unset | color のように継承されるプロパティでは inherit、それ以外では initial |
Revert | revert | ブラウザかユーザーのスタイルシートの値 |
RevertLayer | revert-layer | 1 つ前のカスケードレイヤーの値 |
El::css の宣言の場合、それより前のレイヤーには Global のルールがあります。そのため revert-layer は、Global のルールがその要素に設定する値を、そうしたルールがなければブラウザの既定の値を要素に与えます。
プロパティをキーワードに設定する
Style::keyword(self, property: Property, value: CssWideKeyword) -> Self は property: keyword を書きます。プロパティは Property のバリアントで指定するので、どのプロパティにも 5 つのどれでも設定できます。プロパティのメソッドを 2 回呼んだときと同じく、同じプロパティの前の値を置き換えます。
use hypersig::html::div;
use hypersig::style::value::{Color, CssWideKeyword};
use hypersig::style::{Property, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Note {}
let style = Style::new()
.color(Color::CRIMSON)
// Replaces `color: crimson`, as a second `color` call would.
.keyword(Property::Color, CssWideKeyword::Inherit)
.keyword(Property::Margin, CssWideKeyword::Revert);
let html = Page::new("Note", Note {}).child(div().css(style)).render();
assert!(html.contains("{color:inherit}") && !html.contains("{color:crimson}"));
assert!(html.contains("{margin:revert}"));要素には color: inherit と margin: revert が適用されます。条件の下でキーワードを設定するには、on に渡すスタイルで keyword を呼びます。
コンパイルできないもの
プロパティのメソッドは CssWideKeyword を受け取りません。値の型にそこからの変換がないからで、.color(CssWideKeyword::Inherit) は E0277 になります。Property::Color を指定して keyword を使います。
use hypersig::style::Style;
use hypersig::style::value::CssWideKeyword;
// error[E0277]: the trait bound `hypersig::hypersig_style::value::Color: From<CssWideKeyword>`
// is not satisfied
let link = Style::new().color(CssWideKeyword::Inherit);Property::Colour のように綴りを間違えた Property のバリアントは存在しないので、E0599 になります。
`all` によるリセット
all は direction と unicode-bidi を除くすべてのプロパティのショートハンドで、keyword(Property::All, CssWideKeyword::Unset) はそれらを戻します。スタイルシートは padding を padding-top より前に書くのと同じように、all を戻す対象のプロパティより前に書きます。そのため 1 つのスタイルの中では、呼び出しの順序にかかわらず、color や accent-color などの宣言が all: unset に勝ちます。下の例はこの順序を前提にしています。
例: リンクのように見えるボタン
all: unset でブラウザのボタンの見た目を消し、inherit で周りの文と同じ色とフォントにします。下線とポインタのカーソルを付けると、リンクのように見えます。
use hypersig::html::{El, button, p};
use hypersig::style::value::{CssWideKeyword, Cursor, TextDecorationLine};
use hypersig::style::{Property, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Terms {}
/// A button that reads as a link inside a sentence: the browser's button
/// look removed, then the surrounding text's color and font.
fn text_button(label: &str) -> El<Terms> {
button()
.css(
Style::new()
.keyword(Property::All, CssWideKeyword::Unset)
.keyword(Property::Color, CssWideKeyword::Inherit)
.keyword(Property::Font, CssWideKeyword::Inherit)
.text_decoration_line(TextDecorationLine::Underline)
.cursor(Cursor::Pointer),
)
.text(label)
}
let html = Page::new("Terms", Terms {})
.child(p().text("By signing up you accept the ").child(text_button("terms")))
.render();次の擬似クラスではポインタが要素の上にある間など、条件の下でだけスタイルを適用する方法を説明します。