English

ボタンの見た目のリンク

button_style はリンクにボタンの見た目を与えます。要素は <a> のままなので、ページを移動し、スクリーンリーダーもリンクとして読み上げます。

実行結果

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 {},
    )
    .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("プランを見る"),
                    )
                    .child(
                        a().href(PLANS)
                            .css(button_style(ButtonVariant::Outline, ButtonSize::Default))
                            .text("比べる"),
                    )
                    .child(
                        button(ButtonVariant::Outline, ButtonSize::Default).text("ボタン"),
                    ),
            )
            .child(p().id(hypersig::html::Id::new("plans")).text("Hobby は無料で、Pro は月 20 ドルです。")),
    )
}

アクセシビリティ

別のページへ移動するものは、ボタンの見た目でもリンクにします。リンクならスクリーンリーダーはリンクとして読み上げ、Enter で移動し、ブラウザは新しいタブで開く操作やアドレスのコピーを提供します。<button> にはどれもありません。上のデモには 2 つのリンクと 1 つのボタンがあり、これらがないのはボタンだけです。

キー動作
Enterリンク先に移動します。Space はほかのリンクと同じくページをスクロールします。

`button_style`

button_style は、同じ ButtonVariant と ButtonSize で button が <button> に付けるのと同じ Style を返します。これを El::css で <a> に付けます。

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

付くのはクラスだけなので、<a> は自分の属性を保ちます。a().href(SIGNUP.link()).css(button_style(ButtonVariant::Default, ButtonSize::Lg)).text("Sign up") は、class を除くと次のように描画されます。

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

ほかのリンクと同じく、アドレスはルートか SafeUrl から取ります。href には &str からの変換がないので、文字列で書いたアドレスは 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 自体も button と同じ 2 つの列挙型を受け取るので、バリアントを文字列で書くと E0308 でコンパイルできません。ボタンにその例があります。

型では検査しないこと

  • button_style は Style なので、どの要素にも付けられます。<div> や <span> に付けると、ボタンのように見えても、フォーカスを受け取らず、Enter と Space に反応せず、ロールもありません。href を持つ <a> に付けます。
  • href のない <a> はリンクではありません。フォーカスを受け取らず、スクリーンリーダーもリンクとして読み上げません。
  • El::disabled はリンクには効きません。移動させたくないリンクは置かないか、テキストとして表示します。
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"),
        ),
);

次に読むページ

ルートから href を作る方法はリンクとリダイレクトで説明しています。ページネーションはページへのリンクを同じスタイルで描きます。