Skip to content

Latest commit

 

History

History
198 lines (144 loc) · 9.17 KB

button.md

File metadata and controls

198 lines (144 loc) · 9.17 KB

Button

ui.button · parts: Button · elements: <a>, <button>

Purpose

Button triggers an action. With href it renders an anchor (navigation that looks like an action). Event handlers (onClick) flow through attrs/rest. Pressed / Expanded are the semantic ARIA state props (toggle, disclosure).

Notes

  • Use href for navigation that looks like an action; plain links use Link.
  • disabled on the anchor form becomes aria-disabled="true" + tabindex="-1" (anchors have no native disabled).
  • Event handlers (onClick, @click, on:click) go through rest/attrs.
  • State through props, not ARIA strings (ui8kit/contract rule 02): a toggle is <Button pressed={on}>, a disclosure trigger <Button expanded={open} aria-controls="menu">. Both are tri-state: true / false print aria-pressed / aria-expanded as "true" / "false", unset prints nothing (a plain action button). templ: Pressed: new(on) (*bool, nil = unset); Twig/Latte/Jinja2: null / None = unset; Web Components: pressed="false" on the host is false, no attribute is unset.
  • pressed applies in button mode only (aria-pressed is not allowed on links); expanded works on both forms.
  • The raw aria-pressed / aria-expanded (named props or attrs/rest) still work and win over pressed / expanded when set and non-blank. Prefer the semantic props in compositions.
  • For an on/off setting use Switch (role="switch" + aria-checked), not a pressed Button.

Button renders a button element, or an anchor when Href is set. Type/Form apply only in button mode; Disabled on anchors uses aria-disabled. Pressed (button mode) and Expanded print aria-pressed / aria-expanded as "true"/"false"; unset omits the attribute.

Props

Canonical (templ, Jinja2, MiniJinja) React / Solid HTML (Svelte, Vue, WC) Type Default Kind Description
Type type type "button" | "submit" | "reset" — prop button type (default button)
Form form form string — passthrough attr associated form id (button mode)
Href href href string — prop renders an anchor when set
Disabled disabled disabled boolean — prop blocks interaction
Pressed pressed pressed boolean — prop toggle state → aria-pressed (button mode); unset omits it
Expanded expanded expanded boolean — prop disclosure state → aria-expanded; unset omits it
AriaLabel aria-label aria-label string — passthrough attr
AriaPressed aria-pressed aria-pressed string — passthrough attr
AriaExpanded aria-expanded aria-expanded string — passthrough attr
AriaControls aria-controls aria-controls string — passthrough attr

Also accepts: class (className in React), extra attributes (rest props in UI frameworks, Attrs in templ/Jinja2/MiniJinja, attrs in Twig/Latte; caller wins), children.

Variants

No variants: the primitive ships no visual classes; style it with token classes via class.

Children

Accepts children (default slot / children / { children... } / trusted HTML string in Twig, Latte, Jinja2, MiniJinja — see runtimes). Void tags selected via tag render no children.

ARIA

  • Computed/forwarded: aria-expanded, aria-label, aria-pressed, aria-controls, aria-disabled (omitted when empty).
  • Typed ARIA props: aria-label, aria-pressed, aria-expanded, aria-controls — static values only.

Classes escape hatch

Compose the part's classes without rendering it (custom root element, no asChild outside React):

Runtime Helper
React / Solid / Vue / Svelte / WC buttonClasses({ ... }) from button/button.shared.ts
templ button.ButtonClasses(button.ButtonProps{...})
Twig ui8kit_classes('buttonClasses', { class: '…' })
Latte \UI8Kit\Latte\Classes::buttonClasses(['class' => '…'])
Jinja2 button_classes(p)
MiniJinja button_classes(p) (registered by ui8kit_minijinja::register)

React additionally supports asChild (merges props onto the single child via Slot).

Rendered DOM per state

Canonical renderer output (children = Content); all 10 runtimes are parity-tested to produce this DOM.

