ngx-scroll-to-vk
Version:
A simple Angular 4+ plugin enabling you to smooth scroll to any element on your page and enhance scroll-based features in your app.
246 lines (238 loc) • 10.2 kB
TypeScript
import * as i0 from '@angular/core';
import { ElementRef, AfterViewInit, Renderer2, ModuleWithProviders } from '@angular/core';
import { Observable } from 'rxjs';
/** Named Easing Function */
type ScrollToAnimationEasing = 'easeInQuad' | 'easeOutQuad' | 'easeInOutQuad' | 'easeInCubic' | 'easeOutCubic' | 'easeInOutCubic' | 'easeInQuart' | 'easeOutQuart' | 'easeInOutQuart' | 'easeInQuint' | 'easeOutQuint' | 'easeInOutQuint' | 'easeOutElastic';
/**
* The Easing Function controlling the Easing
* Animation over Time.
*/
type ScrollToAnimationEasingFunction = (time: number) => number;
/** The collection of named Easing Functions */
interface ScrollToAnimationEasingCollection {
[key: string]: ScrollToAnimationEasingFunction;
}
/** Supported Events to trigger the scroll animation */
type ScrollToEvent = 'click' | 'mouseenter' | 'mouseover' | 'mousedown' | 'mouseup' | 'dblclick' | 'contextmenu' | 'wheel' | 'mouseleave' | 'mouseout';
/** The target of the Scroll Animation */
type ScrollToTarget = string | number | ElementRef | HTMLElement;
/**
* The container on which the Scroll Animation
* will happen.
*/
type ScrollToContainer = string | number | ElementRef | HTMLElement;
/**
* The Listener Target is responsive for listening
* to events that could interrupt the Scroll Animation.
*/
type ScrollToListenerTarget = HTMLElement | Window;
/**
* A mapped list of breakpoints with accompanying
* values for the offset of that specific breakpoint.
*/
type ScrollToOffsetMap = Map<number, number>;
interface ScrollToConfigOptionsBase {
/** The Container to scroll */
container?: ScrollToContainer;
/** Duration of the Scroll Animation */
duration?: number;
/** The named Easing Function to use */
easing?: ScrollToAnimationEasing;
/** A mapped list of offsets */
offsetMap?: ScrollToOffsetMap;
}
/** Configuration Options Target */
interface ScrollToConfigOptionsTarget extends ScrollToConfigOptionsBase {
/** The target to scroll to */
target: ScrollToTarget;
/** The offset from the top of the Element in pixels */
offset?: number;
}
/** Configuration Options Offset Only */
interface ScrollToConfigOptionsOffsetOnly extends ScrollToConfigOptionsBase {
/** The offset from the top of the Element in pixels */
offset: number;
}
/** The Configuration Object */
type ScrollToConfigOptions = ScrollToConfigOptionsTarget | ScrollToConfigOptionsOffsetOnly;
/** The Default Configuration Object */
interface ScrollToDefaultConfigOptions extends ScrollToConfigOptionsTarget {
action: ScrollToEvent;
}
/**
* The Scroll To Service handles starting, interrupting
* and ending the actual Scroll Animation. It provides
* some utilities to find the proper HTML Element on a
* given page to setup Event Listeners and calculate
* distances for the Animation.
*/
declare class ScrollToService {
private document;
private platformId;
/**
* The animation that provides the scrolling
* to happen smoothly over time. Defining it here
* allows for usage of e.g. `start` and `stop`
* methods within this Angular Service.
*/
private animation;
/**
* Interruptive Events allow to scrolling animation
* to be interrupted before it is finished. The list
* of Interruptive Events represents those.
*/
private interruptiveEvents;
/**
* Construct and setup required paratemeters.
*
* @param document A Reference to the Document
* @param platformId Angular Platform ID
*/
constructor(document: any, platformId: any);
/**
* Target an Element to scroll to. Notice that the `TimeOut` decorator
* ensures the executing to take place in the next Angular lifecycle.
* This allows for scrolling to elements that are e.g. initially hidden
* by means of `*ngIf`, but ought to be scrolled to eventually.
*
* @todo type 'any' in Observable should become custom type like 'ScrollToEvent' (base class), see issue comment:
* - https://github.com/nicky-lenaers/ngx-scroll-to/issues/10#issuecomment-317198481
*
* @param options Configuration Object
* @returns Observable
*/
scrollTo(options: ScrollToConfigOptions): Observable<any>;
/**
* Start a new Animation.
*
* @todo Emit proper events from subscription
*
* @param options Configuration Object
* @returns Observable
*/
private start;
/**
* Subscribe to the events emitted from the Scrolling
* Animation. Events might be used for e.g. unsubscribing
* once finished.
*
* @param animation$ The Animation Observable
* @param listenerTarget The Listener Target for events
* @param onInterrupt The handler for Interruptive Events
* @returns Void
*/
private subscribeToAnimation;
/**
* Get the container HTML Element in which
* the scrolling should happen.
*
* @param options The Merged Configuration Object
* @param targetNode the targeted HTMLElement
*/
private getContainer;
/**
* Add listeners for the Animation Interruptive Events
* to the Listener Target.
*
* @param events List of events to listen to
* @param listenerTarget Target to attach the listener on
* @param handler Handler for when the listener fires
* @returns Void
*/
private addInterruptiveEventListeners;
/**
* Feature-detect support for passive event listeners.
*
* @returns Whether or not passive event listeners are supported
*/
private supportPassive;
/**
* Remove listeners for the Animation Interrupt Event from
* the Listener Target. Specifying the correct handler prevents
* memory leaks and makes the allocated memory available for
* Garbage Collection.
*
* @param events List of Interruptive Events to remove
* @param listenerTarget Target to attach the listener on
* @param handler Handler for when the listener fires
* @returns Void
*/
private removeInterruptiveEventListeners;
/**
* Find the first scrollable parent Node of a given
* Element. The DOM Tree gets searched upwards
* to find this first scrollable parent. Parents might
* be ignored by CSS styles applied to the HTML Element.
*
* @param nativeElement The Element to search the DOM Tree upwards from
* @returns The first scrollable parent HTML Element
*/
private getFirstScrollableParent;
/**
* Get the Target Node to scroll to.
*
* @param id The given ID of the node, either a string or
* an element reference
* @param allowBodyTag Indicate whether or not the Document Body is
* considered a valid Target Node
* @returns The Target Node to scroll to
*/
private getNode;
/**
* Retrieve the Listener target. This Listener Target is used
* to attach Event Listeners on. In case of the target being
* the Document Body, we need the actual `window` to listen
* for events.
*
* @param container The HTML Container element
* @returns The Listener Target to attach events on
*/
private getListenerTarget;
/**
* Test if a given HTML Element is the Document Body.
*
* @param element The given HTML Element
* @returns Whether or not the Element is the
* Document Body Element
*/
private isDocumentBody;
static ɵfac: i0.ɵɵFactoryDeclaration<ScrollToService, never>;
static ɵprov: i0.ɵɵInjectableDeclaration<ScrollToService>;
}
declare class ScrollToDirective implements AfterViewInit {
private elementRef;
private scrollToService;
private renderer2;
ngxScrollTo: ScrollToTarget;
ngxScrollToEvent: ScrollToEvent;
ngxScrollToDuration: number;
ngxScrollToEasing: ScrollToAnimationEasing;
ngxScrollToOffset: number;
ngxScrollToOffsetMap: ScrollToOffsetMap;
private options;
constructor(elementRef: ElementRef, scrollToService: ScrollToService, renderer2: Renderer2);
/**
* Angular Lifecycle Hook - After View Init
*
* @todo Implement Subscription for Events
*
* @returns void
*/
ngAfterViewInit(): void;
static ɵfac: i0.ɵɵFactoryDeclaration<ScrollToDirective, never>;
static ɵdir: i0.ɵɵDirectiveDeclaration<ScrollToDirective, "[ngxScrollTo]", never, { "ngxScrollTo": { "alias": "ngxScrollTo"; "required": false; }; "ngxScrollToEvent": { "alias": "ngxScrollToEvent"; "required": false; }; "ngxScrollToDuration": { "alias": "ngxScrollToDuration"; "required": false; }; "ngxScrollToEasing": { "alias": "ngxScrollToEasing"; "required": false; }; "ngxScrollToOffset": { "alias": "ngxScrollToOffset"; "required": false; }; "ngxScrollToOffsetMap": { "alias": "ngxScrollToOffsetMap"; "required": false; }; }, {}, never, never, true, never>;
}
/** Scroll To Module */
declare class ScrollToModule {
/**
* Guaranteed singletons for provided Services across App.
*
* @return An Angular Module with Providers
*/
static forRoot(): ModuleWithProviders<ScrollToModule>;
static ɵfac: i0.ɵɵFactoryDeclaration<ScrollToModule, never>;
static ɵmod: i0.ɵɵNgModuleDeclaration<ScrollToModule, never, [typeof ScrollToDirective], [typeof ScrollToDirective]>;
static ɵinj: i0.ɵɵInjectorDeclaration<ScrollToModule>;
}
export { ScrollToDirective, ScrollToModule, ScrollToService };
export type { ScrollToAnimationEasing, ScrollToAnimationEasingCollection, ScrollToAnimationEasingFunction, ScrollToConfigOptions, ScrollToConfigOptionsBase, ScrollToConfigOptionsOffsetOnly, ScrollToConfigOptionsTarget, ScrollToContainer, ScrollToDefaultConfigOptions, ScrollToListenerTarget, ScrollToOffsetMap, ScrollToTarget };