UNPKG

@atlaskit/profilecard

Version:

A React component to display a card with user information.

120 lines 5.09 kB
import _extends from "@babel/runtime/helpers/extends"; import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties"; var _excluded = ["trigger", "ariaLabelledBy", "children", "renderProfileCard", "fetchProfile", "disabledAriaAttributes", "profileCardType", "fireAnalytics"], _excluded2 = ["aria-expanded", "aria-haspopup"]; import _regeneratorRuntime from "@babel/runtime/regenerator"; 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'; var DELAY_MS_SHOW = 800; var DELAY_MS_HIDE = 200; function ProfileCardTrigger(_ref) { var _popupProps$autoFocus; var _trigger = _ref.trigger, ariaLabelledBy = _ref.ariaLabelledBy, children = _ref.children, renderProfileCard = _ref.renderProfileCard, fetchProfile = _ref.fetchProfile, disabledAriaAttributes = _ref.disabledAriaAttributes, profileCardType = _ref.profileCardType, fireAnalytics = _ref.fireAnalytics, popupProps = _objectWithoutProperties(_ref, _excluded); var showDelay = _trigger === 'click' ? 0 : DELAY_MS_SHOW; var hideDelay = _trigger === 'click' ? 0 : DELAY_MS_HIDE; var showTimer = useRef(0); var hideTimer = useRef(0); var _useState = useState(false), _useState2 = _slicedToArray(_useState, 2), visible = _useState2[0], setVisible = _useState2[1]; var _useProfileInfo = useProfileInfo({ fetchUserProfile: fetchProfile }), profileData = _useProfileInfo.profileData, isLoading = _useProfileInfo.isLoading, error = _useProfileInfo.error, getProfileData = _useProfileInfo.getProfileData; useEffect(function () { return function () { clearTimeout(showTimer.current); clearTimeout(hideTimer.current); }; }, []); var hideProfilecard = useCallback(function () { clearTimeout(showTimer.current); clearTimeout(hideTimer.current); hideTimer.current = window.setTimeout(function () { setVisible(false); }, hideDelay); }, [hideDelay]); var showProfilecard = useCallback( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: clearTimeout(hideTimer.current); clearTimeout(showTimer.current); showTimer.current = window.setTimeout(function () { if (!visible) { getProfileData === null || getProfileData === void 0 || getProfileData(); setVisible(true); if (fireAnalytics) { fireAnalytics(cardTriggered(profileCardType, _trigger)); } } }, showDelay); case 3: case "end": return _context.stop(); } }, _callee); })), [showDelay, visible, getProfileData, fireAnalytics, profileCardType, _trigger]); var onMouseEnter = useCallback(function () { 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: function trigger(triggerProps) { var _ = triggerProps['aria-expanded'], __ = triggerProps['aria-haspopup'], restInnerProps = _objectWithoutProperties(triggerProps, _excluded2); return /*#__PURE__*/React.createElement(PopupTrigger, _extends({}, disabledAriaAttributes ? restInnerProps : triggerProps, { ref: triggerProps.ref, hideProfilecard: hideProfilecard, showProfilecard: showProfilecard, children: children, ariaLabelledBy: ariaLabelledBy, trigger: _trigger })); }, content: function content() { return ( /*#__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: profileData, error: error })) ); } })); } export default ProfileCardTrigger;