Skip to content

Commit 26680eb

Browse files
authored
Merge pull request #7 from bartholomej/svelte5
Svelte 5 compat
2 parents f1203e3 + 3ed383e commit 26680eb

File tree

8 files changed

+1590
-905
lines changed

8 files changed

+1590
-905
lines changed

.github/workflows/publish.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ jobs:
1010
runs-on: ubuntu-latest
1111

1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414
- name: Use Node.js
15-
uses: actions/setup-node@v3
15+
uses: actions/setup-node@v4
1616
with:
17-
node-version: 18.x
17+
node-version: 22
1818
registry-url: 'https://registry.npmjs.org'
1919

2020
- name: Cache node modules
21-
uses: actions/cache@v3
21+
uses: actions/cache@v4
2222
with:
2323
path: node_modules
2424
key: ${{ runner.OS }}-build-${{ hashFiles('**/yarn.lock') }}

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ jobs:
1616
runs-on: ubuntu-latest
1717

1818
steps:
19-
- uses: actions/checkout@v3
19+
- uses: actions/checkout@v4
2020
- name: Use Node.js
21-
uses: actions/setup-node@v3
21+
uses: actions/setup-node@v4
2222
with:
23-
node-version: 18.x
23+
node-version: 22
2424

2525
- name: Cache node modules
26-
uses: actions/cache@v3
26+
uses: actions/cache@v4
2727
with:
2828
path: node_modules
2929
key: ${{ runner.OS }}-build-${{ hashFiles('**/yarn.lock') }}

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
18
1+
22

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"printWidth": 100,
2+
"printWidth": 150,
33
"singleQuote": true,
44
"useTabs": false,
55
"tabWidth": 2,

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,23 @@
2525
"author": "Bartholomej <bart@bartweb.cz>",
2626
"license": "MIT",
2727
"dependencies": {
28-
"svelte": "3.55.1"
28+
"svelte": "5.16.1"
2929
},
3030
"peerDependencies": {
31-
"svelte": "^3.0.0 || ^4.0.0"
31+
"svelte": "^4.0.0 || ^5.0.0"
3232
},
3333
"type": "module",
3434
"devDependencies": {
35-
"@typescript-eslint/eslint-plugin": "^5.54.0",
36-
"@typescript-eslint/parser": "^5.54.0",
37-
"eslint": "^8.35.0",
35+
"@typescript-eslint/eslint-plugin": "^8.19.0",
36+
"@typescript-eslint/parser": "^8.19.0",
37+
"eslint": "^9.17.0",
3838
"eslint-config-airbnb-base": "^15.0.0",
39-
"eslint-plugin-import": "^2.27.5",
40-
"husky": "^8.0.3",
41-
"npm-prepare-dist": "^0.4.1",
42-
"prettier": "^2.8.4",
43-
"pretty-quick": "^3.1.3",
44-
"rimraf": "^4.3.0",
45-
"typescript": "^4.9.5"
39+
"eslint-plugin-import": "^2.31.0",
40+
"husky": "^9.1.7",
41+
"npm-prepare-dist": "^0.5.0",
42+
"prettier": "^3.4.2",
43+
"pretty-quick": "^4.0.0",
44+
"rimraf": "^6.0.1",
45+
"typescript": "^5.7.2"
4646
}
47-
}
47+
}

src/helpers/helper.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ export const $ = (selector: HTMLElement): HTMLElement => {
77
return selector;
88
};
99

10-
export const extend = (...args: ScrollToElementOptions[]): ScrollToElementOptions =>
11-
Object.assign({}, ...args);
10+
export const extend = (...args: ScrollToElementOptions[]): ScrollToElementOptions => Object.assign({}, ...args);
1211

1312
export const cumulativeOffset = (element: HTMLElement | any): ScrollToElementPosition => {
1413
let top = 0;
@@ -26,8 +25,7 @@ export const cumulativeOffset = (element: HTMLElement | any): ScrollToElementPos
2625
};
2726
};
2827

29-
export const directScroll = (element: HTMLElement | any): boolean =>
30-
element && element !== document && element !== document.body;
28+
export const directScroll = (element: HTMLElement | any): boolean => element && element !== document && element !== document.body;
3129

