1.0.0 • Published 4 years ago

@miraidesigns/button v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

Buttons

Buttons allow users to take actions with a single click or tap.


HTML

<button class="mdf-button">
    Button
</button>

Sass

// Include default styles without configuration
@forward '@miraidesigns/button/styles';
// Configure appearance
@use '@miraidesigns/button' with (
    $variable: value
);

@include button.styles();

Examples

Some basic examples on how the module can be used.

Icons

Icons can help give more meaning or emphasis to your button.

<!-- Leading icon -->
<button class="mdf-button mdf-button--leading-icon">
    <svg class="mdf-icon" viewBox="0 0 24 24" aria-hidden="true">
        <use href="icons.svg#favorite"></use>
    </svg>

    Favorite
</button>

<!-- Trailing icon -->
<button class="mdf-button mdf-button--trailing-icon">
    Favorite

    <svg class="mdf-icon" viewBox="0 0 24 24" aria-hidden="true">
        <use href="icons.svg#favorite"></use>
    </svg>
</button>

Implementation

Classes

NameTypeDescription
mdf-buttonParentApply basic styling to a button element
mdf-button--borderedModifierAdd a border to the button
mdf-button--filledModifierFill the button with the brand color
mdf-button--smallModifierReduced the height and padding of the button
mdf-button--largeModifierIncrease the height and padding of the button
mdf-button--blockModifierStretches the button to width of its parent
mdf-button--block-responsiveModifierStretches the button to width of its parent (only on small devices)
mdf-button--leading-iconModifierAdjust spacing for the leading icon
mdf-button--trailing-iconModifierAdjust spacing for the trailing icon
mdf-button--iconModifierIcon only button
mdf-button--raisedModifierElevate button by adding a shadow
mdf-button--no-hoverModifierRemove the button hover effect

Toggles

A group of buttons where only one option can be active at a time.


HTML

<div class="mdf-toggles">
    <button class="mdf-button mdf-button--toggle" data-toggle-callback="callback1" aria-pressed="false" aria-label="Toggle 1">
        <svg class="mdf-icon" viewBox="0 0 24 24" aria-hidden="true">
            <use href="icons.svg#add"></use>
        </svg>
    </button>

    <button class="mdf-button mdf-button--toggle mdf-button--active" data-toggle-callback="callback2" aria-pressed="true" aria-label="Toggle 2">
        <svg class="mdf-icon" viewBox="0 0 24 24" aria-hidden="true">
            <use href="icons.svg#favorite"></use>
        </svg>
    </button>

    <button class="mdf-button mdf-button--toggle" data-toggle-callback="callback3" aria-pressed="false" aria-label="Toggle 3">
        <svg class="mdf-icon" viewBox="0 0 24 24" aria-hidden="true">
            <use href="icons.svg#clear"></use>
        </svg>
    </button>
</div>

TypeScript

import { MDFToggles } from '@miraidesigns/button';

new MDFToggles(document.querySelector('.mdf-toggles'), {
	callback1: () => {
		console.log('Pressed toggle 1');
	},
	callback2: () => {
		console.log('Pressed toggle 2');
	},
	callback3: () => {
		console.log('Pressed toggle 3');
	},
});

Implementation

Attributes

Please see the WAI-ARIA page for attributes and best practices regarding button toggles.

NameElementDescription
data-toggle-callback<button>The name of the callback associated with the toggle

Classes

NameTypeDescription
mdf-togglesParentContains the toggle elements
mdf-button--toggleChild / ModifierToggle button element. Child to .mdf-toggles
mdf-button--activeModifierSet toggle as active

Events

NameDataDescription
MDFToggle:changed{callback: string, index: number, toggle: HTMLButtonElement}Fires whenever the active toggle element changes. Includes the callback name, the index and the element itself

Properties

NameTypeDescription
.containerHTMLElementReturns the toggles container element
.togglesHTMLButtonElement[]Returns an Array of all toggle elements
.callbacksRecord<string, () => void>Returns an Object holding the toggle callbacks
.getToggle(index)(number): HTMLButtonElementReturns the toggle button element with the given index
.getActiveToggle()(): HTMLButtonElementReturns the currently active toggle element
.activateToggle(elem)(HTMLButtonElement): voidActivate the given toggle element

Options

NameTypeDefaultDescription
callbacksRecord<string, () => void>nullObject containing the toggle callbacks