From 2a6e91477a737d9a7f41ab0360187e8bd6170eee Mon Sep 17 00:00:00 2001 From: Cory LaViska Date: Mon, 13 Dec 2021 16:56:03 -0500 Subject: [PATCH] refactor button using static expressions --- docs/resources/changelog.md | 1 + src/components/button/button.ts | 183 +++++++++++++------------------- 2 files changed, 75 insertions(+), 109 deletions(-) diff --git a/docs/resources/changelog.md b/docs/resources/changelog.md index 4d146212..4f24708c 100644 --- a/docs/resources/changelog.md +++ b/docs/resources/changelog.md @@ -9,6 +9,7 @@ _During the beta period, these restrictions may be relaxed in the event of a mis ## Next - Fixed bug where setting `tooltipFormatter` on `` in JSX causes React@experimental to error out +- Refactored `` to use Lit's static expressions to reduce code ## 2.0.0-beta.62 diff --git a/src/components/button/button.ts b/src/components/button/button.ts index 438f93e2..5617b002 100644 --- a/src/components/button/button.ts +++ b/src/components/button/button.ts @@ -1,5 +1,6 @@ -import { LitElement, html } from 'lit'; +import { LitElement } from 'lit'; import { customElement, property, query, state } from 'lit/decorators.js'; +import { html, literal } from 'lit/static-html.js'; import { classMap } from 'lit/directives/class-map.js'; import { ifDefined } from 'lit/directives/if-defined.js'; import { emit } from '../../internal/event'; @@ -126,116 +127,80 @@ export default class SlButton extends LitElement { render() { const isLink = this.href ? true : false; + const tag = isLink ? literal`a` : literal`button`; - const interior = html` - - - - - - - - - - ${this.caret - ? html` - - - - - - ` - : ''} - ${this.loading ? html`` : ''} + return html` + <${tag} + part="base" + class=${classMap({ + button: true, + 'button--default': this.type === 'default', + 'button--primary': this.type === 'primary', + 'button--success': this.type === 'success', + 'button--neutral': this.type === 'neutral', + 'button--warning': this.type === 'warning', + 'button--danger': this.type === 'danger', + 'button--text': this.type === 'text', + 'button--small': this.size === 'small', + 'button--medium': this.size === 'medium', + 'button--large': this.size === 'large', + 'button--caret': this.caret, + 'button--circle': this.circle, + 'button--disabled': this.disabled, + 'button--focused': this.hasFocus, + 'button--loading': this.loading, + 'button--standard': !this.outline, + 'button--outline': this.outline, + 'button--pill': this.pill, + 'button--has-label': this.hasLabel, + 'button--has-prefix': this.hasPrefix, + 'button--has-suffix': this.hasSuffix + })} + ?disabled=${ifDefined(isLink ? undefined : this.disabled)} + type=${ifDefined(isLink ? undefined : this.submit ? 'submit' : 'button')} + name=${ifDefined(isLink ? undefined : this.name)} + value=${ifDefined(isLink ? undefined : this.value)} + href=${ifDefined(this.href)} + target=${ifDefined(this.target)} + download=${ifDefined(this.download)} + rel=${ifDefined(this.target ? 'noreferrer noopener' : undefined)} + role="button" + aria-disabled=${this.disabled ? 'true' : 'false'} + tabindex=${this.disabled ? '-1' : '0'} + @blur=${this.handleBlur} + @focus=${this.handleFocus} + @click=${this.handleClick} + > + + + + + + + + + + ${ + this.caret + ? html` + + + + + + ` + : '' + } + ${this.loading ? html`` : ''} + `; - - return isLink - ? html` - - ${interior} - - ` - : html` - - `; } }