Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tooltip position appear below #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ You can customize almost every aspect of this component using the below props, o
| containerStyles | style object | none | The styles to be applied to the container. |
| tooltipStyles | style object | none | The styles to be applied to the tooltip. |
| anchorStyles | style object | none | The styles to be applied to the anchor. |
| tooltipPosition | string | "above" | The position of the tooltip. |

## Development

Expand Down
2 changes: 1 addition & 1 deletion src/demo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const App = () => (
style={{ display: "flex", alignItems: "center", flexDirection: "column" }}
>
<h1 style={{ marginBottom: "50px" }}>Copy email address to clipboard</h1>
<CopyMailTo email="email@domain.com" />
<CopyMailTo email="email@domain.com" tooltipPosition="below" />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned previously as well, please don't modify the demo.

</div>
);

Expand Down
20 changes: 12 additions & 8 deletions src/lib/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React, { MouseEvent } from "react";

type TooltipPosition = "above" | "below";

const copyToClipboard = (str: string) => {
const el = document.createElement("textarea"); // Create a <textarea> element
el.value = str; // Set its value to the string that you want copied
Expand All @@ -25,8 +27,8 @@ const containerBaseStyles: React.CSSProperties = {
position: "relative",
};

const tooltipBaseStyles: React.CSSProperties = {
bottom: "26px",
const tooltipBaseStyles = (tooltipPosition: string): React.CSSProperties => ({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type should be ToolTipPosition, right

[tooltipPosition === "above" ? "bottom" : "top"]: "26px",
maxWidth: "fit-content",
position: "absolute",
width: "auto",
Expand All @@ -41,10 +43,10 @@ const tooltipBaseStyles: React.CSSProperties = {
padding: "6px 8px",
borderRadius: "5px",
opacity: 0,
transform: "translateY(-5px)",
transform: `translateY(${tooltipPosition === "above" ? "-5px": "5px"})`,
visibility: "hidden",
transition: "all 0.2s ease-in-out",
};
});

const toolTipVisibleStyles: React.CSSProperties = {
opacity: 1,
Expand All @@ -60,6 +62,7 @@ const CopyMailTo = ({
containerStyles = {},
tooltipStyles = {},
anchorStyles = {},
tooltipPosition = "above",
}: {
email: string;
children?: React.ReactNode;
Expand All @@ -68,6 +71,7 @@ const CopyMailTo = ({
containerStyles?: React.CSSProperties;
tooltipStyles?: React.CSSProperties;
anchorStyles?: React.CSSProperties;
tooltipPosition?: TooltipPosition;
}): JSX.Element => {
const [showCopied, setShowCopied] = React.useState(false);
const [showTooltip, setShowTooltip] = React.useState(false);
Expand Down Expand Up @@ -101,10 +105,10 @@ const CopyMailTo = ({
};

const allTooltipStyles = {
...tooltipBaseStyles,
...tooltipStyles,
...(showTooltip && toolTipVisibleStyles),
};
...tooltipBaseStyles(tooltipPosition),
...tooltipStyles,
...(showTooltip && toolTipVisibleStyles),
};

return (
<span style={allContainerStyles}>
Expand Down