Skip to content

Commit 0deb1bd

Browse files
committed
feat: support documentParent mode
1 parent 3eb7dc5 commit 0deb1bd

File tree

7 files changed

+77
-18
lines changed

7 files changed

+77
-18
lines changed

.github/workflows/storybook.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ on:
1717
- README.md
1818

1919
jobs:
20-
build:
20+
deploy:
2121
runs-on: ubuntu-latest
2222

2323
steps:
@@ -52,7 +52,7 @@ jobs:
5252
# env:
5353
# CI: true
5454

55-
- name: Release
55+
- name: Deploy
5656
run: |
5757
npm run deploy-storybook -- --ci
5858
env:

.storybook/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const path = require('path');
22
const basePath = path.resolve(__dirname, '../', 'src');
33

44
module.exports = {
5-
stories: ['../(stories|example)/**/*.stories.(ts|tsx)'],
5+
stories: ['../@(stories|example)/*.stories.@(ts|tsx)'],
66
addons: [
77
'@storybook/addon-actions',
88
'@storybook/addon-links',

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ npm install react-smooth-scroll-hook
2626
import React, { useRef } from 'react';
2727
import useSmoothScroll from 'react-smooth-scroll-hook';
2828
export const Demo = () => {
29-
const ref = useRef<HTMLDivElement>(null);
29+
// A custom scroll container
30+
const ref = useRef(null);
31+
32+
// Also support document.body / document.documentElement, and you don't need to set a ref passing to jsx
33+
const ref = useRef(document.body);
34+
3035
const { scrollTo } = useSmoothScroll({
3136
ref,
3237
speed: 100,
@@ -37,6 +42,7 @@ export const Demo = () => {
3742
<>
3843
<button onClick={() => scrollTo('#item-20')}>scrollTo('#item-20')</button>
3944
<div
45+
// if use custom scroll container, pass ref
4046
ref={ref}
4147
style={{
4248
overflowY: 'scroll',

example/Body.stories.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import useSmoothScroll from 'react-smooth-scroll-hook';
2+
import React, { useRef } from 'react';
3+
4+
export const Body = () => {
5+
const ref = useRef<HTMLElement>(document.documentElement);
6+
const { scrollTo } = useSmoothScroll({
7+
ref,
8+
});
9+
10+
return (
11+
<>
12+
<button onClick={() => scrollTo('#item-20')}>scrollTo('#item-20')</button>
13+
<button onClick={() => scrollTo(400)}>scrollTo(400)</button>
14+
<br />
15+
<div
16+
style={{
17+
padding: '10px',
18+
}}
19+
>
20+
{Array(100)
21+
.fill(null)
22+
.map((_item, i) => (
23+
<div key={i} id={`item-${i}`}>
24+
item-{i}
25+
</div>
26+
))}
27+
</div>
28+
<button onClick={() => scrollTo('#item-20')}>scrollTo('#item-20')</button>
29+
</>
30+
);
31+
};

example/DirectionX.stories.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import React, { useState, useRef } from "react";
2-
import useSmoothScroll from "react-smooth-scroll-hook";
1+
import React, { useState, useRef } from 'react';
2+
import useSmoothScroll from 'react-smooth-scroll-hook';
33

44
export const DirectionX = () => {
55
const [speed, setSpeed] = useState(200);
66
const ref = useRef<HTMLDivElement>(null);
77
const { scrollTo, reachTop, reachBottom, scrollToPage } = useSmoothScroll({
88
ref,
9-
direction: "x",
10-
speed
9+
direction: 'x',
10+
speed,
1111
});
1212
const onChange = (evt: React.ChangeEvent<HTMLInputElement>) => {
1313
setSpeed(Number(evt.target.value));
@@ -28,9 +28,12 @@ export const DirectionX = () => {
2828
<br />
2929
<strong>
3030
Pass string:
31-
<button onClick={() => scrollTo("#x-item-20")}>
31+
<button onClick={() => scrollTo('#x-item-20')}>
3232
scrollTo('#x-item-20')
3333
</button>
34+
<button onClick={() => scrollTo('#x-item-20', -10)}>
35+
scrollTo('#x-item-20', -10)
36+
</button>
3437
</strong>
3538
<br />
3639
<strong>
@@ -63,13 +66,13 @@ export const DirectionX = () => {
6366
<div
6467
ref={ref}
6568
style={{
66-
overflowX: "scroll"
69+
overflowX: 'scroll',
6770
}}
6871
>
6972
<div
7073
style={{
71-
display: "flex",
72-
padding: "10px"
74+
display: 'flex',
75+
padding: '10px',
7376
}}
7477
>
7578
{Array(50)
@@ -80,7 +83,7 @@ export const DirectionX = () => {
8083
id={`x-item-${i}`}
8184
style={{
8285
flexShrink: 0,
83-
padding: "10px"
86+
padding: '10px',
8487
}}
8588
>
8689
x-item-{i}

src/index.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,17 @@ const getRelativeDistance = (
5151
console.warn('Please pass correct selector string for scrollTo()!');
5252
return 0;
5353
}
54-
const dis =
55-
elm.getBoundingClientRect()[attrMap.leftTop] -
56-
parent.getBoundingClientRect()[attrMap.leftTop];
54+
let dis = 0;
55+
56+
// if parent is document.documentElement or document.body
57+
if (!parent.parentElement || !parent.parentElement.parentElement) {
58+
dis = elm.getBoundingClientRect()[attrMap.leftTop];
59+
} else {
60+
dis =
61+
elm.getBoundingClientRect()[attrMap.leftTop] -
62+
parent.getBoundingClientRect()[attrMap.leftTop];
63+
}
64+
5765
return dis;
5866
}
5967
return 0;
@@ -111,7 +119,7 @@ export const useSmoothScroll = ({
111119
isBottomEdge() ? setReachBottom(true) : setReachBottom(false);
112120
});
113121

114-
const scrollTo = (target?: number | string) => {
122+
const scrollTo = (target?: number | string, offset?: number) => {
115123
if (!ref || !ref.current) {
116124
console.warn(
117125
'Please pass `ref` property for your scroll container! \n Get more info at https://github.com/ron0115/react-smooth-scroll-hook'
@@ -125,8 +133,15 @@ export const useSmoothScroll = ({
125133
'Please pass a valid property for `scrollTo()`! \n Get more info at https://github.com/ron0115/react-smooth-scroll-hook'
126134
);
127135
}
136+
128137
const initScrollLeftTop = elm[attrMap.scrollLeftTop];
129-
const distance = getRelativeDistance(target, elm, attrMap);
138+
139+
let distance = getRelativeDistance(target, elm, attrMap);
140+
console.log(distance);
141+
// set a offset
142+
if (typeof offset === 'number') {
143+
distance += offset;
144+
}
130145

131146
let _speed = speed;
132147
const cb = () => {

stories/Demo.stories.tsx renamed to stories/index.stories.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ export default {
99
};
1010
import { Demo } from '../example/Demo.stories';
1111
import { DirectionX } from '../example/DirectionX.stories';
12+
import { Body } from '../example/Body.stories';
1213
export const Docs = () => (
1314
<>
1415
<Description markdown={require('../README.md').default} />
1516
</>
1617
);
1718
export { Demo };
1819
export { DirectionX };
20+
export { Body };
1921
// @ts-ignore
2022
Demo.storyName = 'Basic';
2123
// @ts-ignore
2224
DirectionX.storyName = 'More Use';
25+
// @ts-ignore
26+
Body.storyName = 'Body Parent';

0 commit comments

Comments
 (0)