Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/ui / src / components / features / ClickBoundary / ClickBoundary.js
Size: Mime:
"use strict";

var __rest = this && this.__rest || function (s, e) {
  var t = {};

  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];

  if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]];
  return t;
};

var __importDefault = this && this.__importDefault || function (mod) {
  return mod && mod.__esModule ? mod : {
    "default": mod
  };
};

Object.defineProperty(exports, "__esModule", {
  value: true
});

const react_1 = __importDefault(require("react"));

const classnames_1 = __importDefault(require("classnames"));

const exotic_1 = require("exotic");

const _deps_1 = require("./_deps"); // for the sake of seeing the props added
// const ariaLabel = props['aria-label'] || ''
// props['aria-label'] =
//   ariaLabel +
//   ' when this element is active and you click outside of it, it loses focus in a visual way'


class ClickBoundary extends react_1.default.PureComponent {
  constructor() {
    super(...arguments);
    this.isListening = false;
    this.isActive = false;

    this.setRef = dom => {
      this.dom = dom;

      if (exotic_1.isFunction(this.props.innerRef)) {
        this.props.innerRef(dom);
      }
    };

    this.handleMouseEnter = event => {
      this.isActive = false; // this.state.focus()
      // console.log(
      //   'handleMouseEnter event:',
      //   'isActive:',
      //   this.state.isActive,
      //   'isFocused:',
      //   this.state.isFocused
      // )
    };

    this.handleMouseLeave = event => {
      this.isActive = true; // this.state.blur()
      // this.state.setActive(true)
      // console.debug(
      //   '[ClickBoundary] handleMouseLeave',
      //   'isActive',
      //   this.state.isActive,
      //   'isFocused:',
      //   this.state.isFocused
      // )

      this.subscribeListener();
    };

    this.handleClick = event => {
      // console.debug('[ClickBoundary] handleClick ' + this.props.className)
      _deps_1.onClick(event, this.props, this.dom);

      this.isActive = false; // this.state.setActive(false)

      this.unSubscribeListener();
    };
  }
  /**
   * only happens in browser, `willMount` in server
   */


  subscribeListener() {
    if (this.isActive && !this.isListening) {
      // console.debug('[click boundary] subscribing')
      // @note - document.body
      window.addEventListener('click', this.handleClick);
      window.addEventListener('ontouchstart', this.handleClick);
      this.isListening = true;
    }
  }
  /**
   * only happens browser
   */


  unSubscribeListener() {
    if (this.isListening) {
      // console.debug('[click boundary] unsubscribing')
      // remember to remove all events to avoid memory leaks
      window.removeEventListener('click', this.handleClick);
      window.removeEventListener('ontouchstart', this.handleClick);
      this.isListening = false;
    }
  }

  renderChildren(children, attributes) {
    const onChild = child => {
      // 2. safety
      if (typeof child === 'string' || typeof child === 'number') {
        return child;
      } // 3. merge props


      const props = Object.assign({}, attributes, child.props);
      return react_1.default.cloneElement(child, props);
    };

    return react_1.default.Children.map(children, onChild);
  }

  render() {
    const _a = this.props,
          {
      className,
      children,
      onClickOutside,
      nowrap
    } = _a,
          remainingProps = __rest(_a, ["className", "children", "onClickOutside", "nowrap"]);

    const attributes = Object.assign({}, remainingProps, {
      isActive: this.isActive,
      onMouseEnter: this.handleMouseEnter,
      onMouseLeave: this.handleMouseLeave,
      className: classnames_1.default('click-boundary', className)
    });

    if (nowrap) {
      return this.renderChildren(children, attributes);
    } else {
      return react_1.default.createElement("div", Object.assign({}, attributes, {
        ref: this.setRef
      }), this.props.children);
    }
  }

}

exports.ClickBoundary = ClickBoundary;
exports.default = ClickBoundary; //# sourceMappingURL=ClickBoundary.js.map