Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using react tooltip with styled components

anyone got any experience using react-tooltip with styled-components?

I tried to wrap my tooltip inside a styled component but they styles weren't fully showing

I wanted a yellow background but got a yellow background with a massive black border

I also want to place the tooltip directly over the top of where I hover, can anyone show me how to do this if possible?

code:

<div>
    <ReactTooltip className="extraClass" effect="solid">
        {'I shall be the text and this is where I will go'}
    </ReactTooltip>
</div>

how do I add the extraClass if im using styled-comps?

like image 541
donut Avatar asked Jul 26 '18 13:07

donut


People also ask

How do I customize React tooltip?

The Tooltip can be customized by using the cssClass property, which accepts custom CSS class names that define specific user-defined styles and themes to be applied on the Tooltip element.


2 Answers

This answer is maybe not by the book but I also did not manage to find a way to style at proper way using styled components.

This is my working example.

import styled from 'styled-components';
import ReactTooltip from 'react-tooltip';

export const ReactTooltipStyled = styled(ReactTooltip)`
  &.type-dark.place-top {
    background-color: blue;
    padding: 0.3rem 1rem;

    &:after { 
      border-top-color: blue;
    }
  }
`;

You just need to import the newly styled component in your React file and use it instead of original ReactTooltip component.

like image 104
Stevan Tosic Avatar answered Sep 20 '22 01:09

Stevan Tosic


For me, this solution finally worked as expected.

const StyledTooltip = styled(ReactTooltip)`
  max-width: 20vh;
  white-space: normal;
`
const Tooltip = ({ ...props }) => (
  <StyledTooltip effect="solid" multiline place="top" {...props} />
)
like image 28
yqbk Avatar answered Sep 19 '22 01:09

yqbk