使用带有样式组件的react工具提示

时间:2018-07-26 13:57:56

标签: javascript css reactjs tooltip styled-components

有人在react-tooltip上使用styled-components有经验吗?

我试图将工具提示包裹在一个样式化的组件中,但是它们的样式无法完全显示

我想要一个黄色背景,但有一个带有黑色边框的黄色背景

我还想将工具提示直接放在我悬停的顶部上方,如果可以的话,谁能告诉我该怎么做?

代码:

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

如果我使用styles-comps,如何添加extraClass?

4 个答案:

答案 0 :(得分:3)

对我来说,该解决方案终于按预期工作了。

const StyledTooltip = styled(ReactTooltip)`
  max-width: 20vh;
  white-space: normal;
`
const Tooltip = ({ ...props }) => (
  <StyledTooltip effect="solid" multiline place="top" {...props} />
)

答案 1 :(得分:1)

由于您尚未共享代码,因此这里是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

编辑 这是工作代码:

<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;
}

答案 2 :(得分:1)

这个答案可能不是本书所提供的,但我也没有设法找到一种使用样式化组件以正确方式进行样式化的方法。

这是我的工作示例。

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;
    }
  }
`;

您只需要在React文件中导入新样式的组件,然后使用它代替原始的ReactTooltip组件即可。

答案 3 :(得分:0)

一种方法是

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;
    }
`;