-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1642 from Giveth/hotfix-2.3.1
Hotfix 2.3.1
- Loading branch information
Showing
7 changed files
with
250 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import { | ||
CSSProperties, | ||
FC, | ||
ReactNode, | ||
RefObject, | ||
useEffect, | ||
useRef, | ||
useState, | ||
} from 'react'; | ||
import styled from 'styled-components'; | ||
import { createPortal } from 'react-dom'; | ||
import { zIndex } from '@/lib/constants/constants'; | ||
|
||
export interface ITooltipDirection { | ||
direction: 'right' | 'left' | 'top' | 'bottom'; | ||
align?: 'center' | 'right' | 'left' | 'top' | 'bottom'; | ||
} | ||
|
||
interface ITooltipProps extends ITooltipDirection { | ||
parentRef: RefObject<HTMLDivElement>; | ||
children: ReactNode; | ||
} | ||
|
||
const ARROW_SIZE = 8; | ||
|
||
export const Tooltip: FC<ITooltipProps> = ({ | ||
parentRef, | ||
direction, | ||
align, | ||
children, | ||
}) => { | ||
const [style, setStyle] = useState<CSSProperties>({}); | ||
const el = useRef(document.createElement('div')); | ||
const childRef = useRef<HTMLDivElement>(null); | ||
useEffect(() => { | ||
const current = el.current; | ||
const body = document.querySelector('body') as HTMLElement; | ||
|
||
if (body) { | ||
body.appendChild(current); | ||
} | ||
return () => { | ||
body!.removeChild(current); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (!parentRef.current) return; | ||
// if (!childRef.current) return; | ||
if (typeof window === 'undefined') return; | ||
const parentRect = parentRef.current.getBoundingClientRect(); | ||
const childRect = childRef.current?.getBoundingClientRect(); | ||
const _style = tooltipStyleCalc( | ||
{ | ||
direction, | ||
align, | ||
}, | ||
parentRect, | ||
childRect, | ||
); | ||
setStyle(_style); | ||
}, [align, direction, parentRef, childRef]); | ||
|
||
return createPortal( | ||
style.top ? ( | ||
<TooltipContainer | ||
ref={childRef} | ||
style={style} | ||
direction={direction} | ||
align={align} | ||
> | ||
{children} | ||
</TooltipContainer> | ||
) : null, | ||
el.current, | ||
); | ||
}; | ||
|
||
const translateXForTopBottom = ( | ||
align: ITooltipDirection['align'], | ||
parentRect: DOMRect, | ||
) => { | ||
switch (align) { | ||
case 'right': | ||
return `-${ARROW_SIZE}px`; | ||
|
||
case 'left': | ||
return `calc(-100% + ${parentRect.width + ARROW_SIZE}px)`; | ||
|
||
default: | ||
return `calc(-50% + ${parentRect.width / 2}px)`; | ||
} | ||
}; | ||
|
||
const translateYForRightLeft = ( | ||
align: ITooltipDirection['align'], | ||
parentRect: DOMRect, | ||
) => { | ||
switch (align) { | ||
case 'top': | ||
return `-100%`; | ||
|
||
case 'bottom': | ||
return `-${parentRect.width + ARROW_SIZE}px`; | ||
|
||
default: | ||
return `calc(-50% - ${parentRect.height / 2}px)`; | ||
} | ||
}; | ||
|
||
const tooltipStyleCalc = ( | ||
position: ITooltipDirection, | ||
parentRect: DOMRect, | ||
childRect?: DOMRect, // left it here for future usage | ||
): CSSProperties => { | ||
const { align, direction } = position; | ||
let style = {}; | ||
let translateX; | ||
let translateY; | ||
switch (direction) { | ||
case 'top': | ||
translateX = translateXForTopBottom(align, parentRect); | ||
style = { | ||
top: parentRect.top - ARROW_SIZE, | ||
left: parentRect.left, | ||
transform: `translate(${translateX}, -100%)`, | ||
}; | ||
break; | ||
case 'bottom': | ||
translateX = translateXForTopBottom(align, parentRect); | ||
style = { | ||
top: parentRect.bottom + ARROW_SIZE, | ||
left: parentRect.left, | ||
transform: `translate(${translateX}, 0)`, | ||
}; | ||
break; | ||
case 'right': | ||
translateY = translateYForRightLeft(align, parentRect); | ||
style = { | ||
top: parentRect.bottom, | ||
left: parentRect.right + ARROW_SIZE, | ||
transform: `translate(0, ${translateY})`, | ||
}; | ||
break; | ||
case 'left': | ||
translateY = translateYForRightLeft(align, parentRect); | ||
style = { | ||
top: parentRect.bottom, | ||
left: parentRect.left - ARROW_SIZE, | ||
transform: `translate(-100%, ${translateY})`, | ||
}; | ||
break; | ||
} | ||
return style; | ||
}; | ||
|
||
const TooltipContainer = styled.div<ITooltipDirection>` | ||
position: fixed; | ||
background-color: black; | ||
color: #fff; | ||
border-radius: 6px; | ||
padding: 8px; | ||
z-index: ${zIndex.TOOLTIP}; | ||
top: 0; | ||
left: 0; | ||
`; |
Oops, something went wrong.
cacc004
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
giveth-dapps-v2 – ./
www.giveth.io
giveth-dapps-v2-git-main-givethio.vercel.app
giveth-dapps-v2-givethio.vercel.app
giveth.io