"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PanelBody = void 0;
exports.UnforwardedPanelBody = UnforwardedPanelBody;
exports.default = void 0;
var _clsx = _interopRequireDefault(require("clsx"));
var _compose = require("@wordpress/compose");
var _element = require("@wordpress/element");
var _icons = require("@wordpress/icons");
var _button = _interopRequireDefault(require("../button"));
var _icon = _interopRequireDefault(require("../icon"));
var _utils = require("../utils");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const noop = () => {};
function UnforwardedPanelBody(props, ref) {
  const {
    buttonProps = {},
    children,
    className,
    icon,
    initialOpen,
    onToggle = noop,
    opened,
    title,
    scrollAfterOpen = true
  } = props;
  const [isOpened, setIsOpened] = (0, _utils.useControlledState)(opened, {
    initial: initialOpen === undefined ? true : initialOpen,
    fallback: false
  });
  const nodeRef = (0, _element.useRef)(null);

  // Defaults to 'smooth' scrolling
  // https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
  const scrollBehavior = (0, _compose.useReducedMotion)() ? 'auto' : 'smooth';
  const handleOnToggle = event => {
    event.preventDefault();
    const next = !isOpened;
    setIsOpened(next);
    onToggle(next);
  };

  // Ref is used so that the effect does not re-run upon scrollAfterOpen changing value.
  const scrollAfterOpenRef = (0, _element.useRef)();
  scrollAfterOpenRef.current = scrollAfterOpen;
  // Runs after initial render.
  (0, _utils.useUpdateEffect)(() => {
    if (isOpened && scrollAfterOpenRef.current && nodeRef.current?.scrollIntoView) {
      /*
       * Scrolls the content into view when visible.
       * This improves the UX when there are multiple stacking <PanelBody />
       * components in a scrollable container.
       */
      nodeRef.current.scrollIntoView({
        inline: 'nearest',
        block: 'nearest',
        behavior: scrollBehavior
      });
    }
  }, [isOpened, scrollBehavior]);
  const classes = (0, _clsx.default)('components-panel__body', className, {
    'is-opened': isOpened
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: classes,
    ref: (0, _compose.useMergeRefs)([nodeRef, ref]),
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(PanelBodyTitle, {
      icon: icon,
      isOpened: Boolean(isOpened),
      onClick: handleOnToggle,
      title: title,
      ...buttonProps
    }), typeof children === 'function' ? children({
      opened: Boolean(isOpened)
    }) : isOpened && children]
  });
}
const PanelBodyTitle = (0, _element.forwardRef)(({
  isOpened,
  icon,
  title,
  ...props
}, ref) => {
  if (!title) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("h2", {
    className: "components-panel__body-title",
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_button.default, {
      __next40pxDefaultSize: true,
      className: "components-panel__body-toggle",
      "aria-expanded": isOpened,
      ref: ref,
      ...props,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
        "aria-hidden": "true",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.default, {
          className: "components-panel__arrow",
          icon: isOpened ? _icons.chevronUp : _icons.chevronDown
        })
      }), title, icon && /*#__PURE__*/(0, _jsxRuntime.jsx)(_icon.default, {
        icon: icon,
        className: "components-panel__icon",
        size: 20
      })]
    })
  });
});
const PanelBody = exports.PanelBody = (0, _element.forwardRef)(UnforwardedPanelBody);
var _default = exports.default = PanelBody;
//# sourceMappingURL=body.js.map