3230
export const scrollTop = (element: HTMLElement | any, value?: number): number => {
3331
const inSetter = value !== undefined;
@@ -48,3 +46,26 @@ export const scrollLeft = (element: HTMLElement, value?: number): number => {
4846
? (document.documentElement.scrollLeft = document.body.scrollLeft = value)
4947
: window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
5048
};
49+
50+
export const noop = () => {};
51+
52+
// Custom animation loop
53+
export const startAnimationLoop = (callback: (time: number) => boolean): (() => void) => {
54+
let running = true;
55+
56+
const loop = (time: number) => {
57+
if (!running) return;
58+
if (callback(time)) {
59+
requestAnimationFrame(loop);
60+
}
61+
};
62+
63+
requestAnimationFrame(loop);
64+
65+
// Return function to stop the loop
66+
return () => {
67+
running = false;
68+
};
69+
};
70+
71+
export const now = () => performance.now();

src/services/service.ts

Lines changed: 59 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
// animateScroll.ts
12
import { cubicInOut } from 'svelte/easing';
2-
import { loop, noop, now } from 'svelte/internal';
3-
import { ScrollToElementOptions } from '../global.interface';
4-
import { $, cumulativeOffset, extend, scrollLeft, scrollTop } from '../helpers/helper.js';
3+
import { $, cumulativeOffset, extend, noop, scrollLeft, scrollTop, startAnimationLoop } from '../helpers/helper.js';
4+
import { ScrollToElementOptions } from './../global.interface.js';
55

6+
// Default options
67
const defaultOptions: ScrollToElementOptions = {
78
container: 'body',
89
duration: 500,
@@ -16,26 +17,14 @@ const defaultOptions: ScrollToElementOptions = {
1617
scrollY: true
1718
};
1819

20+
// Scroll to internal implementation
1921
const scrollToInternal = (options: ScrollToElementOptions): (() => void) => {
20-
const {
21-
duration,
22-
delay,
23-
easing,
24-
x = 0,
25-
y = 0,
26-
scrollX,
27-
scrollY,
28-
onStart,
29-
onDone,
30-
container,
31-
onAborting,
32-
element
33-
} = options;
22+
const { duration, delay, easing, x = 0, y = 0, scrollX, scrollY, onStart, onDone, onAborting, container, element } = options;
3423

3524
let { offset } = options;
3625

3726
if (typeof offset === 'function') {
38-
offset = offset() as Function;
27+
offset = offset() as number;
3928
}
4029

4130
const cumulativeOffsetContainer = cumulativeOffset(container);
@@ -52,32 +41,32 @@ const scrollToInternal = (options: ScrollToElementOptions): (() => void) => {
5241

5342
let scrolling = true;
5443
let started = false;
55-
const startTime = now() + delay;
44+
const startTime = performance.now() + delay;
5645
const endTime = startTime + duration;
5746

58-
function scrollToTopLeft(element: HTMLElement, top: number, left: number): void {
59-
if (scrollX) scrollLeft(element, left);
60-
if (scrollY) scrollTop(element, top);
61-
}
47+
const scrollToTopLeft = (el: HTMLElement, top: number, left: number): void => {
48+
if (scrollX) scrollLeft(el, left);
49+
if (scrollY) scrollTop(el, top);
50+
};
6251

63-
function start(delayStart: number | boolean): void {
64-
if (!delayStart) {
52+
const start = () => {
53+
if (!started) {
6554
started = true;
6655
onStart(element, { x, y });
6756
}
68-
}
57+
};
6958

70-
function tick(progress: number): void {
59+
const tick = (progress: number) => {
7160
scrollToTopLeft(container, initialY + diffY * progress, initialX + diffX * progress);
72-
}
61+
};
7362

74-
function stop(): void {
63+
const stop = () => {
7564
scrolling = false;
76-
}
65+
};
7766

78-
loop((now): boolean => {
67+
startAnimationLoop((now) => {
7968
if (!started && now >= startTime) {
80-
start(false);
69+
start();
8170
}
8271

8372
if (started && now >= endTime) {
@@ -90,22 +79,22 @@ const scrollToInternal = (options: ScrollToElementOptions): (() => void) => {
9079
onAborting(element, { x, y });
9180
return false;
9281
}
82+
9383
if (started) {
94-
const p = now - startTime;
95-
const t = 0 + 1 * easing(p / duration);
84+
const p = (now - startTime) / duration;
85+
const t = easing(p);
9686
tick(t);
9787
}
9888

99-
return true;
89+
return scrolling;
10090
});
10191

102-
start(delay);
103-
104-
tick(0);
92+
tick(0); // Initial tick
10593

10694
return stop;
10795
};
10896

97+
// Helper functions
10998
const proceedOptions = (options: ScrollToElementOptions): ScrollToElementOptions => {
11099
const opts = extend({}, defaultOptions, options);
111100
opts.container = $(opts.container);
@@ -115,76 +104,71 @@ const proceedOptions = (options: ScrollToElementOptions): ScrollToElementOptions
115104

116105
const scrollContainerHeight = (containerElement: HTMLElement | Document): number => {
117106
if (containerElement && containerElement !== document && containerElement !== document.body) {
118-
return (
119-
(containerElement as HTMLElement).scrollHeight -
120-
(containerElement as HTMLElement).offsetHeight
121-
);
107+
return (containerElement as HTMLElement).scrollHeight - (containerElement as HTMLElement).offsetHeight;
122108
}
123109
const { body } = document;
124110
const html = document.documentElement;
125111

126-
return Math.max(
127-
body.scrollHeight,
128-
body.offsetHeight,
129-
html.clientHeight,
130-
html.scrollHeight,
131-
html.offsetHeight
132-
);
112+
return Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
133113
};
134114

135115
const setGlobalOptions = (options: ScrollToElementOptions): void => {
136116
extend(defaultOptions, options || {});
137117
};
138118

139-
const scrollTo = (options: ScrollToElementOptions): (() => void) =>
140-
scrollToInternal(proceedOptions(options));
119+
// Scroll functions
120+
const scrollTo = (options: ScrollToElementOptions): (() => void) => scrollToInternal(proceedOptions(options));
141121

142122
const scrollToBottom = (options?: ScrollToElementOptions): (() => void) => {
143-
options = proceedOptions(options);
123+
const opts = proceedOptions(options);
144124

145125
return scrollToInternal(
146-
extend(options, {
126+
extend(opts, {
147127
element: null,
148-
y: scrollContainerHeight(options.container)
128+
y: scrollContainerHeight(opts.container)
149129
})
150130
);
151131
};
152132

153133
const scrollToTop = (options?: ScrollToElementOptions): (() => void) => {
154-
options = proceedOptions(options);
134+
const opts = proceedOptions(options);
155135

156136
return scrollToInternal(
157-
extend(options, {
137+
extend(opts, {
158138
element: null,
159139
y: 0
160140
})
161141
);
162142
};
163143

164-
const makeScrollToAction =
165-
(scrollToFunc: Function) => (node: Node, options: ScrollToElementOptions) => {
166-
let current = options;
167-
const handle: EventListenerOrEventListenerObject = (e: Event) => {
168-
e.preventDefault();
169-
scrollToFunc(typeof current === 'string' ? { element: current } : current);
170-
};
171-
node.addEventListener('click', handle);
172-
node.addEventListener('touchstart', handle);
173-
return {
174-
update(options: ScrollToElementOptions): void {
175-
current = options;
176-
},
177-
destroy(): void {
178-
node.removeEventListener('click', handle);
179-
node.removeEventListener('touchstart', handle);
180-
}
181-
};
144+
const makeScrollToAction = (scrollToFunc: Function) => (node: Node, options: ScrollToElementOptions) => {
145+
let current = options;
146+
const handle: EventListenerOrEventListenerObject = (e: Event) => {
147+
e.preventDefault();
148+
scrollToFunc(typeof current === 'string' ? { element: current } : current);
182149
};
150+
node.addEventListener('click', handle);
151+
node.addEventListener('touchstart', handle);
152+
return {
153+
update(options: ScrollToElementOptions): void {
154+
current = options;
155+
},
156+
destroy(): void {
157+
node.removeEventListener('click', handle);
158+
node.removeEventListener('touchstart', handle);
159+
}
160+
};
161+
};
183162

184163
// Actions
185164
export const scrollto = makeScrollToAction(scrollTo);
186165
export const scrolltotop = makeScrollToAction(scrollToTop);
187166
export const scrolltobottom = makeScrollToAction(scrollToBottom);
188167

189168
// Methods
190-
export const animateScroll = { scrollTo, scrollToTop, scrollToBottom, setGlobalOptions };
169+
export const animateScroll = {
170+
scrollTo,
171+
scrollToTop,
172+
scrollToBottom,
173+
setGlobalOptions
174+
};

0 commit comments

Comments
 (0)