|
| 1 | +import type { PointerEventHandler } from 'react'; |
| 2 | +import { useCallback, useEffect, useRef } from 'react'; |
| 3 | + |
| 4 | +import type { LocalizationKey } from '../../customizables'; |
| 5 | +import { Col, descriptors, Flex, Link, Text } from '../../customizables'; |
| 6 | +import { Portal } from '../../elements/Portal'; |
| 7 | +import { InternalThemeProvider, mqu } from '../../styledSystem'; |
| 8 | + |
| 9 | +type AccountlessPromptProps = { |
| 10 | + url?: string; |
| 11 | +}; |
| 12 | + |
| 13 | +type FabContentProps = { title?: LocalizationKey | string; signOutText: LocalizationKey | string; url: string }; |
| 14 | + |
| 15 | +const FabContent = ({ title, signOutText, url }: FabContentProps) => { |
| 16 | + return ( |
| 17 | + <Col |
| 18 | + sx={t => ({ |
| 19 | + width: '100%', |
| 20 | + paddingLeft: t.sizes.$4, |
| 21 | + paddingRight: t.sizes.$6, |
| 22 | + whiteSpace: 'nowrap', |
| 23 | + })} |
| 24 | + > |
| 25 | + <Text |
| 26 | + colorScheme='secondary' |
| 27 | + elementDescriptor={descriptors.impersonationFabTitle} |
| 28 | + variant='buttonLarge' |
| 29 | + truncate |
| 30 | + localizationKey={title} |
| 31 | + /> |
| 32 | + <Link |
| 33 | + variant='buttonLarge' |
| 34 | + elementDescriptor={descriptors.impersonationFabActionLink} |
| 35 | + sx={t => ({ |
| 36 | + alignSelf: 'flex-start', |
| 37 | + color: t.colors.$primary500, |
| 38 | + ':hover': { |
| 39 | + cursor: 'pointer', |
| 40 | + }, |
| 41 | + })} |
| 42 | + localizationKey={signOutText} |
| 43 | + onClick={ |
| 44 | + () => (window.location.href = url) |
| 45 | + // clerk-js has been loaded at this point so we can safely access session |
| 46 | + // handleSignOutSessionClicked(session!) |
| 47 | + } |
| 48 | + /> |
| 49 | + </Col> |
| 50 | + ); |
| 51 | +}; |
| 52 | + |
| 53 | +export const _AccountlessPrompt = (props: AccountlessPromptProps) => { |
| 54 | + // const { parsedInternalTheme } = useAppearance(); |
| 55 | + const containerRef = useRef<HTMLDivElement>(null); |
| 56 | + |
| 57 | + //essentials for calcs |
| 58 | + // const eyeWidth = parsedInternalTheme.sizes.$16; |
| 59 | + // const eyeHeight = eyeWidth; |
| 60 | + const topProperty = '--cl-impersonation-fab-top'; |
| 61 | + const rightProperty = '--cl-impersonation-fab-right'; |
| 62 | + const defaultTop = 109; |
| 63 | + const defaultRight = 23; |
| 64 | + |
| 65 | + const handleResize = () => { |
| 66 | + const current = containerRef.current; |
| 67 | + if (!current) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const offsetRight = window.innerWidth - current.offsetLeft - current.offsetWidth; |
| 72 | + const offsetBottom = window.innerHeight - current.offsetTop - current.offsetHeight; |
| 73 | + |
| 74 | + const outsideViewport = [current.offsetLeft, offsetRight, current.offsetTop, offsetBottom].some(o => o < 0); |
| 75 | + |
| 76 | + if (outsideViewport) { |
| 77 | + document.documentElement.style.setProperty(rightProperty, `${defaultRight}px`); |
| 78 | + document.documentElement.style.setProperty(topProperty, `${defaultTop}px`); |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + const onPointerDown: PointerEventHandler = () => { |
| 83 | + window.addEventListener('pointermove', onPointerMove); |
| 84 | + window.addEventListener( |
| 85 | + 'pointerup', |
| 86 | + () => { |
| 87 | + window.removeEventListener('pointermove', onPointerMove); |
| 88 | + handleResize(); |
| 89 | + }, |
| 90 | + { once: true }, |
| 91 | + ); |
| 92 | + }; |
| 93 | + |
| 94 | + const onPointerMove = useCallback((e: PointerEvent) => { |
| 95 | + e.stopPropagation(); |
| 96 | + e.preventDefault(); |
| 97 | + const current = containerRef.current; |
| 98 | + if (!current) { |
| 99 | + return; |
| 100 | + } |
| 101 | + const rightOffestBasedOnViewportAndContent = `${ |
| 102 | + window.innerWidth - current.offsetLeft - current.offsetWidth - e.movementX |
| 103 | + }px`; |
| 104 | + document.documentElement.style.setProperty(rightProperty, rightOffestBasedOnViewportAndContent); |
| 105 | + document.documentElement.style.setProperty(topProperty, `${current.offsetTop - -e.movementY}px`); |
| 106 | + }, []); |
| 107 | + |
| 108 | + const repositionFabOnResize = () => { |
| 109 | + window.addEventListener('resize', handleResize); |
| 110 | + return () => { |
| 111 | + window.removeEventListener('resize', handleResize); |
| 112 | + }; |
| 113 | + }; |
| 114 | + |
| 115 | + useEffect(repositionFabOnResize, []); |
| 116 | + |
| 117 | + if (!props.url) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + return ( |
| 122 | + <Portal> |
| 123 | + <Flex |
| 124 | + ref={containerRef} |
| 125 | + elementDescriptor={descriptors.impersonationFab} |
| 126 | + onPointerDown={onPointerDown} |
| 127 | + align='center' |
| 128 | + sx={t => ({ |
| 129 | + touchAction: 'none', //for drag to work on mobile consistently |
| 130 | + position: 'fixed', |
| 131 | + overflow: 'hidden', |
| 132 | + top: `var(${topProperty}, ${defaultTop}px)`, |
| 133 | + right: `var(${rightProperty}, ${defaultRight}px)`, |
| 134 | + padding: `10px`, |
| 135 | + zIndex: t.zIndices.$fab, |
| 136 | + boxShadow: t.shadows.$fabShadow, |
| 137 | + borderRadius: t.radii.$halfHeight, //to match the circular eye perfectly |
| 138 | + backgroundColor: t.colors.$white, |
| 139 | + fontFamily: t.fonts.$main, |
| 140 | + ':hover': { |
| 141 | + cursor: 'grab', |
| 142 | + }, |
| 143 | + ':hover #cl-impersonationText': { |
| 144 | + transition: `max-width ${t.transitionDuration.$slowest} ease, opacity 0ms ease ${t.transitionDuration.$slowest}`, |
| 145 | + maxWidth: `min(calc(50vw - 2 * ${defaultRight}px), ${15}ch)`, |
| 146 | + [mqu.md]: { |
| 147 | + maxWidth: `min(calc(100vw - 2 * ${defaultRight}px), ${15}ch)`, |
| 148 | + }, |
| 149 | + opacity: 1, |
| 150 | + }, |
| 151 | + })} |
| 152 | + > |
| 153 | + 🔓Accountless Mode |
| 154 | + <Flex |
| 155 | + id='cl-impersonationText' |
| 156 | + sx={t => ({ |
| 157 | + transition: `max-width ${t.transitionDuration.$slowest} ease, opacity ${t.transitionDuration.$fast} ease`, |
| 158 | + maxWidth: '0px', |
| 159 | + opacity: 1, |
| 160 | + })} |
| 161 | + > |
| 162 | + <FabContent |
| 163 | + url={props.url} |
| 164 | + signOutText={'Claim your keys'} |
| 165 | + /> |
| 166 | + </Flex> |
| 167 | + </Flex> |
| 168 | + </Portal> |
| 169 | + ); |
| 170 | +}; |
| 171 | + |
| 172 | +export const AccountlessPrompt = (props: AccountlessPromptProps) => ( |
| 173 | + <InternalThemeProvider> |
| 174 | + <_AccountlessPrompt {...props} /> |
| 175 | + </InternalThemeProvider> |
| 176 | +); |
0 commit comments