-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
regression: emojiPicker position (#29408)
- Loading branch information
1 parent
01a5a6b
commit ed84cdf
Showing
3 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
apps/meteor/client/views/composer/EmojiPicker/EmojiPickerDesktopDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Box, Tile } from '@rocket.chat/fuselage'; | ||
import { useMergedRefs, usePosition } from '@rocket.chat/fuselage-hooks'; | ||
import type { ReactNode, Ref, RefObject } from 'react'; | ||
import React, { useMemo, useRef, forwardRef } from 'react'; | ||
|
||
const getDropdownContainer = (descendant: HTMLElement | null) => { | ||
for (let element = descendant ?? document.body; element !== document.body; element = element.parentElement ?? document.body) { | ||
if ( | ||
getComputedStyle(element).transform !== 'none' || | ||
getComputedStyle(element).position === 'fixed' || | ||
getComputedStyle(element).willChange === 'transform' | ||
) { | ||
return element; | ||
} | ||
} | ||
|
||
return document.body; | ||
}; | ||
|
||
const useDropdownPosition = (reference: RefObject<HTMLElement>, target: RefObject<HTMLElement>) => { | ||
const innerContainer = getDropdownContainer(reference.current); | ||
const boundingRect = innerContainer.getBoundingClientRect(); | ||
|
||
const viewHeight = document.body.getBoundingClientRect().height; | ||
const refTop = reference.current?.getBoundingClientRect().top ?? 0; | ||
const targetHeight = target.current?.getBoundingClientRect().height || 0; | ||
|
||
const placement = useMemo(() => { | ||
if (boundingRect.top === 0) { | ||
return 'top-start'; | ||
} | ||
if (refTop >= viewHeight / 2) { | ||
return 'top-end'; | ||
} | ||
return 'bottom-end'; | ||
}, [targetHeight, refTop]); | ||
|
||
const maxHeight = useMemo(() => (placement === 'bottom-end' ? '482px' : `${refTop - 12}px`), [placement, refTop]); | ||
|
||
const { style } = usePosition(reference, target, { | ||
placement, | ||
container: innerContainer, | ||
}); | ||
|
||
return useMemo(() => { | ||
return { ...style, maxHeight }; | ||
}, [style]); | ||
}; | ||
|
||
type EmojiPickerDesktopDropdownProps = { | ||
children: ReactNode; | ||
reference: RefObject<HTMLElement>; | ||
}; | ||
|
||
const EmojiPickerDesktopDropdown = forwardRef(function ToolboxDropdownDesktop( | ||
{ reference, children }: EmojiPickerDesktopDropdownProps, | ||
ref: Ref<HTMLElement>, | ||
) { | ||
const targetRef = useRef<HTMLElement>(null); | ||
const mergedRef = useMergedRefs(ref, targetRef); | ||
|
||
const style = useDropdownPosition(reference, targetRef); | ||
|
||
return ( | ||
<Tile is='ul' style={style} ref={mergedRef} elevation='2' pi='0' pb='0' display='flex' flexDirection='column' overflow='auto'> | ||
<Box flexShrink={1} pb='x12'> | ||
{children} | ||
</Box> | ||
</Tile> | ||
); | ||
}); | ||
|
||
export default EmojiPickerDesktopDropdown; |
28 changes: 28 additions & 0 deletions
28
apps/meteor/client/views/composer/EmojiPicker/EmojiPickerDropDown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Dropdown as DropdownMobile } from '@rocket.chat/fuselage'; | ||
import { useLayout } from '@rocket.chat/ui-contexts'; | ||
import type { ReactNode } from 'react'; | ||
import React, { forwardRef } from 'react'; | ||
|
||
import EmojiPickerDesktopDropdown from './EmojiPickerDesktopDropdown'; | ||
|
||
type EmojiPickerDropdownProps<R> = { | ||
children: ReactNode; | ||
reference: React.RefObject<R>; | ||
}; | ||
|
||
const EmojiPickerDropdown = forwardRef(function EmojiPickerDropdown<TReferenceElement extends HTMLElement>( | ||
{ children, reference }: EmojiPickerDropdownProps<TReferenceElement>, | ||
ref: React.ForwardedRef<HTMLElement>, | ||
) { | ||
const { isMobile } = useLayout(); | ||
|
||
const Dropdown = isMobile ? DropdownMobile : EmojiPickerDesktopDropdown; | ||
|
||
return ( | ||
<Dropdown ref={ref} reference={reference}> | ||
{children} | ||
</Dropdown> | ||
); | ||
}); | ||
|
||
export default EmojiPickerDropdown; |