-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: useExpandableModal, abstract away components
- Loading branch information
1 parent
e8c820f
commit 5469e60
Showing
5 changed files
with
182 additions
and
86 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
apps/web/src/components/AdPanel/Expandable/ExpandableContent.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,37 @@ | ||
import { Box, Text } from '@pancakeswap/uikit' | ||
import { PropsWithChildren, ReactNode } from 'react' | ||
import { Divider } from './styles' | ||
|
||
interface ExpandableContentProps extends PropsWithChildren { | ||
title: string | ||
isExpanded: boolean | ||
|
||
extendedContentRef: React.RefObject<HTMLDivElement> | ||
|
||
expandableContent?: ReactNode | ||
defaultContent?: ReactNode | ||
} | ||
|
||
export const ExpandableContent = ({ | ||
title, | ||
isExpanded, | ||
extendedContentRef, | ||
expandableContent, | ||
defaultContent, | ||
}: ExpandableContentProps) => { | ||
return ( | ||
<> | ||
{isExpanded ? ( | ||
<Box ref={extendedContentRef} overflow="hidden"> | ||
<Text bold as="h1" textAlign="center" p="16px"> | ||
{title} | ||
</Text> | ||
<Divider /> | ||
<Box p="16px">{expandableContent}</Box> | ||
</Box> | ||
) : ( | ||
defaultContent | ||
)} | ||
</> | ||
) | ||
} |
22 changes: 22 additions & 0 deletions
22
apps/web/src/components/AdPanel/Expandable/ExpandableModal.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,22 @@ | ||
import { Modal, ModalV2, useMatchBreakpoints } from '@pancakeswap/uikit' | ||
import { ReactNode } from 'react' | ||
|
||
interface ExpandableModalProps { | ||
title: string | ||
isOpen: boolean | ||
onDismiss: () => void | ||
expandableContent?: ReactNode | ||
} | ||
|
||
export const ExpandableModal = ({ title, isOpen, expandableContent, onDismiss }: ExpandableModalProps) => { | ||
const { isDesktop } = useMatchBreakpoints() | ||
return ( | ||
<> | ||
<ModalV2 isOpen={isOpen} onDismiss={onDismiss} closeOnOverlayClick> | ||
<Modal title={title} onDismiss={onDismiss} maxWidth={isDesktop ? '438px' : 'unset'}> | ||
{expandableContent} | ||
</Modal> | ||
</ModalV2> | ||
</> | ||
) | ||
} |
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,17 @@ | ||
import { Box, Flex } from '@pancakeswap/uikit' | ||
import styled from 'styled-components' | ||
|
||
export const Divider = styled(Box)` | ||
width: 100%; | ||
background-color: ${({ theme }) => theme.colors.cardBorder}; | ||
height: 1px; | ||
` | ||
|
||
export const ActionContainer = styled(Flex)<{ $isExpanded?: boolean }>` | ||
${({ theme, $isExpanded }) => | ||
$isExpanded && | ||
` | ||
border-top: 1px solid ${theme.colors.cardBorder}; | ||
`} | ||
` |
66 changes: 66 additions & 0 deletions
66
apps/web/src/components/AdPanel/Expandable/useExpandableCard.ts
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,66 @@ | ||
import { useMatchBreakpoints, useModalV2 } from '@pancakeswap/uikit' | ||
import { useCallback, useEffect, useMemo, useRef } from 'react' | ||
import { useIsSlideExpanded } from '../useIsSlideExpanded' | ||
|
||
interface UseExpandableCardProps { | ||
/** Id unique to this expandable card */ | ||
adId: string | ||
forceMobile?: boolean | ||
} | ||
|
||
export const useExpandableCard = ({ adId, forceMobile }: UseExpandableCardProps) => { | ||
const { slideExpanded, setSlideExpanded } = useIsSlideExpanded() | ||
const { isDesktop } = useMatchBreakpoints() | ||
const { isOpen, onDismiss, setIsOpen } = useModalV2() | ||
|
||
const extendedContentRef = useRef<HTMLDivElement>(null) | ||
const adCardRef = useRef<HTMLDivElement>(null) | ||
const actionPanelRef = useRef<HTMLDivElement>(null) | ||
|
||
const isMobile = useMemo(() => forceMobile || !isDesktop, [forceMobile, isDesktop]) | ||
|
||
const isExpanded = useMemo(() => { | ||
return !isMobile ? slideExpanded[adId] || false : false | ||
}, [adId, isMobile, slideExpanded]) | ||
|
||
const handleExpand = useCallback(() => { | ||
setSlideExpanded(adId, true) | ||
if (isMobile) setIsOpen(true) | ||
}, [adId, isMobile, setIsOpen, setSlideExpanded]) | ||
|
||
const handleDismiss = useCallback(() => { | ||
setSlideExpanded(adId, false) | ||
onDismiss() | ||
}, [adId, onDismiss, setSlideExpanded]) | ||
|
||
const toggleHeight = useCallback((isAuto: boolean, isExpend: boolean) => { | ||
const targetCard = adCardRef.current | ||
if (!targetCard || !isExpend) return | ||
targetCard.style.height = isAuto ? 'auto' : `${targetCard.offsetHeight}px` | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (isExpanded && extendedContentRef.current && adCardRef.current && actionPanelRef.current) { | ||
const targetCard = adCardRef.current | ||
|
||
const contentPanelHeight = extendedContentRef.current.scrollHeight - 13 | ||
const actionPanelHeight = actionPanelRef.current.scrollHeight || 64 | ||
|
||
targetCard.style.height = `${contentPanelHeight + actionPanelHeight}px` | ||
} else if (!isExpanded && adCardRef.current) { | ||
const targetCard = adCardRef.current | ||
targetCard.style.height = `164px` | ||
} | ||
}, [isExpanded]) | ||
|
||
return { | ||
isExpanded, | ||
isOpen, | ||
extendedContentRef, | ||
adCardRef, | ||
actionPanelRef, | ||
handleExpand, | ||
handleDismiss, | ||
toggleHeight, | ||
} | ||
} |
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