-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbutton-base.ts
More file actions
161 lines (139 loc) · 4.31 KB
/
button-base.ts
File metadata and controls
161 lines (139 loc) · 4.31 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
import { html, LitElement, nothing, 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 { 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';
export interface IgcButtonEventMap {
// For analyzer meta only:
/* skipWCPrefix */
focus: FocusEvent;
/* skipWCPrefix */
blur: FocusEvent;
}
@blazorDeepImport
@shadowOptions({ delegatesFocus: true })
export abstract class IgcButtonBaseComponent extends EventEmitterMixin<
IgcButtonEventMap,
Constructor<LitElement>
>(LitElement) {
public static readonly formAssociated = true;
protected readonly _internals = addInternalsController(this);
private readonly _focusRingManager = addKeyboardFocusRing(this);
protected _disabled = false;
@query('[part="base"]', true)
private readonly _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 set disabled(value: boolean) {
this._disabled = value;
this.toggleAttribute('disabled', Boolean(this._disabled));
}
public get disabled(): boolean {
return this._disabled;
}
/* blazorCSSuppress */
/* alternateType: object */
/** Returns the HTMLFormElement associated with this element. */
public get form(): HTMLFormElement | null {
return this._internals.form;
}
/* alternateName: focusComponent */
/** Sets focus in the button. */
public override focus(options?: FocusOptions): void {
this._nativeButton.focus(options);
}
/** Simulates a mouse click on the element */
public override click(): void {
this._nativeButton.click();
}
/* alternateName: blurComponent */
/** Removes focus from the button. */
public override blur(): void {
this._nativeButton.blur();
}
protected _handleClick(): void {
if (this.type === 'submit') {
this.form?.requestSubmit();
} else if (this.type === 'reset') {
this.form?.reset();
}
}
protected formDisabledCallback(state: boolean): void {
this._disabled = state;
this.requestUpdate();
}
private renderButton() {
return html`
<button
part=${partMap({ base: true, focused: this._focusRingManager.focused })}
aria-label=${ifDefined(this.ariaLabel ?? nothing)}
?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=${ifDefined(this.ariaLabel ?? nothing)}
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() {
const link = this.href !== undefined;
return link ? this.renderLinkButton() : this.renderButton();
}
}