Skip to content

Commit

Permalink
Merge pull request #1642 from Giveth/hotfix-2.3.1
Browse files Browse the repository at this point in the history
Hotfix 2.3.1
  • Loading branch information
alireza-sharifpour committed Oct 11, 2022
2 parents 69a5a80 + 3058730 commit cacc004
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 120 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "givethdapp",
"version": "2.3.0",
"version": "2.3.1",
"private": true,
"scripts": {
"build": "next build",
Expand Down
43 changes: 42 additions & 1 deletion pages/test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import styled from 'styled-components';
import { GetServerSideProps } from 'next';
import { useWeb3React } from '@web3-react/core';
import { IconHelp } from '@giveth/ui-design-system';
import { gToast, ToastType } from '@/components/toasts';
import { useAppDispatch } from '@/features/hooks';
import { fetchXDaiInfoAsync } from '@/features/subgraph/subgraph.thunks';
import { FlowRateTooltip } from '@/components/homeTabs/GIVstream.sc';
import { IconWithTooltip } from '@/components/IconWithToolTip';
import { zIndex } from '@/lib/constants/constants';

const TestRoute = () => {
// const xDaiValues = useSelector(
Expand Down Expand Up @@ -38,10 +42,24 @@ const TestRoute = () => {
// };
// }, [library]);
// console.log('****data', data);
// console.log('rect', rect);

return (
<>
<TestContainer>
<IconWithTooltip
icon={<IconHelp size={16} />}
direction='top'
align='left'
>
<FlowRateTooltip>
The rate at which you receive liquid GIV from your
GIVstream.
</FlowRateTooltip>
</IconWithTooltip>
{/* <Tooltip direction={direction} align={align} parentRef={elRef}>
<div>Test</div>
</Tooltip> */}
<button
onClick={() => {
if (account) {
Expand All @@ -60,6 +78,19 @@ const TestRoute = () => {
>
Throw error
</button>
<div>
--------------------------------------------
<IconWithTooltip
icon={<IconHelp size={16} />}
direction='right'
align='bottom'
>
<FlowRateTooltip>
The rate at which you receive liquid GIV from your
GIVstream.
</FlowRateTooltip>
</IconWithTooltip>
</div>
</TestContainer>
</>
);
Expand All @@ -79,5 +110,15 @@ export const getServerSideProps: GetServerSideProps = async context => {
};

const TestContainer = styled.div`
padding: 200px;
padding: 200px 0;
`;

const TooltipContainer = styled.div`
position: fixed;
padding: 0;
background-color: black;
color: #fff;
border-radius: 6px;
padding: 8px;
z-index: ${zIndex.TOOLTIP};
`;
110 changes: 18 additions & 92 deletions src/components/IconWithToolTip.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import styled from 'styled-components';
import { useRef, useState } from 'react';
import { ITooltipDirection, Tooltip } from './Tooltip';
import type { FC, ReactNode } from 'react';

interface IDirection {
direction: 'right' | 'left' | 'top' | 'bottom';
align?: 'center' | 'right' | 'left' | 'top' | 'bottom';
}

interface IIconWithTooltipProps extends IDirection {
interface IIconWithTooltipProps extends ITooltipDirection {
icon: ReactNode;
children: ReactNode;
}
Expand All @@ -17,97 +14,26 @@ export const IconWithTooltip: FC<IIconWithTooltipProps> = ({
align = 'center',
children,
}) => {
const [show, setShow] = useState(false);
const elRef = useRef<HTMLDivElement>(null);

return (
<IconWithTooltipContainer direction={direction} align={align}>
<IconWithTooltipContainer
onMouseEnter={() => setShow(true)}
onMouseLeave={() => setShow(false)}
ref={elRef}
>
{icon}
<span>{children}</span>
{show && (
<Tooltip direction={direction} align={align} parentRef={elRef}>
{children}
</Tooltip>
)}
</IconWithTooltipContainer>
);
};

const IconWithTooltipContainer = styled.div<IDirection>`
position: relative;
const IconWithTooltipContainer = styled.div`
cursor: pointer;
&:hover {
span {
visibility: visible;
opacity: 1;
}
}
& > span {
visibility: hidden;
background-color: black;
color: #fff;
border-radius: 6px;
padding: 8px;
position: absolute;
z-index: 1;
${props => {
let directionStyles;
let alignStyles;
switch (props.direction) {
case 'top':
directionStyles = `bottom: 120%;margin-bottom: 4px;`;
switch (props.align) {
case 'left':
alignStyles = `left: 0%;`;
break;
case 'right':
alignStyles = `right: 0%;`;
break;
default:
alignStyles = `left: 50%; transform: translateX(-50%);`;
break;
}
return directionStyles + alignStyles;
case 'right':
directionStyles = `left: 120%;margin-left: 4px;`;
switch (props.align) {
case 'top':
alignStyles = `top: 0%;`;
break;
case 'bottom':
alignStyles = `bottom: 0%;`;
break;
default:
alignStyles = `top: 50%;transform: translateY(-50%)`;
break;
}
return directionStyles + alignStyles;
case 'left':
directionStyles = `right: 120%;margin-right: 4px;`;
switch (props.align) {
case 'top':
alignStyles = `top: 0%;`;
break;
case 'bottom':
alignStyles = `bottom: 0%;`;
break;
default:
alignStyles = `top: 50%;transform: translateY(-50%)`;
break;
}
return directionStyles + alignStyles;
case 'bottom':
directionStyles = `top: 120%;margin-top: 4px;`;
switch (props.align) {
case 'left':
alignStyles = `left: 0%;`;
break;
case 'right':
alignStyles = `right: 0%;`;
break;
default:
alignStyles = `left: 50%; transform: translateX(-50%);`;
break;
}
return directionStyles + alignStyles;
default:
break;
}
}}
/* Fade in tooltip - takes 1 second to go from 0% to 100% opac: */
opacity: 0;
transition: opacity 0.3s;
}
display: inline-block;
`;
166 changes: 166 additions & 0 deletions src/components/Tooltip.tsx
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;
`;
Loading

1 comment on commit cacc004

@vercel
Copy link

@vercel vercel bot commented on cacc004 Oct 11, 2022

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

Please sign in to comment.