-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Init tooltip * Add react-popper * Add title prop and fix children usage * Improve styles and add example * Add visibility toggle through hovering * Add tests * Fix test warnings
- Loading branch information
Showing
8 changed files
with
173 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#### default example: | ||
|
||
```js | ||
<Tooltip title="this is the actual tooltip"> | ||
<button>this is the reference element</button> | ||
</Tooltip> | ||
``` | ||
|
||
#### placed on top example: | ||
|
||
```js | ||
<Tooltip title="this tooltip is on the top of the reference" placement="top"> | ||
<button>this is the reference element</button> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from "react" | ||
import { Meta, Story } from "@storybook/react/types-6-0" | ||
|
||
import Tooltip, { ITooltipProps } from "./Tooltip" | ||
|
||
export default { | ||
title: "Design System/Tooltip", | ||
component: Tooltip, | ||
} as Meta | ||
|
||
const Template: Story<ITooltipProps> = (props) => <Tooltip {...props} /> | ||
|
||
export const Default = Template.bind({}) | ||
|
||
Default.args = { | ||
children: <button>I am the reference</button>, | ||
title: "I'm the title", | ||
} | ||
|
||
export const TopPlacement = Template.bind({}) | ||
|
||
TopPlacement.args = { | ||
children: <button>I am the reference</button>, | ||
title: "I'm the title", | ||
placement: "top", | ||
} |
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,31 @@ | ||
import "@testing-library/jest-dom/extend-expect" | ||
import React from "react" | ||
import { render, waitFor } from "@testing-library/react" | ||
|
||
import Tooltip from "./Tooltip" | ||
|
||
describe("Tooltip", () => { | ||
it("renders successfully", async () => { | ||
const { getByTestId } = render( | ||
<Tooltip title="Title"> | ||
<button>hover me</button> | ||
</Tooltip> | ||
) | ||
|
||
/** | ||
* Check if the element exists | ||
*/ | ||
|
||
await waitFor(() => expect(getByTestId("tooltip")).toBeInTheDocument()) | ||
}) | ||
|
||
it("renders the children succesfully", async () => { | ||
const { getByText } = render( | ||
<Tooltip title="Title"> | ||
<button>hover me</button> | ||
</Tooltip> | ||
) | ||
|
||
await waitFor(() => expect(getByText("hover me")).toBeInTheDocument()) | ||
}) | ||
}) |
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,96 @@ | ||
/** @jsxRuntime classic /* | ||
/** @jsx jsx */ | ||
import React, { Fragment, useState } from "react" | ||
import { jsx, Text } from "theme-ui" | ||
import { usePopper } from "react-popper" | ||
|
||
type Placement = | ||
| "auto" | ||
| "auto-start" | ||
| "auto-end" | ||
| "top" | ||
| "top-start" | ||
| "top-end" | ||
| "bottom" | ||
| "bottom-start" | ||
| "bottom-end" | ||
| "right" | ||
| "right-start" | ||
| "right-end" | ||
| "left" | ||
| "left-start" | ||
| "left-end" | ||
|
||
export interface ITooltipProps { | ||
title: string | ||
children: React.ReactChild | ||
placement?: Placement | ||
} | ||
|
||
export default function Tooltip({ | ||
children, | ||
title, | ||
placement = "bottom", | ||
}: ITooltipProps) { | ||
const [popperElement, setPopperElement] = useState<HTMLDivElement | null>( | ||
null | ||
) | ||
const [ | ||
referenceElement, | ||
setReferenceElement, | ||
] = useState<HTMLSpanElement | null>(null) | ||
const [arrowElement, setArrowElement] = useState<HTMLDivElement | null>(null) | ||
const { styles, attributes } = usePopper(referenceElement, popperElement, { | ||
modifiers: [ | ||
{ name: "arrow", options: { element: arrowElement } }, | ||
{ name: "offset", options: { offset: [0, 8] } }, | ||
], | ||
placement, | ||
}) | ||
|
||
return ( | ||
<Fragment> | ||
<span | ||
sx={{ | ||
"&:hover": { | ||
"+div": { | ||
visibility: "visible", | ||
opacity: 1, | ||
}, | ||
}, | ||
}} | ||
ref={setReferenceElement} | ||
data-testid="tooltip" | ||
> | ||
{children} | ||
</span> | ||
<div | ||
sx={{ | ||
visibility: "hidden", | ||
opacity: 0, | ||
paddingY: 1, | ||
paddingX: 2, | ||
borderRadius: "6px", | ||
backgroundColor: "text", | ||
boxShadow: "0px 1px 4px rgba(0, 0, 0, 0.2)", | ||
transition: "all .2s linear", | ||
transitionDelay: ".2s", | ||
}} | ||
ref={setPopperElement} | ||
style={styles.popper} | ||
{...attributes.popper} | ||
> | ||
<Text | ||
variant="body2" | ||
sx={{ | ||
color: "#fff", | ||
fontWeight: 600, | ||
}} | ||
> | ||
{title} | ||
</Text> | ||
<div ref={setArrowElement} style={styles.arrow} /> | ||
</div> | ||
</Fragment> | ||
) | ||
} |
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
39269b5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: