繁体   English   中英

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

[英]using react tooltip with styled components

任何人都有使用带有styled-components react-tooltip经验吗?

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

我想要一个黄色的背景,但得到了一个带有大量黑色边框的黄色背景

我还想将工具提示直接放在我悬停的位置的顶部,如果可能的话,谁能告诉我如何做到这一点?

代码:

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

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

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

这是我的工作示例。

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 组件。

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

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

由于您还没有分享代码,这里是带有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;
}

一种方法是

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