@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
26 lines (25 loc) • 1.04 kB
JavaScript
import { createElemPropsHook, useForkRef } from '@workday/canvas-kit-react/common';
import { usePopupModel } from './usePopupModel';
import React from 'react';
/**
* Adds the necessary props to a {@link Popper} component. Used by the
* {@link PopupPopper Popup.Popper} subcomponent.
*/
export const usePopupPopper = createElemPropsHook(usePopupModel)(({ state, events }, ref) => {
const elementRef = useForkRef(ref, state.stackRef);
// Create a mutable ref of the placement to keep a stable `onPlacementChange` reference.
const placementRef = React.useRef(state.placement);
placementRef.current = state.placement;
const onPlacementChange = React.useCallback((placement) => {
if (placement !== placementRef.current) {
// only update if the placement has changed
events.updatePlacement({ placement });
}
}, [events]);
return {
open: state.visibility !== 'hidden',
anchorElement: state.targetRef,
ref: elementRef,
onPlacementChange,
};
});