-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbutton-base.ts
More file actions
275 lines (238 loc) · 8.34 KB
/
Copy pathbutton-base.ts
File metadata and controls
275 lines (238 loc) · 8.34 KB
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
import { html, LitElement, type TemplateResult } from 'lit';
import { property, query } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { addKeyboardFocusRing } from '../common/controllers/focus-ring.js';
import { addIdRefResolver } from '../common/controllers/id-resolver.js';
import { addInternalsController } from '../common/controllers/internals.js';
import { blazorDeepImport } from '../common/decorators/blazorDeepImport.js';
import { shadowOptions } from '../common/decorators/shadow-options.js';
import type { Constructor } from '../common/mixins/constructor.js';
import { EventEmitterMixin } from '../common//mixins/event-emitter.js';
import { partMap } from '../common/part-map.js';
import { bindIf, getElementByIdFromRoot } from '../common/util.js';
export interface IgcButtonEventMap {
// For analyzer meta only:
/* skipWCPrefix */
focus: FocusEvent;
/* skipWCPrefix */
blur: FocusEvent;
}
/**
* Abstract base class shared by `igc-button` and `igc-icon-button`.
*
* Provides common form-association behavior, link-button rendering
* (renders as `<a>` when `href` is set), keyboard focus-ring management,
* and the Invoker Commands API (`command` / `commandfor`).
*
* Concrete subclasses must implement `_renderContent()` to supply the
* visual content projected inside the native `<button>` or `<a>` element.
*/
@blazorDeepImport
@shadowOptions({ delegatesFocus: true })
export abstract class IgcButtonBaseComponent extends EventEmitterMixin<
IgcButtonEventMap,
Constructor<LitElement>
>(LitElement) {
public static readonly formAssociated = true;
//#region Internal state
private readonly _focusRingManager = addKeyboardFocusRing(this);
private readonly _internals = addInternalsController(this);
private readonly _resolver = addIdRefResolver(
this,
this._resolveCommandForElement
);
private _disabled = false;
private _commandfor: string | null = null;
private _commandForElement: Element | null = null;
@query('[part="base"]', true)
private readonly _nativeButton?: HTMLButtonElement | HTMLAnchorElement;
//#endregion
//#region Public properties
/* alternateName: displayType */
/**
* The type of the button, which determines its behavior and semantics.
* - `'button'` – no default action; useful for custom JavaScript handlers.
* - `'submit'` – submits the associated form when clicked.
* - `'reset'` – resets the associated form fields to their initial values.
*
* Ignored when the button is rendered as a link (i.e. `href` is set).
* @attr type
* @default 'button'
*/
@property({ reflect: true })
public type: 'button' | 'reset' | 'submit' = 'button';
/**
* The URL the button points to. When set, the component renders as an
* `<a>` element instead of a `<button>`, enabling navigation on click.
* Use together with `target`, `download`, and `rel` for full anchor semantics.
* @attr href
*/
@property()
public href?: string;
/**
* Prompts the browser to download the linked resource rather than navigating
* to it. The optional value is used as the suggested file name.
* Only effective when `href` is set.
* @attr download
*/
@property()
public download?: string;
/**
* Where to open the linked document. Only effective when `href` is set.
* - `'_self'` – current browsing context (default browser behavior).
* - `'_blank'` – new tab or window.
* - `'_parent'` – parent browsing context; falls back to `_self` if none.
* - `'_top'` – top-level browsing context; falls back to `_self` if none.
* @attr target
*/
@property()
public target?: '_blank' | '_parent' | '_self' | '_top';
/**
* The relationship between the current document and the linked URL.
* Accepts a space-separated list of link types (e.g. `'noopener noreferrer'`).
* Only effective when `href` is set. When `target="_blank"` is used,
* setting `rel="noopener noreferrer"` is strongly recommended for security.
* @attr rel
*/
@property()
public rel?: string;
/**
* When set, the button will be disabled and non-interactive.
*
* @attr disabled
* @default false
*/
@property({ type: Boolean, reflect: true })
public set disabled(value: boolean) {
this._disabled = value;
this.toggleAttribute('disabled', Boolean(this._disabled));
}
public get disabled(): boolean {
return this._disabled;
}
/**
* The command to invoke on the target element specified by `commandfor`.
* Part of the [Invoker Commands](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API) API.
* Custom commands must start with two dashes (e.g. `'--my-command'`).
* @attr command
*/
@property({ reflect: true })
public command?: string;
/**
* The ID of the target element for the invoker command.
* Part of the [Invoker Commands API](https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API).
* @attr commandfor
*/
@property({ attribute: 'commandfor' })
public set commandfor(value: string | null) {
this._commandfor = value;
if (value) {
this._resolver.observe();
this._commandForElement = getElementByIdFromRoot(this, value);
} else {
this._commandForElement = null;
this._resolver.unobserve();
}
}
public get commandfor(): string | null {
return this._commandfor;
}
/* blazorCSSuppress */
/* alternateType: object */
/**
* The target element for the invoker command. Resolved from the `commandfor` ID.
*/
public get commandForElement(): Element | null {
return this._commandForElement;
}
/* blazorCSSuppress */
/* alternateType: object */
public set commandForElement(value: Element | null) {
this._commandForElement = value;
this.requestUpdate();
}
/* blazorCSSuppress */
/* alternateType: object */
/**
* The `<form>` element the button is associated with.
* Resolved through the standard form-association mechanism — either the
* closest ancestor `<form>` or the form referenced by the button's `form`
* attribute. Returns `null` when no form is associated.
* Relevant only when `type` is `'submit'` or `'reset'`.
*/
public get form(): HTMLFormElement | null {
return this._internals.form;
}
//#endregion
//#region Lifecycle
protected override firstUpdated(): void {
if (this._commandfor) {
this._commandForElement = getElementByIdFromRoot(this, this._commandfor);
this.requestUpdate();
}
}
//#endregion
//#region Event handlers
protected _handleClick(): void {
switch (this.type) {
case 'submit':
this.form?.requestSubmit();
break;
case 'reset':
this.form?.reset();
break;
}
}
protected formDisabledCallback(state: boolean): void {
this._disabled = state;
this.requestUpdate();
}
//#endregion
private _resolveCommandForElement(ids: Set<string>): void {
if (this._commandfor && ids.has(this._commandfor)) {
this._commandForElement = getElementByIdFromRoot(this, this._commandfor);
this.requestUpdate();
}
}
//#region Public API
/** Simulates a mouse click on the button, triggering its click handler and any associated form action. */
public override click(): void {
this._nativeButton?.click();
}
//#endregion
private _renderButton() {
return html`
<button
command=${ifDefined(this.command)}
.commandForElement=${this._commandForElement}
part=${partMap({ base: true, focused: this._focusRingManager.focused })}
aria-label=${bindIf(this.ariaLabel, this.ariaLabel)}
?disabled=${this.disabled}
type=${ifDefined(this.type)}
@click=${this._handleClick}
>
${this._renderContent()}
</button>
`;
}
private _renderLinkButton() {
return html`
<a
part=${partMap({ base: true, focused: this._focusRingManager.focused })}
role="button"
aria-label=${bindIf(this.ariaLabel, this.ariaLabel)}
aria-disabled=${this.disabled}
href=${ifDefined(this.href)}
target=${ifDefined(this.target)}
download=${ifDefined(this.download)}
rel=${ifDefined(this.rel)}
>
${this._renderContent()}
</a>
`;
}
protected abstract _renderContent(): TemplateResult;
protected override render() {
return this.href != null ? this._renderLinkButton() : this._renderButton();
}
}