-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Interaction): implement DualScroll component for Collection view
- Loading branch information
Showing
5 changed files
with
156 additions
and
44 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/components/Interaction/DualScroll/EndOfResults/index.tsx
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,36 @@ | ||
import classNames from 'classnames' | ||
import { FormattedMessage } from 'react-intl' | ||
|
||
import { capitalizeFirstLetter } from '~/common/utils' | ||
|
||
import styles from './styles.module.css' | ||
|
||
type EndOfResultsProps = { | ||
message?: React.ReactNode | ||
spacingTop?: 'base' | 'xLoose' | ||
} | ||
|
||
const EndOfResults: React.FC<EndOfResultsProps> = ({ | ||
message, | ||
spacingTop = 'xLoose', | ||
}) => { | ||
const containerClasses = classNames({ | ||
[styles.endOfResults]: true, | ||
[styles[`spacingTop${capitalizeFirstLetter(spacingTop)}`]]: true, | ||
}) | ||
return ( | ||
<section className={containerClasses}> | ||
{typeof message === 'boolean' && message ? ( | ||
<FormattedMessage | ||
defaultMessage="That's all" | ||
id="B2As08" | ||
description="src/components/Interaction/InfiniteScroll/EndOfResults/index.tsx" | ||
/> | ||
) : ( | ||
message | ||
)} | ||
</section> | ||
) | ||
} | ||
|
||
export default EndOfResults |
15 changes: 15 additions & 0 deletions
15
src/components/Interaction/DualScroll/EndOfResults/styles.module.css
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,15 @@ | ||
.endOfResults { | ||
@mixin flex-center-all; | ||
|
||
padding-bottom: var(--sp40); | ||
font-size: var(--text14); | ||
color: var(--color-grey); | ||
} | ||
|
||
.spacingTopBase { | ||
padding-top: var(--sp16); | ||
} | ||
|
||
.spacingTopXLoose { | ||
padding-top: var(--sp32); | ||
} |
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,74 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
import { SpinnerBlock, useIntersectionObserver } from '~/components' | ||
|
||
import EndOfResults from './EndOfResults' | ||
|
||
interface Props { | ||
hasNextPage: boolean | ||
hasPreviousPage: boolean | ||
loadMore: () => Promise<any> | ||
loadPrevious: () => Promise<any> | ||
loader?: React.ReactNode | ||
eof?: React.ReactNode | ||
eofSpacingTop?: 'base' | 'xLoose' | ||
scrollableAncestor?: any | ||
className?: string | ||
} | ||
|
||
export const DualScroll: React.FC<React.PropsWithChildren<Props>> = ({ | ||
hasNextPage, | ||
hasPreviousPage, | ||
loader = <SpinnerBlock />, | ||
loadMore, | ||
loadPrevious, | ||
eof, | ||
eofSpacingTop, | ||
scrollableAncestor, | ||
children, | ||
className, | ||
}) => { | ||
const [isPrevLoading, setIsPrevLoading] = useState(false) | ||
|
||
// Only use intersection observer for bottom loading | ||
const bottomObserver = useIntersectionObserver() | ||
|
||
// Handle bottom intersection | ||
useEffect(() => { | ||
if (bottomObserver.isIntersecting && hasNextPage) { | ||
loadMore() | ||
} | ||
}, [bottomObserver.isIntersecting]) | ||
|
||
// Handle scroll for top loading | ||
const handleScroll = async (event: React.UIEvent<HTMLDivElement>) => { | ||
const element = event.currentTarget | ||
if (element.scrollTop === 0 && hasPreviousPage) { | ||
setIsPrevLoading(true) | ||
await loadPrevious() | ||
setIsPrevLoading(false) | ||
} | ||
} | ||
|
||
return ( | ||
<div | ||
onScroll={handleScroll} | ||
style={{ overflow: 'auto' }} | ||
className={className} | ||
> | ||
{/* Top loader */} | ||
{hasPreviousPage && isPrevLoading ? loader : null} | ||
|
||
{/* Main content */} | ||
{children} | ||
|
||
{/* Bottom loader */} | ||
{hasNextPage && <span ref={bottomObserver.ref} />} | ||
{hasNextPage && loader} | ||
|
||
{!hasNextPage && eof && ( | ||
<EndOfResults message={eof} spacingTop={eofSpacingTop} /> | ||
)} | ||
</div> | ||
) | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './Card' | ||
export * from './DualScroll' | ||
export * from './InfiniteScroll' | ||
export * from './LinkWrapper' |
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