-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e05e7cb
commit b6f5143
Showing
7 changed files
with
141 additions
and
111 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
109 changes: 42 additions & 67 deletions
109
packages/react/src/components/ClickOutsideListener/index.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,97 +1,72 @@ | ||
import React from 'react'; | ||
import React, { useRef, useEffect } from 'react'; | ||
import setRef from '../../utils/setRef'; | ||
|
||
export interface ClickOutsideListenerProps< | ||
T extends HTMLElement = HTMLElement | ||
> { | ||
children?: React.ReactNode; | ||
children?: React.ReactElement; | ||
onClickOutside: (e: MouseEvent | TouchEvent) => void; | ||
mouseEvent?: 'mousedown' | 'click' | 'mouseup' | false; | ||
touchEvent?: 'touchstart' | 'touchend' | false; | ||
target?: T; | ||
} | ||
|
||
export default class ClickOutsideListener extends React.Component<ClickOutsideListenerProps> { | ||
static displayName = 'ClickOutsideListener'; | ||
|
||
static defaultProps = { | ||
mouseEvent: 'click', | ||
touchEvent: 'touchend' | ||
}; | ||
|
||
private nodeRef: HTMLElement | null; | ||
|
||
handleEvent = (event: MouseEvent | TouchEvent) => { | ||
const { nodeRef, props } = this; | ||
const { onClickOutside, target } = props; | ||
function ClickOutsideListener( | ||
{ | ||
children, | ||
mouseEvent = 'mouseup', | ||
touchEvent = 'touchend', | ||
target, | ||
onClickOutside = () => null | ||
}: ClickOutsideListenerProps, | ||
ref: React.ForwardedRef<HTMLElement> | ||
): JSX.Element | null { | ||
const childElementRef = useRef<HTMLElement>(); | ||
|
||
const handleEvent = (event: MouseEvent | TouchEvent) => { | ||
if (event.defaultPrevented) { | ||
return; | ||
} | ||
|
||
const eventTarget = event.target as HTMLElement; | ||
if ( | ||
(target && !target.contains(eventTarget)) || | ||
(nodeRef && !nodeRef.contains(eventTarget)) | ||
(!!target && !target.contains(eventTarget)) || | ||
(childElementRef.current && | ||
!childElementRef.current.contains(eventTarget)) | ||
) { | ||
onClickOutside(event); | ||
} | ||
}; | ||
|
||
componentDidMount() { | ||
this.attachEventListeners(); | ||
} | ||
|
||
componentDidUpdate(prevProps: ClickOutsideListenerProps) { | ||
const { mouseEvent, touchEvent } = this.props; | ||
if ( | ||
prevProps.mouseEvent !== mouseEvent || | ||
prevProps.touchEvent !== touchEvent | ||
) { | ||
this.removeEventListeners(prevProps.mouseEvent, prevProps.touchEvent); | ||
this.attachEventListeners(); | ||
} | ||
} | ||
|
||
componentWillUnmount() { | ||
const { mouseEvent, touchEvent } = this.props; | ||
this.removeEventListeners(mouseEvent, touchEvent); | ||
} | ||
|
||
private attachEventListeners = () => { | ||
const { mouseEvent, touchEvent } = this.props; | ||
typeof mouseEvent === 'string' && | ||
document.addEventListener(mouseEvent, this.handleEvent); | ||
typeof touchEvent === 'string' && | ||
document.addEventListener(touchEvent, this.handleEvent); | ||
const resolveRef = (node: HTMLElement) => { | ||
childElementRef.current = node; | ||
// Ref for this component should pass-through to the child node | ||
setRef(ref, node); | ||
// If child has its own ref, we want to update | ||
// its ref with the newly cloned node | ||
const { ref: childRef } = children as any; | ||
setRef(childRef, node); | ||
}; | ||
|
||
private removeEventListeners = ( | ||
mouseEvent: ClickOutsideListenerProps['mouseEvent'], | ||
touchEvent: ClickOutsideListenerProps['touchEvent'] | ||
) => { | ||
useEffect(() => { | ||
typeof mouseEvent === 'string' && | ||
document.removeEventListener(mouseEvent, this.handleEvent); | ||
document.addEventListener(mouseEvent, handleEvent); | ||
typeof touchEvent === 'string' && | ||
document.removeEventListener(touchEvent, this.handleEvent); | ||
}; | ||
document.addEventListener(touchEvent, handleEvent); | ||
|
||
resolveRef = (node: HTMLElement) => { | ||
this.nodeRef = node; | ||
return () => { | ||
typeof mouseEvent === 'string' && | ||
document.removeEventListener(mouseEvent, handleEvent); | ||
typeof touchEvent === 'string' && | ||
document.removeEventListener(touchEvent, handleEvent); | ||
}; | ||
}, [mouseEvent, touchEvent]); | ||
|
||
setRef; | ||
// If child has its own ref, we want to update | ||
// its ref with the newly cloned node | ||
const { ref } = this.props.children as any; | ||
setRef(ref, node); | ||
}; | ||
|
||
render() { | ||
const { props, resolveRef } = this; | ||
return !props.children | ||
? null | ||
: React.cloneElement(props.children as any, { | ||
ref: resolveRef | ||
}); | ||
} | ||
return !children | ||
? null | ||
: React.cloneElement(children as React.ReactElement, { ref: resolveRef }); | ||
} | ||
|
||
ClickOutsideListener.displayName = 'ClickOutsideListener'; | ||
|
||
export default React.forwardRef(ClickOutsideListener); |
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
Oops, something went wrong.