Repository URL to install this package:
|
Version:
0.9.5 ▾
|
import { isFunction, isNil, isObj } from 'exotic'
import { ClickBoundaryProps, ClickBoundaryRef } from './typings'
export const IS_BROWSER = typeof window === 'object'
export function isClickOutsideElement(event: Event, dom: Element): boolean {
// get direct click event target
const { target } = event
// get container that we'll wait to be clicked outside
const container = dom
// if target is container - container was not clicked outside
// if container contains clicked target - click was not outside of it
if (
target !== container &&
isObj(container) &&
isFunction(container.contains) &&
!container.contains(target)
) {
// clicked outside - fire callback
return true
} else {
return false
}
}
// toBoolean
export function isClickInsideElement(event: Event, dom: Element): boolean {
if (isNil(dom) === true) {
return false
} else {
return isClickOutsideElement(event, dom) === false
}
}
export function toChildren(props: ClickBoundaryProps, ref: ClickBoundaryRef) {
return isFunction(props.children)
? props.children(props, ref)
: props.children
}
export function onClick(
event: Event,
props: ClickBoundaryProps,
dom: ClickBoundaryRef
) {
// if there is no proper callback - no point of checking
if (isFunction(props.onClickOutside) === false) {
return
}
if (isClickOutsideElement(event, dom) === true) {
props.onClickOutside(event, props, dom)
}
}