日本語

Link styled as a button

button_style gives a link the look of a button. It stays an <a>, so it navigates and a screen reader announces it as a link.

Result

link_button.rs
use hypersig::html::{a, div, p};
use hypersig::style::pattern::{stack, wrap};
use hypersig::style::value::rem;
use hypersig::{Page, SafeUrl, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, button, button_style};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Plans {}

/// The part of this page that the links lead to, since the frame has no
/// other page to open.
const PLANS: SafeUrl = SafeUrl::new("#plans");

pub(super) fn page() -> Page<Plans> {
    Page::new(
        "Plans",
        Plans {},
    )
    .child(
        div()
            .css(stack(rem(1)))
            .child(
                div()
                    .css(wrap(rem(0.5)))
                    .child(
                        a().href(PLANS)
                            .css(button_style(ButtonVariant::Default, ButtonSize::Default))
                            .text("See the plans"),
                    )
                    .child(
                        a().href(PLANS)
                            .css(button_style(ButtonVariant::Outline, ButtonSize::Default))
                            .text("Compare"),
                    )
                    .child(
                        button(ButtonVariant::Outline, ButtonSize::Default).text("A button"),
                    ),
            )
            .child(p().id(hypersig::html::Id::new("plans")).text("Hobby is free, and Pro is $20 a month.")),
    )
}

Accessibility

A link that goes to another page should be a link, even where it looks like a button: a screen reader announces it as a link, Enter follows it, and the browser offers to open it in a new tab or copy its address. A <button> does none of these. The demo above has two links and one button, and only the button lacks them.

KeyWhat it does
EnterFollows the link. Space scrolls the page, as on any link.

`button_style`

button_style returns the Style that button puts on its <button>, for the same ButtonVariant and ButtonSize. Put it on an <a> with El::css:

pub fn button_style(variant: ButtonVariant, size: ButtonSize) -> Style

It adds only a class, so the <a> keeps its own attributes. a().href(SIGNUP.link()).css(button_style(ButtonVariant::Default, ButtonSize::Lg)).text("Sign up") renders as follows, without the class:

<a href="/signup">Sign up</a>

The address comes from a route or a SafeUrl, as on any link. href has no conversion from &str, so an address written as text fails to compile with E0277:

use hypersig::Signals;
use hypersig::html::a;
use hypersig_ui::{ButtonSize, ButtonVariant, button_style};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct App {}

// The address written as a string rather than taken from a route.
let signup = a::<App>()
    .href("/signup")
    .css(button_style(ButtonVariant::Default, ButtonSize::Lg))
    .text("Sign up");

button_style itself takes the two enums of button, so a variant written as text fails to compile with E0308, as Button shows.

What the types leave unchecked

  • button_style is a Style and goes on any element. On a <div> or a <span>, the element looks like a button but takes no focus, ignores Enter and Space, and has no role. Put it on an <a> with an href.
  • An <a> without href is not a link: it takes no focus and a screen reader does not announce it as a link.
  • El::disabled does not apply to a link. A link that should not be followed is left out, or shown as text.
use hypersig::html::{a, div, h1};
use hypersig::{Page, Route, SafeUrl, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, button_style};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Home {}

hypersig::routes! {
    fn pages(Router<()>);
    const HOME: Route = "/" => home;
    const SIGNUP: Route = "/signup" => signup;
}

// In a `const`, a URL whose scheme can run a script fails to compile.
const SOURCE: SafeUrl = SafeUrl::new("https://github.com/imaimai17468/hypersig");

let page = Page::new("Home", Home {}).child(
    div()
        .child(h1().text("Plan your week"))
        // A link to a page of this site, from its route.
        .child(
            a().href(SIGNUP.link())
                .css(button_style(ButtonVariant::Default, ButtonSize::Lg))
                .text("Sign up"),
        )
        // A link to another site.
        .child(
            a().href(SOURCE)
                .css(button_style(ButtonVariant::Outline, ButtonSize::Lg))
                .text("Source"),
        ),
);

Next

Links and redirects builds an href from a route. Pagination draws its links to the pages with the same style.