All files / src/components/Button index.ts

78.57% Statements 33/42
50% Branches 10/20
83.33% Functions 5/6
73.07% Lines 19/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160                                                              73x 73x                                     158x             158x           158x           158x           158x           158x   78x 79x 78x         78x 79x 78x                   79x 78x           78x                                                                       79x       79x                  
import { LitElement, html, unsafeCSS } from "lit"
import { customElement, property } from "lit/decorators.js"
import { classMap } from "lit/directives/class-map.js"
 
import style from "./style.scss?inline"
 
/**
 * Supported visual variants for {@link VgButton}.
 */
export type ButtonVariant = "primary" | "secondary" | "ghost"
 
/**
 * Supported button size tokens.
 */
export type ButtonSize = "sm" | "md" | "lg"
 
/**
 * Supported button types that align with the native `HTMLButtonElement` contract.
 */
export type ButtonNativeType = "button" | "submit" | "reset"
 
/**
 * Event detail emitted alongside the {@link VgButton | button}'s `click` event.
 */
export interface ButtonClickDetail {
    /**
     * Original pointer event that triggered the handler.
     */
    readonly originalEvent: PointerEvent
}
 
const SUPPORTED_VARIANTS: readonly ButtonVariant[] = ["primary", "secondary", "ghost"]
const SUPPORTED_SIZES: readonly ButtonSize[] = ["sm", "md", "lg"]
 
/**
 * Accessible, theme-aware button component with size and variant controls.
 *
 * @tag vg-button
 * @tagname vg-button
 * @summary The Button component, used for calling attention to actions.
 *
 * @slot prefix - Optional leading icon or element rendered before the label
 * @slot - Button label content
 * @slot suffix - Optional trailing icon or element rendered after the label
 *
 * @csspart button - No input css variables
 *
 * @fires {ButtonClickDetail} vg-click - Fired when the button is activated
 *
 */
@customElement("vg-button")
export class VgButton extends LitElement {
    static styles = unsafeCSS(style)
 
    /**
     * Disables pointer interaction and visually indicates an unavailable state.
     */
    @property({ type: Boolean, reflect: true })
    public disabled = false
 
    /**
     * Renders a lightweight loading indicator and prevents interaction while true.
     */
    @property({ type: Boolean, reflect: true })
    public loading = false
 
    /**
     * Visual style variant for the button.
     */
    @property({ type: String })
    public variant: ButtonVariant = "primary"
 
    /**
     * Controls the paddings and font sizing of the button.
     */
    @property({ type: String })
    public size: ButtonSize = "md"
 
    /**
     * Button type attribute mirroring the native element contract.
     */
    @property({ type: String, attribute: "buttonType" })
    public buttonType: ButtonNativeType = "button"
    protected willUpdate(changedProperties: Map<string | number | symbol, unknown>) {
        if (changedProperties.has("variant")) {
            const normalisedVariant = this.normaliseVariant(this.variant)
            Iif (Ithis.variant !== normalisedVariant) {
                this.variant = normalisedVariant
            }
        }
 
        if (changedProperties.has("size")) {
            const normalisedSize = this.normaliseSize(this.size)
            Iif (Ithis.size !== normalisedSize) {
                this.size = normalisedSize
            }
        }
    }
 
    /**
     * @inheritdoc
     */
    protected render() {
        const isDisabled = this.disabled || this.loading
        const classes = {
            [this.variant]: true,
            [this.size]: true,
            loading: this.loading,
        }
 
        return html`
            <button
                class=${classMap(classes)}
                type=${this["buttonType"]}
                ?disabled=${isDisabled}
                aria-live="polite"
                aria-busy=${this.loading}
                @click=${this.handleClick}
            >
                ${this.loading ? html`<span class="loader" aria-hidden="true"></span>` : null}
                <span class="content">
                    <slot name="prefix"></slot>
                    <span class="label"><slot></slot></span>
                    <slot name="suffix"></slot>
                </span>
            </button>
        `
    }
 
    private handleClick(event: PointerEvent) {
        Iif (Ithis.disabled || this.loading) {
            event.preventDefault()
            event.stopImmediatePropagation()
            return
        }
 
        this.dispatchEvent(new CustomEvent<ButtonClickDetail>("vg-click", {
            detail: { originalEvent: event },
            // bubbles: true,
            composed: true,
            cancelable: true,
        }))
 
    }
 
    private normaliseVariant(value: string): ButtonVariant {
        return SUPPORTED_VARIANTS.includes(value as ButtonVariant) ? (value as ButtonVariant) : "primary"
    }
 
    private normaliseSize(value: string): ButtonSize {
        return SUPPORTED_SIZES.includes(value as ButtonSize) ? (value as ButtonSize) : "md"
    }
}
 
declare global {
    interface HTMLElementTagNameMap {
        "vg-button": VgButton
    }
}