|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {DOCUMENT} from '@angular/common'; |
| 10 | +import { |
| 11 | + ANIMATION_MODULE_TYPE, |
| 12 | + ElementRef, |
| 13 | + Injectable, |
| 14 | + NgZone, |
| 15 | + OnDestroy, |
| 16 | + inject, |
| 17 | +} from '@angular/core'; |
| 18 | +import { |
| 19 | + MAT_RIPPLE_GLOBAL_OPTIONS, |
| 20 | + MatRipple, |
| 21 | + RippleConfig, |
| 22 | + RippleGlobalOptions, |
| 23 | + RippleRenderer, |
| 24 | + RippleTarget, |
| 25 | +} from '@angular/material/core'; |
| 26 | +import {Platform} from '@angular/cdk/platform'; |
| 27 | + |
| 28 | +/** The options for the MatButtonRippleLoader's event listeners. */ |
| 29 | +const eventListenerOptions = {capture: true}; |
| 30 | + |
| 31 | +/** The events that should trigger the initialization of the ripple. */ |
| 32 | +const rippleInteractionEvents = ['focus', 'click', 'mouseenter', 'touchstart']; |
| 33 | + |
| 34 | +/** The attribute attached to a mat-button whose ripple has not yet been initialized. */ |
| 35 | +export const MAT_BUTTON_RIPPLE_UNINITIALIZED = 'mat-button-ripple-uninitialized'; |
| 36 | + |
| 37 | +/** |
| 38 | + * Handles attaching the MatButton's ripple on demand. |
| 39 | + * |
| 40 | + * This service allows us to avoid eagerly creating & attaching the MatButton's ripple. |
| 41 | + * It works by creating & attaching the ripple only when a MatButton is first interacted with. |
| 42 | + */ |
| 43 | +@Injectable({providedIn: 'root'}) |
| 44 | +export class MatButtonLazyLoader implements OnDestroy { |
| 45 | + private _document = inject(DOCUMENT, {optional: true}); |
| 46 | + private _animationMode = inject(ANIMATION_MODULE_TYPE, {optional: true}); |
| 47 | + private _globalRippleOptions = inject(MAT_RIPPLE_GLOBAL_OPTIONS, {optional: true}); |
| 48 | + private _platform = inject(Platform); |
| 49 | + private _ngZone = inject(NgZone); |
| 50 | + |
| 51 | + constructor() { |
| 52 | + this._ngZone.runOutsideAngular(() => { |
| 53 | + for (const event of rippleInteractionEvents) { |
| 54 | + this._document?.addEventListener(event, this._onInteraction, eventListenerOptions); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + ngOnDestroy() { |
| 60 | + for (const event of rippleInteractionEvents) { |
| 61 | + this._document?.removeEventListener(event, this._onInteraction, eventListenerOptions); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** Handles creating and attaching button internals when a button is initially interacted with. */ |
| 66 | + private _onInteraction = (event: Event) => { |
| 67 | + if (event.target === this._document) { |
| 68 | + return; |
| 69 | + } |
| 70 | + const eventTarget = event.target as Element; |
| 71 | + |
| 72 | + // TODO(wagnermaciel): Consider batching these events to improve runtime performance. |
| 73 | + |
| 74 | + const button = eventTarget.closest(`[${MAT_BUTTON_RIPPLE_UNINITIALIZED}]`); |
| 75 | + if (button) { |
| 76 | + button.removeAttribute(MAT_BUTTON_RIPPLE_UNINITIALIZED); |
| 77 | + this._appendRipple(button as HTMLElement); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + /** Creates a MatButtonRipple and appends it to the given button element. */ |
| 82 | + private _appendRipple(button: HTMLElement): void { |
| 83 | + if (!this._document) { |
| 84 | + return; |
| 85 | + } |
| 86 | + const ripple = this._document.createElement('span'); |
| 87 | + ripple.classList.add('mat-mdc-button-ripple'); |
| 88 | + |
| 89 | + const target = new MatButtonRippleTarget( |
| 90 | + button, |
| 91 | + this._globalRippleOptions ? this._globalRippleOptions : undefined, |
| 92 | + this._animationMode ? this._animationMode : undefined, |
| 93 | + ); |
| 94 | + target.rippleConfig.centered = button.hasAttribute('mat-icon-button'); |
| 95 | + |
| 96 | + const rippleRenderer = new RippleRenderer(target, this._ngZone, ripple, this._platform); |
| 97 | + rippleRenderer.setupTriggerEvents(button); |
| 98 | + button.append(ripple); |
| 99 | + } |
| 100 | + |
| 101 | + _createMatRipple(button: HTMLElement): MatRipple | undefined { |
| 102 | + if (!this._document) { |
| 103 | + return; |
| 104 | + } |
| 105 | + button.querySelector('.mat-mdc-button-ripple')?.remove(); |
| 106 | + button.removeAttribute(MAT_BUTTON_RIPPLE_UNINITIALIZED); |
| 107 | + const rippleEl = this._document!.createElement('span'); |
| 108 | + rippleEl.classList.add('mat-mdc-button-ripple'); |
| 109 | + const ripple = new MatRipple( |
| 110 | + new ElementRef(rippleEl), |
| 111 | + this._ngZone, |
| 112 | + this._platform, |
| 113 | + this._globalRippleOptions ? this._globalRippleOptions : undefined, |
| 114 | + this._animationMode ? this._animationMode : undefined, |
| 115 | + ); |
| 116 | + ripple._isInitialized = true; |
| 117 | + ripple.trigger = button; |
| 118 | + button.append(rippleEl); |
| 119 | + return ripple; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * The RippleTarget for the lazily rendered MatButton ripple. |
| 125 | + * It handles ripple configuration and disabled state for ripples interactions. |
| 126 | + * |
| 127 | + * Note that this configuration is usually handled by the MatRipple, but the MatButtonLazyLoader does not use the |
| 128 | + * MatRipple Directive. In order to create & attach a ripple on demand, it uses the "lower level" RippleRenderer. |
| 129 | + */ |
| 130 | +class MatButtonRippleTarget implements RippleTarget { |
| 131 | + rippleConfig: RippleConfig & RippleGlobalOptions; |
| 132 | + |
| 133 | + constructor( |
| 134 | + private _button: HTMLElement, |
| 135 | + private _globalRippleOptions?: RippleGlobalOptions, |
| 136 | + animationMode?: string, |
| 137 | + ) { |
| 138 | + this._setRippleConfig(_globalRippleOptions, animationMode); |
| 139 | + } |
| 140 | + |
| 141 | + private _setRippleConfig(globalRippleOptions?: RippleGlobalOptions, animationMode?: string) { |
| 142 | + this.rippleConfig = globalRippleOptions || {}; |
| 143 | + if (animationMode === 'NoopAnimations') { |
| 144 | + this.rippleConfig.animation = {enterDuration: 0, exitDuration: 0}; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + get rippleDisabled(): boolean { |
| 149 | + return this._button.hasAttribute('disabled') || !!this._globalRippleOptions?.disabled; |
| 150 | + } |
| 151 | +} |
0 commit comments