All files / src/display Button.ts

91.37% Statements 286/313
100% Branches 15/15
42.85% Functions 3/7
91.37% Lines 286/313

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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314125x 168x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 88x 88x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x                                   125x             125x         125x 125x 125x 91x 91x 91x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 125x 338x 338x 125x 125x 125x 125x 125x  
import { LitElement, TemplateResult, html, css } from 'lit';
import { getClasses } from '../utils';
 
import { property } from 'lit/decorators.js';
 
export class Button extends LitElement {
  static get styles() {
    return css`
      :host {
        display: inline-flex;
        align-self: stretch;
        font-family: var(--font);
        /* Match the styleguide's .ds Inter rendering — the stylistic
           sets (ss01/cv11) and the "tnum 0" off-switch — so glyph
           shapes line up with the design-system buttons. */
        font-feature-settings:
          'ss01',
          'cv11',
          'tnum' 0;
      }
 
      /* DS .btn-sm — sizing, type, transition, shape */
      .button-container {
        display: inline-flex;
        align-items: center;
        justify-content: var(--button-justify, center);
        gap: 6px;
        flex-grow: 1;
        height: var(--button-height, 28px);
        padding: 0 10px;
        border: 1px solid transparent;
        border-radius: var(--r-sm);
        font-size: 12.5px;
        font-weight: var(--w-regular);
        letter-spacing: -0.005em;
        line-height: 1;
        white-space: nowrap;
        cursor: pointer;
        user-select: none;
        -webkit-user-select: none;
        outline: none;
        box-sizing: border-box;
        transition:
          background 120ms,
          border-color 120ms,
          color 120ms,
          box-shadow 120ms;
      }
 
      .button-mask {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        line-height: 1;
      }
 
      .button-name {
        white-space: nowrap;
      }
 
      /* even smaller variant (compact lists, list-row actions, etc.).
         The vars let dense consumers (e.g. slim status rows) compact
         it further without another named size. */
      .small {
        height: var(--button-small-height, 24px);
        padding: 0 var(--button-small-x-pad, 8px);
        font-size: var(--button-small-font-size, 12px);
      }
 
      /* DS .btn-primary — solid accent fill with a faint top
         highlight and a ground shadow so it sits a touch above the
         surface. The base 1px transparent border stays in the box
         model so the primary's footprint matches the secondary's. */
      .primary-button {
        background: var(--accent-600);
        color: #fff;
        box-shadow:
          inset 0 1px 0 rgba(255, 255, 255, 0.18),
          0 1px 1px rgba(15, 22, 36, 0.1);
      }
      .primary-button:hover {
        background: var(--accent-700);
      }
 
      /* Ghost-style secondary — transparent fill so it blends into
         any surface (the dialog gutter's grey, a white card, etc.),
         no border, with a semi-transparent wash on hover that shows
         up regardless of the underlying colour. */
      .secondary-button {
        background: transparent;
        color: var(--text-1);
      }
      .secondary-button:hover {
        background: rgba(0, 0, 0, 0.05);
      }
 
      /* Flat success fill — mirrors the DS .btn-danger chrome (no
         lift), tinted green for go-ahead CTAs. */
      .attention-button,
      .affirmative {
        background: var(--success, #16a34a);
        color: #fff;
      }
      /* Hover darkening: static pre-mixed values first for browsers
         without color-mix(), re-derived from the token below. */
      .attention-button:hover,
      .affirmative:hover {
        background: #138f41;
      }
 
      /* DS .btn-danger — flat danger fill (no lift). */
      .destructive-button {
        background: var(--danger, #d03f3f);
        color: #fff;
      }
      .destructive-button:hover {
        background: #b73737;
      }
 
      @supports (color: color-mix(in srgb, red, red)) {
        .attention-button:hover,
        .affirmative:hover {
          background: color-mix(in srgb, var(--success, #16a34a) 88%, black);
        }
        .destructive-button:hover {
          background: color-mix(in srgb, var(--danger, #d03f3f) 88%, black);
        }
      }
 
      /* DS .btn-ghost — text-only, hover fills with sunken */
      .light-button,
      .lined-button {
        background: var(--surface);
        border-color: var(--border-strong);
        color: var(--text-1);
      }
      .light-button:hover,
      .lined-button:hover {
        background: var(--sunken);
      }
 
      /* icon-only button — square footprint, ghost chrome */
      .icon-button {
        width: var(--button-height, 28px);
        padding: 0;
        background: transparent;
        border-color: transparent;
        color: var(--text-2);
      }
      .icon-button:hover {
        background: var(--sunken);
        color: var(--text-1);
      }
      .icon-button.small {
        width: 24px;
      }
 
      /* active = pressed-down look — slightly inset */
      .button-container.active-button {
        box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12);
      }
      .secondary-button.active-button {
        background: rgba(0, 0, 0, 0.08);
      }
 
      /* disabled */
      .button-container.disabled-button {
        opacity: 0.45;
        cursor: not-allowed;
        pointer-events: none;
      }
 
      /* focus ring — keyboard nav only */
      .button-container:focus-visible {
        box-shadow: var(--widget-box-shadow-focused);
      }
 
      .submit-animation {
        padding: 1px 4px;
      }
 
      .submit-animation temba-loading {
        margin-bottom: -3px;
        line-height: normal;
      }
    `;
  }
 
