简体   繁体   English

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

[英]using react tooltip with styled components

anyone got any experience using react-tooltip with styled-components ?任何人都有使用带有styled-components react-tooltip经验吗?

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?如果我使用 styled-comps,我该如何添加 extraClass?

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.你只需要在你的 React 文件中导入新样式的组件并使用它而不是原始的 ReactTooltip 组件。

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} />
)

As you haven't shared the code,here is the stateless component with react-tooltip由于您还没有分享代码,这里是带有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;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM