UNPKG

@ng-matero/extensions

Version:
915 lines 136 kB
import { coerceBooleanProperty, coerceNumberProperty, } from '@angular/cdk/coercion'; import { ESCAPE, hasModifierKey } from '@angular/cdk/keycodes'; import { Overlay, } from '@angular/cdk/overlay'; import { normalizePassiveListenerOptions } from '@angular/cdk/platform'; import { ComponentPortal } from '@angular/cdk/portal'; import { DOCUMENT, NgClass, NgTemplateOutlet } from '@angular/common'; import { afterNextRender, ANIMATION_MODULE_TYPE, ChangeDetectionStrategy, Component, Directive, ElementRef, Inject, inject, InjectionToken, Injector, Input, Optional, TemplateRef, ViewChild, ViewEncapsulation, } from '@angular/core'; import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { MtxIsTemplateRefPipe } from '@ng-matero/extensions/core'; import * as i0 from "@angular/core"; import * as i1 from "@angular/cdk/overlay"; import * as i2 from "@angular/cdk/platform"; import * as i3 from "@angular/cdk/a11y"; import * as i4 from "@angular/cdk/bidi"; /** Time in ms to throttle repositioning after scroll events. */ export const SCROLL_THROTTLE_MS = 20; /** * Creates an error to be thrown if the user supplied an invalid tooltip position. * @docs-private */ export function getMtxTooltipInvalidPositionError(position) { return Error(`Tooltip position "${position}" is invalid.`); } /** Injection token that determines the scroll handling while a tooltip is visible. */ export const MTX_TOOLTIP_SCROLL_STRATEGY = new InjectionToken('mtx-tooltip-scroll-strategy', { providedIn: 'root', factory: () => { const overlay = inject(Overlay); return () => overlay.scrollStrategies.reposition({ scrollThrottle: SCROLL_THROTTLE_MS }); }, }); /** @docs-private */ export function MTX_TOOLTIP_SCROLL_STRATEGY_FACTORY(overlay) { return () => overlay.scrollStrategies.reposition({ scrollThrottle: SCROLL_THROTTLE_MS }); } /** @docs-private */ export const MTX_TOOLTIP_SCROLL_STRATEGY_FACTORY_PROVIDER = { provide: MTX_TOOLTIP_SCROLL_STRATEGY, deps: [Overlay], useFactory: MTX_TOOLTIP_SCROLL_STRATEGY_FACTORY, }; /** @docs-private */ export function MTX_TOOLTIP_DEFAULT_OPTIONS_FACTORY() { return { showDelay: 0, hideDelay: 0, touchendHideDelay: 1500, }; } /** Injection token to be used to override the default options for `mtxTooltip`. */ export const MTX_TOOLTIP_DEFAULT_OPTIONS = new InjectionToken('mtx-tooltip-default-options', { providedIn: 'root', factory: MTX_TOOLTIP_DEFAULT_OPTIONS_FACTORY, }); /** * CSS class that will be attached to the overlay panel. * @deprecated * @breaking-change 13.0.0 remove this variable */ export const TOOLTIP_PANEL_CLASS = 'mtx-mdc-tooltip-panel'; const PANEL_CLASS = 'tooltip-panel'; /** Options used to bind passive event listeners. */ const passiveListenerOptions = normalizePassiveListenerOptions({ passive: true }); // These constants were taken from MDC's `numbers` object. We can't import them from MDC, // because they have some top-level references to `window` which break during SSR. const MIN_VIEWPORT_TOOLTIP_THRESHOLD = 8; const UNBOUNDED_ANCHOR_GAP = 8; const MIN_HEIGHT = 24; const MAX_WIDTH = 200; /** * Directive that attaches a material design tooltip to the host element. Animates the showing and * hiding of a tooltip provided position (defaults to below the element). * * https://material.io/design/components/tooltips.html */ export class MtxTooltip { /** Allows the user to define the position of the tooltip relative to the parent element */ get position() { return this._position; } set position(value) { if (value !== this._position) { this._position = value; if (this._overlayRef) { this._updatePosition(this._overlayRef); this._tooltipInstance?.show(0); this._overlayRef.updatePosition(); } } } /** * Whether tooltip should be relative to the click or touch origin * instead of outside the element bounding box. */ get positionAtOrigin() { return this._positionAtOrigin; } set positionAtOrigin(value) { this._positionAtOrigin = coerceBooleanProperty(value); this._detach(); this._overlayRef = null; } /** Disables the display of the tooltip. */ get disabled() { return this._disabled; } set disabled(value) { this._disabled = coerceBooleanProperty(value); // If tooltip is disabled, hide immediately. if (this._disabled) { this.hide(0); } else { this._setupPointerEnterEventsIfNeeded(); } } /** The default delay in ms before showing the tooltip after show is called */ get showDelay() { return this._showDelay; } set showDelay(value) { this._showDelay = coerceNumberProperty(value); } /** The default delay in ms before hiding the tooltip after hide is called */ get hideDelay() { return this._hideDelay; } set hideDelay(value) { this._hideDelay = coerceNumberProperty(value); if (this._tooltipInstance) { this._tooltipInstance._mouseLeaveHideDelay = this._hideDelay; } } /** The message to be displayed in the tooltip */ get message() { return this._message; } set message(value) { this._ariaDescriber.removeDescription(this._elementRef.nativeElement, this._message, 'tooltip'); // TODO: If the message is a TemplateRef, it's hard to support a11y. // If the message is not a string (e.g. number), convert it to a string and trim it. // Must convert with `String(value)`, not `${value}`, otherwise Closure Compiler optimises // away the string-conversion: https://github.com/angular/components/issues/20684 this._message = value instanceof TemplateRef ? value : value != null ? `${value}`.trim() : ''; if (!this._message && this._isTooltipVisible()) { this.hide(0); } else { this._setupPointerEnterEventsIfNeeded(); this._updateTooltipMessage(); this._ngZone.runOutsideAngular(() => { // The `AriaDescriber` has some functionality that avoids adding a description if it's the // same as the `aria-label` of an element, however we can't know whether the tooltip trigger // has a data-bound `aria-label` or when it'll be set for the first time. We can avoid the // issue by deferring the description by a tick so Angular has time to set the `aria-label`. Promise.resolve().then(() => { this._ariaDescriber.describe(this._elementRef.nativeElement, this.message, 'tooltip'); }); }); } } /** Context to be passed to the tooltip. */ get tooltipContext() { return this._tooltipContext; } set tooltipContext(value) { this._tooltipContext = value; this._setTooltipContext(this._tooltipContext); } /** Classes to be passed to the tooltip. Supports the same syntax as `ngClass`. */ get tooltipClass() { return this._tooltipClass; } set tooltipClass(value) { this._tooltipClass = value; if (this._tooltipInstance) { this._setTooltipClass(this._tooltipClass); } } constructor(_overlay, _elementRef, _scrollDispatcher, _viewContainerRef, _ngZone, _platform, _ariaDescriber, _focusMonitor, scrollStrategy, _dir, _defaultOptions, _document) { this._overlay = _overlay; this._elementRef = _elementRef; this._scrollDispatcher = _scrollDispatcher; this._viewContainerRef = _viewContainerRef; this._ngZone = _ngZone; this._platform = _platform; this._ariaDescriber = _ariaDescriber; this._focusMonitor = _focusMonitor; this._dir = _dir; this._defaultOptions = _defaultOptions; this._overlayRef = null; this._tooltipInstance = null; this._position = 'below'; this._positionAtOrigin = false; this._disabled = false; this._viewInitialized = false; this._pointerExitEventsInitialized = false; this._tooltipComponent = TooltipComponent; this._viewportMargin = 8; this._cssClassPrefix = 'mtx-mdc'; /** * How touch gestures should be handled by the tooltip. On touch devices the tooltip directive * uses a long press gesture to show and hide, however it can conflict with the native browser * gestures. To work around the conflict, Angular Material disables native gestures on the * trigger, but that might not be desirable on particular elements (e.g. inputs and draggable * elements). The different values for this option configure the touch event handling as follows: * - `auto` - Enables touch gestures for all elements, but tries to avoid conflicts with native * browser gestures on particular elements. In particular, it allows text selection on inputs * and textareas, and preserves the native browser dragging on elements marked as `draggable`. * - `on` - Enables touch gestures for all elements and disables native * browser gestures with no exceptions. * - `off` - Disables touch gestures. Note that this will prevent the tooltip from * showing on touch devices. */ this.touchGestures = 'auto'; this._message = ''; /** Manually-bound passive event listeners. */ this._passiveListeners = []; /** Emits when the component is destroyed. */ this._destroyed = new Subject(); this._injector = inject(Injector); this._scrollStrategy = scrollStrategy; this._document = _document; if (_defaultOptions) { this._showDelay = _defaultOptions.showDelay; this._hideDelay = _defaultOptions.hideDelay; if (_defaultOptions.position) { this.position = _defaultOptions.position; } if (_defaultOptions.positionAtOrigin) { this.positionAtOrigin = _defaultOptions.positionAtOrigin; } if (_defaultOptions.touchGestures) { this.touchGestures = _defaultOptions.touchGestures; } } _dir.change.pipe(takeUntil(this._destroyed)).subscribe(() => { if (this._overlayRef) { this._updatePosition(this._overlayRef); } }); this._viewportMargin = MIN_VIEWPORT_TOOLTIP_THRESHOLD; } ngAfterViewInit() { // This needs to happen after view init so the initial values for all inputs have been set. this._viewInitialized = true; this._setupPointerEnterEventsIfNeeded(); this._focusMonitor .monitor(this._elementRef) .pipe(takeUntil(this._destroyed)) .subscribe(origin => { // Note that the focus monitor runs outside the Angular zone. if (!origin) { this._ngZone.run(() => this.hide(0)); } else if (origin === 'keyboard') { this._ngZone.run(() => this.show()); } }); } /** * Dispose the tooltip when destroyed. */ ngOnDestroy() { const nativeElement = this._elementRef.nativeElement; clearTimeout(this._touchstartTimeout); if (this._overlayRef) { this._overlayRef.dispose(); this._tooltipInstance = null; } // Clean up the event listeners set in the constructor this._passiveListeners.forEach(([event, listener]) => { nativeElement.removeEventListener(event, listener, passiveListenerOptions); }); this._passiveListeners.length = 0; this._destroyed.next(); this._destroyed.complete(); this._ariaDescriber.removeDescription(nativeElement, this.message, 'tooltip'); this._focusMonitor.stopMonitoring(nativeElement); } /** Shows the tooltip after the delay in ms, defaults to tooltip-delay-show or 0ms if no input */ show(delay = this.showDelay, origin) { if (this.disabled || !this.message || this._isTooltipVisible()) { this._tooltipInstance?._cancelPendingAnimations(); return; } const overlayRef = this._createOverlay(origin); this._detach(); this._portal = this._portal || new ComponentPortal(this._tooltipComponent, this._viewContainerRef); const instance = (this._tooltipInstance = overlayRef.attach(this._portal).instance); instance._triggerElement = this._elementRef.nativeElement; instance._mouseLeaveHideDelay = this._hideDelay; instance .afterHidden() .pipe(takeUntil(this._destroyed)) .subscribe(() => this._detach()); this._setTooltipClass(this._tooltipClass); this._setTooltipContext(this._tooltipContext); this._updateTooltipMessage(); instance.show(delay); } /** Hides the tooltip after the delay in ms, defaults to tooltip-delay-hide or 0ms if no input */ hide(delay = this.hideDelay) { const instance = this._tooltipInstance; if (instance) { if (instance.isVisible()) { instance.hide(delay); } else { instance._cancelPendingAnimations(); this._detach(); } } } /** Shows/hides the tooltip */ toggle(origin) { this._isTooltipVisible() ? this.hide() : this.show(undefined, origin); } /** Returns true if the tooltip is currently visible to the user */ _isTooltipVisible() { return !!this._tooltipInstance && this._tooltipInstance.isVisible(); } /** Create the overlay config and position strategy */ _createOverlay(origin) { if (this._overlayRef) { const existingStrategy = this._overlayRef.getConfig() .positionStrategy; if ((!this.positionAtOrigin || !origin) && existingStrategy._origin instanceof ElementRef) { return this._overlayRef; } this._detach(); } const scrollableAncestors = this._scrollDispatcher.getAncestorScrollContainers(this._elementRef); // Create connected position strategy that listens for scroll events to reposition. const strategy = this._overlay .position() .flexibleConnectedTo(this.positionAtOrigin ? origin || this._elementRef : this._elementRef) .withTransformOriginOn(`.${this._cssClassPrefix}-tooltip`) .withFlexibleDimensions(false) .withViewportMargin(this._viewportMargin) .withScrollableContainers(scrollableAncestors); strategy.positionChanges.pipe(takeUntil(this._destroyed)).subscribe(change => { this._updateCurrentPositionClass(change.connectionPair); if (this._tooltipInstance) { if (change.scrollableViewProperties.isOverlayClipped && this._tooltipInstance.isVisible()) { // After position changes occur and the overlay is clipped by // a parent scrollable then close the tooltip. this._ngZone.run(() => this.hide(0)); } } }); this._overlayRef = this._overlay.create({ direction: this._dir, positionStrategy: strategy, panelClass: `${this._cssClassPrefix}-${PANEL_CLASS}`, scrollStrategy: this._scrollStrategy(), }); this._updatePosition(this._overlayRef); this._overlayRef .detachments() .pipe(takeUntil(this._destroyed)) .subscribe(() => this._detach()); this._overlayRef .outsidePointerEvents() .pipe(takeUntil(this._destroyed)) .subscribe(() => this._tooltipInstance?._handleBodyInteraction()); this._overlayRef .keydownEvents() .pipe(takeUntil(this._destroyed)) .subscribe(event => { if (this._isTooltipVisible() && event.keyCode === ESCAPE && !hasModifierKey(event)) { event.preventDefault(); event.stopPropagation(); this._ngZone.run(() => this.hide(0)); } }); if (this._defaultOptions?.disableTooltipInteractivity) { this._overlayRef.addPanelClass(`${this._cssClassPrefix}-tooltip-panel-non-interactive`); } return this._overlayRef; } /** Detaches the currently-attached tooltip. */ _detach() { if (this._overlayRef && this._overlayRef.hasAttached()) { this._overlayRef.detach(); } this._tooltipInstance = null; } /** Updates the position of the current tooltip. */ _updatePosition(overlayRef) { const position = overlayRef.getConfig().positionStrategy; const origin = this._getOrigin(); const overlay = this._getOverlayPosition(); position.withPositions([ this._addOffset({ ...origin.main, ...overlay.main }), this._addOffset({ ...origin.fallback, ...overlay.fallback }), ]); } /** Adds the configured offset to a position. Used as a hook for child classes. */ _addOffset(position) { const offset = UNBOUNDED_ANCHOR_GAP; const isLtr = !this._dir || this._dir.value == 'ltr'; if (position.originY === 'top') { position.offsetY = -offset; } else if (position.originY === 'bottom') { position.offsetY = offset; } else if (position.originX === 'start') { position.offsetX = isLtr ? -offset : offset; } else if (position.originX === 'end') { position.offsetX = isLtr ? offset : -offset; } return position; } /** * Returns the origin position and a fallback position based on the user's position preference. * The fallback position is the inverse of the origin (e.g. `'below' -> 'above'`). */ _getOrigin() { const isLtr = !this._dir || this._dir.value == 'ltr'; const position = this.position; let originPosition; if (position == 'above' || position == 'below') { originPosition = { originX: 'center', originY: position == 'above' ? 'top' : 'bottom' }; } else if (position == 'before' || (position == 'left' && isLtr) || (position == 'right' && !isLtr)) { originPosition = { originX: 'start', originY: 'center' }; } else if (position == 'after' || (position == 'right' && isLtr) || (position == 'left' && !isLtr)) { originPosition = { originX: 'end', originY: 'center' }; } else { throw getMtxTooltipInvalidPositionError(position); } const { x, y } = this._invertPosition(originPosition.originX, originPosition.originY); return { main: originPosition, fallback: { originX: x, originY: y }, }; } /** Returns the overlay position and a fallback position based on the user's preference */ _getOverlayPosition() { const isLtr = !this._dir || this._dir.value == 'ltr'; const position = this.position; let overlayPosition; if (position == 'above') { overlayPosition = { overlayX: 'center', overlayY: 'bottom' }; } else if (position == 'below') { overlayPosition = { overlayX: 'center', overlayY: 'top' }; } else if (position == 'before' || (position == 'left' && isLtr) || (position == 'right' && !isLtr)) { overlayPosition = { overlayX: 'end', overlayY: 'center' }; } else if (position == 'after' || (position == 'right' && isLtr) || (position == 'left' && !isLtr)) { overlayPosition = { overlayX: 'start', overlayY: 'center' }; } else { throw getMtxTooltipInvalidPositionError(position); } const { x, y } = this._invertPosition(overlayPosition.overlayX, overlayPosition.overlayY); return { main: overlayPosition, fallback: { overlayX: x, overlayY: y }, }; } /** Updates the tooltip message and repositions the overlay according to the new message length */ _updateTooltipMessage() { // Must wait for the message to be painted to the tooltip so that the overlay can properly // calculate the correct positioning based on the size of the text. if (this._tooltipInstance) { this._tooltipInstance.message = this.message; this._tooltipInstance._markForCheck(); afterNextRender(() => { if (this._tooltipInstance) { this._overlayRef.updatePosition(); } }, { injector: this._injector, }); } } /** Updates the tooltip context */ _setTooltipContext(tooltipContext) { if (this._tooltipInstance) { this._tooltipInstance.tooltipContext = tooltipContext; this._tooltipInstance._markForCheck(); } } /** Updates the tooltip class */ _setTooltipClass(tooltipClass) { if (this._tooltipInstance) { this._tooltipInstance.tooltipClass = tooltipClass; this._tooltipInstance._markForCheck(); } } /** Inverts an overlay position. */ _invertPosition(x, y) { if (this.position === 'above' || this.position === 'below') { if (y === 'top') { y = 'bottom'; } else if (y === 'bottom') { y = 'top'; } } else { if (x === 'end') { x = 'start'; } else if (x === 'start') { x = 'end'; } } return { x, y }; } /** Updates the class on the overlay panel based on the current position of the tooltip. */ _updateCurrentPositionClass(connectionPair) { const { overlayY, originX, originY } = connectionPair; let newPosition; // If the overlay is in the middle along the Y axis, // it means that it's either before or after. if (overlayY === 'center') { // Note that since this information is used for styling, we want to // resolve `start` and `end` to their real values, otherwise consumers // would have to remember to do it themselves on each consumption. if (this._dir && this._dir.value === 'rtl') { newPosition = originX === 'end' ? 'left' : 'right'; } else { newPosition = originX === 'start' ? 'left' : 'right'; } } else { newPosition = overlayY === 'bottom' && originY === 'top' ? 'above' : 'below'; } if (newPosition !== this._currentPosition) { const overlayRef = this._overlayRef; if (overlayRef) { const classPrefix = `${this._cssClassPrefix}-${PANEL_CLASS}-`; overlayRef.removePanelClass(classPrefix + this._currentPosition); overlayRef.addPanelClass(classPrefix + newPosition); } this._currentPosition = newPosition; } } /** Binds the pointer events to the tooltip trigger. */ _setupPointerEnterEventsIfNeeded() { // Optimization: Defer hooking up events if there's no message or the tooltip is disabled. if (this._disabled || !this.message || !this._viewInitialized || this._passiveListeners.length) { return; } // The mouse events shouldn't be bound on mobile devices, because they can prevent the // first tap from firing its click event or can cause the tooltip to open for clicks. if (this._platformSupportsMouseEvents()) { this._passiveListeners.push([ 'mouseenter', event => { this._setupPointerExitEventsIfNeeded(); let point = undefined; if (event.x !== undefined && event.y !== undefined) { point = event; } this.show(undefined, point); }, ]); } else if (this.touchGestures !== 'off') { this._disableNativeGesturesIfNecessary(); this._passiveListeners.push([ 'touchstart', event => { const touch = event.targetTouches?.[0]; const origin = touch ? { x: touch.clientX, y: touch.clientY } : undefined; // Note that it's important that we don't `preventDefault` here, // because it can prevent click events from firing on the element. this._setupPointerExitEventsIfNeeded(); clearTimeout(this._touchstartTimeout); const DEFAULT_LONGPRESS_DELAY = 500; this._touchstartTimeout = setTimeout(() => this.show(undefined, origin), this._defaultOptions.touchLongPressShowDelay ?? DEFAULT_LONGPRESS_DELAY); }, ]); } this._addListeners(this._passiveListeners); } _setupPointerExitEventsIfNeeded() { if (this._pointerExitEventsInitialized) { return; } this._pointerExitEventsInitialized = true; const exitListeners = []; if (this._platformSupportsMouseEvents()) { exitListeners.push([ 'mouseleave', event => { const newTarget = event.relatedTarget; if (!newTarget || !this._overlayRef?.overlayElement.contains(newTarget)) { this.hide(); } }, ], ['wheel', event => this._wheelListener(event)]); } else if (this.touchGestures !== 'off') { this._disableNativeGesturesIfNecessary(); const touchendListener = () => { clearTimeout(this._touchstartTimeout); this.hide(this._defaultOptions.touchendHideDelay); }; exitListeners.push(['touchend', touchendListener], ['touchcancel', touchendListener]); } this._addListeners(exitListeners); this._passiveListeners.push(...exitListeners); } _addListeners(listeners) { listeners.forEach(([event, listener]) => { this._elementRef.nativeElement.addEventListener(event, listener, passiveListenerOptions); }); } _platformSupportsMouseEvents() { return !this._platform.IOS && !this._platform.ANDROID; } /** Listener for the `wheel` event on the element. */ _wheelListener(event) { if (this._isTooltipVisible()) { const elementUnderPointer = this._document.elementFromPoint(event.clientX, event.clientY); const element = this._elementRef.nativeElement; // On non-touch devices we depend on the `mouseleave` event to close the tooltip, but it // won't fire if the user scrolls away using the wheel without moving their cursor. We // work around it by finding the element under the user's cursor and closing the tooltip // if it's not the trigger. if (elementUnderPointer !== element && !element.contains(elementUnderPointer)) { this.hide(); } } } /** Disables the native browser gestures, based on how the tooltip has been configured. */ _disableNativeGesturesIfNecessary() { const gestures = this.touchGestures; if (gestures !== 'off') { const element = this._elementRef.nativeElement; const style = element.style; // If gestures are set to `auto`, we don't disable text selection on inputs and // textareas, because it prevents the user from typing into them on iOS Safari. if (gestures === 'on' || (element.nodeName !== 'INPUT' && element.nodeName !== 'TEXTAREA')) { style.userSelect = style.msUserSelect = style.webkitUserSelect = style.MozUserSelect = 'none'; } // If we have `auto` gestures and the element uses native HTML dragging, // we don't set `-webkit-user-drag` because it prevents the native behavior. if (gestures === 'on' || !element.draggable) { style.webkitUserDrag = 'none'; } style.touchAction = 'none'; style.webkitTapHighlightColor = 'transparent'; } } /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.0", ngImport: i0, type: MtxTooltip, deps: [{ token: i1.Overlay }, { token: i0.ElementRef }, { token: i1.ScrollDispatcher }, { token: i0.ViewContainerRef }, { token: i0.NgZone }, { token: i2.Platform }, { token: i3.AriaDescriber }, { token: i3.FocusMonitor }, { token: MTX_TOOLTIP_SCROLL_STRATEGY }, { token: i4.Directionality }, { token: MTX_TOOLTIP_DEFAULT_OPTIONS, optional: true }, { token: DOCUMENT }], target: i0.ɵɵFactoryTarget.Directive }); } /** @nocollapse */ static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "18.2.0", type: MtxTooltip, isStandalone: true, selector: "[mtxTooltip]", inputs: { position: ["mtxTooltipPosition", "position"], positionAtOrigin: ["mtxTooltipPositionAtOrigin", "positionAtOrigin"], disabled: ["mtxTooltipDisabled", "disabled"], showDelay: ["mtxTooltipShowDelay", "showDelay"], hideDelay: ["mtxTooltipHideDelay", "hideDelay"], touchGestures: ["mtxTooltipTouchGestures", "touchGestures"], message: ["mtxTooltip", "message"], tooltipContext: ["mtxTooltipContext", "tooltipContext"], tooltipClass: ["mtxTooltipClass", "tooltipClass"] }, host: { properties: { "class.mtx-mdc-tooltip-disabled": "disabled" }, classAttribute: "mtx-mdc-tooltip-trigger" }, exportAs: ["mtxTooltip"], ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.0", ngImport: i0, type: MtxTooltip, decorators: [{ type: Directive, args: [{ selector: '[mtxTooltip]', exportAs: 'mtxTooltip', host: { 'class': 'mtx-mdc-tooltip-trigger', '[class.mtx-mdc-tooltip-disabled]': 'disabled', }, standalone: true, }] }], ctorParameters: () => [{ type: i1.Overlay }, { type: i0.ElementRef }, { type: i1.ScrollDispatcher }, { type: i0.ViewContainerRef }, { type: i0.NgZone }, { type: i2.Platform }, { type: i3.AriaDescriber }, { type: i3.FocusMonitor }, { type: undefined, decorators: [{ type: Inject, args: [MTX_TOOLTIP_SCROLL_STRATEGY] }] }, { type: i4.Directionality }, { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [MTX_TOOLTIP_DEFAULT_OPTIONS] }] }, { type: undefined, decorators: [{ type: Inject, args: [DOCUMENT] }] }], propDecorators: { position: [{ type: Input, args: ['mtxTooltipPosition'] }], positionAtOrigin: [{ type: Input, args: ['mtxTooltipPositionAtOrigin'] }], disabled: [{ type: Input, args: ['mtxTooltipDisabled'] }], showDelay: [{ type: Input, args: ['mtxTooltipShowDelay'] }], hideDelay: [{ type: Input, args: ['mtxTooltipHideDelay'] }], touchGestures: [{ type: Input, args: ['mtxTooltipTouchGestures'] }], message: [{ type: Input, args: ['mtxTooltip'] }], tooltipContext: [{ type: Input, args: ['mtxTooltipContext'] }], tooltipClass: [{ type: Input, args: ['mtxTooltipClass'] }] } }); /** * Internal component that wraps the tooltip's content. * @docs-private */ export class TooltipComponent { constructor(_changeDetectorRef, _elementRef, animationMode) { this._changeDetectorRef = _changeDetectorRef; this._elementRef = _elementRef; /* Whether the tooltip text overflows to multiple lines */ this._isMultiline = false; /** Whether interactions on the page should close the tooltip */ this._closeOnInteraction = false; /** Whether the tooltip is currently visible. */ this._isVisible = false; /** Subject for notifying that the tooltip has been hidden from the view */ this._onHide = new Subject(); /** Name of the show animation and the class that toggles it. */ this._showAnimation = 'mtx-mdc-tooltip-show'; /** Name of the hide animation and the class that toggles it. */ this._hideAnimation = 'mtx-mdc-tooltip-hide'; this._animationsDisabled = animationMode === 'NoopAnimations'; } /** * Shows the tooltip with an animation originating from the provided origin * @param delay Amount of milliseconds to the delay showing the tooltip. */ show(delay) { // Cancel the delayed hide if it is scheduled if (this._hideTimeoutId != null) { clearTimeout(this._hideTimeoutId); } this._showTimeoutId = setTimeout(() => { this._toggleVisibility(true); this._showTimeoutId = undefined; }, delay); } /** * Begins the animation to hide the tooltip after the provided delay in ms. * @param delay Amount of milliseconds to delay showing the tooltip. */ hide(delay) { // Cancel the delayed show if it is scheduled if (this._showTimeoutId != null) { clearTimeout(this._showTimeoutId); } this._hideTimeoutId = setTimeout(() => { this._toggleVisibility(false); this._hideTimeoutId = undefined; }, delay); } /** Returns an observable that notifies when the tooltip has been hidden from view. */ afterHidden() { return this._onHide; } /** Whether the tooltip is being displayed. */ isVisible() { return this._isVisible; } ngOnDestroy() { this._cancelPendingAnimations(); this._onHide.complete(); this._triggerElement = null; } /** * Interactions on the HTML body should close the tooltip immediately as defined in the * material design spec. * https://material.io/design/components/tooltips.html#behavior */ _handleBodyInteraction() { if (this._closeOnInteraction) { this.hide(0); } } /** * Marks that the tooltip needs to be checked in the next change detection run. * Mainly used for rendering the initial text before positioning a tooltip, which * can be problematic in components with OnPush change detection. */ _markForCheck() { this._changeDetectorRef.markForCheck(); } _handleMouseLeave({ relatedTarget }) { if (!relatedTarget || !this._triggerElement.contains(relatedTarget)) { if (this.isVisible()) { this.hide(this._mouseLeaveHideDelay); } else { this._finalizeAnimation(false); } } } /** * Callback for when the timeout in this.show() gets completed. * This method is only needed by the mdc-tooltip, and so it is only implemented * in the mdc-tooltip, not here. */ _onShow() { this._isMultiline = this._isTooltipMultiline(); this._markForCheck(); } /** Whether the tooltip text has overflown to the next line */ _isTooltipMultiline() { const rect = this._elementRef.nativeElement.getBoundingClientRect(); return rect.height > MIN_HEIGHT && rect.width >= MAX_WIDTH; } /** Event listener dispatched when an animation on the tooltip finishes. */ _handleAnimationEnd({ animationName }) { if (animationName === this._showAnimation || animationName === this._hideAnimation) { this._finalizeAnimation(animationName === this._showAnimation); } } /** Cancels any pending animation sequences. */ _cancelPendingAnimations() { if (this._showTimeoutId != null) { clearTimeout(this._showTimeoutId); } if (this._hideTimeoutId != null) { clearTimeout(this._hideTimeoutId); } this._showTimeoutId = this._hideTimeoutId = undefined; } /** Handles the cleanup after an animation has finished. */ _finalizeAnimation(toVisible) { if (toVisible) { this._closeOnInteraction = true; } else if (!this.isVisible()) { this._onHide.next(); } } /** Toggles the visibility of the tooltip element. */ _toggleVisibility(isVisible) { // We set the classes directly here ourselves so that toggling the tooltip state // isn't bound by change detection. This allows us to hide it even if the // view ref has been detached from the CD tree. const tooltip = this._tooltip.nativeElement; const showClass = this._showAnimation; const hideClass = this._hideAnimation; tooltip.classList.remove(isVisible ? hideClass : showClass); tooltip.classList.add(isVisible ? showClass : hideClass); if (this._isVisible !== isVisible) { this._isVisible = isVisible; this._changeDetectorRef.markForCheck(); } // It's common for internal apps to disable animations using `* { animation: none !important }` // which can break the opening sequence. Try to detect such cases and work around them. if (isVisible && !this._animationsDisabled && typeof getComputedStyle === 'function') { const styles = getComputedStyle(tooltip); // Use `getPropertyValue` to avoid issues with property renaming. if (styles.getPropertyValue('animation-duration') === '0s' || styles.getPropertyValue('animation-name') === 'none') { this._animationsDisabled = true; } } if (isVisible) { this._onShow(); } if (this._animationsDisabled) { tooltip.classList.add('_mtx-animation-noopable'); this._finalizeAnimation(isVisible); } } /** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "18.2.0", ngImport: i0, type: TooltipComponent, deps: [{ token: i0.ChangeDetectorRef }, { token: i0.ElementRef }, { token: ANIMATION_MODULE_TYPE, optional: true }], target: i0.ɵɵFactoryTarget.Component }); } /** @nocollapse */ static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "18.2.0", type: TooltipComponent, isStandalone: true, selector: "mtx-tooltip-component", host: { attributes: { "aria-hidden": "true" }, listeners: { "mouseleave": "_handleMouseLeave($event)" }, properties: { "style.zoom": "isVisible() ? 1 : null" } }, viewQueries: [{ propertyName: "_tooltip", first: true, predicate: ["tooltip"], descendants: true, static: true }], ngImport: i0, template: "<div #tooltip\n class=\"mdc-tooltip mtx-mdc-tooltip\"\n [ngClass]=\"tooltipClass\"\n (animationend)=\"_handleAnimationEnd($event)\"\n [class.mdc-tooltip--multiline]=\"_isMultiline\">\n <div class=\"mtx-mdc-tooltip-surface mdc-tooltip__surface\">\n @if (message | isTemplateRef) {\n <ng-template [ngTemplateOutlet]=\"$any(message)\"\n [ngTemplateOutletContext]=\"{ $implicit: tooltipContext }\"></ng-template>\n } @else {\n {{message}}\n }\n </div>\n</div>\n", styles: [".mtx-mdc-tooltip{position:relative;transform:scale(0);display:inline-flex}.mtx-mdc-tooltip:before{content:\"\";inset:0;z-index:-1;position:absolute}.mtx-mdc-tooltip-panel-below .mtx-mdc-tooltip:before{top:-8px}.mtx-mdc-tooltip-panel-above .mtx-mdc-tooltip:before{bottom:-8px}.mtx-mdc-tooltip-panel-right .mtx-mdc-tooltip:before{left:-8px}.mtx-mdc-tooltip-panel-left .mtx-mdc-tooltip:before{right:-8px}.mtx-mdc-tooltip._mat-animation-noopable{animation:none;transform:scale(1)}.mtx-mdc-tooltip-surface{word-break:normal;overflow-wrap:anywhere;padding:4px 8px;min-width:40px;max-width:200px;min-height:24px;max-height:40vh;box-sizing:border-box;overflow:hidden;text-align:center;will-change:transform,opacity;background-color:var(--mdc-plain-tooltip-container-color);color:var(--mdc-plain-tooltip-supporting-text-color);border-radius:var(--mdc-plain-tooltip-container-shape);font-family:var(--mdc-plain-tooltip-supporting-text-font);font-size:var(--mdc-plain-tooltip-supporting-text-size);font-weight:var(--mdc-plain-tooltip-supporting-text-weight);line-height:var(--mdc-plain-tooltip-supporting-text-line-height);letter-spacing:var(--mdc-plain-tooltip-supporting-text-tracking)}.mtx-mdc-tooltip-surface:before{position:absolute;box-sizing:border-box;width:100%;height:100%;top:0;left:0;border:1px solid transparent;border-radius:inherit;content:\"\";pointer-events:none}.mdc-tooltip--multiline .mtx-mdc-tooltip-surface{text-align:left}[dir=rtl] .mdc-tooltip--multiline .mtx-mdc-tooltip-surface{text-align:right}.mtx-mdc-tooltip-panel.mtx-mdc-tooltip-panel-non-interactive{pointer-events:none}@keyframes mtx-mdc-tooltip-show{0%{opacity:0;transform:scale(.8)}to{opacity:1;transform:scale(1)}}@keyframes mtx-mdc-tooltip-hide{0%{opacity:1;transform:scale(1)}to{opacity:0;transform:scale(.8)}}.mtx-mdc-tooltip-show{animation:mtx-mdc-tooltip-show .15s cubic-bezier(0,0,.2,1) forwards}.mtx-mdc-tooltip-hide{animation:mtx-mdc-tooltip-hide 75ms cubic-bezier(.4,0,1,1) forwards}\n"], dependencies: [{ kind: "directive", type: NgClass, selector: "[ngClass]", inputs: ["class", "ngClass"] }, { kind: "directive", type: NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet", "ngTemplateOutletInjector"] }, { kind: "pipe", type: MtxIsTemplateRefPipe, name: "isTemplateRef" }], changeDetection: i0.ChangeDetectionStrategy.OnPush, encapsulation: i0.ViewEncapsulation.None }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.0", ngImport: i0, type: TooltipComponent, decorators: [{ type: Component, args: [{ selector: 'mtx-tooltip-component', encapsulation: ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush, host: { // Forces the element to have a layout in IE and Edge. This fixes issues where the element // won't be rendered if the animations are disabled or there is no web animations polyfill. '[style.zoom]': 'isVisible() ? 1 : null', '(mouseleave)': '_handleMouseLeave($event)', 'aria-hidden': 'true', }, standalone: true, imports: [NgClass, NgTemplateOutlet, MtxIsTemplateRefPipe], template: "<div #tooltip\n class=\"mdc-tooltip mtx-mdc-tooltip\"\n [ngClass]=\"tooltipClass\"\n (animationend)=\"_handleAnimationEnd($event)\"\n [class.mdc-tooltip--multiline]=\"_isMultiline\">\n <div class=\"mtx-mdc-tooltip-surface mdc-tooltip__surface\">\n @if (message | isTemplateRef) {\n <ng-template [ngTemplateOutlet]=\"$any(message)\"\n [ngTemplateOutletContext]=\"{ $implicit: tooltipContext }\"></ng-template>\n } @else {\n {{message}}\n }\n </div>\n</div>\n", styles: [".mtx-mdc-tooltip{position:relative;transform:scale(0);display:inline-flex}.mtx-mdc-tooltip:before{content:\"\";inset:0;z-index:-1;position:absolute}.mtx-mdc-tooltip-panel-below .mtx-mdc-tooltip:before{top:-8px}.mtx-mdc-tooltip-panel-above .mtx-mdc-tooltip:before{bottom:-8px}.mtx-mdc-tooltip-panel-right .mtx-mdc-tooltip:before{left:-8px}.mtx-mdc-tooltip-panel-left .mtx-mdc-tooltip:before{right:-8px}.mtx-mdc-tooltip._mat-animation-noopable{animation:none;transform:scale(1)}.mtx-mdc-tooltip-surface{word-break:normal;overflow-wrap:anywhere;padding:4px 8px;min-width:40px;max-width:200px;min-height:24px;max-height:40vh;box-sizing:border-box;overflow:hidden;text-align:center;will-change:transform,opacity;background-color:var(--mdc-plain-tooltip-container-color);color:var(--mdc-plain-tooltip-supporting-text-color);border-radius:var(--mdc-plain-tooltip-container-shape);font-family:var(--mdc-plain-tooltip-supporting-text-font);font-size:var(--mdc-plain-tooltip-supporting-text-size);font-weight:var(--mdc-plain-tooltip-supporting-text-weight);line-height:var(--mdc-plain-tooltip-supporting-text-line-height);letter-spacing:var(--mdc-plain-tooltip-supporting-text-tracking)}.mtx-mdc-tooltip-surface:before{position:absolute;box-sizing:border-box;width:100%;height:100%;top:0;left:0;border:1px solid transparent;border-radius:inherit;content:\"\";pointer-events:none}.mdc-tooltip--multiline .mtx-mdc-tooltip-surface{text-align:left}[dir=rtl] .mdc-tooltip--multiline .mtx-mdc-tooltip-surface{text-align:right}.mtx-mdc-tooltip-panel.mtx-mdc-tooltip-panel-non-interactive{pointer-events:none}@keyframes mtx-mdc-tooltip-show{0%{opacity:0;transform:scale(.8)}to{opacity:1;transform:scale(1)}}@keyframes mtx-mdc-tooltip-hide{0%{opacity:1;transform:scale(1)}to{opacity:0;transform:scale(.8)}}.mtx-mdc-tooltip-show{animation:mtx-mdc-tooltip-show .15s cubic-bezier(0,0,.2,1) forwards}.mtx-mdc-tooltip-hide{animation:mtx-mdc-tooltip-hide 75ms cubic-bezier(.4,0,1,1) forwards}\n"] }] }], ctorParameters: () => [{ type: i0.ChangeDetectorRef }, { type: i0.ElementRef }, { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [ANIMATION_MODULE_TYPE] }] }], propDecorators: { _tooltip: [{ type: ViewChild, args: ['tooltip', { // Use a static query here since we interact directly with // the DOM which can happen before `ngAfterViewInit`. static: true, }] }] } }); //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidG9vbHRpcC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uL3Byb2plY3RzL2V4dGVuc2lvbnMvdG9vbHRpcC90b29sdGlwLnRzIiwiLi4vLi4vLi4vLi4vcHJvamVjdHMvZXh0ZW5zaW9ucy90b29sdGlwL3Rvb2x0aXAuaHRtbCJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFFQSxPQUFPLEVBRUwscUJBQXFCLEVBQ3JCLG9CQUFvQixHQUVyQixNQUFNLHVCQUF1QixDQUFDO0FBQy9CLE9BQU8sRUFBRSxNQUFNLEVBQUUsY0FBYyxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFDL0QsT0FBTyxFQU1MLE9BQU8sR0FNUixNQUFNLHNCQUFzQixDQUFDO0FBQzlCLE9BQU8sRUFBRSwrQkFBK0IsRUFBWSxNQUFNLHVCQUF1QixDQUFDO0FBQ2xGLE9BQU8sRUFBRSxlQUFlLEVBQUUsTUFBTSxxQkFBcUIsQ0FBQztBQUN0RCxPQUFPLEVBQUUsUUFBU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