@atlaskit/profilecard
Version:
A React component to display a card with user information.
117 lines • 4.14 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react';
import Popup from '@atlaskit/popup';
import { layers } from '@atlaskit/theme/constants';
import { getActionSubject, PACKAGE_META_DATA } from '../../util/analytics';
import { getPageTime } from '../../util/performance';
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 ProfileCardTriggerInner({
trigger,
ariaLabelledBy,
children,
renderProfileCard,
fetchProfile,
disabledAriaAttributes,
profileCardType,
testId,
fireAnalytics,
...popupProps
}, ref) {
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]);
useImperativeHandle(ref, () => ({
hideProfilecard
}), [hideProfilecard]);
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(`ui.${getActionSubject(profileCardType)}.triggered`, {
method: trigger,
...PACKAGE_META_DATA,
firedAt: Math.round(getPageTime())
});
}
}
}, 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,
"data-testid": testId
}));
},
content: () => /*#__PURE__*/React.createElement("div", {
onMouseEnter: onMouseEnter
// eslint-disable-next-line @atlassian/a11y/mouse-events-have-key-events
,
onMouseLeave: hideProfilecard,
onFocus: showProfilecard,
"data-testid": "profile-card--trigger-content"
}, isLoading ? /*#__PURE__*/React.createElement(ProfileCardWrapper, {
testId: "profilecard.profilecardtrigger.loading"
}, /*#__PURE__*/React.createElement(LoadingState, {
fireAnalytics: fireAnalytics,
profileType: profileCardType
})) : renderProfileCard({
profileData,
error
}))
}));
}
const ProfileCardTrigger = /*#__PURE__*/forwardRef(ProfileCardTriggerInner);
export default ProfileCardTrigger;