Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import ReactDOM from "react-dom";

import CopyMailTo from "../lib";

const App = () => (
<div
style={{ display: "flex", alignItems: "center", flexDirection: "column" }}
>
<h1 style={{ marginBottom: "50px" }}>Copy email address to clipboard</h1>
<CopyMailTo email="[email protected]" />
</div>
);
const App = () => {
let email = "[email protected]";
let isSecure = false;
return (
<div
style={{ display: "flex", alignItems: "center", flexDirection: "column" }}
>
<h1 style={{ marginBottom: "50px" }}>Copy email address to clipboard</h1>
<CopyMailTo email={email} isSecure={isSecure} />
</div>
)
};

ReactDOM.render(<App />, document.getElementById("app"));
20 changes: 15 additions & 5 deletions src/lib/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { MouseEvent } from "react";
type theme = "dark" | "light";
interface CopyMailToPropsInterface {
email: string;
isSecure: boolean;
theme: theme;
children?: React.ReactNode;
defaultTooltip?: string;
Expand Down Expand Up @@ -66,6 +67,7 @@ const toolTipVisibleStyles: React.CSSProperties = {

const CopyMailTo = ({
email,
isSecure,
theme = "dark",
children = null,
defaultTooltip = "Copy email address",
Expand All @@ -77,9 +79,13 @@ const CopyMailTo = ({
const [showCopied, setShowCopied] = React.useState(false);
const [showTooltip, setShowTooltip] = React.useState(false);

const copyEmail = (e: MouseEvent) => {
const copyEmail = (e: MouseEvent, isSecure: Boolean) => {
e.preventDefault();
copyToClipboard(email);
if(isSecure){
copyToClipboard(window.atob(email));
} else {
copyToClipboard(email);
}
setShowCopied(true);
setShowTooltip(true);
};
Expand Down Expand Up @@ -109,19 +115,23 @@ const CopyMailTo = ({
...(showTooltip && toolTipVisibleStyles)
};

if(isSecure){
email = window.btoa(email);
}

return (
<span style={allContainerStyles}>
<a
aria-label={defaultTooltip}
onClick={copyEmail}
onClick={(e) => copyEmail(e, isSecure)}
onMouseOver={toggleTooltip}
onMouseOut={toggleTooltip}
onFocus={toggleTooltip}
onBlur={toggleTooltip}
href={`mailto:${email}`}
href={isSecure ? "mailto:" + window.atob(email) : "mailto:" + email}
style={anchorStyles}
>
{children || email}
{children || isSecure ? window.atob(email) : email}
</a>
<span style={allTooltipStyles}>
{showCopied ? copiedTooltip : defaultTooltip}
Expand Down