Skip to content

Commit 800ef7a

Browse files
committed
debounce MutationObserver callback
1 parent 2e5e359 commit 800ef7a

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"dotenv": "^16.4.5",
4343
"feed": "^4.2.2",
4444
"giscus": "^1.5.0",
45+
"lodash.debounce": "^4.0.8",
4546
"mdast-util-to-string": "^4.0.0",
4647
"nanostores": "^0.10.3",
4748
"object-treeify": "^4.0.1",
@@ -60,6 +61,7 @@
6061
"@iconify-json/mdi": "^1.1.66",
6162
"@tailwindcss/typography": "^0.5.13",
6263
"@types/debounce": "^1.2.4",
64+
"@types/lodash.debounce": "^4.0.9",
6365
"@types/mdast": "^4.0.3",
6466
"@types/react": "^18.3.3",
6567
"@types/react-dom": "^18.3.0",

src/components/react/ScrollToTop.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React, { useEffect, useRef, useState } from 'react';
22

3+
import debounce from 'lodash.debounce';
4+
35
import { cn } from '@/utils/styles';
46

57
import type { ReactNode } from 'react';
@@ -53,7 +55,8 @@ const ScrollToTop: React.FC<Props> = ({ children }) => {
5355
}
5456
};
5557

56-
const intersect = new IntersectionObserver(callback);
58+
const debouncedCallback = debounce(callback, 500);
59+
const intersect = new IntersectionObserver(debouncedCallback);
5760

5861
if (topRef.current) intersect.observe(topRef.current);
5962
if (bottomRef.current) intersect.observe(bottomRef.current);

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,18 @@
11151115
resolved "https://registry.yarnpkg.com/@types/is-empty/-/is-empty-1.2.3.tgz#a2d55ea8a5ec57bf61e411ba2a9e5132fe4f0899"
11161116
integrity sha512-4J1l5d79hoIvsrKh5VUKVRA1aIdsOb10Hu5j3J2VfP/msDnfTdGPmNp2E1Wg+vs97Bktzo+MZePFFXSGoykYJw==
11171117

1118+
"@types/lodash.debounce@^4.0.9":
1119+
version "4.0.9"
1120+
resolved "https://registry.yarnpkg.com/@types/lodash.debounce/-/lodash.debounce-4.0.9.tgz#0f5f21c507bce7521b5e30e7a24440975ac860a5"
1121+
integrity sha512-Ma5JcgTREwpLRwMM+XwBR7DaWe96nC38uCBDFKZWbNKD+osjVzdpnUSwBcqCptrp16sSOLBAUb50Car5I0TCsQ==
1122+
dependencies:
1123+
"@types/lodash" "*"
1124+
1125+
"@types/lodash@*":
1126+
version "4.17.7"
1127+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.17.7.tgz#2f776bcb53adc9e13b2c0dfd493dfcbd7de43612"
1128+
integrity sha512-8wTvZawATi/lsmNu10/j2hk1KEP0IvjubqPE3cu1Xz7xfXXt5oCq3SNUz4fMIP4XGF9Ky+Ue2tBA3hcS7LSBlA==
1129+
11181130
"@types/mdast@^3.0.0":
11191131
version "3.0.15"
11201132
resolved "https://registry.yarnpkg.com/@types/mdast/-/mdast-3.0.15.tgz#49c524a263f30ffa28b71ae282f813ed000ab9f5"
@@ -3944,6 +3956,11 @@ lodash.castarray@^4.4.0:
39443956
resolved "https://registry.yarnpkg.com/lodash.castarray/-/lodash.castarray-4.4.0.tgz#c02513515e309daddd4c24c60cfddcf5976d9115"
39453957
integrity sha512-aVx8ztPv7/2ULbArGJ2Y42bG1mEQ5mGjpdvrbJcJFU3TbYybe+QlLS4pst9zV52ymy2in1KpFPiZnAOATxD4+Q==
39463958

3959+
lodash.debounce@^4.0.8:
3960+
version "4.0.8"
3961+
resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af"
3962+
integrity sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==
3963+
39473964
lodash.isplainobject@^4.0.6:
39483965
version "4.0.6"
39493966
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"

0 commit comments

Comments
 (0)