Skip to content

Commit 7561c55

Browse files
Migrate to Typescript (#350)
* Migrate to Typescript * Add types and adjust reame Co-authored-by: alpanayotov <[email protected]>
1 parent d788d63 commit 7561c55

17 files changed

+1070
-2003
lines changed

.eslintrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"browser": true
5+
},
6+
"extends": ["prettier", "plugin:@typescript-eslint/recommended"],
7+
"parser": "@typescript-eslint/parser",
8+
"parserOptions": {
9+
"project": "tsconfig.json",
10+
"sourceType": "module"
11+
},
12+
"plugins": ["@typescript-eslint"],
13+
"ignorePatterns": ["*.js", "*.mjs"],
14+
"rules": {}
15+
}

.github/workflows/build.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3-
41
name: Build
52

6-
on:
7-
push:
8-
branches: [master]
9-
pull_request:
10-
branches: [master]
3+
on: [push, pull_request]
114

125
jobs:
136
build:
147
runs-on: ubuntu-latest
158

169
strategy:
1710
matrix:
18-
node-version: [12.x, 14.x, 16.x]
11+
node-version: [18.x]
1912

2013
steps:
2114
- uses: actions/checkout@v3
@@ -24,4 +17,5 @@ jobs:
2417
with:
2518
node-version: ${{ matrix.node-version }}
2619
- run: yarn
20+
- run: yarn lint
2721
- run: yarn build

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ node_modules/
1616
.Trashes
1717
ehthumbs.db
1818
Thumbs.db
19+
20+
dist

.npmignore

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ yarn-debug.log*
66
yarn-error.log*
77

88
# Dependency directory
9-
node_modules/
9+
node_modules
1010

1111
# Misc
1212
.DS_Store
@@ -17,27 +17,27 @@ node_modules/
1717
ehthumbs.db
1818
Thumbs.db
1919

20-
# EditorConfig
20+
# Config folders and files
21+
.github
22+
_config.yml
23+
_.config.yml
24+
.codebeatsettings
2125
.editorconfig
22-
23-
# Git
24-
.gitignore
26+
.eslintrc
2527
.gitattributes
26-
27-
# CI
28-
.travis.yml
29-
30-
# lock files
31-
yarn.lock
32-
33-
# Prettier
34-
.prettierrc
28+
.gitignore
29+
.nvmrc
3530
.prettierignore
36-
37-
# Config
38-
.babelrc
39-
.github
31+
.prettierrc
32+
.stylelintignore
33+
.stylelintrc
34+
.travis.yml
4035
.whitesource
4136
renovate.json
42-
rollup.config.js
43-
webpack.config.js
37+
tsconfig.json
38+
tslint.json
39+
yarn.lock
40+
41+
!dist
42+
demo
43+
rollup.config.mjs

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ node_js:
55
install:
66
- yarn
77
script:
8+
- yarn lint
89
- yarn build

.whitesource

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"checkRunSettings": {
3-
"vulnerableCheckRunConclusionLevel": "success"
4-
},
5-
"issueSettings": {
6-
"minSeverityLevel": "LOW"
7-
}
2+
"generalSettings": {
3+
"shouldScanRepo": true
4+
},
5+
"checkRunSettings": {
6+
"vulnerableCheckRunConclusionLevel": "success"
7+
}
88
}

