Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / @skava/ui   js

Repository URL to install this package:

Version: 2.8.8 

/ src / components / features / ClickBoundary / _deps.ts

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) {
    console.warn('why are you using ClickBoundary without props.onClickOutside')
    return
  }

  /**
   * @todo @deprecated do not pass all 3
   */
  if (isClickOutsideElement(event, dom) === true) {
    props.onClickOutside(event, props, dom)
  }
}