import _pt from "prop-types";
var _excluded = ["className", "children", "inline", "showDrawer", "drawerOpen"];

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import Button from '../Button/Button';
import React, { useEffect, useRef } from 'react';
import classNames from 'classnames';
import usePrevious from '../utilities/usePrevious';

/**
 * A link that triggers the visibility of a drawer
 */
export var DrawerToggle = function DrawerToggle(_ref) {
  var className = _ref.className,
      children = _ref.children,
      inline = _ref.inline,
      showDrawer = _ref.showDrawer,
      drawerOpen = _ref.drawerOpen,
      others = _objectWithoutProperties(_ref, _excluded);

  var buttonRef = useRef(null);
  var prevDrawerOpenProp = usePrevious(drawerOpen);
  useEffect(function () {
    // if drawer was open but now closed, focus the toggle
    if (prevDrawerOpenProp && !drawerOpen && buttonRef.current) {
      buttonRef.current.focus();
    }
  }, [drawerOpen]);
  var classes = classNames('ds-c-drawer__toggle', inline && 'ds-c-drawer__toggle--inline', className);
  return /*#__PURE__*/React.createElement(Button, _extends({
    className: classes,
    inputRef: function inputRef(el) {
      return buttonRef.current = el;
    },
    onClick: showDrawer,
    variation: "ghost"
  }, others), children);
};
DrawerToggle.propTypes = {
  /**
     * Determines if Drawer is open or closed.
     * This value is used to re-focus the toggle that opened the drawer when the drawer closes.
     */
  drawerOpen: _pt.bool.isRequired,

  /**
     * DrawerToggle content.
     */
  children: _pt.node.isRequired,

  /**
     * Additional classes for the toggle button anchor element.
     */
  className: _pt.string,

  /**
     * Adds `display: inline` to the DrawerToggle.
     */
  inline: _pt.bool,

  /**
     * This function is called with an id that the toggle generates.
     * It can be used in implementing the Drawer for keeping track of which drawer the toggle controls.
     */
  showDrawer: _pt.func.isRequired
};
export default DrawerToggle;