Skip to content

Commit

Permalink
Fix dropdown menu position in playground when scroll (#4496)
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyuan0801 authored Jun 19, 2023
1 parent 2a71261 commit b364470
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions packages/lexical-playground/src/ui/DropDown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

import * as React from 'react';
import {
ReactNode,
useCallback,
Expand All @@ -14,7 +15,6 @@ import {
useRef,
useState,
} from 'react';
import * as React from 'react';
import {createPortal} from 'react-dom';

type DropDownContextType = {
Expand All @@ -23,6 +23,8 @@ type DropDownContextType = {

const DropDownContext = React.createContext<DropDownContextType | null>(null);

const dropDownPadding = 4;

export function DropDownItem({
children,
className,
Expand Down Expand Up @@ -167,7 +169,7 @@ export default function DropDown({

if (showDropDown && button !== null && dropDown !== null) {
const {top, left} = button.getBoundingClientRect();
dropDown.style.top = `${top + 40}px`;
dropDown.style.top = `${top + button.offsetHeight + dropDownPadding}px`;
dropDown.style.left = `${Math.min(
left,
window.innerWidth - dropDown.offsetWidth - 20,
Expand Down Expand Up @@ -200,6 +202,28 @@ export default function DropDown({
}
}, [dropDownRef, buttonRef, showDropDown, stopCloseOnClickSelf]);

useEffect(() => {
const handleButtonPositionUpdate = () => {
if (showDropDown) {
const button = buttonRef.current;
const dropDown = dropDownRef.current;
if (button !== null && dropDown !== null) {
const {top} = button.getBoundingClientRect();
const newPosition = top + button.offsetHeight + dropDownPadding;
if (newPosition !== dropDown.getBoundingClientRect().top) {
dropDown.style.top = `${newPosition}px`;
}
}
}
};

document.addEventListener('scroll', handleButtonPositionUpdate);

return () => {
document.removeEventListener('scroll', handleButtonPositionUpdate);
};
}, [buttonRef, dropDownRef, showDropDown]);

return (
<>
<button
Expand Down

2 comments on commit b364470

@vercel
Copy link

@vercel vercel bot commented on b364470 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical-fbopensource.vercel.app
www.lexical.dev
lexical-git-main-fbopensource.vercel.app
lexicaljs.com
lexical.dev
lexicaljs.org

@vercel
Copy link

@vercel vercel bot commented on b364470 Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

lexical-playground-fbopensource.vercel.app
lexical-playground-git-main-fbopensource.vercel.app
lexical-playground.vercel.app
playground.lexical.dev

Please sign in to comment.