-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ModalBasicLayout): fix for logic of when to show modal footer shadow
- Loading branch information
1 parent
0855390
commit eedff88
Showing
3 changed files
with
49 additions
and
19 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
18 changes: 10 additions & 8 deletions
18
packages/core/src/components/Modal/layouts/ModalLayoutScrollableContent.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 |
---|---|---|
@@ -1,14 +1,16 @@ | ||
import React from "react"; | ||
import React, { ForwardedRef, forwardRef } from "react"; | ||
import cx from "classnames"; | ||
import styles from "./ModalLayoutScrollableContent.module.scss"; | ||
import { ModalLayoutScrollableContentProps } from "./ModalLayoutScrollableContent.types"; | ||
|
||
const ModalLayoutScrollableContent = ({ onScroll, className, children }: ModalLayoutScrollableContentProps) => { | ||
return ( | ||
<div className={cx(styles.content, className)} onScroll={onScroll}> | ||
{children} | ||
</div> | ||
); | ||
}; | ||
const ModalLayoutScrollableContent = forwardRef( | ||
({ onScroll, className, children }: ModalLayoutScrollableContentProps, ref: ForwardedRef<HTMLDivElement>) => { | ||
return ( | ||
<div ref={ref} className={cx(styles.content, className)} onScroll={onScroll}> | ||
{children} | ||
</div> | ||
); | ||
} | ||
); | ||
|
||
export default ModalLayoutScrollableContent; |
44 changes: 36 additions & 8 deletions
44
packages/core/src/components/Modal/layouts/useLayoutScrolledContent.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 |
---|---|---|
@@ -1,16 +1,44 @@ | ||
import { UIEventHandler, useCallback, useState } from "react"; | ||
import { UIEventHandler, useCallback, useEffect, useRef, useState } from "react"; | ||
|
||
const useLayoutScrolledContent = () => { | ||
const [isContentScrolled, setContentScrolled] = useState<boolean>(false); | ||
const [isScrollable, setScrollable] = useState<boolean>(false); | ||
const [isScrolledToEnd, setScrolledToEnd] = useState<boolean>(false); | ||
|
||
const onScroll: UIEventHandler<HTMLDivElement> = useCallback( | ||
e => { | ||
setContentScrolled(e.currentTarget?.scrollTop > 0); | ||
}, | ||
[setContentScrolled] | ||
); | ||
const ref = useRef<HTMLDivElement>(null); | ||
|
||
return { isContentScrolled, onScroll }; | ||
const checkScroll = useCallback(() => { | ||
const element = ref.current; | ||
if (element) { | ||
const { scrollTop, scrollHeight, clientHeight } = element; | ||
setScrollable(scrollHeight > clientHeight); | ||
setContentScrolled(scrollTop > 0); | ||
setScrolledToEnd(scrollTop + clientHeight >= scrollHeight); | ||
} | ||
}, []); | ||
|
||
const onScroll: UIEventHandler<HTMLDivElement> = useCallback(() => { | ||
checkScroll(); | ||
}, [checkScroll]); | ||
|
||
useEffect(() => { | ||
const element = ref.current; | ||
if (!element) return; | ||
|
||
const resizeObserver = new ResizeObserver(() => { | ||
checkScroll(); | ||
}); | ||
|
||
resizeObserver.observe(element); | ||
|
||
checkScroll(); | ||
|
||
return () => { | ||
resizeObserver.disconnect(); | ||
}; | ||
}, [checkScroll]); | ||
|
||
return { ref, isContentScrolled, isScrollable, isScrolledToEnd, onScroll }; | ||
}; | ||
|
||
export default useLayoutScrolledContent; |