@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
124 lines (123 loc) • 7.63 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 (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MenuItem = exports.useMenuItem = exports.useMenuItemFocus = exports.useMenuItemArrowReturn = exports.StyledMenuItem = exports.menuItemStencil = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const React = __importStar(require("react"));
const canvas_kit_styling_1 = require("@workday/canvas-kit-styling");
const canvas_tokens_web_1 = require("@workday/canvas-tokens-web");
const common_1 = require("@workday/canvas-kit-react/common");
const icon_1 = require("@workday/canvas-kit-react/icon");
const tooltip_1 = require("@workday/canvas-kit-react/tooltip");
const layout_1 = require("@workday/canvas-kit-react/layout");
const collection_1 = require("@workday/canvas-kit-react/collection");
const useMenuModel_1 = require("./useMenuModel");
exports.menuItemStencil = (0, canvas_kit_styling_1.createStencil)({
parts: {
text: 'menu-item-text',
icon: 'menu-item-icon',
selected: 'menu-item-selected',
},
base: { name: "3gs3z7", styles: "box-sizing:border-box;font-family:var(--cnvs-sys-font-family-default);font-weight:var(--cnvs-sys-font-weight-normal);line-height:var(--cnvs-sys-line-height-subtext-large);font-size:var(--cnvs-sys-font-size-subtext-large);letter-spacing:var(--cnvs-base-letter-spacing-150);display:flex;align-items:center;width:100%;gap:var(--cnvs-sys-space-x4);padding:var(--cnvs-sys-space-x2) var(--cnvs-sys-space-x4);cursor:pointer;color:var(--cnvs-sys-color-fg-default);border-width:0;text-align:start;transition:background-color 80ms, color 80ms;background-color:inherit;min-height:var(--cnvs-sys-space-x10);overflow-wrap:anywhere;--color-system-icon-99ce3e:currentcolor;& :where([data-part=\"menu-item-selected\"]){transition:opacity 80ms linear;opacity:var(--cnvs-sys-opacity-zero);}&:where(:has(span)){display:flex;}&[aria-selected=true]{color:var(--cnvs-brand-primary-dark);background-color:var(--cnvs-brand-primary-lightest);& :where([data-part=\"menu-item-selected\"]){opacity:var(--cnvs-sys-opacity-full);}&:where(.focus, :focus){--color-system-icon-99ce3e:var(--cnvs-brand-primary-accent);outline:none;background-color:var(--cnvs-brand-primary-base);color:var(--color-system-icon-99ce3e);}}&:is(.hover, :hover){color:var(--cnvs-sys-color-fg-strong);background-color:var(--cnvs-brand-neutral-lightest);}&:is(.focus, :focus){color:var(--cnvs-brand-primary-accent);background-color:var(--cnvs-brand-primary-base);outline:0.125rem solid transparent;outline-offset:-0.125rem;}&:is(:disabled, [aria-disabled=true]){color:var(--cnvs-sys-color-text-disabled);cursor:default;&:where(.focus, :focus){background-color:var(--cnvs-brand-primary-light);}}& :where([data-part=\"menu-item-text\"]){flex-grow:1;align-self:center;}& :where([data-part=\"menu-item-icon\"]){align-self:start;}" }
}, "menu-item-0e1e84");
const MenuItemIcon = (elemProps) => {
return (0, jsx_runtime_1.jsx)(icon_1.SystemIcon, { ...exports.menuItemStencil.parts.icon, ...elemProps });
};
const MenuItemText = (0, common_1.createComponent)('span')({
Component: ({ ...elemProps }, ref, Element) => {
return (0, jsx_runtime_1.jsx)(Element, { ref: ref, ...exports.menuItemStencil.parts.text, ...elemProps });
},
});
exports.StyledMenuItem = (0, common_1.createComponent)('button')({
displayName: 'MenuItem',
Component: ({ children, isDisabled, ...elemProps }, ref, Element) => {
return ((0, jsx_runtime_1.jsx)(Element, { ref: ref, "aria-disabled": isDisabled, ...(0, layout_1.mergeStyles)(elemProps, (0, exports.menuItemStencil)({})), children: typeof children === 'string' ? (0, jsx_runtime_1.jsx)(MenuItemText, { children: children }) : children }));
},
});
exports.useMenuItemArrowReturn = (0, common_1.createElemPropsHook)(useMenuModel_1.useMenuModel)(model => {
return {
onKeyDown(event) {
const styles = getComputedStyle(event.currentTarget);
if (event.key === 'ArrowLeft' && styles.direction === 'ltr' && model.UNSTABLE_parentModel) {
model.events.hide(event);
}
},
};
});
exports.useMenuItemFocus = (0, common_1.createElemPropsHook)(useMenuModel_1.useMenuModel)((model, ref, elemProps = { 'data-id': '' }) => {
const { localRef, elementRef } = (0, common_1.useLocalRef)(ref);
const id = elemProps['data-id'];
// focus on the item with the cursor
React.useLayoutEffect(() => {
if (model.state.mode === 'single') {
if ((0, collection_1.isCursor)(model.state, id)) {
// delay focus changes to allow PopperJS to position
requestAnimationFrame(() => {
var _a;
(_a = localRef.current) === null || _a === void 0 ? void 0 : _a.focus();
});
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [id, localRef, model.state.cursorId, model.state.mode]);
return {
ref: elementRef,
className: (0, collection_1.isCursor)(model.state, elemProps['data-id']) ? 'focus' : undefined,
};
});
function hideParent(model) {
if (model.UNSTABLE_parentModel) {
model.UNSTABLE_parentModel.events.hide();
hideParent(model.UNSTABLE_parentModel);
}
}
exports.useMenuItem = (0, common_1.composeHooks)((0, common_1.createElemPropsHook)(useMenuModel_1.useMenuModel)(model => {
return {
role: 'menuitem',
onMouseDown(event) {
model.events.goTo({ id: event.currentTarget.getAttribute('data-id') });
},
onClick: model.state.mode === 'single'
? (event) => {
// only hide if the item isn't disabled
if (event.currentTarget.getAttribute('aria-disabled') !== 'true') {
model.events.hide(event);
hideParent(model);
}
}
: undefined,
};
}), exports.useMenuItemFocus, exports.useMenuItemArrowReturn, collection_1.useListItemSelect, collection_1.useListItemRovingFocus, collection_1.useListItemRegister);
exports.MenuItem = (0, common_1.createSubcomponent)('button')({
displayName: 'Menu.Item',
modelHook: useMenuModel_1.useMenuModel,
elemPropsHook: exports.useMenuItem,
subComponents: {
Icon: MenuItemIcon,
Text: MenuItemText,
},
})(({ children, ...elemProps }, Element) => {
return ((0, jsx_runtime_1.jsx)(tooltip_1.OverflowTooltip, { placement: "left", children: (0, jsx_runtime_1.jsx)(exports.StyledMenuItem, { as: Element, ...elemProps, children: children }) }));
});