forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
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 processing#2309 from lindapaiste/refactor/overlay
Convert `Overlay` to a function component, shares logic with `Modal`
- Loading branch information
Showing
7 changed files
with
118 additions
and
150 deletions.
There are no files selected for viewing
File renamed without changes.
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,45 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import useKeyDownHandlers from './useKeyDownHandlers'; | ||
|
||
/** | ||
* Common logic for Modal, Overlay, etc. | ||
* | ||
* Pass in the `onClose` handler. | ||
* | ||
* Can optionally pass in a ref, in case the `onClose` function needs to use the ref. | ||
* | ||
* Calls the provided `onClose` function on: | ||
* - Press Escape key. | ||
* - Click outside the element. | ||
* | ||
* Returns a ref to attach to the outermost element of the modal. | ||
* | ||
* @param {() => void} onClose | ||
* @param {React.MutableRefObject<HTMLElement | null>} [passedRef] | ||
* @return {React.MutableRefObject<HTMLElement | null>} | ||
*/ | ||
export default function useModalClose(onClose, passedRef) { | ||
const createdRef = useRef(null); | ||
const modalRef = passedRef || createdRef; | ||
|
||
useEffect(() => { | ||
modalRef.current?.focus(); | ||
|
||
function handleClick(e) { | ||
// ignore clicks on the component itself | ||
if (modalRef.current && !modalRef.current.contains(e.target)) { | ||
onClose?.(); | ||
} | ||
} | ||
|
||
document.addEventListener('click', handleClick, false); | ||
|
||
return () => { | ||
document.removeEventListener('click', handleClick, false); | ||
}; | ||
}, [onClose, modalRef]); | ||
|
||
useKeyDownHandlers({ escape: onClose }); | ||
|
||
return modalRef; | ||
} |
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
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