2021-09-24 21:31:43 +03:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2021 Google LLC
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-08-24 21:00:50 +03:00
|
|
|
import '@material/web/focus/focus-ring.js';
|
2022-09-16 23:51:46 +03:00
|
|
|
import '@material/web/ripple/ripple.js';
|
2021-09-24 21:31:43 +03:00
|
|
|
|
2022-09-28 23:33:19 +03:00
|
|
|
import {ActionElement, BeginPressConfig, EndPressConfig} from '@material/web/actionelement/action-element.js';
|
2022-08-24 21:00:50 +03:00
|
|
|
import {FormController, getFormValue} from '@material/web/controller/form-controller.js';
|
|
|
|
import {ariaProperty} from '@material/web/decorators/aria-property.js';
|
|
|
|
import {pointerPress as focusRingPointerPress, shouldShowStrongFocus} from '@material/web/focus/strong-focus.js';
|
2022-09-16 23:51:46 +03:00
|
|
|
import {MdRipple} from '@material/web/ripple/ripple.js';
|
2022-07-22 20:13:21 +03:00
|
|
|
import {html, TemplateResult} from 'lit';
|
2022-09-16 23:51:46 +03:00
|
|
|
import {eventOptions, property, query, state} from 'lit/decorators.js';
|
2022-08-24 21:00:50 +03:00
|
|
|
import {ClassInfo, classMap} from 'lit/directives/class-map.js';
|
|
|
|
import {ifDefined} from 'lit/directives/if-defined.js';
|
2021-12-17 00:36:39 +03:00
|
|
|
|
2021-09-28 02:27:12 +03:00
|
|
|
/** @soyCompatible */
|
2022-07-22 20:13:21 +03:00
|
|
|
export class Switch extends ActionElement {
|
2021-12-29 20:45:59 +03:00
|
|
|
static override shadowRootOptions:
|
|
|
|
ShadowRootInit = {mode: 'open', delegatesFocus: true};
|
|
|
|
|
|
|
|
@property({type: Boolean, reflect: true}) disabled = false;
|
2021-09-24 21:31:43 +03:00
|
|
|
@property({type: Boolean}) processing = false;
|
|
|
|
@property({type: Boolean}) selected = false;
|
2022-04-29 21:43:31 +03:00
|
|
|
@property({type: Boolean}) icons = false;
|
2022-07-26 19:20:02 +03:00
|
|
|
@property({type: Boolean}) showOnlySelectedIcon = false;
|
2021-09-24 21:31:43 +03:00
|
|
|
|
|
|
|
// Aria
|
2022-07-21 20:55:19 +03:00
|
|
|
@ariaProperty // tslint:disable-line:no-new-decorators
|
2021-12-17 00:36:39 +03:00
|
|
|
// TODO(b/210730484): replace with @soyParam annotation
|
|
|
|
@property({type: String, attribute: 'data-aria-label', noAccessor: true})
|
|
|
|
override ariaLabel!: string;
|
2021-09-24 21:31:43 +03:00
|
|
|
|
2021-12-17 00:36:39 +03:00
|
|
|
// TODO: Add support in @ariaProperty for idref aria attributes
|
2022-09-28 23:33:19 +03:00
|
|
|
@ariaProperty // tslint:disable-line:no-new-decorators
|
|
|
|
@property({type: String, attribute: 'data-aria-labelledby', noAccessor: true})
|
2021-09-24 21:31:43 +03:00
|
|
|
ariaLabelledBy = '';
|
|
|
|
|
2022-03-29 23:50:10 +03:00
|
|
|
@state() protected showFocusRing = false;
|
|
|
|
|
2022-09-16 23:51:46 +03:00
|
|
|
// Ripple
|
|
|
|
@query('md-ripple') readonly ripple!: MdRipple;
|
|
|
|
|
2021-12-29 20:45:59 +03:00
|
|
|
// FormController
|
|
|
|
get form() {
|
|
|
|
return this.closest('form');
|
|
|
|
}
|
2021-09-24 21:31:43 +03:00
|
|
|
@property({type: String, reflect: true}) name = '';
|
|
|
|
@property({type: String}) value = 'on';
|
2021-12-29 20:45:59 +03:00
|
|
|
[getFormValue]() {
|
|
|
|
return this.selected ? this.value : null;
|
2021-09-24 21:31:43 +03:00
|
|
|
}
|
|
|
|
|
2021-12-29 20:45:59 +03:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.addController(new FormController(this));
|
|
|
|
}
|
2021-09-24 21:31:43 +03:00
|
|
|
|
|
|
|
override click() {
|
2022-07-22 20:13:21 +03:00
|
|
|
this.endPress({cancelled: false});
|
2021-12-29 20:45:59 +03:00
|
|
|
super.click();
|
2021-09-24 21:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @soyTemplate */
|
|
|
|
protected override render(): TemplateResult {
|
2022-07-01 21:29:51 +03:00
|
|
|
const ariaLabelValue = this.ariaLabel ? this.ariaLabel : undefined;
|
|
|
|
const ariaLabelledByValue =
|
|
|
|
this.ariaLabelledBy ? this.ariaLabelledBy : undefined;
|
2022-04-29 21:43:31 +03:00
|
|
|
// TODO(b/230763631): update this template to include spans instead of divs
|
2021-09-24 21:31:43 +03:00
|
|
|
return html`
|
|
|
|
<button
|
|
|
|
type="button"
|
2022-03-23 23:50:50 +03:00
|
|
|
class="md3-switch ${classMap(this.getRenderClasses())}"
|
2021-09-24 21:31:43 +03:00
|
|
|
role="switch"
|
|
|
|
aria-checked="${this.selected}"
|
2022-07-01 21:29:51 +03:00
|
|
|
aria-label="${ifDefined(ariaLabelValue)}"
|
|
|
|
aria-labelledby="${ifDefined(ariaLabelledByValue)}"
|
2022-07-23 01:26:44 +03:00
|
|
|
?disabled=${this.disabled}
|
2021-09-24 21:31:43 +03:00
|
|
|
@click=${this.handleClick}
|
|
|
|
@focus="${this.handleFocus}"
|
|
|
|
@blur="${this.handleBlur}"
|
2022-07-22 20:13:21 +03:00
|
|
|
@pointerdown=${this.handlePointerDown}
|
2022-09-16 23:51:46 +03:00
|
|
|
@pointerenter=${this.handlePointerEnter}
|
2022-07-22 20:13:21 +03:00
|
|
|
@pointerup=${this.handlePointerUp}
|
|
|
|
@pointercancel=${this.handlePointerCancel}
|
|
|
|
@pointerleave=${this.handlePointerLeave}
|
|
|
|
@contextmenu=${this.handleContextMenu}
|
2021-09-24 21:31:43 +03:00
|
|
|
>
|
2022-03-29 23:50:10 +03:00
|
|
|
${this.renderFocusRing()}
|
2022-04-29 21:43:31 +03:00
|
|
|
<div class="md3-switch__track">
|
2021-09-24 21:31:43 +03:00
|
|
|
${this.renderHandle()}
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<input
|
2022-07-27 05:19:22 +03:00
|
|
|
class="md3-switch__input"
|
2021-09-24 21:31:43 +03:00
|
|
|
type="checkbox"
|
|
|
|
aria-hidden="true"
|
|
|
|
name="${this.name}"
|
2022-07-23 01:26:44 +03:00
|
|
|
?checked=${this.selected}
|
2021-09-24 21:31:43 +03:00
|
|
|
.value=${this.value}
|
|
|
|
>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @soyTemplate */
|
|
|
|
protected getRenderClasses(): ClassInfo {
|
|
|
|
return {
|
2022-03-23 23:50:50 +03:00
|
|
|
'md3-switch--processing': this.processing,
|
|
|
|
'md3-switch--selected': this.selected,
|
|
|
|
'md3-switch--unselected': !this.selected,
|
2021-09-24 21:31:43 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-16 23:51:46 +03:00
|
|
|
/** @soyTemplate */
|
|
|
|
protected renderRipple(): TemplateResult {
|
|
|
|
return html`
|
|
|
|
<div class="md3-switch__ripple">
|
|
|
|
<md-ripple
|
|
|
|
?disabled="${this.disabled}"
|
|
|
|
unbounded>
|
|
|
|
</md-ripple>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-03-29 23:50:10 +03:00
|
|
|
/** @soyTemplate */
|
|
|
|
protected renderFocusRing(): TemplateResult {
|
|
|
|
return html`<md-focus-ring .visible="${
|
|
|
|
this.showFocusRing}"></md-focus-ring>`;
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:31:43 +03:00
|
|
|
/** @soyTemplate */
|
|
|
|
protected renderHandle(): TemplateResult {
|
2022-05-11 02:01:43 +03:00
|
|
|
/** @classMap */
|
|
|
|
const classes = {
|
2022-07-26 19:20:02 +03:00
|
|
|
'md3-switch__handle--big': this.icons && !this.showOnlySelectedIcon,
|
2022-05-11 02:01:43 +03:00
|
|
|
};
|
2021-09-24 21:31:43 +03:00
|
|
|
return html`
|
2022-05-11 02:01:43 +03:00
|
|
|
<div class="md3-switch__handle-container">
|
2022-09-16 23:51:46 +03:00
|
|
|
${this.renderRipple()}
|
2022-05-11 02:01:43 +03:00
|
|
|
<div class="md3-switch__handle ${classMap(classes)}">
|
|
|
|
${this.shouldShowIcons() ? this.renderIcons() : html``}
|
|
|
|
</div>
|
|
|
|
${this.renderTouchTarget()}
|
2021-09-24 21:31:43 +03:00
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @soyTemplate */
|
2022-05-06 20:25:04 +03:00
|
|
|
private renderIcons(): TemplateResult {
|
2021-09-24 21:31:43 +03:00
|
|
|
return html`
|
2022-04-29 21:43:31 +03:00
|
|
|
<div class="md3-switch__icons">
|
|
|
|
${this.renderOnIcon()}
|
2022-07-26 19:20:02 +03:00
|
|
|
${this.showOnlySelectedIcon ? html`` : this.renderOffIcon()}
|
2021-09-24 21:31:43 +03:00
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-05-06 20:25:04 +03:00
|
|
|
/**
|
|
|
|
* https://fonts.google.com/icons?selected=Material%20Symbols%20Outlined%3Acheck%3AFILL%400%3Bwght%40500%3BGRAD%400%3Bopsz%4024
|
|
|
|
*
|
|
|
|
* @soyTemplate
|
|
|
|
*/
|
2021-09-24 21:31:43 +03:00
|
|
|
protected renderOnIcon(): TemplateResult {
|
|
|
|
return html`
|
2022-03-23 23:50:50 +03:00
|
|
|
<svg class="md3-switch__icon md3-switch__icon--on" viewBox="0 0 24 24">
|
2022-05-06 20:25:04 +03:00
|
|
|
<path d="M9.55 18.2 3.65 12.3 5.275 10.675 9.55 14.95 18.725 5.775 20.35 7.4Z"/>
|
2021-09-24 21:31:43 +03:00
|
|
|
</svg>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-05-06 20:25:04 +03:00
|
|
|
/**
|
|
|
|
* https://fonts.google.com/icons?selected=Material%20Symbols%20Outlined%3Aclose%3AFILL%400%3Bwght%40500%3BGRAD%400%3Bopsz%4024
|
|
|
|
*
|
|
|
|
* @soyTemplate
|
|
|
|
*/
|
2021-09-24 21:31:43 +03:00
|
|
|
protected renderOffIcon(): TemplateResult {
|
|
|
|
return html`
|
2022-03-23 23:50:50 +03:00
|
|
|
<svg class="md3-switch__icon md3-switch__icon--off" viewBox="0 0 24 24">
|
2022-05-06 20:25:04 +03:00
|
|
|
<path d="M6.4 19.2 4.8 17.6 10.4 12 4.8 6.4 6.4 4.8 12 10.4 17.6 4.8 19.2 6.4 13.6 12 19.2 17.6 17.6 19.2 12 13.6Z"/>
|
2021-09-24 21:31:43 +03:00
|
|
|
</svg>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2022-05-06 20:25:04 +03:00
|
|
|
/** @soyTemplate */
|
|
|
|
private renderTouchTarget(): TemplateResult {
|
|
|
|
return html`<span class="md3-switch__touch"></span>`;
|
|
|
|
}
|
|
|
|
|
2022-05-11 02:01:43 +03:00
|
|
|
/** @soyTemplate */
|
|
|
|
private shouldShowIcons(): boolean {
|
2022-07-26 19:20:02 +03:00
|
|
|
return this.icons || this.showOnlySelectedIcon;
|
2022-05-06 20:25:04 +03:00
|
|
|
}
|
|
|
|
|
2022-09-16 23:51:46 +03:00
|
|
|
override beginPress({positionEvent}: BeginPressConfig) {
|
|
|
|
this.ripple.beginPress(positionEvent);
|
|
|
|
}
|
|
|
|
|
2022-07-22 20:13:21 +03:00
|
|
|
override endPress({cancelled}: EndPressConfig) {
|
2022-09-16 23:51:46 +03:00
|
|
|
this.ripple.endPress();
|
|
|
|
|
2022-07-22 20:13:21 +03:00
|
|
|
if (cancelled || this.disabled) {
|
2022-03-23 04:18:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.selected = !this.selected;
|
2022-07-22 20:13:21 +03:00
|
|
|
super.endPress({cancelled, actionData: {selected: this.selected}});
|
2021-09-24 21:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected handleFocus() {
|
2022-03-29 23:50:10 +03:00
|
|
|
this.showFocusRing = shouldShowStrongFocus();
|
2021-09-24 21:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected handleBlur() {
|
2022-03-29 23:50:10 +03:00
|
|
|
this.showFocusRing = false;
|
2021-09-24 21:31:43 +03:00
|
|
|
}
|
|
|
|
|
2022-09-16 23:51:46 +03:00
|
|
|
protected handlePointerEnter(e: PointerEvent) {
|
|
|
|
this.ripple.beginHover(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
override handlePointerLeave(e: PointerEvent) {
|
|
|
|
super.handlePointerLeave(e);
|
|
|
|
this.ripple.endHover();
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:31:43 +03:00
|
|
|
@eventOptions({passive: true})
|
2022-07-22 20:13:21 +03:00
|
|
|
override handlePointerDown(event: PointerEvent) {
|
|
|
|
super.handlePointerDown(event);
|
2022-04-29 21:43:31 +03:00
|
|
|
focusRingPointerPress();
|
|
|
|
this.showFocusRing = false;
|
2021-09-24 21:31:43 +03:00
|
|
|
}
|
|
|
|
}
|