Files
tubestation/toolkit/content/widgets/moz-button/moz-button.mjs
Tim Giles 632e1e9672 Bug 1902083 - Support outer padding in moz-button. r=reusable-components-reviewers,desktop-theme-reviewers,jules,hjones,dao
Add CSS variables that allows a padded click area around the button
element.

Add test to verify a click event is sent when clicking on the padding.

Add "Toolbar" story to demonstrate how these new CSS variables
can be used.

Move `width` from .button-background to button to restore existing
button behavior.

Differential Revision: https://phabricator.services.mozilla.com/D218599
2024-09-12 14:35:55 +00:00

145 lines
5.4 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { html, ifDefined, classMap } from "../vendor/lit.all.mjs";
import { MozLitElement } from "../lit-utils.mjs";
// eslint-disable-next-line import/no-unassigned-import
import "chrome://global/content/elements/moz-label.mjs";
/**
* A button with multiple types and two sizes.
*
* @tagname moz-button
* @property {string} label - The button's label, will be overridden by slotted content.
* @property {string} type - The button type.
* Options: default, primary, destructive, icon, icon ghost, ghost.
* @property {string} size - The button size.
* Options: default, small.
* @property {boolean} disabled - The disabled state.
* @property {string} title - The button's title attribute, used in shadow DOM and therefore not as an attribute on moz-button.
* @property {string} titleAttribute - Internal, map title attribute to the title JS property.
* @property {string} tooltipText - Set the title property, the title attribute will be used first.
* @property {string} ariaLabel - The button's arial-label attribute, used in shadow DOM and therefore not as an attribute on moz-button.
* @property {string} iconSrc - Path to the icon that should be displayed in the button.
* @property {string} ariaLabelAttribute - Internal, map aria-label attribute to the ariaLabel JS property.
* @property {string} hasVisibleLabel - Internal, tracks whether or not the button has a visible label.
* @property {HTMLButtonElement} buttonEl - The internal button element in the shadow DOM.
* @property {HTMLButtonElement} slotEl - The internal slot element in the shadow DOM.
* @cssproperty [--button-outer-padding-inline] - Used to set the outer inline padding of toolbar style buttons
* @csspropert [--button-outer-padding-block] - Used to set the outer block padding of toolbar style buttons.
* @cssproperty [--button-outer-padding-inline-start] - Used to set the outer inline-start padding of toolbar style buttons
* @cssproperty [--button-outer-padding-inline-end] - Used to set the outer inline-end padding of toolbar style buttons
* @cssproperty [--button-outer-padding-block-start] - Used to set the outer block-start padding of toolbar style buttons
* @cssproperty [--button-outer-padding-block-end] - Used to set the outer block-end padding of toolbar style buttons
* @slot default - The button's content, overrides label property.
* @fires click - The click event.
*/
export default class MozButton extends MozLitElement {
static shadowRootOptions = {
...MozLitElement.shadowRootOptions,
delegatesFocus: true,
};
static properties = {
label: { type: String, reflect: true, fluent: true },
type: { type: String, reflect: true },
size: { type: String, reflect: true },
disabled: { type: Boolean, reflect: true },
title: { type: String, state: true },
titleAttribute: { type: String, attribute: "title", reflect: true },
tooltipText: { type: String, fluent: true },
ariaLabelAttribute: {
type: String,
attribute: "aria-label",
reflect: true,
},
ariaLabel: { type: String, state: true },
iconSrc: { type: String },
hasVisibleLabel: { type: Boolean, state: true },
accessKeyAttribute: { type: String, attribute: "accesskey", reflect: true },
accessKey: { type: String, state: true },
};
static queries = {
buttonEl: "button",
slotEl: "slot",
backgroundEl: ".button-background",
};
constructor() {
super();
this.type = "default";
this.size = "default";
this.disabled = false;
this.hasVisibleLabel = !!this.label;
}
willUpdate(changes) {
if (changes.has("titleAttribute")) {
this.title = this.titleAttribute;
this.titleAttribute = null;
}
if (changes.has("ariaLabelAttribute")) {
this.ariaLabel = this.ariaLabelAttribute;
this.ariaLabelAttribute = null;
}
if (changes.has("accessKeyAttribute")) {
this.accessKey = this.accessKeyAttribute;
this.accessKeyAttribute = null;
}
}
// Delegate clicks on host to the button element.
click() {
this.buttonEl.click();
}
checkForLabelText() {
this.hasVisibleLabel = this.slotEl
?.assignedNodes()
.some(node => node.textContent.trim());
}
labelTemplate() {
if (this.label) {
return this.label;
}
return html`<slot @slotchange=${this.checkForLabelText}></slot>`;
}
render() {
return html`
<link
rel="stylesheet"
href="chrome://global/content/elements/moz-button.css"
/>
<button
?disabled=${this.disabled}
title=${ifDefined(this.title || this.tooltipText)}
aria-label=${ifDefined(this.ariaLabel)}
accesskey=${ifDefined(this.accessKey)}
>
<span
class=${classMap({
labelled: this.label || this.hasVisibleLabel,
"button-background": true,
})}
part="button"
type=${this.type}
size=${this.size}
>
${this.iconSrc
? html`<img src=${this.iconSrc} role="presentation" />`
: ""}
<label is="moz-label" shownaccesskey=${ifDefined(this.accessKey)}>
${this.labelTemplate()}
</label>
</span>
</button>
`;
}
}
customElements.define("moz-button", MozButton);