  @property({ type: Boolean })
  primary: boolean;
 
  @property({ type: Boolean })
  secondary: boolean;
 
  @property({ type: Boolean })
  attention: boolean;
 
  @property({ type: Number })
  v = 1;
 
  @property({ type: Boolean })
  destructive: boolean;
 
  @property({ type: Boolean })
  light: boolean;
 
  @property()
  name: string;
 
  @property({ type: Boolean })
  disabled: boolean;
 
  @property({ type: Boolean })
  submitting: boolean;
 
  @property({ type: Boolean })
  active: boolean;
 
  @property({ type: Boolean })
  small: boolean;
 
  @property({ type: Boolean })
  lined: boolean;
 
  @property({ type: String })
  href: string;
 
  @property({ type: Number })
  index?: number;
 
  @property({ type: String })
  icon?: string;
 
  private handleClick(evt: MouseEvent) {
    if (this.disabled) {
      evt.preventDefault();
      evt.stopPropagation();
    }
 
    if (this.href && !this.disabled) {
      this.ownerDocument.location.href = this.href;
      evt.preventDefault();
      evt.stopPropagation();
    }
  }
 
  private handleKeyUp(event: KeyboardEvent): void {
    this.active = false;
    if (event.key === 'Enter') {
      this.click();
    }
  }
 
  private handleMouseDown(): void {
    if (!this.disabled && !this.submitting) {
      this.active = true;
      this.classList.add('active');
    }
  }

  private handleMouseUp(): void {
    this.active = false;
    this.classList.remove('active');
  }

  public render(): TemplateResult {
    const buttonName = this.submitting
      ? html`<div class="submit-animation">
          <temba-loading units="3" size="8" color="#eee"></temba-loading>
        </div>`
      : this.name;

    return html`
      <div
        class="button-container 
          v-${this.v}
          ${getClasses({
          'primary-button':
            this.primary ||
            (!this.primary &&
              !this.secondary &&
              !this.attention &&
              !this.destructive &&
              !this.lined &&
              !this.light &&
              this.v == 1),
          'secondary-button': this.secondary,
          'disabled-button': this.disabled,
          'active-button': this.active,
          'attention-button': this.attention,
          'destructive-button': this.destructive,
          'light-button': this.light,
          'lined-button': this.lined,
          'icon-button': !!this.icon && !this.name,
          small: this.small
        })}"
        tabindex="0"
        @mousedown=${this.handleMouseDown}
        @mouseup=${this.handleMouseUp}
        @mouseleave=${this.handleMouseUp}
        @keyup=${this.handleKeyUp}
        @click=${this.handleClick}
      >
        <div class="button-mask">
          ${this.icon
            ? html`<temba-icon name="${this.icon}"></temba-icon>`
            : null}
          <div class="button-name"><slot name="name">${buttonName}</slot></div>
        </div>
      </div>
    `;
  }
}