using react tooltip with styled components

2019-07-18 19:26发布

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?

3条回答
姐就是有狂的资本
2楼-- · 2019-07-18 19:50

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 newly styledthe component in your React file and use it instead of original ReactTooltip component.

查看更多
beautiful°
3楼-- · 2019-07-18 19:52

One way would be

import * as React from "react";
import styled from "styled-components";
import OriginalReactTooltip from "react-tooltip";

export const ReactTooltip = styled(OriginalReactTooltip).attrs({
    className: "custom-tooltip",
})`
    &.custom-tooltip {
        background-color: red;
    }
`;
查看更多
女痞
4楼-- · 2019-07-18 19:56

As you haven't shared the code,here is the stateless component with react-tooltip

import React from "react";
import ReactTooltip from 'react-tooltip'
import {Link} from "react-router-dom"

const UserActionLink = ({to,tooltip,alignRight,btnClassname,iconClassname,name,toolTipName}) =>{

  const buttonClassname = btnClassname ? " btn mx-2 " + btnClassname : " btn mx-2 "
  const iconsClassname = iconClassname ? " fa " + iconClassname : " fa "
  const align = alignRight ? " float-right "  : " float-left "

  return (
    <span>
      <Link className={[buttonClassname , align ].join(' ')} data-tip={toolTipName} to={to}>
      <i className={iconsClassname} aria-hidden="true"></i> {name}
      </Link>
      <ReactTooltip />
    </span>
  );
}
export default UserActionLink

EDIT Here is the working code:

<div className="customTooltip">
    <ReactTooltip  effect="solid">
        {'I shall be the text and this is where I will go'}
    </ReactTooltip>
</div>
.customTooltip .__react_component_tooltip.place-top{
margin-top:0 !important;
}
.customTooltip .__react_component_tooltip.type-dark{
background-color: #e6bd32;
}
.customTooltip .__react_component_tooltip.type-dark.place-top:after {
    border-top-color: #d2ac2d;
    border-top-style: solid;
    border-top-width: 6px;
}
查看更多
登录 后发表回答