-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(FullscreenModal): add component
- Loading branch information
Showing
37 changed files
with
869 additions
and
3 deletions.
There are no files selected for viewing
Binary file added
BIN
+99.9 KB
.storybook/image-snapshots/expected/components_FullscreenModal_Large Size.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+65.3 KB
.storybook/image-snapshots/expected/components_FullscreenModal_Medium Size.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+98.2 KB
.storybook/image-snapshots/expected/components_FullscreenModal_Small Size.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,5 @@ | ||
import React from 'react'; | ||
|
||
const Content: React.FC = ({ children }) => <div>{children}</div>; | ||
|
||
export default Content; |
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,12 @@ | ||
import React from 'react'; | ||
import { noop } from 'ramda-adjunct'; | ||
|
||
import { ContextProps } from './Context.types'; | ||
|
||
const Context = React.createContext<ContextProps>({ | ||
size: 'lg', | ||
modalRef: null, | ||
handleClose: noop, | ||
}); | ||
|
||
export default Context; |
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,7 @@ | ||
import { Sizes } from '../FullscreenModal.types'; | ||
|
||
export interface ContextProps { | ||
size: Sizes; | ||
modalRef: React.MutableRefObject<HTMLElement>; | ||
handleClose: () => void; | ||
} |
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,68 @@ | ||
import React, { useContext, useRef } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
import { getColor, pxToRem } from '../../../utils/helpers'; | ||
import { FlexContainer } from '../../FlexContainer'; | ||
import { ScrollToTop } from '../ScrollToTop'; | ||
import { FullscreenModalProps } from '../FullscreenModal.types'; | ||
import ModalContext from '../Context/Context'; | ||
import { useStickyFooter } from './useStickyFooter'; | ||
|
||
const BaseStickyFooter = styled.footer` | ||
position: fixed; | ||
bottom: 0; | ||
left: 0; | ||
width: 100%; | ||
padding: ${pxToRem(10, 0)}; | ||
background-color: ${getColor('graphite5H')}; | ||
border-top: 1px solid ${getColor('graphiteHB')}; | ||
`; | ||
const BaseFooter = styled.footer` | ||
background-color: ${getColor('graphite5H')}; | ||
border-top: 1px solid ${getColor('graphiteHB')}; | ||
padding-top: ${pxToRem(24)}; | ||
margin-top: ${pxToRem(40)}; | ||
`; | ||
|
||
const FooterInnerContainer = styled.div<Pick<FullscreenModalProps, 'size'>>` | ||
width: ${({ size, theme }) => pxToRem(theme.modals.size[size])}; | ||
margin: 0 auto; | ||
`; | ||
|
||
const Footer: React.FC = ({ children }) => { | ||
const { size, modalRef } = useContext(ModalContext); | ||
const modalFooterRef = useRef(null); | ||
|
||
const { isFixed, shouldShowScrollToTopButton } = useStickyFooter( | ||
modalRef, | ||
modalFooterRef, | ||
); | ||
|
||
const scrollToTop = () => { | ||
modalRef.current.scrollTo(0, 0); | ||
}; | ||
|
||
return ( | ||
<> | ||
{isFixed && ( | ||
<BaseStickyFooter> | ||
<FooterInnerContainer size={size}>{children}</FooterInnerContainer> | ||
</BaseStickyFooter> | ||
)} | ||
<BaseFooter ref={modalFooterRef}> | ||
{children} | ||
{shouldShowScrollToTopButton && ( | ||
<FlexContainer | ||
alignItems="center" | ||
justifyContent="center" | ||
margin={{ vertical: 1.2 }} | ||
> | ||
<ScrollToTop onClick={scrollToTop} /> | ||
</FlexContainer> | ||
)} | ||
</BaseFooter> | ||
</> | ||
); | ||
}; | ||
|
||
export default Footer; |
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,75 @@ | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import debounce from 'lodash.debounce'; | ||
|
||
export const useStickyFooter = ( | ||
modalRef: React.MutableRefObject<HTMLElement>, | ||
modalFooterRef: React.MutableRefObject<HTMLElement>, | ||
): { isFixed: boolean; shouldShowScrollToTopButton: boolean } => { | ||
const [isFixed, setIsFixed] = useState(false); | ||
const [ | ||
shouldShowScrollToTopButton, | ||
setShouldShowScrollToTopButton, | ||
] = useState(false); | ||
const showScrollToTopButton = useCallback(() => { | ||
if (modalRef.current === null) return; | ||
const isScrollable = | ||
modalRef.current.scrollHeight > modalRef.current.offsetHeight; | ||
setShouldShowScrollToTopButton(isScrollable); | ||
setIsFixed(false); | ||
}, [modalRef, setShouldShowScrollToTopButton]); | ||
|
||
useEffect(() => { | ||
showScrollToTopButton(); | ||
window.addEventListener('resize', debounce(showScrollToTopButton, 200)); | ||
}, [modalRef, showScrollToTopButton]); | ||
|
||
const isInView = useCallback(() => { | ||
if (modalRef.current === null || modalFooterRef.current === null) return; | ||
const scrollOffset = | ||
modalRef.current.scrollTop + modalRef.current.offsetHeight; | ||
const contentHeight = | ||
modalRef.current.scrollHeight - modalFooterRef.current.scrollHeight; | ||
|
||
if (isFixed && scrollOffset >= contentHeight) { | ||
setIsFixed(false); | ||
} else if ( | ||
!isFixed && | ||
scrollOffset < contentHeight - contentHeight * 0.001 | ||
) { | ||
setIsFixed(true); | ||
} | ||
}, [isFixed, setIsFixed, modalRef, modalFooterRef]); | ||
const [observer] = useState(new MutationObserver(() => isInView())); | ||
|
||
useEffect(() => { | ||
if ( | ||
modalRef.current !== null && | ||
modalFooterRef.current !== null && | ||
shouldShowScrollToTopButton | ||
) { | ||
observer.observe(modalRef.current, { | ||
childList: true, | ||
subtree: true, | ||
attributes: true, | ||
}); | ||
|
||
isInView(); | ||
modalRef.current.addEventListener('scroll', debounce(isInView, 100)); | ||
window.addEventListener('resize', debounce(isInView, 200)); | ||
} | ||
return () => { | ||
observer.disconnect(); | ||
}; | ||
}, [ | ||
observer, | ||
modalRef, | ||
modalFooterRef, | ||
shouldShowScrollToTopButton, | ||
isInView, | ||
]); | ||
|
||
return { | ||
isFixed, | ||
shouldShowScrollToTopButton, | ||
}; | ||
}; |
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,5 @@ | ||
export const FullscreenModalSizes = { | ||
lg: 'lg', | ||
md: 'md', | ||
sm: 'sm', | ||
} as const; |
Oops, something went wrong.