State Props HTML
defaults — <button type="button">Content</button>
submit {"Type":"submit","Class":"bg-primary text-primary-foreground"} <button type="submit" class="bg-primary text-primary-foreground">Content</button>
disabled {"Disabled":true} <button type="button" disabled>Content</button>
form {"Form":"f1","AriaLabel":"Save"} <button type="button" form="f1" aria-label="Save">Content</button>
pressed {"AriaPressed":"true","AriaExpanded":"false","AriaControls":"m1"} <button type="button" aria-pressed="true" aria-expanded="false" aria-controls="m1">Content</button>
href {"Href":"/docs","Class":"underline"} <a href="/docs" class="underline">Content</a>
hrefDisabled {"Href":"/x","Disabled":true} <a href="/x" aria-disabled="true" tabindex="-1">Content</a>
toggleOn {"Pressed":true,"Class":"bg-primary text-primary-foreground"} <button type="button" class="bg-primary text-primary-foreground" aria-pressed="true">Content</button>
toggleOff {"Pressed":false} <button type="button" aria-pressed="false">Content</button>
menuExpanded {"Expanded":true} <button type="button" aria-expanded="true">Content</button>
menuCollapsed {"Expanded":false} <button type="button" aria-expanded="false">Content</button>
hrefExpanded {"Href":"/menu","Expanded":false} <a href="/menu" aria-expanded="false">Content</a>
pressedOverride {"Pressed":true,"AriaPressed":"mixed"} <button type="button" aria-pressed="mixed">Content</button>
pressedBlankRaw {"Pressed":false,"AriaPressed":" "} <button type="button" aria-pressed="false">Content</button>
Type=button {"Type":"button"} <button type="button">Content</button>
Type=submit {"Type":"submit"} <button type="submit">Content</button>
Type=reset {"Type":"reset"} <button type="reset">Content</button>
Disabled {"Disabled":true} <button type="button" disabled>Content</button>
Pressed {"Pressed":true} <button type="button" aria-pressed="true">Content</button>
Expanded {"Expanded":true} <button type="button" aria-expanded="true">Content</button>

Usage

Fixture submit ({"Type":"submit","Class":"bg-primary text-primary-foreground"}) — every snippet renders:

<button type="submit" class="bg-primary text-primary-foreground">Content</button>

React (runtime notes)

import { Button } from "./ui/button/button";

<Button type="submit" className="bg-primary text-primary-foreground">Content</Button>

Vue 3 (runtime notes)

<script setup lang="ts">
import Button from "./ui/button/Button.vue";
</script>

<template>
  <Button type="submit" class="bg-primary text-primary-foreground">Content</Button>
</template>

Svelte 5 (runtime notes)

<script lang="ts">
  import Button from "./ui/button/Button.svelte";
</script>

<Button type="submit" class="bg-primary text-primary-foreground">Content</Button>

SolidJS (runtime notes)

import { Button } from "./ui/button/button.solid";

<Button type="submit" class="bg-primary text-primary-foreground">Content</Button>

Web Components (runtime notes)

import { renderButton } from "./ui/button/button.wc";

document.body.append(renderButton({ type: "submit", class: "bg-primary text-primary-foreground" }, "Content"));

Go templ (runtime notes)

import "github.com/ui8kit/codegen/generated/templ/ui/button"

@button.Button(button.ButtonProps{Type: "submit", Class: "bg-primary text-primary-foreground"}) {
	Content
}

Twig (runtime notes)

{% include 'button/Button.html.twig' with { type: 'submit', class: 'bg-primary text-primary-foreground', children: 'Content' } only %}

Latte (runtime notes)

{include 'button/Button.latte', type: 'submit', class: 'bg-primary text-primary-foreground', children: 'Content'}

Jinja2 (runtime notes)

{% with p = {'Type': 'submit', 'Class': 'bg-primary text-primary-foreground'}, children = 'Content' %}{% include 'ui/button/button.html.j2' %}{% endwith %}

MiniJinja (Rust) (runtime notes)

{% import 'ui8kit.html' as ui %}
{{ ui.button({'Type': 'submit', 'Class': 'bg-primary text-primary-foreground'}, children='Content') }}

Rules: contract · Index: docs