Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style using styled components #221

Open
net-bet opened this issue Aug 17, 2020 · 6 comments
Open

Style using styled components #221

net-bet opened this issue Aug 17, 2020 · 6 comments

Comments

@net-bet
Copy link

net-bet commented Aug 17, 2020

Hi,

Is it possible to style the tooltip using styled components?
When I wrap the components with styled(Tooltip)'
// all css here
'
It doesn't work.

I know I can use my own styled container and pass it to overlay, but I need a solution to style the arrow....

@07akioni
Copy link

it may not be easy to do this since content is detached to document.body. use plain css instead

@zaycker
Copy link

zaycker commented Mar 9, 2021

we created our own layer through the prefixCls. it's not an ideal but works... and it requires to define all styles (copy-paste from original css and replace prefix with &- or &&- sometimes)

const StyledTooltip = styled.div`
  position: absolute;
  color: #fff;
  background: #000;
  border-radius: 0.5rem;
  width: 18.75rem;
  padding: 2.375rem 1.5625rem;
  box-sizing: border-box;

  &-hidden {
    display: none;
  }

  &-arrow {
    position: absolute;
    width: 1.625rem;
    height: 1.625rem;
    left: 0;
    top: 0;

    background: #000;;

    border-radius: 0.5rem;
    transform: matrix(0.71, -0.74, 0.68, 0.71, 0, 0);
  }

  &&-placement-left &-arrow {
    top: calc(50% - 0.8125rem);
    left: calc(100% - 1.1875rem);
  }

  ...
`;

type StyledTooltipByPrefixClsProps = ComponentProps<typeof RCTooltip> & {
  className?: string;
};

const StyledTooltipByPrefixCls: FC<StyledTooltipByPrefixClsProps> = ({
  className,
  ...props
}) => <RCTooltip prefixCls={className} {...props} />;

export const Tooltip: FC<TooltipProps> = (props) => {
  const { overlay, closeWithButton, placement } = props;

  const overlayModified = (
    <>
      {closeWithButton && (
        <StyledCloseButton onClick={() => props.onVisibleChange?.(false)}>
          <CrossSvg />
        </StyledCloseButton>
      )}
      {typeof overlay === 'function' ? overlay() : overlay}
    </>
  );

  // blahblahlbah

  return (
    <StyledTooltip
      as={StyledTooltipByPrefixCls}
      {...props}
      overlay={overlayModified}
    />
  );
}

@VasilikiLoukoumi
Copy link

VasilikiLoukoumi commented May 14, 2021

You can change the arrow and whatever you like by using the function getTooltipContainer. By default it returns the body element but you can make your own wrapper element (styled component) and use that.
For example:

//styled component - wrapper element

export const StyledDiv = styled.div`
  .rc-tooltip-arrow {
      border-right-color: red;
      }`;

//ref

const divRef = useRef();

//getTooltipContainer built-in function

const getTooltipContainer = () => divRef.current;

//Tooltip wrapped in your styled component

<StyledDiv ref={divRef}><Tooltip getTooltipContainer = { getTooltipContainer } /> </StyledDiv>

@VasilikiLoukoumi
Copy link

VasilikiLoukoumi commented May 17, 2021

Or... you can override it in your Theme

@andymerskin
Copy link

Another option is using Styled Components' global styles helper, and rc-tooltip's default set of classnames. A little more straightforward than reworking all of the classNames by hand:

import { createGlobalStyle } from 'styled-components';

export const MyTooltipStyles = createGlobalStyle`
  .rc-tooltip.rc-tooltip-zoom-appear,
  .rc-tooltip.rc-tooltip-zoom-enter {
    opacity: 0;
  }
  .rc-tooltip.rc-tooltip-zoom-enter,
  .rc-tooltip.rc-tooltip-zoom-leave {
    display: block;
  }
  ...
`;

Customize away! And then just render this component wherever you want these to brought in. Since tooltips are usually an app-wide component, render <MyTooltipStyles /> at your app's root somewhere.

@Argam11
Copy link

Argam11 commented Jul 26, 2024

const Tooltip = ({
  children,
  title,
  visible,
  className
}) => {
  return (
    <RcTooltip
      trigger="click"
      visible={visible}
      placement="top"
      prefixCls={`${className} rc-tooltip`} // <---
      overlay={...}
    >
      {children}
    </RcTooltip>
  );
};

const StyledTooltip = styled(Tooltip)`
    // styles...
`;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants