-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45f43a0
commit 3c6d113
Showing
18 changed files
with
1,048 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { | ||
useEffect, | ||
useRef, | ||
} from 'react'; | ||
import { | ||
isMobile, | ||
} from '../../util/util'; | ||
import { findDOMNode } from 'react-dom'; | ||
import { notify } from '../../events/events'; | ||
import { ScrollViewData } from '../navigation/scroll-bar'; | ||
|
||
const useScrollbar = (controller: string) => { | ||
const observer = useRef<IntersectionObserver | null>(null); | ||
|
||
const lastScrollViewHeight = useRef(0); | ||
const lastContentHeight = useRef(0); | ||
const lastOffset = useRef(0); | ||
|
||
useEffect(() => { | ||
if (isMobile()) { | ||
return; | ||
} | ||
|
||
return () => { | ||
if (observer.current) { | ||
observer.current.disconnect(); | ||
observer.current = null; | ||
} | ||
} | ||
}, []); | ||
|
||
return useRef({ | ||
onLayout: (params) => { | ||
if (isMobile()) { | ||
return; | ||
} | ||
|
||
lastScrollViewHeight.current = params.nativeEvent.layout.height; | ||
|
||
notify<ScrollViewData>( | ||
'main-scroll-view', | ||
{ | ||
controller, | ||
scrollViewHeight: lastScrollViewHeight.current, | ||
contentHeight: lastContentHeight.current, | ||
offset: lastOffset.current, | ||
} | ||
); | ||
}, | ||
onContentSizeChange: (contentWidth, contentHeight) => { | ||
if (isMobile()) { | ||
return; | ||
} | ||
|
||
lastContentHeight.current = contentHeight; | ||
|
||
notify<ScrollViewData>( | ||
'main-scroll-view', | ||
{ | ||
controller, | ||
scrollViewHeight: lastScrollViewHeight.current, | ||
contentHeight: lastContentHeight.current, | ||
offset: lastOffset.current, | ||
} | ||
); | ||
}, | ||
onScroll: ({nativeEvent}) => { | ||
if (isMobile()) { | ||
return; | ||
} | ||
|
||
lastOffset.current = nativeEvent.contentOffset.y; | ||
|
||
notify<ScrollViewData>( | ||
'main-scroll-view', | ||
{ | ||
controller, | ||
offset: lastOffset.current, | ||
} | ||
) | ||
}, | ||
showsVerticalScrollIndicator: isMobile(), | ||
observeListRef: (node): void => { | ||
if (isMobile()) { | ||
return; | ||
} | ||
if (!node) { | ||
return; | ||
} | ||
|
||
if (observer.current) { | ||
observer.current.disconnect(); | ||
} | ||
|
||
const onThumbDrag = (offset: number) => { | ||
if (typeof node.scrollToOffset === 'function') { | ||
node.scrollToOffset({ offset, animated: false }); | ||
} else if (typeof node.scrollTo === 'function') { | ||
node.scrollTo({ y: offset, animated: false }); | ||
} else { | ||
throw new Error('No scroll method found on ref'); | ||
} | ||
}; | ||
|
||
observer.current = new IntersectionObserver( | ||
([entry]) => { | ||
if (!entry.isIntersecting) { | ||
notify<ScrollViewData>( | ||
'main-scroll-view', | ||
{ | ||
controller, | ||
onThumbDrag: null, | ||
} | ||
); | ||
|
||
return; | ||
} | ||
|
||
notify<ScrollViewData>( | ||
'main-scroll-view', | ||
{ | ||
controller, | ||
scrollViewHeight: lastScrollViewHeight.current, | ||
contentHeight: lastContentHeight.current, | ||
offset: lastOffset.current, | ||
onThumbDrag: onThumbDrag, | ||
} | ||
); | ||
}, | ||
{ root: null } | ||
); | ||
|
||
observer.current.observe(findDOMNode(node)); | ||
} | ||
}).current; | ||
}; | ||
|
||
export { | ||
useScrollbar, | ||
}; |
Oops, something went wrong.