@fluid-topics/ftds-button 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,19 @@
1
+ The design system button component
2
+
3
+ ## Install
4
+
5
+ ```shell
6
+ npm install @fluid-topics/ftds-button
7
+ yarn add @fluid-topics/ftds-button
8
+ ```
9
+
10
+ ## Usage
11
+
12
+ ```typescript
13
+ import { html } from "lit"
14
+ import "@fluid-topics/ftds-button"
15
+
16
+ function render() {
17
+ return html` <ftds-button foo="bar"></ftds-button> `
18
+ }
19
+ ```
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import { customElements } from "@fluid-topics/ft-wc-utils";
2
+ import definitions from "./definitions";
3
+ customElements(definitions);
@@ -0,0 +1,5 @@
1
+ import { FtdsButton } from "./ftds-button";
2
+ declare const _default: {
3
+ "ftds-button": typeof FtdsButton;
4
+ };
5
+ export default _default;
@@ -0,0 +1,4 @@
1
+ import { FtdsButton } from "./ftds-button";
2
+ export default {
3
+ "ftds-button": FtdsButton,
4
+ };
@@ -0,0 +1,53 @@
1
+ import { HTMLTemplateResult } from "lit";
2
+ import { ElementDefinitionsMap, FtLitElement } from "@fluid-topics/ft-wc-utils";
3
+ import { FtdsButtonProperties, HTMLAnchorElementTargetAttribute } from "./ftds-button.properties";
4
+ import { Position } from "@fluid-topics/ft-tooltip";
5
+ import { DesignSystemFamily } from "@fluid-topics/design-system-variables";
6
+ import { ClassInfo } from "lit/directives/class-map.js";
7
+ import { FtdsIconVariants } from "@fluid-topics/ftds-icon";
8
+ declare const FtdsButton_base: import("@fluid-topics/ft-wc-utils").FtFormComponentType<import("@fluid-topics/ft-wc-utils").FtdsBaseType<typeof FtLitElement>>;
9
+ export declare class FtdsButton extends FtdsButton_base implements FtdsButtonProperties {
10
+ static elementDefinitions: ElementDefinitionsMap;
11
+ primary: boolean;
12
+ secondary: boolean;
13
+ tertiary: boolean;
14
+ round: boolean;
15
+ href?: string;
16
+ target?: HTMLAnchorElementTargetAttribute;
17
+ type: "button" | "submit" | "reset";
18
+ disabled: boolean;
19
+ label: string;
20
+ icon?: string;
21
+ iconVariant?: FtdsIconVariants;
22
+ trailingIcon: boolean;
23
+ loading: boolean;
24
+ ariaLabel: string | null;
25
+ ariaHasPopup: string | null;
26
+ ariaPressed: string | null;
27
+ tooltipPosition: Position;
28
+ hideTooltip: boolean;
29
+ forceTooltip: boolean;
30
+ activated: boolean;
31
+ private button?;
32
+ private slottedContent?;
33
+ private tooltip?;
34
+ family: DesignSystemFamily;
35
+ static styles: (import("lit").CSSResult | import("lit").CSSResult[])[];
36
+ get buttonClasses(): ClassInfo;
37
+ get typographyVariant(): string;
38
+ constructor();
39
+ protected render(): HTMLTemplateResult;
40
+ private addTooltipIfNeeded;
41
+ private resolveIcon;
42
+ focus(): void;
43
+ showTooltip(): void;
44
+ showTemporaryTooltip(text: string, duration: number): Promise<void> | undefined;
45
+ private setTooltipManual;
46
+ click(): void;
47
+ private getLabel;
48
+ getTextContent(): string;
49
+ protected hasTextContent(): boolean;
50
+ private onSlotchange;
51
+ private isDisabled;
52
+ }
53
+ export {};
@@ -0,0 +1,258 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { html, } from "lit";
8
+ import { property, query, } from "lit/decorators.js";
9
+ import { FtLitElement, isSafari, safariEllipsisFix, toFtdsBase, toFtFormComponent, unslotText, } from "@fluid-topics/ft-wc-utils";
10
+ import { FtLoader } from "@fluid-topics/ft-loader";
11
+ import { FtRipple } from "@fluid-topics/ft-ripple";
12
+ import { FtTooltip, } from "@fluid-topics/ft-tooltip";
13
+ import { DesignSystemFamily, DesignSystemSize, } from "@fluid-topics/design-system-variables";
14
+ import { FtdsTypography, FtdsTypographyVariants, } from "@fluid-topics/ftds-typography";
15
+ import { ifDefined } from "lit/directives/if-defined.js";
16
+ import { classMap, } from "lit/directives/class-map.js";
17
+ import { style } from "./ftds-button.styles";
18
+ import { FtdsIcon, } from "@fluid-topics/ftds-icon";
19
+ export class FtdsButton extends toFtFormComponent(toFtdsBase(FtLitElement)) {
20
+ get buttonClasses() {
21
+ return {
22
+ "ftds-button": true,
23
+ "ftds-button--primary": this.primary,
24
+ "ftds-button--secondary": this.secondary,
25
+ "ftds-button--tertiary": this.tertiary || (!this.primary && !this.secondary),
26
+ "ftds-button--icon-only": !this.hasTextContent(),
27
+ "ftds-button--trailing-icon": this.trailingIcon,
28
+ "ftds-button--loading": this.trailingIcon,
29
+ "ft-no-text-select": true,
30
+ "ftds-button--no-icon": !this.icon,
31
+ "ftds-button--round": this.round,
32
+ ...this.getDesignSystemBaseClasses(),
33
+ };
34
+ }
35
+ get typographyVariant() {
36
+ switch (this.size) {
37
+ case DesignSystemSize.large:
38
+ return FtdsTypographyVariants.body2medium;
39
+ case DesignSystemSize.medium:
40
+ return FtdsTypographyVariants.caption1medium;
41
+ case DesignSystemSize.small:
42
+ return FtdsTypographyVariants.caption2medium;
43
+ default:
44
+ return FtdsTypographyVariants.caption1medium;
45
+ }
46
+ }
47
+ constructor() {
48
+ super();
49
+ this.primary = false;
50
+ this.secondary = false;
51
+ this.tertiary = false;
52
+ this.round = false;
53
+ this.href = undefined;
54
+ this.target = undefined;
55
+ this.type = "button";
56
+ this.disabled = false;
57
+ this.label = "";
58
+ this.icon = undefined;
59
+ this.trailingIcon = false;
60
+ this.loading = false;
61
+ this.ariaLabel = null;
62
+ this.ariaHasPopup = null;
63
+ this.ariaPressed = null;
64
+ this.tooltipPosition = "bottom";
65
+ this.hideTooltip = false;
66
+ this.forceTooltip = false;
67
+ this.activated = false;
68
+ this.family = DesignSystemFamily.brand;
69
+ this.addEventListener("click", (e) => {
70
+ var _a;
71
+ if (this.isDisabled()) {
72
+ e.preventDefault();
73
+ e.stopPropagation();
74
+ e.stopImmediatePropagation();
75
+ }
76
+ else if (this.type == "submit") {
77
+ (_a = this.form) === null || _a === void 0 ? void 0 : _a.requestSubmit();
78
+ }
79
+ });
80
+ }
81
+ render() {
82
+ var _a, _b, _c;
83
+ const content = html `
84
+ <ft-ripple part="ripple"
85
+ ?disabled=${this.isDisabled()}
86
+ ?activated=${this.activated}
87
+ ></ft-ripple>
88
+ <ftds-typography part="label"
89
+ variant="${this.typographyVariant}"
90
+ element="span"
91
+ class="ftds-button--label ${isSafari ? "ft-safari-ellipsis-fix" : ""}"
92
+ ?hidden=${!this.hasTextContent()}>
93
+ <slot @slotchange=${this.onSlotchange}></slot>
94
+ </ftds-typography>
95
+ ${this.resolveIcon()}
96
+ `;
97
+ const href = this.href != null && this.href.trim().length > 0 ? this.href : undefined;
98
+ return this.addTooltipIfNeeded(href ? html `
99
+ <a href="${href}"
100
+ target="${(_a = this.target) !== null && _a !== void 0 ? _a : "_self"}"
101
+ part="button"
102
+ class="${classMap(this.buttonClasses)}"
103
+ aria-label="${(_b = this.ariaLabel) !== null && _b !== void 0 ? _b : this.getLabel()}"
104
+ ?disabled=${this.isDisabled()}>
105
+ ${content}
106
+ </a>
107
+ ` : html `
108
+ <button part="button"
109
+ class="${classMap(this.buttonClasses)}"
110
+ aria-label="${(_c = this.ariaLabel) !== null && _c !== void 0 ? _c : this.getLabel()}"
111
+ aria-haspopup="${ifDefined(this.ariaHasPopup)}"
112
+ aria-pressed="${ifDefined(this.ariaPressed)}"
113
+ ?disabled=${this.isDisabled()}>
114
+ ${content}
115
+ </button>
116
+ `);
117
+ }
118
+ addTooltipIfNeeded(button) {
119
+ return this.getLabel().trim().length > 0 && (this.forceTooltip || (!this.hasTextContent() && !this.hideTooltip))
120
+ ? html `
121
+ <ft-tooltip part="tooltip"
122
+ text="${this.getLabel()}"
123
+ position="${this.tooltipPosition}">
124
+ ${button}
125
+ </ft-tooltip>
126
+ `
127
+ : button;
128
+ }
129
+ resolveIcon() {
130
+ if (this.loading) {
131
+ return html `
132
+ <ft-loader part="loader icon"></ft-loader> `;
133
+ }
134
+ else {
135
+ return this.icon ? html `
136
+ <ftds-icon part="icon" .variant=${this.iconVariant} .icon=${this.icon}></ftds-icon>
137
+ ` : html `
138
+ <slot part="icon" name="icon"></slot>
139
+ `;
140
+ }
141
+ }
142
+ focus() {
143
+ var _a;
144
+ (_a = this.button) === null || _a === void 0 ? void 0 : _a.focus();
145
+ }
146
+ showTooltip() {
147
+ var _a;
148
+ (_a = this.tooltip) === null || _a === void 0 ? void 0 : _a.show();
149
+ }
150
+ showTemporaryTooltip(text, duration) {
151
+ var _a;
152
+ return (_a = this.tooltip) === null || _a === void 0 ? void 0 : _a.showTemporaryText(text, duration);
153
+ }
154
+ setTooltipManual(manualTooltip) {
155
+ if (this.tooltip) {
156
+ this.tooltip.manual = manualTooltip;
157
+ }
158
+ }
159
+ click() {
160
+ var _a;
161
+ (_a = this.button) === null || _a === void 0 ? void 0 : _a.click();
162
+ }
163
+ getLabel() {
164
+ return this.label || this.getTextContent();
165
+ }
166
+ getTextContent() {
167
+ return unslotText(this.slottedContent).trim();
168
+ }
169
+ hasTextContent() {
170
+ return this.getTextContent().length > 0;
171
+ }
172
+ onSlotchange() {
173
+ this.requestUpdate();
174
+ }
175
+ isDisabled() {
176
+ return this.disabled || this.loading;
177
+ }
178
+ }
179
+ FtdsButton.elementDefinitions = {
180
+ "ft-ripple": FtRipple,
181
+ "ft-tooltip": FtTooltip,
182
+ "ftds-icon": FtdsIcon,
183
+ "ft-loader": FtLoader,
184
+ "ftds-typography": FtdsTypography,
185
+ };
186
+ FtdsButton.styles = [
187
+ safariEllipsisFix,
188
+ style,
189
+ ];
190
+ __decorate([
191
+ property({ type: Boolean })
192
+ ], FtdsButton.prototype, "primary", void 0);
193
+ __decorate([
194
+ property({ type: Boolean })
195
+ ], FtdsButton.prototype, "secondary", void 0);
196
+ __decorate([
197
+ property({ type: Boolean })
198
+ ], FtdsButton.prototype, "tertiary", void 0);
199
+ __decorate([
200
+ property({ type: Boolean })
201
+ ], FtdsButton.prototype, "round", void 0);
202
+ __decorate([
203
+ property()
204
+ ], FtdsButton.prototype, "href", void 0);
205
+ __decorate([
206
+ property()
207
+ ], FtdsButton.prototype, "target", void 0);
208
+ __decorate([
209
+ property()
210
+ ], FtdsButton.prototype, "type", void 0);
211
+ __decorate([
212
+ property({ type: Boolean })
213
+ ], FtdsButton.prototype, "disabled", void 0);
214
+ __decorate([
215
+ property()
216
+ ], FtdsButton.prototype, "label", void 0);
217
+ __decorate([
218
+ property()
219
+ ], FtdsButton.prototype, "icon", void 0);
220
+ __decorate([
221
+ property()
222
+ ], FtdsButton.prototype, "iconVariant", void 0);
223
+ __decorate([
224
+ property({ type: Boolean })
225
+ ], FtdsButton.prototype, "trailingIcon", void 0);
226
+ __decorate([
227
+ property({ type: Boolean })
228
+ ], FtdsButton.prototype, "loading", void 0);
229
+ __decorate([
230
+ property({ attribute: "aria-label" })
231
+ ], FtdsButton.prototype, "ariaLabel", void 0);
232
+ __decorate([
233
+ property()
234
+ ], FtdsButton.prototype, "ariaHasPopup", void 0);
235
+ __decorate([
236
+ property({ attribute: "aria-pressed" })
237
+ ], FtdsButton.prototype, "ariaPressed", void 0);
238
+ __decorate([
239
+ property()
240
+ ], FtdsButton.prototype, "tooltipPosition", void 0);
241
+ __decorate([
242
+ property({ type: Boolean })
243
+ ], FtdsButton.prototype, "hideTooltip", void 0);
244
+ __decorate([
245
+ property({ type: Boolean })
246
+ ], FtdsButton.prototype, "forceTooltip", void 0);
247
+ __decorate([
248
+ property({ type: Boolean })
249
+ ], FtdsButton.prototype, "activated", void 0);
250
+ __decorate([
251
+ query(".ftds-button")
252
+ ], FtdsButton.prototype, "button", void 0);
253
+ __decorate([
254
+ query(".ftds-button--label slot")
255
+ ], FtdsButton.prototype, "slottedContent", void 0);
256
+ __decorate([
257
+ query("[part=tooltip]")
258
+ ], FtdsButton.prototype, "tooltip", void 0);