-
Notifications
You must be signed in to change notification settings - Fork 2
/
button-base.ts
189 lines (161 loc) · 4.67 KB
/
button-base.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
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
import { LitElement, type TemplateResult, html, nothing } from 'lit';
import { property, query } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { EventEmitterMixin } from '../common//mixins/event-emitter.js';
import { addKeyboardFocusRing } from '../common/controllers/focus-ring.js';
import { blazorDeepImport } from '../common/decorators/blazorDeepImport.js';
import type { Constructor } from '../common/mixins/constructor.js';
import { SizableMixin } from '../common/mixins/sizable.js';
import { partNameMap } from '../common/util.js';
export interface IgcButtonEventMap {
igcFocus: CustomEvent<void>;
igcBlur: CustomEvent<void>;
}
@blazorDeepImport
export abstract class IgcButtonBaseComponent extends SizableMixin(
EventEmitterMixin<IgcButtonEventMap, Constructor<LitElement>>(LitElement)
) {
public static readonly formAssociated = true;
protected static shadowRootOptions = {
...LitElement.shadowRootOptions,
delegatesFocus: true,
};
private _kbFocus = addKeyboardFocusRing(this);
protected __internals: ElementInternals;
protected _disabled = false;
@query('[part="base"]', true)
private _nativeButton!: HTMLButtonElement;
/* alternateName: displayType */
/**
* The type of the button. Defaults to `button`.
* @attr
*/
@property({ reflect: true })
public type: 'button' | 'reset' | 'submit' = 'button';
/**
* The URL the button points to.
* @attr
*/
@property()
public href!: string;
/**
* Prompts to save the linked URL instead of navigating to it.
* @attr
*/
@property()
public download!: string;
/**
* Where to display the linked URL, as the name for a browsing context.
* @attr
*/
@property()
public target?: '_blank' | '_parent' | '_self' | '_top';
/**
* The relationship of the linked URL.
* See https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types
* @attr
*/
@property()
public rel!: string;
/**
* The disabled state of the component
* @attr [disabled=false]
*/
@property({ type: Boolean, reflect: true })
public get disabled(): boolean {
return this._disabled;
}
public set disabled(value: boolean) {
this._disabled = value;
this.toggleAttribute('disabled', Boolean(this._disabled));
}
/** Returns the HTMLFormElement associated with this element. */
public get form() {
return this.__internals.form;
}
constructor() {
super();
this.__internals = this.attachInternals();
this.size = 'medium';
}
/* alternateName: focusComponent */
/** Sets focus in the button. */
public override focus(options?: FocusOptions) {
this._nativeButton.focus(options);
}
/** Simulates a mouse click on the element */
public override click() {
this._nativeButton.click();
}
/* alternateName: blurComponent */
/** Removes focus from the button. */
public override blur() {
this._nativeButton.blur();
}
protected handleFocus() {
this.emitEvent('igcFocus');
}
protected handleBlur() {
this.emitEvent('igcBlur');
this._kbFocus.reset();
}
protected handleClick() {
this._kbFocus.reset();
switch (this.type) {
case 'submit':
return this.form?.requestSubmit();
case 'reset':
return this.form?.reset();
default:
return;
}
}
protected formDisabledCallback(state: boolean) {
this._disabled = state;
this.requestUpdate();
}
private renderButton() {
return html`
<button
part=${partNameMap({
base: true,
focused: this._kbFocus.focused,
})}
aria-label=${ifDefined(this.ariaLabel ?? nothing)}
?disabled=${this.disabled}
type=${ifDefined(this.type)}
@click=${this.handleClick}
@focus=${this.handleFocus}
@blur=${this.handleBlur}
>
${this.renderContent()}
</button>
`;
}
private renderLinkButton() {
return html`
<a
part=${partNameMap({
base: true,
focused: this._kbFocus.focused,
})}
role="button"
aria-label=${ifDefined(this.ariaLabel ?? nothing)}
aria-disabled=${this.disabled ? 'true' : 'false'}
href=${ifDefined(this.href)}
target=${ifDefined(this.target)}
download=${ifDefined(this.download)}
rel=${ifDefined(this.rel)}
@focus=${this.disabled ? nothing : this.handleFocus}
@blur=${this.disabled ? nothing : this.handleBlur}
>
${this.renderContent()}
</a>
`;
}
protected abstract renderContent(): TemplateResult;
protected override render() {
const link = this.href !== undefined;
return link ? this.renderLinkButton() : this.renderButton();
}
}