Skip to content

Commit

Permalink
added tooltip support, all known bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterK0927 committed Aug 16, 2024
1 parent 968a1bf commit 1382ea4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const Header: React.FC<HeaderProps> = ({
<StyledHeaderControlsBar />
<Tooltip
text={message}
isVisible={!isVerificationFailed}
isVisible={isVerificationFailed}
isError={true}
>
<StyledHeaderButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,53 @@
* You can obtain one at https://mozilla.org/MPL/2.0/. */
import * as React from 'react';
import { TooltipContainer, TooltipText } from './styles';

interface TooltipProps {
children: React.ReactNode;
text: string;
isVisible?: boolean;
isError?: boolean;
}
import { TooltipProps } from '../../utils/types';

export const Tooltip: React.FC<TooltipProps> = ({
children,
text,
isVisible = true,
isError = false
isError = false,
errorDelay = 1000
}) => {
const [isHovered, setIsHovered] = React.useState(false);
const [showErrorTooltip, setShowErrorTooltip] = React.useState(false);
const firstHoverRef = React.useRef(true);

React.useEffect(() => {
let timer: NodeJS.Timeout;
if (isError && isHovered) {
if (firstHoverRef.current) {
timer = setTimeout(() => {
setShowErrorTooltip(true);
firstHoverRef.current = false;
}, errorDelay);
} else {
setShowErrorTooltip(true);
}
} else {
setShowErrorTooltip(false);
}
return () => clearTimeout(timer);
}, [isError, isHovered, errorDelay]);

return (
<TooltipContainer>
<TooltipContainer
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => {
setIsHovered(false);
setShowErrorTooltip(false);
}}
>
{children}
<TooltipText isVisible={isVisible} isError={isError}>
{text}
</TooltipText>
{isVisible && (
<TooltipText
isVisible={isError ? showErrorTooltip : isHovered}
isError={isError}
>
{text}
</TooltipText>
)}
</TooltipContainer>
);
};
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
import styled from 'styled-components';
import styled from 'styled-components';

export const TooltipContainer = styled.div`
export const TooltipContainer = styled.div`
position: relative;
display: inline-block;
`;

export const TooltipText = styled.div<{ isVisible: boolean; isError: boolean }>`
visibility: hidden;
width: fit-content;
text-wrap: wrap;
background-color: ${props => props.isError ? '#ff4d4d' : '#555'};
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px;
position: absolute;
z-index: 1;
top: 100%;
left: 50%;
margin-left: -60px;
transition: opacity 0.3s, visibility 0.3s;
${TooltipContainer}:hover & {
visibility: ${props => props.isVisible ? 'visible' : 'hidden'};
}
&::after {
content: "";
position: absolute;
bottom: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent ${props => props.isError ? '#ff4d4d' : '#555'} transparent;
}
`;

export const TooltipText = styled.div<{ isVisible: boolean; isError: boolean }>`
visibility: ${props => props.isVisible ? 'visible' : 'hidden'};
opacity: ${props => props.isVisible ? 1 : 0};
width: max-content;
max-width: 200px;
text-wrap: wrap;
background-color: ${props => props.isError ? '#ff4d4d' : '#555'};
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px;
position: absolute;
z-index: 1;
top: 100%;
left: 50%;
transform: translateX(-50%);
margin-top: 5px;
transition: opacity 0.3s, visibility 0.3s;
&::after {
content: "";
position: absolute;
bottom: 100%;
left: 50%;
transform: translateX(-50%);
border-width: 5px;
border-style: solid;
border-color: transparent transparent ${props => props.isError ? '#ff4d4d' : '#555'} transparent;
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const PdfRenderer: React.FC = () => {
const pageRefs = useRef<Array<HTMLDivElement | null>>([]);
const fileInputRef = useRef<HTMLInputElement | null>(null);
const [isSigned, setIsSigned] = useState<boolean>(false);
const [verificationErrorMessage, setVerificationErrorMessage] = useState<string>('');

const resetSignatureState = useCallback(() => {
setIsSelectionEnabled(false);
Expand Down Expand Up @@ -313,6 +314,7 @@ export const PdfRenderer: React.FC = () => {
} catch (error) {
setStatusMessage('Verification Failed');
setStatusType('error');
setVerificationErrorMessage(`${error}`);
setIsVerified(false);
setIsVerificationFailed(true);
} finally {
Expand Down Expand Up @@ -451,7 +453,7 @@ export const PdfRenderer: React.FC = () => {
handleLogoClick={handleLogoClick}
fileInputRef={fileInputRef}
isSigned={isSigned}
message={errorMessage}
message={verificationErrorMessage}
/>
<S.PdfContainer>
{!pdfFile ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ export type PdfPageProps = {
}

export type TooltipProps = {
text: string;
isVisible: boolean;
isVerificationFailed: boolean;
children: React.ReactNode;
text: string;
isVisible?: boolean;
isError?: boolean;
errorDelay?: number;
}

0 comments on commit 1382ea4

Please sign in to comment.