@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
28 lines (25 loc) • 1.13 kB
text/typescript
import {createElemPropsHook, useForkRef} from '@workday/canvas-kit-react/common';
import {usePopupModel} from './usePopupModel';
/**
* Adds the necessary props to a {@link PopupTarget Popup.Target} subcomponent.
*/
export const usePopupTarget = createElemPropsHook(usePopupModel)(({events, state}, ref) => {
const elementRef = useForkRef(ref, state.targetRef);
return {
ref: elementRef,
onClick: (event: React.MouseEvent) => {
// 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 as React.MutableRefObject<any>).current = event.currentTarget;
}
if (state.visibility !== 'hidden') {
events.hide(event);
} else {
events.show(event);
}
},
};
});