@octopusdeploy/design-system-components
Version:
The design systems component library.
110 lines (109 loc) • 7.97 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.buttonSizes = exports.buttonImportanceLevels = exports.Button = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const css_1 = require("@emotion/css");
const design_system_icons_1 = require("@octopusdeploy/design-system-icons");
const react_1 = __importStar(require("react"));
const useIsPromisePending_1 = require("../../hooks/useIsPromisePending");
const routing_1 = require("../../routing");
const buttonStyles_1 = require("./buttonStyles");
exports.Button = (0, react_1.forwardRef)((props, ref) => {
const title = props.title ?? props.label;
if (isLinkButtonProps(props)) {
return ((0, jsx_runtime_1.jsx)(LinkButton
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
, {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
ref: ref, label: props.label, href: props.href, onClick: props.onClick, importance: props.importance, size: props.size, disabled: props.disabled, icon: props.icon, external: props.external, isDownload: props.isDownload, opensInNewTab: props.opensInNewTab, title: title, accessibleName: props.accessibleName, tabIndex: props.tabIndex }));
}
return ((0, jsx_runtime_1.jsx)(BasicButton
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
, {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
ref: ref, onClick: props.onClick, label: props.label, importance: props.importance, size: props.size, disabled: props.disabled, icon: props.icon, showExpandCollapseIcon: props.showExpandCollapseIcon, title: title, accessibleName: props.accessibleName, isSubmit: props.isSubmit, tabIndex: props.tabIndex, autoFocus: props.autoFocus, formId: props.formId, "aria-controls": props["aria-controls"], "aria-haspopup": props["aria-haspopup"], "aria-expanded": props["aria-expanded"] }));
});
exports.Button.displayName = "Button";
function isLinkButtonProps(props) {
return "href" in props && !!props.href;
}
const LinkButton = (0, react_1.forwardRef)(({ external, isDownload, disabled = false, size = "medium", onClick, href, importance, label, title, accessibleName, icon, tabIndex, ...props }, ref) => {
const Link = (0, routing_1.useOctopusLinkComponent)();
const buttonStyles = (0, buttonStyles_1.getButtonStyles)(importance, disabled, size, !!label);
const buttonIconStyles = (0, buttonStyles_1.getButtonIconStyles)(size);
const opensInNewTab = external ? true : props.opensInNewTab;
const showExternalLinkIcon = opensInNewTab && !isDownload;
const renderIcon = isDownload ? ((0, jsx_runtime_1.jsx)("span", { className: buttonIconStyles, children: (0, jsx_runtime_1.jsx)(design_system_icons_1.ArrowDownToSquareIcon, { size: 20 }) })) : (icon && (0, jsx_runtime_1.jsx)("span", { className: buttonIconStyles, children: icon }));
const handleOnClick = react_1.default.useCallback((event) => {
onClick?.(event);
}, [onClick]);
const linkContent = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [renderIcon, label && (0, jsx_runtime_1.jsx)("span", { className: buttonStyles_1.buttonLabelStyles, children: label }), showExternalLinkIcon && ((0, jsx_runtime_1.jsx)("span", { className: buttonIconStyles, children: (0, jsx_runtime_1.jsx)(design_system_icons_1.ArrowUpRightFromSquareIcon, { size: 20 }) }))] }));
if (disabled) {
return ((0, jsx_runtime_1.jsx)("a", { ref: ref, role: "link", className: buttonStyles, "aria-disabled": true, title: title, children: linkContent }));
}
const target = opensInNewTab ? "_blank" : "_self";
if (external) {
const stringHref = typeof href === "string" ? href : undefined;
return ((0, jsx_runtime_1.jsx)("a", { ref: ref, className: buttonStyles, target: target, rel: "noreferrer", href: stringHref, onClick: handleOnClick, title: title, tabIndex: tabIndex, children: linkContent }));
}
return ((0, jsx_runtime_1.jsx)(Link, { ref: ref, className: buttonStyles, target: target, href: href, onClick: handleOnClick, "aria-label": accessibleName, title: title, tabIndex: tabIndex, children: linkContent }));
});
LinkButton.displayName = "LinkButton";
const BasicButton = (0, react_1.forwardRef)(({ icon, importance, size = "medium", onClick, disabled = false, label, title, accessibleName, "aria-controls": ariaControls, "aria-haspopup": ariaHasPopup, "aria-expanded": ariaExpanded, showExpandCollapseIcon, isSubmit, tabIndex, autoFocus, formId, }, ref) => {
const buttonStyles = (0, buttonStyles_1.getButtonStyles)(importance, disabled, size, !!label);
const buttonIconStyles = (0, buttonStyles_1.getButtonIconStyles)(size);
// FFT TODO Temporary - need to update every instance to set menuIcon explicitly
const showMenuIcon = ariaExpanded ? (showExpandCollapseIcon ?? true) : false;
const caretIcon = (0, jsx_runtime_1.jsx)(design_system_icons_1.CaretDownIcon, { size: 20 });
const [onClickPromise, setOnClickPromise] = (0, react_1.useState)();
const isOnClickPromisePending = (0, useIsPromisePending_1.useIsPromisePending)(onClickPromise);
const handleClick = (0, react_1.useCallback)(async (event) => {
const result = onClick?.(event);
if (result instanceof Promise) {
setOnClickPromise(result);
}
}, [onClick]);
return ((0, jsx_runtime_1.jsxs)("button", { ref: ref, type: isSubmit ? "submit" : "button", className: buttonStyles, onClick: handleClick, disabled: disabled || isOnClickPromisePending, title: title ?? label, tabIndex: tabIndex, autoFocus: autoFocus, form: formId, "aria-label": accessibleName, "aria-controls": ariaControls, "aria-haspopup": ariaHasPopup, "aria-expanded": ariaExpanded, children: [icon && (0, jsx_runtime_1.jsx)("span", { className: buttonIconStyles, children: icon }), label && (0, jsx_runtime_1.jsx)("span", { className: buttonStyles_1.buttonLabelStyles, children: label }), label && showMenuIcon && (0, jsx_runtime_1.jsx)("span", { className: (0, css_1.cx)(buttonIconStyles, buttonMenuStyles, { [buttonMenuExpandedStyles]: ariaExpanded === "true" }), children: caretIcon })] }));
});
BasicButton.displayName = "BasicButton";
exports.buttonImportanceLevels = ["primary", "secondary", "tertiary", "destructive", "quiet", "loud", "neutral", "ghost"];
exports.buttonSizes = ["medium", "small"];
const buttonMenuStyles = (0, css_1.css)({
transition: "transform 150ms ease-in-out",
});
const buttonMenuExpandedStyles = (0, css_1.css)({
transform: "rotate(-180deg)",
});