focus-monitor.d-CvvJeQRc.d.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import * as i0 from '@angular/core';
  2. import { InjectionToken, OnDestroy, ElementRef, AfterViewInit, EventEmitter } from '@angular/core';
  3. import { Observable } from 'rxjs';
  4. type FocusOrigin = 'touch' | 'mouse' | 'keyboard' | 'program' | null;
  5. /**
  6. * Corresponds to the options that can be passed to the native `focus` event.
  7. * via https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus
  8. */
  9. interface FocusOptions {
  10. /** Whether the browser should scroll to the element when it is focused. */
  11. preventScroll?: boolean;
  12. }
  13. /** Detection mode used for attributing the origin of a focus event. */
  14. declare enum FocusMonitorDetectionMode {
  15. /**
  16. * Any mousedown, keydown, or touchstart event that happened in the previous
  17. * tick or the current tick will be used to assign a focus event's origin (to
  18. * either mouse, keyboard, or touch). This is the default option.
  19. */
  20. IMMEDIATE = 0,
  21. /**
  22. * A focus event's origin is always attributed to the last corresponding
  23. * mousedown, keydown, or touchstart event, no matter how long ago it occurred.
  24. */
  25. EVENTUAL = 1
  26. }
  27. /** Injectable service-level options for FocusMonitor. */
  28. interface FocusMonitorOptions {
  29. detectionMode?: FocusMonitorDetectionMode;
  30. }
  31. /** InjectionToken for FocusMonitorOptions. */
  32. declare const FOCUS_MONITOR_DEFAULT_OPTIONS: InjectionToken<FocusMonitorOptions>;
  33. /** Monitors mouse and keyboard events to determine the cause of focus events. */
  34. declare class FocusMonitor implements OnDestroy {
  35. private _ngZone;
  36. private _platform;
  37. private readonly _inputModalityDetector;
  38. /** The focus origin that the next focus event is a result of. */
  39. private _origin;
  40. /** The FocusOrigin of the last focus event tracked by the FocusMonitor. */
  41. private _lastFocusOrigin;
  42. /** Whether the window has just been focused. */
  43. private _windowFocused;
  44. /** The timeout id of the window focus timeout. */
  45. private _windowFocusTimeoutId;
  46. /** The timeout id of the origin clearing timeout. */
  47. private _originTimeoutId;
  48. /**
  49. * Whether the origin was determined via a touch interaction. Necessary as properly attributing
  50. * focus events to touch interactions requires special logic.
  51. */
  52. private _originFromTouchInteraction;
  53. /** Map of elements being monitored to their info. */
  54. private _elementInfo;
  55. /** The number of elements currently being monitored. */
  56. private _monitoredElementCount;
  57. /**
  58. * Keeps track of the root nodes to which we've currently bound a focus/blur handler,
  59. * as well as the number of monitored elements that they contain. We have to treat focus/blur
  60. * handlers differently from the rest of the events, because the browser won't emit events
  61. * to the document when focus moves inside of a shadow root.
  62. */
  63. private _rootNodeFocusListenerCount;
  64. /**
  65. * The specified detection mode, used for attributing the origin of a focus
  66. * event.
  67. */
  68. private readonly _detectionMode;
  69. /**
  70. * Event listener for `focus` events on the window.
  71. * Needs to be an arrow function in order to preserve the context when it gets bound.
  72. */
  73. private _windowFocusListener;
  74. /** Used to reference correct document/window */
  75. protected _document?: Document | null | undefined;
  76. /** Subject for stopping our InputModalityDetector subscription. */
  77. private readonly _stopInputModalityDetector;
  78. constructor(...args: unknown[]);
  79. /**
  80. * Event listener for `focus` and 'blur' events on the document.
  81. * Needs to be an arrow function in order to preserve the context when it gets bound.
  82. */
  83. private _rootNodeFocusAndBlurListener;
  84. /**
  85. * Monitors focus on an element and applies appropriate CSS classes.
  86. * @param element The element to monitor
  87. * @param checkChildren Whether to count the element as focused when its children are focused.
  88. * @returns An observable that emits when the focus state of the element changes.
  89. * When the element is blurred, null will be emitted.
  90. */
  91. monitor(element: HTMLElement, checkChildren?: boolean): Observable<FocusOrigin>;
  92. /**
  93. * Monitors focus on an element and applies appropriate CSS classes.
  94. * @param element The element to monitor
  95. * @param checkChildren Whether to count the element as focused when its children are focused.
  96. * @returns An observable that emits when the focus state of the element changes.
  97. * When the element is blurred, null will be emitted.
  98. */
  99. monitor(element: ElementRef<HTMLElement>, checkChildren?: boolean): Observable<FocusOrigin>;
  100. /**
  101. * Stops monitoring an element and removes all focus classes.
  102. * @param element The element to stop monitoring.
  103. */
  104. stopMonitoring(element: HTMLElement): void;
  105. /**
  106. * Stops monitoring an element and removes all focus classes.
  107. * @param element The element to stop monitoring.
  108. */
  109. stopMonitoring(element: ElementRef<HTMLElement>): void;
  110. /**
  111. * Focuses the element via the specified focus origin.
  112. * @param element Element to focus.
  113. * @param origin Focus origin.
  114. * @param options Options that can be used to configure the focus behavior.
  115. */
  116. focusVia(element: HTMLElement, origin: FocusOrigin, options?: FocusOptions): void;
  117. /**
  118. * Focuses the element via the specified focus origin.
  119. * @param element Element to focus.
  120. * @param origin Focus origin.
  121. * @param options Options that can be used to configure the focus behavior.
  122. */
  123. focusVia(element: ElementRef<HTMLElement>, origin: FocusOrigin, options?: FocusOptions): void;
  124. ngOnDestroy(): void;
  125. /** Access injected document if available or fallback to global document reference */
  126. private _getDocument;
  127. /** Use defaultView of injected document if available or fallback to global window reference */
  128. private _getWindow;
  129. private _getFocusOrigin;
  130. /**
  131. * Returns whether the focus event should be attributed to touch. Recall that in IMMEDIATE mode, a
  132. * touch origin isn't immediately reset at the next tick (see _setOrigin). This means that when we
  133. * handle a focus event following a touch interaction, we need to determine whether (1) the focus
  134. * event was directly caused by the touch interaction or (2) the focus event was caused by a
  135. * subsequent programmatic focus call triggered by the touch interaction.
  136. * @param focusEventTarget The target of the focus event under examination.
  137. */
  138. private _shouldBeAttributedToTouch;
  139. /**
  140. * Sets the focus classes on the element based on the given focus origin.
  141. * @param element The element to update the classes on.
  142. * @param origin The focus origin.
  143. */
  144. private _setClasses;
  145. /**
  146. * Updates the focus origin. If we're using immediate detection mode, we schedule an async
  147. * function to clear the origin at the end of a timeout. The duration of the timeout depends on
  148. * the origin being set.
  149. * @param origin The origin to set.
  150. * @param isFromInteraction Whether we are setting the origin from an interaction event.
  151. */
  152. private _setOrigin;
  153. /**
  154. * Handles focus events on a registered element.
  155. * @param event The focus event.
  156. * @param element The monitored element.
  157. */
  158. private _onFocus;
  159. /**
  160. * Handles blur events on a registered element.
  161. * @param event The blur event.
  162. * @param element The monitored element.
  163. */
  164. _onBlur(event: FocusEvent, element: HTMLElement): void;
  165. private _emitOrigin;
  166. private _registerGlobalListeners;
  167. private _removeGlobalListeners;
  168. /** Updates all the state on an element once its focus origin has changed. */
  169. private _originChanged;
  170. /**
  171. * Collects the `MonitoredElementInfo` of a particular element and
  172. * all of its ancestors that have enabled `checkChildren`.
  173. * @param element Element from which to start the search.
  174. */
  175. private _getClosestElementsInfo;
  176. /**
  177. * Returns whether an interaction is likely to have come from the user clicking the `label` of
  178. * an `input` or `textarea` in order to focus it.
  179. * @param focusEventTarget Target currently receiving focus.
  180. */
  181. private _isLastInteractionFromInputLabel;
  182. static ɵfac: i0.ɵɵFactoryDeclaration<FocusMonitor, never>;
  183. static ɵprov: i0.ɵɵInjectableDeclaration<FocusMonitor>;
  184. }
  185. /**
  186. * Directive that determines how a particular element was focused (via keyboard, mouse, touch, or
  187. * programmatically) and adds corresponding classes to the element.
  188. *
  189. * There are two variants of this directive:
  190. * 1) cdkMonitorElementFocus: does not consider an element to be focused if one of its children is
  191. * focused.
  192. * 2) cdkMonitorSubtreeFocus: considers an element focused if it or any of its children are focused.
  193. */
  194. declare class CdkMonitorFocus implements AfterViewInit, OnDestroy {
  195. private _elementRef;
  196. private _focusMonitor;
  197. private _monitorSubscription;
  198. private _focusOrigin;
  199. readonly cdkFocusChange: EventEmitter<FocusOrigin>;
  200. constructor(...args: unknown[]);
  201. get focusOrigin(): FocusOrigin;
  202. ngAfterViewInit(): void;
  203. ngOnDestroy(): void;
  204. static ɵfac: i0.ɵɵFactoryDeclaration<CdkMonitorFocus, never>;
  205. static ɵdir: i0.ɵɵDirectiveDeclaration<CdkMonitorFocus, "[cdkMonitorElementFocus], [cdkMonitorSubtreeFocus]", ["cdkMonitorFocus"], {}, { "cdkFocusChange": "cdkFocusChange"; }, never, never, true, never>;
  206. }
  207. export { CdkMonitorFocus as C, FocusMonitorDetectionMode as b, FOCUS_MONITOR_DEFAULT_OPTIONS as d, FocusMonitor as e };
  208. export type { FocusOrigin as F, FocusOptions as a, FocusMonitorOptions as c };