@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
30 lines (29 loc) • 1.32 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePopupTarget = void 0;
const common_1 = require("@workday/canvas-kit-react/common");
const usePopupModel_1 = require("./usePopupModel");
/**
* Adds the necessary props to a {@link PopupTarget Popup.Target} subcomponent.
*/
exports.usePopupTarget = (0, common_1.createElemPropsHook)(usePopupModel_1.usePopupModel)(({ events, state }, ref) => {
const elementRef = (0, common_1.useForkRef)(ref, state.targetRef);
return {
ref: elementRef,
onClick: (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 instanceof Element)) {
state.targetRef.current = event.currentTarget;
}
if (state.visibility !== 'hidden') {
events.hide(event);
}
else {
events.show(event);
}
},
};
});
;