Skip to content

Button ​

<s-button> renders a native <button> inside its shadow root. Keyboard activation, focus, the button role and form behavior come from the platform.

PrimarySecondaryGhostDangerDisabledSaving
html
<s-button>Primary</s-button>
<s-button variant="secondary">Secondary</s-button>
<s-button variant="ghost">Ghost</s-button>
<s-button variant="danger">Danger</s-button>
<s-button disabled>Disabled</s-button>
<s-button loading>Saving</s-button>

Sizes ​

SmallMediumLarge
html
<s-button size="sm">Small</s-button>
<s-button size="md">Medium</s-button>
<s-button size="lg">Large</s-button>

Attributes and properties ​

Attribute / propertyTypeDefaultDescription
variant'primary' | 'secondary' | 'ghost' | 'danger'primaryvisual style
size'sm' | 'md' | 'lg'mdheight, padding, font size
type'button' | 'submit' | 'reset'buttonsubmit calls requestSubmit() on the closest form, reset resets it
disabledbooleanfalsedisables the inner button: out of the tab order, no clicks
loadingbooleanfalseshows a spinner, sets aria-busy and aria-disabled, blocks clicks, stays focusable
labelstringaccessible name for buttons without text, set as aria-label

Properties reflect to attributes, so el.variant = 'danger' and variant="danger" are the same.

Slots and parts ​

SlotContent
(default)the label
startan icon before the label
endan icon after the label
PartElement
basethe inner <button>
spinnerthe loading spinner
css
s-button::part(base) {
  text-transform: uppercase;
}

Custom properties ​

Every visual value reads a --s-button-* property first, then the variant or size default, then the theme token. Variants change only the defaults, so an override applies to every variant.

PillOutlineTall
html
<s-button style="--s-button-radius: 999px; --s-button-bg: #0b7285">Pill</s-button>
css
/* every button in the toolbar */
.toolbar s-button {
  --s-button-height: 2rem;
}
/* only danger buttons */
s-button[variant='danger'] {
  --s-button-font-weight: 700;
}
PropertyDefault
--s-button-bgvariant: --s-color-primary, --s-color-surface, transparent, --s-color-danger
--s-button-bg-hovervariant -hover color
--s-button-bg-activevariant -active color
--s-button-fg--s-color-on-primary, --s-color-text, --s-color-primary-text, --s-color-on-danger
--s-button-border-colortransparent; --s-color-border for secondary
--s-button-border-width1px
--s-button-radius--s-radius-md
--s-button-height2rem / 2.5rem / 3rem by size
--s-button-padding-x0.75rem / 1rem / 1.25rem by size
--s-button-gap0.5rem
--s-button-font-family--s-font-family
--s-button-font-size--s-font-size-sm / -md / -lg by size
--s-button-font-weight--s-font-weight-medium
--s-button-focus-ring-color--s-color-focus
--s-button-focus-ring-width2px
--s-button-focus-ring-offset2px
--s-button-disabled-opacity0.5

Accessibility ​

  • The accessible name is the slotted text; icon-only buttons take label.
  • disabled uses the native attribute: the button leaves the tab order and assistive technology reports it as unavailable.
  • loading keeps the button focusable, so focus does not jump away while an action runs, and announces it with aria-busy="true" and aria-disabled="true".
  • Focus is delegated: el.focus() focuses the inner button and the ring appears on :focus-visible only.
  • A disabled <fieldset> disables the button (it is form-associated).
  • The spinner slows down with prefers-reduced-motion, and borders and focus rings use system colors in forced-colors mode.

Forms ​

html
<form>
  <input name="email" type="email" required />
  <s-button type="submit">Subscribe</s-button>
  <s-button type="reset" variant="ghost">Clear</s-button>
</form>

type="submit" runs form.requestSubmit(), so constraint validation and submit listeners work as with a native button. The button uses its closest ancestor <form>; the form="id" attribute is not supported yet.

Server rendering ​

ts
import { renderButton } from '@surgijs/ui/server';

const html = renderButton({ variant: 'secondary', type: 'submit' }, 'Send');
// <s-button variant="secondary" type="submit"><template shadowrootmode="open" shadowrootdelegatesfocus>…</template>Send</s-button>

The second argument is light DOM HTML and is inserted as is: escape user content before passing it. On the client, enable hydration before registering the component so it adopts the server nodes instead of rendering again:

ts
import { enableHydration } from '@surgijs/core/hydrate';

enableHydration();
await import('@surgijs/ui/button');

MIT License