-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #472 from pixiv/toshusai/fix-modal-scroll
Fix: The modal closed when clicking the scrollbar on the modal background.
- Loading branch information
Showing
4 changed files
with
63 additions
and
20 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
42 changes: 42 additions & 0 deletions
42
packages/react/src/components/Modal/useCustomModalOverlay.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,42 @@ | ||
import * as React from 'react' | ||
import { | ||
AriaModalOverlayProps, | ||
ModalOverlayAria, | ||
ariaHideOutside, | ||
useOverlay, | ||
useOverlayFocusContain, | ||
} from '@react-aria/overlays' | ||
|
||
/** | ||
* We want to enable scrolling on the modal background, | ||
* but `useModalOverlay` (specifically, `useOverlay` within it) detects pointer events on the scrollbar. | ||
* Therefore, to prevent this issue, we need to override `shouldCloseOnInteractOutside` in `useModalOverlay`. | ||
*/ | ||
export function useCharcoalModalOverlay( | ||
props: AriaModalOverlayProps, | ||
state: { isOpen: boolean; onClose: () => void }, | ||
ref: React.RefObject<HTMLElement> | ||
): ModalOverlayAria { | ||
const { overlayProps, underlayProps } = useOverlay( | ||
{ | ||
...props, | ||
isOpen: state.isOpen, | ||
onClose: state.onClose, | ||
shouldCloseOnInteractOutside: () => false, | ||
}, | ||
ref | ||
) | ||
|
||
useOverlayFocusContain() | ||
|
||
React.useEffect(() => { | ||
if (state.isOpen && ref.current) { | ||
return ariaHideOutside([ref.current]) | ||
} | ||
}, [state.isOpen, ref]) | ||
|
||
return { | ||
modalProps: overlayProps, | ||
underlayProps, | ||
} | ||
} |