@k4k3ru/design-system-button
v1.1.0
Published
Utility for button component
Readme
@k4k3ru/design-system-button
A lightweight, accessible Button component for HTML applications. It provides BEM-based CSS, semantic color variants, responsive theme fallbacks, and an optional TypeScript-powered ripple interaction.
Installation
npm install @k4k3ru/design-system-buttonThe shared design tokens are optional. When they are not installed, the Button component uses its own light and dark fallback values.
npm install @k4k3ru/tokensUsage
Import the shared tokens first when using them, followed by the Button stylesheet.
@import "@k4k3ru/tokens/dist/style.css";
@import "@k4k3ru/design-system-button/style.css";Create a button using the .button block and its elements.
<button class="button button--success button--round" type="button">
<svg class="button__icon" viewBox="0 0 24 24" aria-hidden="true">
<!-- icon path -->
</svg>
<span class="button__label">Save changes</span>
</button>Initialize the optional ripple interaction once per document.
import { Button } from "@k4k3ru/design-system-button";
const button = new Button();
button.run();
// Remove the delegated event listener when it is no longer needed.
button.destroy();The CSS can be used without initializing JavaScript. Only the pointer ripple interaction requires JavaScript.
Modifiers
Appearance
.button--outlined.button--text.button--round
Size
.button--small- Default medium size
.button--large
Semantic color
.button--error.button--info.button--success.button--warning
Modifiers can be combined.
<button class="button button--large button--error button--outlined button--round" type="button">
<span class="button__label">Delete account</span>
</button>Button group
Use the standalone .button-group block to arrange multiple buttons.
<div class="button-group">
<button class="button button--text" type="button">
<span class="button__label">Cancel</span>
</button>
<button class="button button--success" type="button">
<span class="button__label">Save</span>
</button>
</div>Disabled state
Use the native disabled attribute for <button> elements.
<button class="button" disabled type="button">
<span class="button__label">Unavailable</span>
</button>aria-disabled="true" is also styled and excluded from ripple interactions. ARIA does not prevent activation by itself, so applications must suppress activation when it is used on links or other non-button elements.
Themes
Without shared tokens, the component follows the operating-system color scheme. An explicit theme can be selected on the root HTML element.
<html data-theme="light"><html data-theme="dark">When @k4k3ru/tokens is loaded, its color variables take precedence over the built-in fallback palette.
Customization
Override the public Button custom properties after importing the stylesheet.
:root {
--button-background: #f1f3f5;
--button-color-success: #087f5b;
--button-height-medium: 2.75rem;
--button-radius-round: 999px;
}Variables prefixed with --_button- are internal implementation details and are not part of the public customization API.
Accessibility
- Prefer the native
<button>element. - Set
type="button"unless the button submits a form. - Provide visible label text or an accessible name for icon-only buttons.
- Decorative SVG icons should use
aria-hidden="true". - Reduced-motion preferences disable ripple animation.
Development
From the repository root:
npm run typecheck
npm run build:buttonTo view the example with a local HTTP server:
python3 -m http.server 8000Open http://localhost:8000/web/components/button/example/.
License
Smaller size
Use button--smaller on the component root. Height, padding, text and icon; alias of button--small.
See shared sizing guidance and examples.
Both button--smaller and its existing alias button--small apply the small
font size and line height to the button itself as well as .button__label.
Direct text and wrapped labels therefore both use 13px text / 18px line height
with the shared tokens. Default and large button sizing is unchanged.
Default horizontal padding is .75rem per side for small/smaller, 1.25rem for
medium, and 1.5rem for large (an increase of .25rem per side for small/smaller
and .5rem for medium/large from the original defaults). Vertical padding
and heights are unchanged. Override the existing --button-padding-small,
--button-padding-medium or --button-padding-large for custom spacing;
Button no longer inherits the generic --control-padding-* defaults.
