UNPKG

@hhgtech/hhg-components

Version:
278 lines (260 loc) • 11 kB
import { a as __awaiter } from './tslib.es6-ea4dfe68.js'; import React__default, { useRef, useState, useMemo } from 'react'; import { User } from '@hhgtech/icons/core'; import { S as Spinner } from './Spinner-2d43eb3a.js'; import { a as Avatar } from './index-5d405c0d.js'; import { B as Button } from './index-c68a0fa7.js'; import { T as Text } from './index-9f5659e8.js'; import { u as useTranslations } from './index-9d21b711.js'; import { C as CommonGAssets } from './index-ebe66e27.js'; import { theme } from './miscTheme.js'; import styled from '@emotion/styled'; import { M as MediaQueries } from './utils-cb7242c7.js'; import { setDefaultClass } from './miscDefaultClassWrapper.js'; import { ProfileNavigation } from './profileNavigation.js'; import { NavigationLogoutPopup } from './navigationLogoutPopup.js'; const useCSSFocusPopupHelper = (configs) => { const { allowBgScroll } = configs || {}; const ref = useRef(); return { containerRef: ref, onContainerFocus: () => { if (!allowBgScroll) document.documentElement.style.overflow = 'hidden'; }, onContainerBlur: () => { if (!allowBgScroll) document.documentElement.style.overflow = ''; }, onButtonMouseDown: (e) => { // this close popup when click, run after REact init if (ref.current && (document.activeElement === ref.current || ref.current.contains(document.activeElement))) { ref.current.blur(); document.activeElement.blur(); e.preventDefault(); } }, onClose: () => { if (ref.current && (document.activeElement === ref.current || ref.current.contains(document.activeElement))) { ref.current.blur(); document.activeElement.blur(); } }, }; }; const StyledPopup = setDefaultClass(styled.div ` position: absolute; right: 0; transition: all 0.5s ease-in-out; overflow-y: auto; &[data-open='true'] { z-index: 200; top: 120%; opacity: 1; visibility: visible; } &[data-open='false'] { display: none; } .popup-content { z-index: 111; height: 100%; border: 1px solid #0000001a; border-radius: 16px; border-top-left-radius: 0; border-top-right-radius: 0; } /* min-height: 415px; */ ${MediaQueries.mbUp} { width: 320px; } ${MediaQueries.mbDown} { position: fixed; left: 0; width: 100%; padding-bottom: 70px; } `, 'hhgcomp-navprofilebutton-popup'); const StyledPopupWrapper = setDefaultClass(styled.div ` position: fixed; z-index: 150; top: var(--header-bottom, 56px); left: 0; width: 100%; height: 100%; align-items: center; ${MediaQueries.mbUp} { background-color: transparent; &[data-type='welcome'] { background: rgba(38, 38, 38, 0.3); } &[data-type='due-date'] { background: rgba(38, 38, 38, 0.3); } } ${MediaQueries.mbDown} { background: rgba(38, 38, 38, 0.3); } &[data-type='welcome'] { top: 0; } &[data-open='false'] { display: none; } `, 'hhgcomp-navprofilebutton-popupwrapper'); const StyledAvatar = styled.div ` width: 36px; height: 36px; cursor: pointer; img { border-radius: 50%; } .avatar { width: 36px; height: 36px; object-fit: cover; } .avatar-default { display: flex; align-items: center; justify-content: center; background-color: ${theme.colors.secondaryBase}; border-radius: 50%; color: white; font-size: 20px; font-weight: bold; } `; const StyledImageWrapper = styled.div ` display: inline-flex; position: relative; width: 36px; height: 36px; display: flex; align-items: center; justify-content: center; text-decoration: none; flex-shrink: 0; cursor: pointer; .user-close-icon { display: none; } &:focus-within { .user-close-icon { display: block; } .user-avatar { display: none; } ${StyledPopup.classSelector} { z-index: 200; top: 120%; opacity: 1; visibility: visible; display: initial; ${MediaQueries.mbDown} { top: 100%; } } ${StyledPopupWrapper.classSelector} { display: initial; } } `; const StyledLoggedOutAnchor = styled.a ` display: inline-flex; .login-btn[data-color='tertiary'][data-size='md'] { padding-left: 12px; padding-right: 12px; } .mobile-only { ${MediaQueries.mbUp} { display: none; } } .login-btn { display: none; ${MediaQueries.mbUp} { display: block; border: 1px solid ${theme.colors.neutral100}; } } `; const ProfilePopup = ({ isOpen, userInfo, closePopup, siteType = 'helloSites', dispatchLogout, }) => { const handleClickLogout = () => { if (dispatchLogout) { dispatchLogout(); closePopup(); } }; return (React__default.createElement(StyledPopup, { "data-open": isOpen }, React__default.createElement("div", { className: "popup-content" }, React__default.createElement(ProfileNavigation, { siteType: siteType, onLogout: handleClickLogout, userInfo: userInfo })))); }; const NavigationProfileButton = ({ userInfo, siteType, onLoginClick, currentUrl, isMinifyLogin, onLogout, style, isLoading, isMobile, }) => { const { t } = useTranslations(); const [isLogoutConfirmOpen, setIsLogoutConfirmOpen] = useState(false); const name = (userInfo === null || userInfo === void 0 ? void 0 : userInfo.name) || (userInfo === null || userInfo === void 0 ? void 0 : userInfo.username); const { containerRef, onContainerFocus, onContainerBlur, onButtonMouseDown, onClose, } = useCSSFocusPopupHelper(); const isLoginIcon = useMemo(() => isMobile && siteType === 'helloSites', [isMobile]); if (isLoading) return isLoginIcon ? (React__default.createElement(Spinner, null)) : (React__default.createElement(Button, { size: 'sm', color: "transparent", loading: true })); return (userInfo === null || userInfo === void 0 ? void 0 : userInfo.id) ? (React__default.createElement(React__default.Fragment, null, React__default.createElement(StyledImageWrapper, { ref: containerRef, style: style, tabIndex: 0, onBlur: onContainerBlur, onFocus: () => { var _a; onContainerFocus(); const popup = (_a = containerRef.current) === null || _a === void 0 ? void 0 : _a.querySelector('#navigation-v2 .hhgcomp-navprofilebutton-popup'); if (popup) { const bound = popup.getBoundingClientRect(); const top = bound.top; const maxHeight = `calc(100vh - ${top}px)`; popup.setAttribute('style', `max-height: ${maxHeight};`); } } }, React__default.createElement("div", { onMouseDown: onButtonMouseDown }, React__default.createElement("svg", { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", className: "user-close-icon" }, React__default.createElement("path", { d: "M18 6L6 18M18 18L6 6", stroke: "#3E3F58", strokeWidth: "1.3", strokeLinecap: "round" })), React__default.createElement(Avatar, { size: 32, className: "user-avatar", src: userInfo === null || userInfo === void 0 ? void 0 : userInfo.avatar, color: siteType === 'marryBaby' ? theme.mbColors.pink : theme.colors.secondaryBase, placeholderText: name && name.length ? name[0].toUpperCase() : 'A' })), React__default.createElement(ProfilePopup, { dispatchLogout: () => { onClose(); setIsLogoutConfirmOpen(true); }, isOpen: false, closePopup: onClose, siteType: siteType, userInfo: userInfo })), React__default.createElement(NavigationLogoutPopup, { openLogoutPopup: isLogoutConfirmOpen, onCloseHandler: () => { document.body.classList.toggle('noscroll', false); setIsLogoutConfirmOpen(false); }, siteType: siteType, onLogoutHandler: () => __awaiter(void 0, void 0, void 0, function* () { setIsLogoutConfirmOpen(false); document.body.classList.toggle('noscroll', false); yield onLogout(); }) }))) : (React__default.createElement(StyledLoggedOutAnchor, { style: Object.assign({ textDecoration: 'none' }, style), "data-event-category": "Sign Up", "data-event-action": "Button Click", "data-event-label": currentUrl, "data-site-type": siteType, onClick: onLoginClick }, isMinifyLogin ? (React__default.createElement(StyledAvatar, null, React__default.createElement("img", { src: CommonGAssets.getAssetPath('lock.png'), className: "avatar", loading: "lazy", alt: "avatar" }))) : ( // LOGIC: // - DEsktop: always show button // - Mobile: show button only for helloSites, show text for MarryBaby React__default.createElement(React__default.Fragment, null, siteType === 'helloSites' && (isLoginIcon ? (React__default.createElement("img", { className: "mobile-only", src: CommonGAssets.getAssetPath('login.svg'), style: { cursor: 'pointer', height: 24, width: 24, } })) : (React__default.createElement(Button, { className: "mobile-only", size: "sm", leftIcon: React__default.createElement(User, { color: "currentColor", isSolid: true, size: 22 }), sx: { paddingLeft: '8px', paddingRight: '12px', }, styles: { leftIcon: { marginRight: 4, }, } }, t('navigation.loginJoin')))), siteType === 'marryBaby' && (React__default.createElement("div", { className: "mobile-only", style: { paddingLeft: 16, } }, React__default.createElement(Text, { size: "p3", color: theme.mbColors.pink }, t('navigation.loginJoin')))), siteType === 'marryBaby' ? (React__default.createElement(Button, { className: "login-btn", size: "md", leftIcon: React__default.createElement(User, { color: "currentColor", isSolid: true, size: 22 }) }, t('navigation.loginJoin'))) : (React__default.createElement(Button, { className: "login-btn", size: "md", leftIcon: React__default.createElement(User, { color: "#595959", isSolid: true, size: 22 }), variant: "ghost", color: "#262626" }, t('navigation.loginJoin'))))))); }; export { NavigationProfileButton as N, useCSSFocusPopupHelper as u };