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

Fix(web-react): Make Tooltip positionStrategy actually work #1560

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
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
42 changes: 22 additions & 20 deletions packages/web-react/src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,27 @@ const Tooltip = (props: SpiritTooltipProps) => {
}

// Get props for the FloatingUI hook
const { getFloatingProps, getReferenceProps, maxWidth, middlewareData, placement, refs, x, y } = useFloating({
arrowRef,
cornerOffset: tooltipCornerOffset,
flipCrossAxis,
flipFallbackAxisSideDirection,
flipFallbackPlacements,
flipProp,
isDismissible,
isFocusableOnHover,
isOpen,
offset: tooltipOffset,
onToggle,
shiftProp,
sizeProp,
positionStrategy,
tooltipArrowWidth,
tooltipMaxWidth,
tooltipPlacement,
trigger,
});
const { getFloatingProps, getReferenceProps, maxWidth, middlewareData, placement, refs, x, y, position } =
useFloating({
arrowRef,
cornerOffset: tooltipCornerOffset,
flipCrossAxis,
flipFallbackAxisSideDirection,
flipFallbackPlacements,
flipProp,
isDismissible,
isFocusableOnHover,
isOpen,
offset: tooltipOffset,
onToggle,
shiftProp,
sizeProp,
positionStrategy,
tooltipArrowWidth,
tooltipMaxWidth,
tooltipPlacement,
trigger,
});

return (
<TooltipProvider
Expand All @@ -97,6 +98,7 @@ const Tooltip = (props: SpiritTooltipProps) => {
triggerRef: refs.setReference,
x,
y,
position,
}}
>
<div
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MiddlewareData, Placement } from '@floating-ui/react';
import { MiddlewareData, Placement, Strategy } from '@floating-ui/react';
import { HTMLProps, MutableRefObject, createContext, useContext } from 'react';

type refType = ((node: HTMLElement | null) => void) & ((node: HTMLElement | null) => void);
Expand All @@ -20,6 +20,7 @@ type TooltipContextType = {
triggerRef: refType;
x: number;
y: number;
position?: Strategy;
};

const defaultContext: TooltipContextType = {
Expand All @@ -45,6 +46,7 @@ const defaultContext: TooltipContextType = {
triggerRef: () => {},
x: 0,
y: 0,
position: 'absolute',
};

const TooltipContext = createContext<TooltipContextType>(defaultContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const TooltipPopover = (props: TooltipPopoverProps) => {
tooltipRef,
x,
y,
position,
sizeMaxWidth,
tooltipMaxWidth,
} = useTooltipContext();
Expand Down Expand Up @@ -83,6 +84,7 @@ const TooltipPopover = (props: TooltipPopoverProps) => {
{...contentOtherProps}
{...getFloatingProps()}
style={{
position,
top: y ?? 0,
left: x ?? 0,
...getMaxHeightAndWidth(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('useFloatingUI', () => {
expect(result.current.getReferenceProps).toBeDefined();
expect(result.current.middlewareData).toBeDefined();
expect(result.current.placement).toBeDefined();
expect(result.current.position).toBe('absolute');
expect(result.current.refs).toBeDefined();
expect(result.current.x).toBeDefined();
expect(result.current.y).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ const meta: Meta<typeof Tooltip> = {
options: Object.values(Placements),
table: { defaultValue: { summary: 'bottom' } },
},
positionStrategy: {
control: 'select',
options: ['absolute', 'fixed'],
table: { defaultValue: { summary: 'absolute' } },
},
trigger: {
control: 'select',
options: ['click, hover', 'hover', 'click'],
Expand All @@ -55,6 +60,7 @@ const meta: Meta<typeof Tooltip> = {
isFocusableOnHover: false,
isOpen: false,
placement: 'bottom',
positionStrategy: 'absolute',
trigger: ['click', 'hover'],
},
};
Expand Down
3 changes: 2 additions & 1 deletion packages/web-react/src/components/Tooltip/useFloating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const useFloating = (props: UseTooltipUIProps) => {
: undefined;

// Floating UI library settings
const { x, y, refs, context, placement, middlewareData } = useFloatingUI({
const { x, y, floatingStyles, refs, context, placement, middlewareData } = useFloatingUI({
open: isOpen,
onOpenChange: (open, event, reason) => {
if (isHoverEnabled) {
Expand Down Expand Up @@ -159,5 +159,6 @@ export const useFloating = (props: UseTooltipUIProps) => {
refs,
x,
y,
position: floatingStyles.position as Strategy,
};
};
Loading