@octopusdeploy/design-system-components
Version:
The design systems component library.
135 lines (134 loc) • 6.61 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;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Chip = Chip;
const jsx_runtime_1 = require("react/jsx-runtime");
const css_1 = require("@emotion/css");
const Chip_1 = __importDefault(require("@mui/material/Chip"));
const design_system_tokens_1 = require("@octopusdeploy/design-system-tokens");
const React = __importStar(require("react"));
const Tooltip_1 = require("../Tooltip");
function Chip(props) {
const rootMarginClass = props.noMargin ? chipStyles.rootNoMargin : chipStyles.rootMargin;
const classes = {
root: (0, css_1.cx)(chipStyles.root, rootMarginClass),
label: labelStyles.root,
};
const rootBackgroundColorStyle = props.backgroundColor ? { backgroundColor: props.backgroundColor } : undefined;
const rootLabelColorStyle = props.labelColor ? { color: props.labelColor } : undefined;
const rootOpacityStyle = props.opacity ? { opacity: props.opacity } : undefined;
const rootBorderColorStyle = props.borderColor ? { borderColor: props.borderColor } : undefined;
const rootStyleProps = rootBackgroundColorStyle || rootLabelColorStyle || rootOpacityStyle || rootBorderColorStyle
? {
style: {
...rootBackgroundColorStyle,
...rootLabelColorStyle,
...rootOpacityStyle,
...rootBorderColorStyle,
},
}
: {};
//We can't just pass role through as undefined as it's a primitive aria attribute which will end up overriding anything material-ui provides
//we therefore omit this prop when it hasn't been specified to allow the role to be changed if needed.
const ariaProps = props.accessibilityRole ? { role: props.accessibilityRole } : {};
const styledLabel = (0, jsx_runtime_1.jsx)("span", { className: (0, css_1.cx)(labelStyles.text, { [labelWithLineThroughStyles]: props.markAsRemoved }, { [labelWithLimitedWidthStyles]: !props.fullWidth }), children: props.label });
// The tooltip injects a div into this chip which can affect positioning of the label.
// Choosing "flex" as the display (or any non-block/inline display) for this div helps achieve proper sizing, vertical centering and overflow for the label.
// This aligns with the flex on labelStyles.root when there is no tooltip.
const labelWithTooltip = props.tooltipContent ? ((0, jsx_runtime_1.jsx)(Tooltip_1.Tooltip, { content: props.tooltipContent, display: "flex", children: styledLabel })) : (styledLabel);
return ((0, jsx_runtime_1.jsx)(Chip_1.default, { component: ChipDiv, classes: classes, icon: props.icon, label: labelWithTooltip, onClick: props.onClick, tabIndex: props.tabIndex, variant: props.variant, onDelete: props.onDelete, deleteIcon: props.deleteIcon, disableRipple: true, ...rootStyleProps, ...ariaProps }));
}
/**
* The MUI Chip does not currently support the disableRipple prop.
* If we augment the props type to add it, it does get respected by ButtonBase and disables the ripple.
* However the Chip component incorrectly passes it on to the underlying div, which React will complain about.
* This ChipDiv is just a simple wrapper that drops the disableRipple prop, passing everything else to the div.
*/
const ChipDiv = React.forwardRef(({ disableRipple, ...props }, ref) => {
return (0, jsx_runtime_1.jsx)("div", { ref: ref, ...props });
});
ChipDiv.displayName = "ChipDiv";
const chipStyles = {
root: (0, css_1.css)({
"&.MuiChip-root": {
lineHeight: "1.5rem",
height: "1.5rem", // Specify height explicitly, or svg icons can push the container height out (eg. if you display
// multiple chips in a row where some do/don't contain an avatar. E.g. environments list).
maxWidth: "100%",
display: "inline-flex",
backgroundColor: design_system_tokens_1.themeTokens.color.portal.chip.background,
verticalAlign: "middle",
// This color affects the text color of the label through inheritance
// We have to set it here so that it can be overridden through the style prop on the _root_ chip element from material-ui
// Since this is the only element that we can affect using the style prop
opacity: 1,
"&:focus-within": {
boxShadow: design_system_tokens_1.themeTokens.shadow.focused,
},
},
}),
rootMargin: (0, css_1.css)({
"&.MuiChip-root": {
margin: design_system_tokens_1.space[4],
},
}),
rootNoMargin: (0, css_1.css)({
"&.MuiChip-root": {
margin: 0,
},
}),
};
const labelStyles = {
root: (0, css_1.css)({
flex: 1,
display: "flex",
}),
text: (0, css_1.css)({
fontSize: "0.75rem",
fontWeight: "normal",
textOverflow: "ellipsis",
whiteSpace: "nowrap",
overflow: "hidden",
}),
};
const labelWithLineThroughStyles = (0, css_1.css)({
textDecoration: "line-through",
});
const labelWithLimitedWidthStyles = (0, css_1.css)({
maxWidth: "100%",
});