Using Popper for Tooltips with TypeScript and Styled-components
July 29, 2020
- javascript
- typescript
- tooltip
- styled-components
In this article, I share my journey of creating a customized tooltip using Popper.js in a React application with TypeScript and styled-components.
Introduction
I needed to create a customized tooltip for a project, and Popper.js (https://popper.js.org) came to mind. However, this was my first time using Popper with React, so I needed to familiarize myself with it.
Exploring the Documentation
My first step was to dive into the Popper.js documentation. Right away, it suggested using the hook-based API instead of the older render props API. Since I prefer hooks, this suited me well. The documentation provided a comprehensive example of using hooks, which I found on this page: https://popper.js.org/react-popper/v2/.
Here’s the basic example provided:
import React, { useState } from 'react';
import { usePopper } from 'react-popper';
const Example = () => {
const [referenceElement, setReferenceElement] = useState(null);
const [popperElement, setPopperElement] = useState(null);
const [arrowElement, setArrowElement] = useState(null);
const { styles, attributes } = usePopper(referenceElement, popperElement, {
modifiers: [{ name: 'arrow', options: { element: arrowElement } }],
});
return (
<>
<button type="button" ref={setReferenceElement}>
Reference element
</button>
<div ref={setPopperElement} style={styles.popper} {...attributes.popper}>
Popper element
<div ref={setArrowElement} style={styles.arrow} />
</div>
</>
);
};
Adding Styled-components and TypeScript
While the basic example was helpful, I needed to integrate styled-components and TypeScript to fully customize the tooltip. During my research, I found a helpful article on Medium that explained how to use usePopper
with styled-components in React: https://medium.com/javascript-in-plain-english/usepopper-with-styled-components-for-react-react-popper-2-568284d029bf. The complete code example is available on CodeSandbox: https://codesandbox.io/s/blue-worker-w9rtu?file=/src/App.js.
Implementing the Tooltip
Here’s how I implemented the tooltip using usePopper
, styled-components, and TypeScript:
import React, { useState, useRef } from 'react';
import { usePopper } from 'react-popper';
import styled from 'styled-components';
const Dropdown = () => {
const [showPopper, setShowPopper] = useState(false);
const buttonRef = useRef(null);
const popperRef = useRef(null);
// the ref for the arrow must be a callback ref
const [arrowRef, setArrowRef] = useState(null);
const { styles, attributes } = usePopper(buttonRef.current, popperRef.current, {
modifiers: [
{
name: 'arrow',
options: {
element: arrowRef,
},
},
{
name: 'offset',
options: {
offset: [0, 10],
},
},
],
});
return (
<Page>
<Info>
<p>Scroll down and right to find the popper!</p>
</Info>
<Button ref={buttonRef} onClick={() => setShowPopper(!showPopper)}>
Click Me
</Button>
{showPopper ? (
<PopperContainer ref={popperRef} style={styles.popper} {...attributes.popper}>
<div ref={setArrowRef} style={styles.arrow} id="arrow" />
<p>I'm a popper</p>
</PopperContainer>
) : null}
</Page>
);
};
// Basic page styling
const Page = styled.div`
width: 250%;
height: 250%;
display: flex;
align-items: center;
justify-content: space-around;
`;
const Info = styled.div`
background: lightgreen;
padding: 1rem;
position: absolute;
top: 20px;
left: 20px;
`;
const Button = styled.button`
background: lightblue;
border: none;
box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.3);
width: 150px;
height: 50px;
outline: none;
`;
const PopperContainer = styled.div`
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
border-radius: 5px;
background-color: white;
padding: 20px;
text-align: center;
#arrow {
position: absolute;
width: 10px;
height: 10px;
&:after {
content: ' ';
background-color: white;
box-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
position: absolute;
top: -25px; // padding + popper height
left: 0;
transform: rotate(45deg);
width: 10px;
height: 10px;
}
}
&[data-popper-placement^='top'] > #arrow {
bottom: -30px;
:after {
box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.1);
}
}
`;
Handling TypeScript Issues
Initially, I encountered a TypeScript error when setting the ref for the arrow element:
<div ref={setArrowRef} style={styles.arrow} className="arrow" />
The error was:
Type 'Dispatch<SetStateAction<null>>' is not assignable to type 'string | ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined'.
Type 'Dispatch<SetStateAction<null>>' is not assignable to type '(instance: HTMLDivElement | null) => void'.
Types of parameters 'value' and 'instance' are incompatible.
Type 'HTMLDivElement | null' is not assignable to type 'SetStateAction<null>'.
Type 'HTMLDivElement' is not assignable to type 'SetStateAction<null>'.
Type 'HTMLDivElement' provides no match for the signature '(prevState: null): null'
To resolve this, I typed the useState
hook like this:
- const [arrowRef, setArrowRef] = useState(null);
+ const [arrowRef, setArrowRef] = useState<HTMLDivElement | null>(null);
This resolved the issue, and you can see the complete working example here: https://codesandbox.io/s/usepopper-styled-components-xblqg?file=/src/components/Tooltip.tsx:317-390.
Conclusion
Integrating Popper.js with styled-components and TypeScript can enhance the functionality and appearance of your tooltips. This approach provides a robust and flexible way to manage tooltips in a React application. If you found this article helpful or have any suggestions, please feel free to comment or share.