@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
32 lines (31 loc) • 1.45 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePopupTargetContext = void 0;
const common_1 = require("@workday/canvas-kit-react/common");
const usePopupModel_1 = require("./usePopupModel");
/**
* Adds the necessary props to a `Target` component. Used by the Popup.Target subcomponent.
*/
exports.usePopupTargetContext = (0, common_1.createElemPropsHook)(usePopupModel_1.usePopupModel)(({ events, state }, ref) => {
const elementRef = (0, common_1.useForkRef)(ref, state.targetRef);
return {
ref: elementRef,
onContextMenu: (event) => {
// If we're wrapping a target component that doesn't handle ref forwarding, update the
// `state.targetRef` manually. This ensures that custom target components don't need to handle
// ref forwarding since ref forwarding is only really needed to programmatically open popups
// around a target _before_ a user clicks. In that rare case, ref forwarding is required.
if (!state.targetRef.current) {
state.targetRef.current = event.currentTarget;
}
if (state.visibility !== 'hidden') {
events.hide(event);
}
else {
events.show(event);
}
// Prevent the default context menu from showing to avoid double menus
event.preventDefault();
},
};
});
;