-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tooltip to "Remind to Vote" Button if email is unavailable
MPR #74
- Loading branch information
Showing
8 changed files
with
164 additions
and
97 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 |
---|---|---|
@@ -1,45 +1,82 @@ | ||
import React, { Fragment } from "react"; | ||
import React, { useCallback, useLayoutEffect, useState } from "react"; | ||
import cn from "classnames"; | ||
import { Transition } from "@headlessui/react"; | ||
import { usePopper } from "react-popper"; | ||
import { Options } from "@popperjs/core"; | ||
|
||
interface TooltipProps extends React.HTMLAttributes<HTMLDivElement> { | ||
popperOptions?: Partial<Options>; | ||
parentElement: HTMLElement | null; | ||
triggerElement?: HTMLElement | null; | ||
content: React.ReactNode | string; | ||
} | ||
|
||
const Tooltip = React.forwardRef<HTMLDivElement, TooltipProps>((props, ref) => { | ||
const { content, ...rest } = props; | ||
const Tooltip: React.FC<TooltipProps> = (props) => { | ||
const { content, popperOptions, parentElement, triggerElement, ...rest } = | ||
props; | ||
|
||
const [showTooltip, setShowTooltip] = useState<boolean>(false); | ||
const [tooltipEle, setTooltipEle] = useState<HTMLDivElement | null>(null); | ||
|
||
const { styles, attributes } = usePopper( | ||
parentElement, | ||
tooltipEle, | ||
popperOptions | ||
); | ||
|
||
const handleMouseEnter = useCallback(() => { | ||
setShowTooltip(true); | ||
}, []); | ||
|
||
const handleMouseLeave = useCallback(() => { | ||
setShowTooltip(false); | ||
}, []); | ||
|
||
useLayoutEffect(() => { | ||
const element = triggerElement ?? parentElement; | ||
element?.addEventListener("mouseenter", handleMouseEnter); | ||
element?.addEventListener("mouseleave", handleMouseLeave); | ||
|
||
return () => { | ||
element?.removeEventListener("mouseenter", handleMouseEnter); | ||
element?.removeEventListener("mouseleave", handleMouseLeave); | ||
}; | ||
}, [handleMouseEnter, handleMouseLeave, parentElement, triggerElement]); | ||
|
||
return ( | ||
<div ref={ref} {...rest}> | ||
<Transition | ||
show={true} | ||
static={true} | ||
as={Fragment} | ||
enter="transition-opacity duration-150" | ||
enterFrom="opacity-0" | ||
enterTo="opacity-100" | ||
leave="transition-opacity duration-150" | ||
leaveFrom="opacity-100" | ||
leaveTo="opacity-0" | ||
<Transition | ||
ref={setTooltipEle} | ||
show={showTooltip} | ||
static={true} | ||
style={styles.popper} | ||
as={"div"} | ||
enter="transition-opacity duration-150" | ||
enterFrom="opacity-0" | ||
enterTo="opacity-100" | ||
leave="transition-opacity duration-150" | ||
leaveFrom="opacity-100" | ||
leaveTo="opacity-0" | ||
{...attributes.popper} | ||
{...rest} | ||
> | ||
<div | ||
role="tooltip" | ||
className={cn( | ||
"bg-gray-600", | ||
"text-white", | ||
"text-xs", | ||
"h-min", | ||
"py-1", | ||
"px-2", | ||
"rounded-md", | ||
"z-50", | ||
"transition-opacity" | ||
)} | ||
> | ||
<div | ||
role="tooltip" | ||
className={cn( | ||
"bg-gray-600", | ||
"text-white", | ||
"text-xs", | ||
"h-min", | ||
"py-1", | ||
"px-2", | ||
"rounded-md", | ||
"z-50", | ||
"transition-opacity" | ||
)} | ||
> | ||
{content} | ||
</div> | ||
</Transition> | ||
</div> | ||
{content} | ||
</div> | ||
</Transition> | ||
); | ||
}); | ||
}; | ||
|
||
export default Tooltip; |
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,33 @@ | ||
import { validateEmail } from "../regex"; | ||
|
||
describe("validateEmail", () => { | ||
it("should fail empty email", () => { | ||
const email = ""; | ||
|
||
expect(validateEmail(email)).toBe(false); | ||
}); | ||
|
||
it("should fail invalid email", () => { | ||
const email = "invalid"; | ||
|
||
expect(validateEmail(email)).toBe(false); | ||
}); | ||
|
||
it("should pass valid email", () => { | ||
const email = "test@exmaple.com"; | ||
|
||
expect(validateEmail(email)).toBe(true); | ||
}); | ||
|
||
it("should pass valid email with plus", () => { | ||
const email = "test+2@example.com"; | ||
|
||
expect(validateEmail(email)).toBe(true); | ||
}); | ||
|
||
it("should pass valid email with dots", () => { | ||
const email = "test.hello.world@example.com"; | ||
|
||
expect(validateEmail(email)).toBe(true); | ||
}); | ||
}); |
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,9 @@ | ||
const EMAIL_REGEX = | ||
/^$|^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/; | ||
|
||
export function validateEmail(e: string): boolean { | ||
if (!e) { | ||
return false; | ||
} | ||
return EMAIL_REGEX.test(e); | ||
} |