Skip to content

Commit 5ea25fa

Browse files
committed
feat: add tooltip position appear below
fix: add type string literal for tooltipPosition Update Readme Revert to previous Readme Update Readme from master
1 parent a07a9bf commit 5ea25fa

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
## react-copy-mailto
2+
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
3+
[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors-)
4+
<!-- ALL-CONTRIBUTORS-BADGE:END -->
25

36
[![npm version](https://badge.fury.io/js/react-copy-mailto.svg)](https://badge.fury.io/js/react-copy-mailto) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/react-copy-mailto)
47

58
A fully customizable React component for copying email from `mailto` links.
69

710
## Motivation
8-
The one thing we all can agree on that we hate it when the default mail app pops up after clicking on the `mailto` links. Most of the time we just want to copy the email address and that's where this module comes into play.
11+
The one thing we all can agree on that we hate it when the default mail app pops up after clicking on the `mailto` links. Most of the time we just want to copy the email address and that's where this module comes into play. Big shout out to [Kuldar](https://twitter.com/kkuldar) whose tweet [thread](https://twitter.com/kkuldar/status/1270736717939716097) inspired us to build this.
912

1013

1114
## Demo
@@ -46,13 +49,14 @@ You can customize almost every aspect of this component using the below props, o
4649

4750
| Name | Type | Default | Description |
4851
|:-: |--- |--- |--- |
49-
| email | string | none | The email to be copied |
52+
| email | string | none | The email to be copied. |
5053
| children | ReactNode | null | Use this if you want to use some custom component inside the anchor tag. |
5154
| defaultTooltip | string | "Copy email address" | Text shown in the tooltip when the user hovers over the link. |
5255
| copiedTooltip | string | "Copied to clipboard!" | Text shown in the tooltip when the user clicks on the link and the text is copied to clipboard. |
53-
| containerStyles | style object | none | The styles to be applied to the container |
54-
| tooltipStyles | style object | none | The styles to be applied to the tooltip |
55-
| anchorStyles | style object | none | The styles to be applied to the anchor |
56+
| containerStyles | style object | none | The styles to be applied to the container. |
57+
| tooltipStyles | style object | none | The styles to be applied to the tooltip. |
58+
| anchorStyles | style object | none | The styles to be applied to the anchor. |
59+
| tooltipPosition | string | "above" | The position of the tooltip. |
5660

5761
## Development
5862

@@ -78,3 +82,23 @@ Feel free to open [issues](https://github.com/devfolioco/react-copy-mailto/issue
7882

7983
[![NPM](https://img.shields.io/npm/l/react-copy-mailto)](https://github.com/devfolioco/react-copy-mailto/blob/master/LICENSE)
8084

85+
86+
## Contributors ✨
87+
88+
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
89+
90+
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
91+
<!-- prettier-ignore-start -->
92+
<!-- markdownlint-disable -->
93+
<table>
94+
<tr>
95+
<td align="center"><a href="http://prateeksurana.me"><img src="https://avatars3.githubusercontent.com/u/21277179?v=4" width="100px;" alt=""/><br /><sub><b>Prateek Surana</b></sub></a><br /><a href="https://github.com/devfolioco/react-copy-mailto/commits?author=prateek3255" title="Code">💻</a> <a href="#design-prateek3255" title="Design">🎨</a> <a href="#content-prateek3255" title="Content">🖋</a> <a href="https://github.com/devfolioco/react-copy-mailto/commits?author=prateek3255" title="Documentation">📖</a></td>
96+
<td align="center"><a href="http://ankiiitraj.github.io"><img src="https://avatars2.githubusercontent.com/u/48787278?v=4" width="100px;" alt=""/><br /><sub><b>Ankit Raj</b></sub></a><br /><a href="#tool-ankiiitraj" title="Tools">🔧</a></td>
97+
</tr>
98+
</table>
99+
100+
<!-- markdownlint-enable -->
101+
<!-- prettier-ignore-end -->
102+
<!-- ALL-CONTRIBUTORS-LIST:END -->
103+
104+
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

src/demo/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const App = () => (
88
style={{ display: "flex", alignItems: "center", flexDirection: "column" }}
99
>
1010
<h1 style={{ marginBottom: "50px" }}>Copy email address to clipboard</h1>
11-
<CopyMailTo email="[email protected]" />
11+
<CopyMailTo email="[email protected]" tooltipPosition="below" />
1212
</div>
1313
);
1414

src/lib/index.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import React, { MouseEvent } from "react";
22

3+
type TooltipPosition = "above" | "below";
4+
35
const copyToClipboard = (str: string) => {
46
const el = document.createElement("textarea"); // Create a <textarea> element
57
el.value = str; // Set its value to the string that you want copied
@@ -25,8 +27,8 @@ const containerBaseStyles: React.CSSProperties = {
2527
position: "relative",
2628
};
2729

28-
const tooltipBaseStyles: React.CSSProperties = {
29-
bottom: "26px",
30+
const tooltipBaseStyles = (tooltipPosition: string): React.CSSProperties => ({
31+
[tooltipPosition === "above" ? "bottom" : "top"]: "26px",
3032
maxWidth: "fit-content",
3133
position: "absolute",
3234
width: "auto",
@@ -41,10 +43,10 @@ const tooltipBaseStyles: React.CSSProperties = {
4143
padding: "6px 8px",
4244
borderRadius: "5px",
4345
opacity: 0,
44-
transform: "translateY(-5px)",
46+
transform: `translateY(${tooltipPosition === "above" ? "-5px": "5px"})`,
4547
visibility: "hidden",
4648
transition: "all 0.2s ease-in-out",
47-
};
49+
});
4850

4951
const toolTipVisibleStyles: React.CSSProperties = {
5052
opacity: 1,
@@ -60,6 +62,7 @@ const CopyMailTo = ({
6062
containerStyles = {},
6163
tooltipStyles = {},
6264
anchorStyles = {},
65+
tooltipPosition = "above",
6366
}: {
6467
email: string;
6568
children?: React.ReactNode;
@@ -68,6 +71,7 @@ const CopyMailTo = ({
6871
containerStyles?: React.CSSProperties;
6972
tooltipStyles?: React.CSSProperties;
7073
anchorStyles?: React.CSSProperties;
74+
tooltipPosition?: TooltipPosition;
7175
}): JSX.Element => {
7276
const [showCopied, setShowCopied] = React.useState(false);
7377
const [showTooltip, setShowTooltip] = React.useState(false);
@@ -101,10 +105,10 @@ const CopyMailTo = ({
101105
};
102106

103107
const allTooltipStyles = {
104-
...tooltipBaseStyles,
105-
...tooltipStyles,
106-
...(showTooltip && toolTipVisibleStyles),
107-
};
108+
...tooltipBaseStyles(tooltipPosition),
109+
...tooltipStyles,
110+
...(showTooltip && toolTipVisibleStyles),
111+
};
108112

109113
return (
110114
<span style={allContainerStyles}>

0 commit comments

Comments
 (0)