-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[@mantine/core] always try centering arrow on target for popover #3065
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,19 @@ | ||
import React, { forwardRef } from 'react'; | ||
import { useMantineTheme } from '@mantine/styles'; | ||
import { getArrowPositionStyles } from './get-arrow-position-styles'; | ||
import { FloatingPosition } from '../types'; | ||
|
||
interface FloatingArrowProps extends React.ComponentPropsWithoutRef<'div'> { | ||
withBorder: boolean; | ||
position: FloatingPosition; | ||
arrowSize: number; | ||
arrowOffset: number; | ||
arrowRadius: number; | ||
arrowX: number; | ||
arrowY: number; | ||
visible: boolean; | ||
} | ||
|
||
export const FloatingArrow = forwardRef<HTMLDivElement, FloatingArrowProps>( | ||
( | ||
{ | ||
withBorder, | ||
position, | ||
arrowSize, | ||
arrowOffset, | ||
arrowRadius, | ||
visible, | ||
arrowX, | ||
arrowY, | ||
...others | ||
}, | ||
ref | ||
) => { | ||
const theme = useMantineTheme(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need the theme at all anymore since the RTL direction is already handled by Floating UI |
||
|
||
({ withBorder, position, arrowSize, arrowRadius, visible, arrowX, arrowY, ...others }, ref) => { | ||
if (!visible) { | ||
return null; | ||
} | ||
|
@@ -43,9 +26,7 @@ export const FloatingArrow = forwardRef<HTMLDivElement, FloatingArrowProps>( | |
withBorder, | ||
position, | ||
arrowSize, | ||
arrowOffset, | ||
arrowRadius, | ||
dir: theme.dir, | ||
arrowX, | ||
arrowY, | ||
})} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,6 @@ | ||
import { CSSObject } from '@mantine/styles'; | ||
import type { FloatingPosition, FloatingSide, FloatingPlacement } from '../types'; | ||
|
||
function horizontalSide( | ||
placement: FloatingPlacement | 'center', | ||
arrowY: number, | ||
arrowOffset: number | ||
) { | ||
if (placement === 'center') { | ||
return { top: arrowY }; | ||
} | ||
|
||
if (placement === 'end') { | ||
return { bottom: arrowOffset }; | ||
} | ||
|
||
if (placement === 'start') { | ||
return { top: arrowOffset }; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
function verticalSide( | ||
placement: FloatingPlacement | 'center', | ||
arrowX: number, | ||
arrowOffset: number, | ||
dir: 'rtl' | 'ltr' | ||
) { | ||
if (placement === 'center') { | ||
return { [dir === 'ltr' ? 'left' : 'right']: arrowX }; | ||
} | ||
|
||
if (placement === 'end') { | ||
return { [dir === 'ltr' ? 'right' : 'left']: arrowOffset }; | ||
} | ||
|
||
if (placement === 'start') { | ||
return { [dir === 'ltr' ? 'left' : 'right']: arrowOffset }; | ||
} | ||
|
||
return {}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those two functions are not needed anymore, as position is correctly handled by Floating UI, including RTL. So we can directly set |
||
|
||
const radiusByFloatingSide: Record< | ||
FloatingSide, | ||
keyof Pick< | ||
|
@@ -62,22 +21,18 @@ export function getArrowPositionStyles({ | |
position, | ||
withBorder, | ||
arrowSize, | ||
arrowOffset, | ||
arrowRadius, | ||
arrowX, | ||
arrowY, | ||
dir, | ||
}: { | ||
position: FloatingPosition; | ||
withBorder: boolean; | ||
arrowSize: number; | ||
arrowOffset: number; | ||
arrowRadius: number; | ||
arrowX: number; | ||
arrowY: number; | ||
dir: 'rtl' | 'ltr'; | ||
}) { | ||
const [side, placement = 'center'] = position.split('-') as [FloatingSide, FloatingPlacement]; | ||
const [side] = position.split('-') as [FloatingSide, FloatingPlacement]; | ||
const baseStyles = { | ||
width: arrowSize, | ||
height: arrowSize, | ||
|
@@ -91,7 +46,7 @@ export function getArrowPositionStyles({ | |
if (side === 'left') { | ||
return { | ||
...baseStyles, | ||
...horizontalSide(placement, arrowY, arrowOffset), | ||
top: arrowY, | ||
right: arrowPosition, | ||
borderLeft: 0, | ||
borderBottom: 0, | ||
|
@@ -101,7 +56,7 @@ export function getArrowPositionStyles({ | |
if (side === 'right') { | ||
return { | ||
...baseStyles, | ||
...horizontalSide(placement, arrowY, arrowOffset), | ||
top: arrowY, | ||
left: arrowPosition, | ||
borderRight: 0, | ||
borderTop: 0, | ||
|
@@ -111,20 +66,20 @@ export function getArrowPositionStyles({ | |
if (side === 'top') { | ||
return { | ||
...baseStyles, | ||
...verticalSide(placement, arrowX, arrowOffset, dir), | ||
left: arrowX, | ||
bottom: arrowPosition, | ||
borderTop: 0, | ||
[dir === 'ltr' ? 'borderLeft' : 'borderRight']: 0, | ||
borderLeft: 0, | ||
}; | ||
} | ||
|
||
if (side === 'bottom') { | ||
return { | ||
...baseStyles, | ||
...verticalSide(placement, arrowX, arrowOffset, dir), | ||
left: arrowX, | ||
top: arrowPosition, | ||
borderBottom: 0, | ||
[dir === 'ltr' ? 'borderRight' : 'borderLeft']: 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check was actually messing things up for RTL, simply removing it fixes things |
||
borderRight: 0, | ||
}; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need the arrow offset anymore in this component, as the offset is now used as a configuration for the
arrow
middleware of floating UI directly.