Pseudo-classes
Style::on with a PseudoClass applies part of a style only while the element is hovered, focused, checked, disabled, or in another state CSS names.
A style under a pseudo-class
Style::on, fn on(self, condition: impl Into<Condition>, style: Style) -> Style, adds the declarations of style and applies them only while condition holds. A PseudoClass converts into a Condition, so it can be passed as it is. The button below changes its background while hovered and pressed, shows an outline while focused from the keyboard, and dims while disabled:
use hypersig::html::button;
use hypersig::style::value::{Color, Cursor, Opacity, OutlineStyle, px, rem};
use hypersig::style::{PseudoClass, Style};
use hypersig::{Page, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Form {
saving: bool,
}
let save = button::<Form>()
.css(
Style::new()
.padding(rem(0.5))
.background_color(Color::hex(0x0025_63eb))
.color(Color::WHITE)
.cursor(Cursor::Pointer)
.on(PseudoClass::Hover, Style::new().background_color(Color::hex(0x001d_4ed8)))
.on(PseudoClass::Active, Style::new().background_color(Color::hex(0x001e_3a8a)))
.on(
PseudoClass::FocusVisible,
Style::new().outline_width(px(2)).outline_style(OutlineStyle::Solid),
)
.on(
PseudoClass::Disabled,
Style::new().opacity(Opacity::Number(0.5)).cursor(Cursor::NotAllowed),
),
)
.disabled(Form::saving())
.text("Save");Writing .save for the button, the browser applies these declarations as this CSS would:
.save { padding: 0.5rem; background-color: #2563eb; color: white; cursor: pointer }
.save:hover { background-color: #1d4ed8 }
.save:focus-visible { outline-width: 2px; outline-style: solid }
.save:active { background-color: #1e3a8a }
.save:disabled { opacity: 0.5; cursor: not-allowed }Which pseudo-class wins
A pressed button is hovered too, so :hover and :active both match it. The rules for the user-action pseudo-classes are written in a fixed order, whatever the order of the on calls, and the later rule wins where two set the same property:
| Order | Pseudo-classes |
|---|---|
| First | Every pseudo-class not listed below |
| Then | Link, Visited, FocusWithin, Hover, Focus, FocusVisible, Active |
| Last | Disabled |
So :active wins over :hover while the button is pressed, and :disabled wins over all of them. The order follows the link, visited, hover, focus, active order that CSS authors use for links.
Two conditions at once
The style passed to on can call on itself, and its declarations then apply only while both conditions hold. on(Visited, Style::new().on(Hover, ..)) and on(Hover, Style::new().on(Visited, ..)) produce the same rule, :visited:hover:
use hypersig::style::value::{Color, TextDecorationLine};
use hypersig::style::{PseudoClass, Style};
let link = Style::new()
.color(Color::hex(0x0025_63eb))
// `:hover`.
.on(PseudoClass::Hover, Style::new().text_decoration_line(TextDecorationLine::Underline))
// `:visited`, and `:visited:hover` for both at once.
.on(
PseudoClass::Visited,
Style::new()
.color(Color::hex(0x006d_28d9))
.on(PseudoClass::Hover, Style::new().color(Color::hex(0x004c_1d95))),
);.link { color: #2563eb }
.link:hover { text-decoration-line: underline }
.link:visited { color: #6d28d9 }
.link:visited:hover { color: #4c1d95 }A misspelled pseudo-class
PseudoClass has one variant per standard pseudo-class without arguments that can match an element's class, generated from MDN's CSS data, such as Hover, Checked, PlaceholderShown, UserInvalid, and PopoverOpen. A pseudo-class that does not exist is a compile error:
use hypersig::style::value::Color;
use hypersig::style::{PseudoClass, Style};
// error[E0599]: no variant, associated function, or constant named `Hovered` found for enum `PseudoClass`
let link = Style::new().on(PseudoClass::Hovered, Style::new().color(Color::RED));Six standard pseudo-classes without arguments have no variant, because a class on an element never matches them. :root and :scope match only <html>, and a Global rule for Selector::Root styles that element instead. :host matches only from inside a shadow tree, and :first, :left, and :right select a page box in @page.
What the types leave unchecked
- Nothing checks that the pseudo-class can match the element.
:checkedon a<div>or:placeholder-shownon a<button>compiles, and the declarations never apply. - A browser that does not know a pseudo-class, such as an older one with
:open, drops the rule that uses it. Each rule holds one declaration, so only that declaration is lost, and the element's other declarations still apply. - The pseudo-classes that take an argument, such as
:nth-child(),:not(), and:has(), have no variant. A rule that needs one goes in a plain stylesheet, which Unchecked values covers.
Next
Pseudo-elements styles a part of an element, such as ::before or ::placeholder. Group and peer applies a style while a pseudo-class matches another element.