@workday/canvas-kit-labs-react-menu
Version:
A container for navigation or action items
200 lines (199 loc) • 8.52 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import styled from '@emotion/styled';
import { colors, commonColors, iconColors, typeColors, spacing, } from '@workday/canvas-kit-react-core';
import type from '@workday/canvas-kit-labs-react-core';
import { SystemIcon } from '@workday/canvas-kit-react-icon';
var Item = styled('li')(__assign(__assign({}, type.body2), { padding: spacing.xxs + " " + spacing.s, height: spacing.xl, boxSizing: 'border-box', cursor: 'pointer', color: colors.blackPepper300, display: 'flex', flexDirection: 'row', alignItems: 'center', transition: 'background-color 80ms, color 80ms', '&:focus': {
outline: 'none',
} }), function (_a) {
var _b;
var isFocused = _a.isFocused, isDisabled = _a.isDisabled;
if (!isFocused && !isDisabled) {
return {
backgroundColor: 'inherit',
'&:hover': {
backgroundColor: commonColors.hoverBackground,
color: colors.blackPepper300,
'.wd-icon-fill': {
fill: iconColors.hover,
},
},
};
}
else if (isDisabled && !isFocused) {
return {
color: colors.licorice100,
cursor: 'default',
'&:hover .wd-icon-fill': {
fill: iconColors.disabled,
},
};
}
else {
return _b = {
backgroundColor: isDisabled ? colors.blueberry200 : commonColors.focusBackground,
color: typeColors.inverse,
'span .wd-icon-fill': {
fill: iconColors.inverse,
}
},
_b["[data-whatinput='mouse'] &,\n [data-whatinput='touch'] &,\n [data-whatinput='pointer'] &"] = {
backgroundColor: 'inherit',
color: colors.blackPepper300,
'&:hover': {
backgroundColor: commonColors.hoverBackground,
'.wd-icon-fill': {
fill: iconColors.hover,
},
},
'.wd-icon-fill': {
fill: iconColors.standard,
},
},
_b;
}
});
var LabelContainer = styled('span')({
flex: '1 1 auto',
overflow: 'hidden',
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
});
var Divider = styled('hr')({
display: 'block',
height: 1,
border: 0,
borderTop: "1px solid " + colors.soap400,
margin: spacing.xxs + " 0",
});
var iconSize = 24;
var iconPadding = 16;
var StyledSystemIcon = styled(SystemIcon)({
minWidth: iconSize + iconPadding,
});
var SecondaryStyledSystemIcon = styled(SystemIcon)({
display: "flex",
minWidth: iconSize + iconPadding,
justifyContent: "flex-end",
});
var iconProps = null;
var secondaryIconProps = null;
var setIconProps = function (icon, isDisabled, isFocused) {
if (!icon) {
return null;
}
var props = {
icon: icon,
size: iconSize,
};
if (isDisabled) {
props = __assign(__assign({}, props), { fillHover: iconColors.disabled, fill: iconColors.disabled });
}
if (isFocused) {
props = __assign(__assign({}, props), { fillHover: iconColors.inverse });
}
return props;
};
var scrollIntoViewIfNeeded = function (elem, centerIfNeeded) {
if (centerIfNeeded === void 0) { centerIfNeeded = true; }
var parent = elem.parentElement;
if (elem && parent) {
var parentComputedStyle = window.getComputedStyle(parent, null), parentBorderTopWidth = parseInt(parentComputedStyle.getPropertyValue('border-top-width'), 10), parentBorderLeftWidth = parseInt(parentComputedStyle.getPropertyValue('border-left-width'), 10), overTop = elem.offsetTop - parent.offsetTop < parent.scrollTop, overBottom = elem.offsetTop - parent.offsetTop + elem.clientHeight - parentBorderTopWidth >
parent.scrollTop + parent.clientHeight, overLeft = elem.offsetLeft - parent.offsetLeft < parent.scrollLeft, overRight = elem.offsetLeft - parent.offsetLeft + elem.clientWidth - parentBorderLeftWidth >
parent.scrollLeft + parent.clientWidth, alignWithTop = overTop && !overBottom;
if ((overTop || overBottom) && centerIfNeeded) {
parent.scrollTop =
elem.offsetTop -
parent.offsetTop -
parent.clientHeight / 2 -
parentBorderTopWidth +
elem.clientHeight / 2;
}
if ((overLeft || overRight) && centerIfNeeded) {
parent.scrollLeft =
elem.offsetLeft -
parent.offsetLeft -
parent.clientWidth / 2 -
parentBorderLeftWidth +
elem.clientWidth / 2;
}
if ((overTop || overBottom || overLeft || overRight) && !centerIfNeeded) {
elem.scrollIntoView(alignWithTop);
}
}
};
var MenuItem = (function (_super) {
__extends(MenuItem, _super);
function MenuItem() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.ref = React.createRef();
_this.componentDidUpdate = function (prevProps) {
if (!prevProps.isFocused && _this.props.isFocused) {
if (_this.ref.current) {
scrollIntoViewIfNeeded(_this.ref.current);
}
}
};
_this.handleClick = function (event) {
if (_this.props.isDisabled) {
return;
}
if (_this.props.onClick) {
_this.props.onClick(event);
}
};
return _this;
}
MenuItem.prototype.render = function () {
var _a = this.props, onClick = _a.onClick, children = _a.children, id = _a.id, icon = _a.icon, secondaryIcon = _a.secondaryIcon, hasDivider = _a.hasDivider, isDisabled = _a.isDisabled, isFocused = _a.isFocused, role = _a.role, elemProps = __rest(_a, ["onClick", "children", "id", "icon", "secondaryIcon", "hasDivider", "isDisabled", "isFocused", "role"]);
iconProps = setIconProps(icon, isDisabled, isFocused);
secondaryIconProps = setIconProps(secondaryIcon, isDisabled, isFocused);
return (React.createElement(React.Fragment, null,
hasDivider && React.createElement(Divider, null),
React.createElement(Item, __assign({ ref: this.ref, tabIndex: -1, id: id, role: role, onClick: this.handleClick, "aria-disabled": !!isDisabled, isDisabled: !!isDisabled, isFocused: !!isFocused }, elemProps),
icon && iconProps && React.createElement(StyledSystemIcon, __assign({}, iconProps)),
React.createElement(LabelContainer, null, children),
secondaryIcon && secondaryIconProps && (React.createElement(SecondaryStyledSystemIcon, __assign({}, secondaryIconProps))))));
};
return MenuItem;
}(React.Component));
MenuItem.defaultProps = {
shouldClose: true,
role: 'menuitem',
};
export default MenuItem;