Added type definitions for moveto (#29362)

This commit is contained in:
Rostislav Shermenyov 2018-10-02 18:14:40 +00:00 committed by Wesley Wigham
parent 93761156bb
commit 1038a88306
4 changed files with 159 additions and 0 deletions

96
types/moveto/index.d.ts vendored Normal file
View File

@ -0,0 +1,96 @@
// Type definitions for moveto 1.7
// Project: https://github.com/hsnaydd/moveTo
// Definitions by: Rostislav Shermenyov <https://github.com/shermendev>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare class MoveTo {
/**
* MoveTo Constructor
* @param options Options
* @param easeFunctions Custom ease functions
*/
constructor(
options?: MoveTo.MoveToOptions,
easeFunctions?: MoveTo.MoveToEaseFunctionsObject
);
/**
* Options
*/
options: MoveTo.MoveToOptions;
/**
* Custom ease functions
*/
easeFunctions: MoveTo.MoveToEaseFunctionsObject;
/**
* Start scroll animation from current position to the anchor point
* @param target Target element/position to be scrolled. Target position is the scrolling distance. It must be negative if the upward movement is desired
* @param options Custom options
*/
move(target: HTMLElement | number, options?: MoveTo.MoveToOptions): void;
/**
* Register a dom element as trigger
* @param dom The trigger element for starting to scroll when on click
* @param callback Callback function to be run after the scroll complete. This will overwrite the callback option
* @return Unregister function
*/
registerTrigger(
dom: HTMLElement,
callback?: MoveTo.callbackType
): MoveTo.unregisterTriggerType;
/**
* Adds custom ease function
* @param name Ease function name
* @param fn Ease function
*/
addEaseFunction(name: string, fn: MoveTo.MoveToEaseFunction): void;
}
declare namespace MoveTo {
type unregisterTriggerType = () => void;
/**
* Callback function to be run after the scroll complete.
*/
type callbackType = (target: HTMLElement | number) => void;
interface MoveToOptions {
/**
* The tolerance of the target to be scrolled, can be negative or positive
*/
tolerance?: number;
/**
* Duration of scrolling, in milliseconds
*/
duration?: number;
/**
* Ease function name
*/
easing?: string;
/**
* The function to be run after scrolling complete. Target passes as the first argument
*/
callback?: callbackType;
}
interface MoveToEaseFunctionsObject {
[key: string]: MoveToEaseFunction;
}
/**
* Easing Function
* @param t Current time
* @param b Start value
* @param c Change in value
* @param d Duration
* @return Calculated value
*/
type MoveToEaseFunction = (
t: number,
b: number,
c: number,
d: number
) => number;
}
export = MoveTo;
export as namespace MoveTo;

View File

@ -0,0 +1,36 @@
const options: MoveTo.MoveToOptions = {
tolerance: 70,
duration: 300,
easing: "easeOutQuart",
callback: () => {}
};
const easeFunctions: MoveTo.MoveToEaseFunctionsObject = {
fun1: () => 123,
fun2: t => t,
fun3: (t, b) => t + b,
fun4: (t, b, c) => t + b + c,
fun5: (t, b, c, d) => t + b + c + d
};
const a = new MoveTo();
const b = new MoveTo(options);
const moveToInstance = new MoveTo(options, easeFunctions);
moveToInstance.move(123);
const element = document.getElementById("#anchor");
if (element) {
moveToInstance.move(element, options);
const unregister = moveToInstance.registerTrigger(element, () => {});
unregister();
}
console.log(moveToInstance.options);
console.log(moveToInstance.easeFunctions);
moveToInstance.addEaseFunction("ease-out", (t, b, c, d) => t + b + c + d);

View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"moveto-tests.ts"
]
}

3
types/moveto/tslint.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}