@helpscout/hsds-react
Version:
React component library for Help Scout's Design System
164 lines (132 loc) • 6.76 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _getValidProps = _interopRequireDefault(require("@helpscout/react-utils/dist/getValidProps"));
var _Page = _interopRequireWildcard(require("./Page"));
var _Page2 = require("./Page.css");
var _Page3 = _interopRequireDefault(require("./Page.StickyActions"));
var _classnames = _interopRequireDefault(require("classnames"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function noop() {}
var ActionContent = function ActionContent(props) {
var innerRef = props.innerRef,
isResponsive = props.isResponsive,
primary = props.primary,
serious = props.serious,
secondary = props.secondary,
direction = props.direction,
zIndex = props.zIndex,
withStickyWrapper = props.withStickyWrapper,
className = props.className,
rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["innerRef", "isResponsive", "primary", "serious", "secondary", "direction", "zIndex", "withStickyWrapper", "className"]);
var componentClassNames = (0, _classnames.default)('c-PageActions', direction && "is-" + direction, primary && 'withPrimary', secondary && 'withSecondary', serious && 'withSerious', className);
var content = /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Page2.ActionsUI, (0, _extends2.default)({}, (0, _getValidProps.default)(rest), {
"data-cy": "PageActionsContent",
className: componentClassNames,
ref: withStickyWrapper ? innerRef : null,
withStickyWrapper: withStickyWrapper,
role: "toolbar",
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Page2.ActionsItemUI, {
className: "c-PageActions__primary",
"data-cy": "PageActionsPrimaryItemWrapper",
children: primary
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Page2.ActionsItemUI, {
className: "c-PageActions__secondary",
"data-cy": "PageActionsSecondaryItemWrapper",
children: secondary
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Page2.ActionsBlockUI, {
className: "c-PageActions__block"
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_Page2.ActionsItemUI, {
className: "c-PageActions__serious",
"data-cy": "PageActionsSeriousItemWrapper",
children: serious
})]
}));
if (!withStickyWrapper) return content;
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Page2.StickyActionsWrapperUI, {
className: "c-PageActions__stickyWrapper",
"data-cy": "PageActionsStickyWrapper",
zIndex: zIndex,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Page.default, {
isResponsive: isResponsive,
children: content
})
});
};
var PageActions = function PageActions(props) {
var stickyOffset = props.stickyOffset,
isSticky = props.isSticky,
onStickyStart = props.onStickyStart,
onStickyEnd = props.onStickyEnd;
var _useState = (0, _react.useState)(isSticky),
isStickyActive = _useState[0],
setStickyActive = _useState[1];
var _useContext = (0, _react.useContext)(_Page.PageContext),
isResponsive = _useContext.isResponsive;
var handleOnStickyStart = function handleOnStickyStart(node) {
setStickyActive(true);
onStickyStart(node);
};
var handleOnStickyStop = function handleOnStickyStop(node) {
setStickyActive(false);
onStickyEnd(node);
};
var showStickyAction = isSticky && isStickyActive;
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
"data-cy": "PageActionsWrapper",
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Page3.default, {
onStickyStart: handleOnStickyStart,
onStickyEnd: handleOnStickyStop,
offset: stickyOffset,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ActionContent, (0, _extends2.default)({}, props, {
withStickyWrapper: false,
isResponsive: isResponsive
}))
}), showStickyAction && /*#__PURE__*/(0, _jsxRuntime.jsx)(ActionContent, (0, _extends2.default)({}, props, {
withStickyWrapper: true,
isResponsive: isResponsive
}))]
});
};
PageActions.defaultProps = {
'data-cy': 'PageActions',
direction: 'right',
innerRef: noop,
onStickyStart: noop,
onStickyEnd: noop,
isResponsive: false,
isSticky: false,
stickyOffset: 10,
zIndex: 10
};
PageActions.propTypes = {
/** Custom class names to be added to the component. */
className: _propTypes.default.string,
/** The alignment of the actions. */
direction: _propTypes.default.string,
/** Stickies the Actions at the bottom of the browser. */
isSticky: _propTypes.default.bool,
/** Callback the moment the component sticks. */
onStickyStart: _propTypes.default.func,
/** Callback the moment the component stops sticking. */
onStickyEnd: _propTypes.default.func,
/** A render slot for the primary action. */
primary: _propTypes.default.any,
/** A render slot for the secondary action. */
secondary: _propTypes.default.any,
/** A render slot for the serious (probably destructive) action. */
serious: _propTypes.default.any,
/** The CSS `z-index` for when the component is sticky. */
zIndex: _propTypes.default.number,
/** Data attr for Cypress tests. */
'data-cy': _propTypes.default.string
};
var _default = PageActions;
exports.default = _default;
;