Group and peer
Condition::Group and Condition::Peer apply a style while a marked ancestor, or a marked sibling before the element, matches a pseudo-class.
The state of another element
A pseudo-class passed to on tests the element itself. Two variants of Condition test another element, which a method of Style marks first:
Style::group,fn group(self) -> Style, marks an ancestor, andCondition::Group(PseudoClass)applies a style while a marked ancestor matches the pseudo-class.Style::peer,fn peer(self) -> Style, marks a sibling, andCondition::Peer(PseudoClass)applies a style while a marked sibling before the element matches the pseudo-class.
Both work like the group and peer of Tailwind CSS and Panda CSS. Below, the arrow shows while the pointer is anywhere on the card, and the label turns green while the checkbox before it is checked:
use hypersig::html::{a, div, input, label, span, attr};
use hypersig::style::value::{Color, Opacity};
use hypersig::style::{Condition, PseudoClass, Style};
use hypersig::{Page, SafeUrl, Signals};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Settings {
agreed: bool,
}
// The arrow shows while the pointer is anywhere on the card.
let card = a::<Settings>()
.href(SafeUrl::new("/docs"))
.css(Style::new().group())
.text("Read the docs")
.child(
span()
.css(
Style::new()
.opacity(Opacity::Number(0.0))
.on(Condition::Group(PseudoClass::Hover), Style::new().opacity(Opacity::Number(1.0))),
)
.text(" →"),
);
// The text turns green while the checkbox before it is checked.
let agree = label::<Settings>()
.child(input().attr(attr::TYPE, "checkbox").css(Style::new().peer()).bind(Settings::agreed()))
.child(
span()
.css(Style::new().on(Condition::Peer(PseudoClass::Checked), Style::new().color(Color::GREEN)))
.text("I agree"),
);Writing .card for the marked link, .arrow for the span inside it, .box for the marked checkbox, and .agree for the span after it, the browser applies:
.arrow { opacity: 0 }
.card:hover .arrow { opacity: 1 }
.box:checked ~ .agree { color: green }The demo below is built the same way, with the components' tokens:
Result
use hypersig::html::{a, div, input, label, p, span, attr};
use hypersig::style::pattern::stack;
use hypersig::style::value::{
BorderStyle, Color, FontWeight, Opacity, TextDecorationLine, px, rem,
};
use hypersig::style::{Condition, PseudoClass, Style};
use hypersig::{Page, SafeUrl, Signals};
use hypersig_ui::token;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Settings {
agreed: bool,
}
pub(super) fn page() -> Page<Settings> {
let card = a()
.href(SafeUrl::new("#"))
.css(
stack(rem(0.25))
.group()
.padding(rem(1))
.border_width(px(1))
.border_style(BorderStyle::Solid)
.border_color(token::border().var())
.border_radius(px(8))
.on(
PseudoClass::Hover,
Style::new().background_color(token::muted().var()),
),
)
.child(
p().css(Style::new().font_weight(FontWeight::Number(600.0)))
.text("Read the docs")
.child(
span()
.css(Style::new().opacity(Opacity::Number(0.0)).on(
Condition::Group(PseudoClass::Hover),
Style::new().opacity(Opacity::Number(1.0)),
))
.text(" →"),
),
)
.child(
p().css(Style::new().color(token::muted_foreground().var()).on(
Condition::Group(PseudoClass::Hover),
Style::new().text_decoration_line(TextDecorationLine::Underline),
))
.text("Hover anywhere on the card."),
);
let agree = label()
.child(
input()
.attr(attr::TYPE, "checkbox")
.css(Style::new().peer())
.bind(Settings::agreed()),
)
.child(
span()
.css(
Style::new().margin_left(rem(0.5)).on(
Condition::Peer(PseudoClass::Checked),
Style::new()
.color(Color::hex(0x0015_803d))
.font_weight(FontWeight::Number(600.0)),
),
)
.text("I agree to the terms"),
);
Page::new(
"Settings",
Settings { agreed: false },
)
.child(
div()
.css(stack(rem(1.25)).max_width(rem(20)))
.child(card)
.child(agree),
)
}Only a pseudo-class
Group and Peer hold a PseudoClass. A media query is the same for every element on the page, so a group or peer condition on one is a compile error:
use hypersig::style::value::Opacity;
use hypersig::style::{Condition, Media, Style};
// error[E0308]: mismatched types: expected `PseudoClass`, found `Media`
let arrow = Style::new().on(Condition::Group(Media::Dark), Style::new().opacity(Opacity::Number(1.0)));Group and peer conditions combine with the others. on(Media::Md, Style::new().on(Condition::Group(PseudoClass::Hover), ..)) applies from 48rem up while a marked ancestor is hovered.
What the types leave unchecked
- Nothing checks that a marked element is there. Without a marked ancestor, or without a marked sibling before the element, the condition never matches.
- A peer has to come before the element and share its parent. A marked sibling after the element, or a marked element elsewhere on the page, does not count.
- Any marked ancestor counts. In a card marked with
groupinside another, hovering the outer card also applies the group styles of the elements inside the inner one. - A rule of a
Globalselects elements by tag and has no marked element, soPage::renderpanics when one uses a group or peer condition. Rules by tag shows it.
Next
Styles switched by a signal applies a style while a signal holds a value, in place of a pseudo-class.