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

Refactor tooltip - use floating-ui/react library instead floating-uiu… #803

Open
wants to merge 1 commit into
base: development/1.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 33 additions & 45 deletions src/lib/components/tooltip/Tooltip.component.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { computePosition, flip, offset, shift } from '@floating-ui/dom';
import { CSSProperties, useEffect, useRef, useState } from 'react';
import { CSSProperties, useState } from 'react';
import styled from 'styled-components';
import { spacing } from '../../spacing';
import { fontSize, zIndex } from '../../style/theme';
import { getThemePropSelector } from '../../utils';
import { useFloating, useHover, useInteractions } from '@floating-ui/react';

Check failure on line 6 in src/lib/components/tooltip/Tooltip.component.tsx

View workflow job for this annotation

GitHub Actions / tests

Cannot find module '@floating-ui/react' or its corresponding type declarations.
export const TOP = 'top';
export const BOTTOM = 'bottom';
export const LEFT = 'left';
Expand Down Expand Up @@ -40,11 +40,9 @@
display: inline-block;
`;
const TooltipOverLayContainer = styled.div<{
placement: Position;
style?: CSSProperties;
}>`
display: inline-block;
opacity: 0;
position: fixed;
width: max-content;
border: 1px solid ${getThemePropSelector('border')};
Expand Down Expand Up @@ -85,51 +83,41 @@
overlay,
...rest
}: Props) {
const childrenRef = useRef<HTMLDivElement | null>(null);
const tooltipRef = useRef<HTMLDivElement | null>(null);

const [isTooltipVisible, setIsTooltipVisible] = useState(false);
useEffect(() => {
if (childrenRef.current && tooltipRef.current) {
computePosition(childrenRef.current, tooltipRef.current, {
placement,
middleware: [offset(5), shift(), flip()],
}).then(({ x, y }) => {
if (tooltipRef.current) {
Object.assign(tooltipRef.current.style, {
opacity: '1',
// we set opacity to 1 to make sure the tooltip is not displayed before the position is computed
left: `${x}px`,
top: `${y}px`,
});
}
});
}
}, [tooltipRef.current, childrenRef.current, isTooltipVisible]);

const { refs, floatingStyles, context } = useFloating({
open: isTooltipVisible,
onOpenChange: setIsTooltipVisible,
placement: placement,
});

const hover = useHover(context);

const { getReferenceProps, getFloatingProps } = useInteractions([hover]);
return (
<TooltipContainer
className="sc-tooltip"
onPointerEnter={() => {
setIsTooltipVisible(true);
}}
onPointerLeave={() => {
setIsTooltipVisible(false);
}}
>
{isTooltipVisible && overlay ? (
<TooltipOverLayContainer
ref={tooltipRef}
className="sc-tooltip-overlay"
placement={placement}
<>
<TooltipContainer className="sc-tooltip">
{isTooltipVisible && overlay ? (
<TooltipOverLayContainer
className="sc-tooltip-overlay"
ref={refs.setFloating}
style={floatingStyles}
{...getFloatingProps()}
>
<TooltipText className="sc-tooltip-overlay-text">
{overlay}
</TooltipText>
</TooltipOverLayContainer>
) : null}
<div
style={overlayStyle}
ref={refs.setReference}
{...getReferenceProps()}
>
<TooltipText className="sc-tooltip-overlay-text">
{overlay}
</TooltipText>
</TooltipOverLayContainer>
) : null}
<div ref={childrenRef}>{children}</div>
</TooltipContainer>
{children}
</div>
</TooltipContainer>
</>
);
}

Expand Down
Loading