Elizabeth Mitchell 35913a6ea4 fix(dialog): buttons not reflecting value attribute when setting property
Fixes #5409

PiperOrigin-RevId: 600541901
2024-01-22 12:48:44 -08:00

203 lines
5.5 KiB
TypeScript

/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import '../../focus/md-focus-ring.js';
import '../../ripple/ripple.js';
import {html, isServer, LitElement, nothing} from 'lit';
import {property, query, queryAssignedElements} from 'lit/decorators.js';
import {ARIAMixinStrict} from '../../internal/aria/aria.js';
import {requestUpdateOnAriaChange} from '../../internal/aria/delegate.js';
import {
FormSubmitter,
setupFormSubmitter,
type FormSubmitterType,
} from '../../internal/controller/form-submitter.js';
import {
dispatchActivationClick,
isActivationClick,
} from '../../internal/events/form-label-activation.js';
import {
internals,
mixinElementInternals,
} from '../../labs/behaviors/element-internals.js';
// Separate variable needed for closure.
const buttonBaseClass = mixinElementInternals(LitElement);
/**
* A button component.
*/
export abstract class Button extends buttonBaseClass implements FormSubmitter {
static {
requestUpdateOnAriaChange(Button);
setupFormSubmitter(Button);
}
/** @nocollapse */
static readonly formAssociated = true;
/** @nocollapse */
static override shadowRootOptions: ShadowRootInit = {
mode: 'open',
delegatesFocus: true,
};
/**
* Whether or not the button is disabled.
*/
@property({type: Boolean, reflect: true}) disabled = false;
/**
* The URL that the link button points to.
*/
@property() href = '';
/**
* Where to display the linked `href` URL for a link button. Common options
* include `_blank` to open in a new tab.
*/
@property() target: '_blank' | '_parent' | '_self' | '_top' | '' = '';
/**
* Whether to render the icon at the inline end of the label rather than the
* inline start.
*
* _Note:_ Link buttons cannot have trailing icons.
*/
@property({type: Boolean, attribute: 'trailing-icon', reflect: true})
trailingIcon = false;
/**
* Whether to display the icon or not.
*/
@property({type: Boolean, attribute: 'has-icon', reflect: true}) hasIcon =
false;
/**
* The default behavior of the button. May be "text", "reset", or "submit"
* (default).
*/
@property() type: FormSubmitterType = 'submit';
/**
* The value added to a form with the button's name when the button submits a
* form.
*/
@property({reflect: true}) value = '';
get name() {
return this.getAttribute('name') ?? '';
}
set name(name: string) {
this.setAttribute('name', name);
}
/**
* The associated form element with which this element's value will submit.
*/
get form() {
return this[internals].form;
}
@query('.button') private readonly buttonElement!: HTMLElement | null;
@queryAssignedElements({slot: 'icon', flatten: true})
private readonly assignedIcons!: HTMLElement[];
constructor() {
super();
if (!isServer) {
this.addEventListener('click', this.handleActivationClick);
}
}
override focus() {
this.buttonElement?.focus();
}
override blur() {
this.buttonElement?.blur();
}
protected override render() {
// Link buttons may not be disabled
const isDisabled = this.disabled && !this.href;
const buttonOrLink = this.href ? this.renderLink() : this.renderButton();
// TODO(b/310046938): due to a limitation in focus ring/ripple, we can't use
// the same ID for different elements, so we change the ID instead.
const buttonId = this.href ? 'link' : 'button';
return html`
${this.renderElevationOrOutline?.()}
<div class="background"></div>
<md-focus-ring part="focus-ring" for=${buttonId}></md-focus-ring>
<md-ripple for=${buttonId} ?disabled="${isDisabled}"></md-ripple>
${buttonOrLink}
`;
}
// Buttons can override this to add elevation or an outline. Use this and
// return `<md-elevation>` (for elevated, filled, and tonal buttons)
// or `<div class="outline">` (for outlined buttons).
// Text buttons that have neither do not need to implement this.
protected renderElevationOrOutline?(): unknown;
private renderButton() {
// Needed for closure conformance
const {ariaLabel, ariaHasPopup, ariaExpanded} = this as ARIAMixinStrict;
return html`<button
id="button"
class="button"
?disabled=${this.disabled}
aria-label="${ariaLabel || nothing}"
aria-haspopup="${ariaHasPopup || nothing}"
aria-expanded="${ariaExpanded || nothing}">
${this.renderContent()}
</button>`;
}
private renderLink() {
// Needed for closure conformance
const {ariaLabel, ariaHasPopup, ariaExpanded} = this as ARIAMixinStrict;
return html`<a
id="link"
class="button"
aria-label="${ariaLabel || nothing}"
aria-haspopup="${ariaHasPopup || nothing}"
aria-expanded="${ariaExpanded || nothing}"
href=${this.href}
target=${this.target || nothing}
>${this.renderContent()}
</a>`;
}
private renderContent() {
const icon = html`<slot
name="icon"
@slotchange="${this.handleSlotChange}"></slot>`;
return html`
<span class="touch"></span>
${this.trailingIcon ? nothing : icon}
<span class="label"><slot></slot></span>
${this.trailingIcon ? icon : nothing}
`;
}
private readonly handleActivationClick = (event: MouseEvent) => {
if (!isActivationClick(event) || !this.buttonElement) {
return;
}
this.focus();
dispatchActivationClick(this.buttonElement);
};
private handleSlotChange() {
this.hasIcon = this.assignedIcons.length > 0;
}
}