README.md

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
[![GitHub release](https://img.shields.io/github/release/three11/animate-top-offset.svg)](https://github.com/three11/animate-top-offset/releases/latest)
22
[![GitHub issues](https://img.shields.io/github/issues/three11/animate-top-offset.svg)](https://github.com/three11/animate-top-offset/issues)
33
[![GitHub last commit](https://img.shields.io/github/last-commit/three11/animate-top-offset.svg)](https://github.com/three11/animate-top-offset/commits/master)
4-
[![Github file size](https://img.shields.io/github/size/three11/animate-top-offset/dist/animate-top-offset.min.js.svg)](https://github.com/three11/animate-top-offset/)
54
[![Build Status](https://travis-ci.org/three11/animate-top-offset.svg?branch=master)](https://travis-ci.org/three11/animate-top-offset)
65
[![npm](https://img.shields.io/npm/dt/@three11/animate-top-offset.svg)](https://www.npmjs.com/package/@three11/animate-top-offset)
76
[![npm](https://img.shields.io/npm/v/@three11/animate-top-offset.svg)](https://www.npmjs.com/package/@three11/animate-top-offset)
87
[![Analytics](https://ga-beacon.appspot.com/UA-83446952-1/github.com/three11/animate-top-offset/README.md)](https://github.com/three11/animate-top-offset/)
98

109
# Animate Top Offset
1110

12-
Vanilla JS animated scroll to a given offset.
11+
> Scroll a container to a specific Y offset
1312
1413
## Install
1514

@@ -23,35 +22,19 @@ or
2322
yarn add @three11/animate-top-offset
2423
```
2524

26-
or
27-
28-
Just download this repository and link the files located in dist folder:
29-
30-
```html
31-
<script src="path-to-animate-top-offset/dist/animate-top-offset.min.js"></script>
32-
```
33-
34-
or
35-
36-
Include it from Unpkg CDN
37-
38-
```html
39-
<script src="//unpkg.com/@three11/animate-top-offset/dist/animate-top-offset.min.js"></script>
40-
```
41-
4225
## Usage
4326

4427
First, `import` the module:
4528

46-
```javascript
29+
```ts
4730
import animateTopOffset from '@three11/animate-top-offset';
4831
```
4932

5033
Then use the module:
5134

52-
#### With one element
35+
### With one element
5336

54-
```javascript
37+
```ts
5538
const button = document.getElementById('button');
5639

5740
button.addEventListener('click', event => {
@@ -64,9 +47,9 @@ button.addEventListener('click', event => {
6447
});
6548
```
6649

67-
#### With many elements
50+
### With many elements
6851

69-
```javascript
52+
```ts
7053
const buttons = document.querySelectorAll('.js-scroll-to');
7154

7255
// Instead of Array.from you can spread the buttons: [...buttons]
@@ -77,14 +60,14 @@ Array.from(buttons).forEach(button => {
7760
const href = event.target.getAttribute('href');
7861
const offset = doc.querySelector(href).offsetTop;
7962

80-
animateTopOffset(offset);
63+
animateTopOffset({ offset });
8164
});
8265
});
8366
```
8467

8568
**The examples above assume that you have a modern ES6 setup installed and configured (Webpack, Babel, etc). If not you can always fallback to ES5:**
8669

87-
```javascript
70+
```ts
8871
const buttons = document.querySelectorAll('.js-scroll-to');
8972

9073
[].forEach.call(buttons, function (button) {
@@ -101,31 +84,44 @@ const buttons = document.querySelectorAll('.js-scroll-to');
10184

10285
## Arguments
10386

104-
The function accepts four arguments:
87+
The function accepts the following options:
10588

106-
- `offset`
107-
- `container`
108-
- `speed`
109-
- `easing`
89+
| Name | Type | Required | Description | Default value |
90+
| ----------- | ---------------------------------------------------- | -------- | ------------------------------ | ------------- |
91+
| `offset` | number | false | Offset to scroll to | 0 |
92+
| `container` | `HTMLElement` \| `Window` | false | The element to scroll | window |
93+
| `speed` | number | false | Speed of the scroll animation | 200 |
94+
| `easing` | 'easeOutSine' \| 'easeInOutSine' \| 'easeInOutQuint' | false | Easing of the scroll animation | 'easeOutSine' |
95+
| `easings` | Record<string, (pos: number) => number> | false | List of easing equations | See below |
11096

111-
```javascript
112-
animateTopOffset(0, window, 2000, 'easeOutSine');
97+
```ts
98+
animateTopOffset({ offset: 0, container: window, speed: 2000, easing: 'easeOutSine', easings: easingEquations });
11399
```
114100

115-
Default values are:
101+
**Calling the function with the default values (`animateTopOffset()`) will scroll the window back to top.**
102+
103+
## Easings
116104

117-
- `offset` = 0
118-
- `container` = `window`
119-
- `speed` = 2000
120-
- `easing` = `'easeOutSine'`
105+
`animateTopOffset` provides the ability to specify a custom list of easing functions.
106+
The default one contains three easings: 'easeOutSine', 'easeInOutSine' and 'easeInOutQuint'.
121107

122-
**Calling the function with the default values (`animateTopOffset()`) will scroll the window back to top.**
108+
The shape of the list is the following:
123109

124-
There are three built-in easing functions:
110+
```ts
111+
const easingEquations: Record<string, (pos: number) => number> = {
112+
easeOutSine: (pos: number) => Math.sin(pos * (Math.PI / 2)),
113+
easeInOutSine: (pos: number) => -0.5 * (Math.cos(Math.PI * pos) - 1),
114+
easeInOutQuint: (pos: number) => {
115+
if ((pos /= 0.5) < 1) {
116+
return 0.5 * Math.pow(pos, 5);
117+
}
118+
119+
return 0.5 * (Math.pow(pos - 2, 5) + 2);
120+
}
121+
};
122+
```
125123

126-
- `'easeOutSine'`
127-
- `'easeInOutSine'`
128-
- `'easeInOutQuint'`
124+
**The easing argument should match one of the keys of the `easings` argument.`**
129125

130126
## Demo
131127

_.config.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins:
2+
- jekyll-relative-links
3+
relative_links:
4+
enabled: true
5+
collections: true
6+
include:
7+
- CONTRIBUTING.md
8+
- README.md
9+
- LICENSE.md
10+
- COPYING.md
11+
- CODE_OF_CONDUCT.md
12+
- CONTRIBUTING.md
13+
- ISSUE_TEMPLATE.md
14+
- PULL_REQUEST_TEMPLATE.md

_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
theme: jekyll-theme-slate
1+
theme: jekyll-theme-slate

dist/animate-top-offset.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)