Skip to content

Commit 7496d55

Browse files
committed
chore: update demo
1 parent fe4d309 commit 7496d55

File tree

1 file changed

+56
-46
lines changed

1 file changed

+56
-46
lines changed

examples/basic/src/components/SideBySide.jsx

+56-46
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,86 @@
1-
import { useLayoutEffect, useRef } from 'react'
1+
import { useEffect, useRef } from 'react'
22

33
export default function SideBySide({ sideBySideContents }) {
44
const baseTop = useRef(0)
5-
useLayoutEffect(() => {
5+
useEffect(() => {
66
baseTop.current = leftContainer.current.getBoundingClientRect().top
77
}, [])
88

99
const leftContainer = useRef(null)
1010
const rightContainer = useRef(null)
11-
12-
let timer = null
13-
let isLeftScroll = false
14-
let isRightScroll = false
15-
function handleScroll(type) {
16-
if (type === 'left') {
17-
if (isRightScroll) return
18-
isLeftScroll = true
19-
clearTimeout(timer)
20-
timer = setTimeout(() => {
21-
isLeftScroll = false
22-
}, 300)
23-
syncScroll(leftContainer.current, rightContainer.current)
24-
} else {
25-
if (isLeftScroll) return
26-
isRightScroll = true
27-
clearTimeout(timer)
28-
timer = setTimeout(() => {
29-
isRightScroll = false
30-
}, 300)
31-
syncScroll(rightContainer.current, leftContainer.current)
32-
}
33-
}
34-
function syncScroll(origin, target) {
35-
let findSeq = ''
36-
let leftTop = 0
37-
for (const el of origin.children) {
38-
if (el.dataset.seq && el.getBoundingClientRect().top > baseTop.current) {
39-
findSeq = el.dataset.seq
40-
leftTop = el.getBoundingClientRect().top
41-
break
11+
useEffect(() => {
12+
let timer = null
13+
let isLeftScroll = false
14+
let isRightScroll = false
15+
function handleScroll(type) {
16+
if (type === 'left') {
17+
if (isRightScroll) return
18+
isLeftScroll = true
19+
clearTimeout(timer)
20+
timer = setTimeout(() => {
21+
isLeftScroll = false
22+
}, 300)
23+
syncScroll(leftContainer.current, rightContainer.current)
24+
} else {
25+
if (isLeftScroll) return
26+
isRightScroll = true
27+
clearTimeout(timer)
28+
timer = setTimeout(() => {
29+
isRightScroll = false
30+
}, 300)
31+
syncScroll(rightContainer.current, leftContainer.current)
4232
}
4333
}
44-
if (!findSeq) return
34+
function syncScroll(origin, target) {
35+
let findSeq = ''
36+
let leftTop = 0
37+
for (const el of origin.children) {
38+
if (el.dataset.seq && el.getBoundingClientRect().top > baseTop.current) {
39+
findSeq = el.dataset.seq
40+
leftTop = el.getBoundingClientRect().top
41+
break
42+
}
43+
}
44+
if (!findSeq) return
4545

46-
let syncEl = null
47-
for (const el of target.children) {
48-
if (el.dataset.seq === findSeq) {
49-
syncEl = el
50-
break
46+
let syncEl = null
47+
for (const el of target.children) {
48+
if (el.dataset.seq === findSeq) {
49+
syncEl = el
50+
break
51+
}
5152
}
53+
if (!syncEl) return
54+
55+
const rightTop = syncEl.getBoundingClientRect().top
56+
const delta = rightTop - leftTop
57+
target.scrollTo({ top: target.scrollTop + delta })
5258
}
53-
if (!syncEl) return
5459

55-
const rightTop = syncEl.getBoundingClientRect().top
56-
const delta = rightTop - leftTop
57-
target.scrollTo({ top: target.scrollTop + delta })
58-
}
60+
const handleLeftScroll = () => handleScroll('left')
61+
const handleRightScroll = () => handleScroll('right')
62+
leftContainer.current.addEventListener('scroll', handleLeftScroll)
63+
rightContainer.current.addEventListener('scroll', handleRightScroll)
64+
65+
return () => {
66+
clearTimeout(timer)
67+
leftContainer.current.removeEventListener('scroll', handleLeftScroll)
68+
rightContainer.current.removeEventListener('scroll', handleRightScroll)
69+
}
70+
}, [])
5971

6072
return (
6173
<div className="flex h-[calc(100%-32px)]">
6274
<div
6375
ref={leftContainer}
6476
className="markdown-body overflow-auto h-full w-1/2"
6577
dangerouslySetInnerHTML={{ __html: sideBySideContents[0] }}
66-
onScroll={e => handleScroll('left')}
6778
/>
6879
<div className="w-2"></div>
6980
<div
7081
ref={rightContainer}
7182
className="markdown-body overflow-auto h-full w-1/2"
7283
dangerouslySetInnerHTML={{ __html: sideBySideContents[1] }}
73-
onScroll={e => handleScroll('right')}
7484
/>
7585
</div>
7686
)

0 commit comments

Comments
 (0)