UNPKG

5.35 kBTypeScriptView Raw
1import { ChangeDetectorRef, OnDestroy } from '@angular/core';
2import { Observable } from 'rxjs';
3import { NgbScrollSpyRef } from './scrollspy';
4import * as i0 from "@angular/core";
5export type NgbScrollSpyProcessChanges = (state: {
6 entries: IntersectionObserverEntry[];
7 rootElement: HTMLElement;
8 fragments: Set<Element>;
9 scrollSpy: NgbScrollSpyService;
10 options: NgbScrollSpyOptions;
11}, changeActive: (active: string) => void, context: object) => void;
12/**
13 * Options passed to the `NgbScrollSpyService.start()` method for scrollspy initialization.
14 *
15 * It contains a subset of the `IntersectionObserverInit` options, as well additional optional properties
16 * like `changeDetectorRef` or `fragments`
17 *
18 * @since 15.1.0
19 */
20export interface NgbScrollSpyOptions extends Pick<IntersectionObserverInit, 'root' | 'rootMargin' | 'threshold'> {
21 /**
22 * An optional reference to the change detector, that should be marked for check when active fragment changes.
23 * If it is not provided, the service will try to get it from the DI. Ex. when using the `ngbScrollSpy` directive,
24 * it will mark for check the directive's host component.
25 *
26 * `.markForCheck()` will be called on the change detector when the active fragment changes.
27 */
28 changeDetectorRef?: ChangeDetectorRef;
29 /**
30 * An optional initial fragment to scroll to when the service starts.
31 */
32 initialFragment?: string | HTMLElement;
33 /**
34 * An optional list of fragments to observe when the service starts.
35 * You can alternatively use `.addFragment()` to add fragments.
36 */
37 fragments?: (string | HTMLElement)[];
38 /**
39 * An optional function that is called when the `IntersectionObserver` detects a change.
40 * It is used to determine if currently active fragment should be changed.
41 *
42 * You can override this function to provide your own scrollspy logic.
43 * It provides:
44 * - a scrollspy `state` (observer entries, root element, fragments, scrollSpy instance, etc.)
45 * - a `changeActive` function that should be called with the new active fragment
46 * - a `context` that is persisted between calls
47 */
48 processChanges?: NgbScrollSpyProcessChanges;
49 /**
50 * An optional `IntersectionObserver` root element. If not provided, the document element will be used.
51 */
52 root?: HTMLElement;
53 /**
54 * An optional `IntersectionObserver` margin for the root element.
55 */
56 rootMargin?: string;
57 /**
58 * An optional default scroll behavior to use when using the `.scrollTo()` method.
59 */
60 scrollBehavior?: 'auto' | 'smooth';
61 /**
62 * An optional `IntersectionObserver` threshold.
63 */
64 threshold?: number | number[];
65}
66/**
67 * Scroll options passed to the `.scrollTo()` method.
68 * An extension of the standard `ScrollOptions` interface.
69 *
70 * @since 15.1.0
71 */
72export interface NgbScrollToOptions extends ScrollOptions {
73 /**
74 * Scroll behavior as defined in the `ScrollOptions` interface.
75 */
76 behavior?: 'auto' | 'smooth';
77}
78/**
79 * A scrollspy service that allows tracking of elements scrolling in and out of view.
80 *
81 * It can be instantiated manually, or automatically by the `ngbScrollSpy` directive.
82 *
83 * @since 15.1.0
84 */
85export declare class NgbScrollSpyService implements NgbScrollSpyRef, OnDestroy {
86 private _observer;
87 private _containerElement;
88 private _fragments;
89 private _preRegisteredFragments;
90 private _active$;
91 private _distinctActive$;
92 private _active;
93 private _config;
94 private _document;
95 private _platformId;
96 private _scrollBehavior;
97 private _diChangeDetectorRef;
98 private _changeDetectorRef;
99 private _zone;
100 constructor();
101 /**
102 * Getter for the currently active fragment id. Returns empty string if none.
103 */
104 get active(): string;
105 /**
106 * An observable emitting the currently active fragment. Emits empty string if none.
107 */
108 get active$(): Observable<string>;
109 /**
110 * Starts the scrollspy service and observes specified fragments.
111 *
112 * You can specify a list of options to pass, like the root element, initial fragment, scroll behavior, etc.
113 * See the [`NgbScrollSpyOptions`](#/components/scrollspy/api#NgbScrollSpyOptions) interface for more details.
114 */
115 start(options?: NgbScrollSpyOptions): void;
116 /**
117 * Stops the service and unobserves all fragments.
118 */
119 stop(): void;
120 /**
121 * Scrolls to a fragment, it must be known to the service and contained in the root element.
122 * An id or an element reference can be passed.
123 *
124 * [`NgbScrollToOptions`](#/components/scrollspy/api#NgbScrollToOptions) can be passed.
125 */
126 scrollTo(fragment: string | HTMLElement, options?: NgbScrollToOptions): void;
127 /**
128 * Adds a fragment to observe. It must be contained in the root element.
129 * An id or an element reference can be passed.
130 */
131 observe(fragment: string | HTMLElement): void;
132 /**
133 * Unobserves a fragment.
134 * An id or an element reference can be passed.
135 */
136 unobserve(fragment: string | HTMLElement): void;
137 ngOnDestroy(): void;
138 private _cleanup;
139 static ɵfac: i0.ɵɵFactoryDeclaration<NgbScrollSpyService, never>;
140 static ɵprov: i0.ɵɵInjectableDeclaration<NgbScrollSpyService>;
141}