@atlaskit/profilecard
Version:
A React component to display a card with user information.
104 lines • 3.56 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import React, { useCallback, useEffect, useRef, useState } from 'react';
import Popup from '@atlaskit/popup';
import { layers } from '@atlaskit/theme/constants';
import { cardTriggered } from '../../util/analytics';
import { useProfileInfo } from '../../util/useProfileInfo';
import { LoadingState } from './LoadingState';
import { PopupTrigger } from './PopupTrigger';
import { ProfileCardWrapper } from './ProfileCardWrapper';
const DELAY_MS_SHOW = 800;
const DELAY_MS_HIDE = 200;
function ProfileCardTrigger({
trigger,
ariaLabelledBy,
children,
renderProfileCard,
fetchProfile,
disabledAriaAttributes,
profileCardType,
fireAnalytics,
...popupProps
}) {
var _popupProps$autoFocus;
const showDelay = trigger === 'click' ? 0 : DELAY_MS_SHOW;
const hideDelay = trigger === 'click' ? 0 : DELAY_MS_HIDE;
const showTimer = useRef(0);
const hideTimer = useRef(0);
const [visible, setVisible] = useState(false);
const {
profileData,
isLoading,
error,
getProfileData
} = useProfileInfo({
fetchUserProfile: fetchProfile
});
useEffect(() => {
return () => {
clearTimeout(showTimer.current);
clearTimeout(hideTimer.current);
};
}, []);
const hideProfilecard = useCallback(() => {
clearTimeout(showTimer.current);
clearTimeout(hideTimer.current);
hideTimer.current = window.setTimeout(() => {
setVisible(false);
}, hideDelay);
}, [hideDelay]);
const showProfilecard = useCallback(async () => {
clearTimeout(hideTimer.current);
clearTimeout(showTimer.current);
showTimer.current = window.setTimeout(() => {
if (!visible) {
getProfileData === null || getProfileData === void 0 ? void 0 : getProfileData();
setVisible(true);
if (fireAnalytics) {
fireAnalytics(cardTriggered(profileCardType, trigger));
}
}
}, showDelay);
}, [showDelay, visible, getProfileData, fireAnalytics, profileCardType, trigger]);
const onMouseEnter = useCallback(() => {
showProfilecard();
}, [showProfilecard]);
return /*#__PURE__*/React.createElement(Popup, _extends({}, popupProps, {
isOpen: !!visible,
onClose: hideProfilecard,
shouldUseCaptureOnOutsideClick: true,
autoFocus: (_popupProps$autoFocus = popupProps.autoFocus) !== null && _popupProps$autoFocus !== void 0 ? _popupProps$autoFocus : trigger === 'click',
zIndex: layers.modal(),
shouldFitContainer: false,
trigger: triggerProps => {
const {
'aria-expanded': _,
'aria-haspopup': __,
...restInnerProps
} = triggerProps;
return /*#__PURE__*/React.createElement(PopupTrigger, _extends({}, disabledAriaAttributes ? restInnerProps : triggerProps, {
ref: triggerProps.ref,
hideProfilecard: hideProfilecard,
showProfilecard: showProfilecard,
children: children,
ariaLabelledBy: ariaLabelledBy,
trigger: trigger
}));
},
content: () =>
/*#__PURE__*/
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
React.createElement("div", {
onMouseEnter: onMouseEnter,
onMouseLeave: hideProfilecard,
onFocus: showProfilecard
}, isLoading ? /*#__PURE__*/React.createElement(ProfileCardWrapper, null, /*#__PURE__*/React.createElement(LoadingState, {
fireAnalytics: fireAnalytics,
profileType: profileCardType
})) : renderProfileCard({
profileData,
error
}))
}));
}
export default ProfileCardTrigger;