-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbutton.ts
160 lines (132 loc) · 4 KB
/
button.ts
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
import { html, LitElement } from 'lit';
import { classMap } from 'lit/directives/class-map.js';
import { createRef, ref } from 'lit/directives/ref.js';
import { customElement, property, state } from 'lit/decorators.js';
import packageJson from '../package.json' with { type: 'json' };
import styles from './button.styles.js';
import shadowRootMode from './library/shadow-root-mode.js';
import final from './library/final.js';
import required from './library/required.js';
declare global {
interface HTMLElementTagNameMap {
'glide-core-button': GlideCoreButton;
}
}
/**
* @attr {string} label
* @attr {boolean} [disabled=false]
* @attr {string} [name='']
* @attr {'large'|'small'} [size='large']
* @attr {'button'|'submit'|'reset'} [type='button']
* @attr {string} [value='']
* @attr {'primary'|'secondary'|'tertiary'} [variant='primary']
*
* @readonly
* @attr {string} [version]
*
* @slot {Element} [prefix-icon] - An icon before the label
* @slot {Element} [suffix-icon] - An icon after the label
*
* @readonly
* @prop {HTMLFormElement | null} form
*/
@customElement('glide-core-button')
@final
export default class GlideCoreButton extends LitElement {
static formAssociated = true;
static override shadowRootOptions: ShadowRootInit = {
...LitElement.shadowRootOptions,
delegatesFocus: true,
mode: shadowRootMode,
};
static override styles = styles;
@property({ type: Boolean, reflect: true }) disabled = false;
@property({ reflect: true })
@required
label?: string;
@property({ reflect: true }) name = '';
@property({ reflect: true })
size: 'large' | 'small' = 'large';
@property({ reflect: true })
type: 'button' | 'submit' | 'reset' = 'button';
@property({ reflect: true }) value = '';
@property({ reflect: true })
variant: 'primary' | 'secondary' | 'tertiary' = 'primary';
@property({ reflect: true })
readonly version: string = packageJson.version;
get form(): HTMLFormElement | null {
return this.#internals.form;
}
override click() {
this.#buttonElementRef.value?.click();
}
override render() {
return html`<button
class=${classMap({
component: true,
primary: this.variant === 'primary',
secondary: this.variant === 'secondary',
tertiary: this.variant === 'tertiary',
large: this.size === 'large',
small: this.size === 'small',
'prefix-icon': this.hasPrefixIcon,
'suffix-icon': this.hasSuffixIcon,
})}
?disabled=${this.disabled}
@click=${this.#onClick}
${ref(this.#buttonElementRef)}
>
<slot
name="prefix-icon"
@slotchange=${this.#onPrefixIconSlotChange}
${ref(this.#prefixIconSlotElementRef)}
>
<!--
An icon before the label
@type {Element}
-->
</slot>
${this.label}
<slot
name="suffix-icon"
@slotchange=${this.#onSuffixIconSlotChange}
${ref(this.#suffixIconSlotElementRef)}
>
<!--
An icon after the label
@type {Element}
-->
</slot>
</button>`;
}
constructor() {
super();
this.#internals = this.attachInternals();
}
@state()
private hasPrefixIcon = false;
@state()
private hasSuffixIcon = false;
#buttonElementRef = createRef<HTMLButtonElement>();
#internals: ElementInternals;
#prefixIconSlotElementRef = createRef<HTMLSlotElement>();
#suffixIconSlotElementRef = createRef<HTMLSlotElement>();
#onClick() {
if (this.type === 'submit') {
this.form?.requestSubmit();
return;
}
if (this.type === 'reset') {
this.form?.reset();
return;
}
}
#onPrefixIconSlotChange() {
const assignedNodes = this.#prefixIconSlotElementRef.value?.assignedNodes();
this.hasPrefixIcon = Boolean(assignedNodes && assignedNodes.length > 0);
}
#onSuffixIconSlotChange() {
const assignedNodes = this.#suffixIconSlotElementRef.value?.assignedNodes();
this.hasSuffixIcon = Boolean(assignedNodes && assignedNodes.length > 0);
}
}