-
-
Notifications
You must be signed in to change notification settings - Fork 51.7k
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
Dropdown "judders" if it can't figure out where to determine placement #51916
Comments
It seems like you're experiencing an issue with the dropdown menu in Ant Design where it "judders" due to placement determination problems. This issue might be linked to changes in the To address this, you can try the following steps:
If none of these solutions work, it might be a good idea to keep an eye on the linked GitHub issues for any updates or fixes from the maintainers. For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me. |
OK, let's see how good this AI bot is... @petercat-assistant With regard to your "custom trigger logic" suggestion, how can the dropdown position/placement be set programmatically? I'm not seeing a way to override that here: https://ant.design/components/dropdown#dropdown |
To set the dropdown position or placement programmatically in Ant Design, you can use the
Here's an example of how you can use these properties: import React from 'react';
import { Dropdown, Menu } from 'antd';
const menu = (
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item key="2">Option 2</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
</Menu>
);
const CustomDropdown = () => {
return (
<Dropdown
overlay={menu}
placement="bottomLeft" // Set your preferred placement
getPopupContainer={triggerNode => triggerNode.parentNode} // Customize the container
>
<a className="ant-dropdown-link" onClick={e => e.preventDefault()}>
Hover me
</a>
</Dropdown>
);
};
export default CustomDropdown; In this example, the dropdown is set to appear at the For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me. |
@petercat-assistant Yes, I know how to set the |
To implement custom trigger logic for setting the dropdown position programmatically, you can use a combination of event handlers and state management to calculate available space and adjust the dropdown's position dynamically. Here's a general approach you can take:
Here's a basic example to illustrate this approach: import React, { useState } from 'react';
import { Dropdown, Menu } from 'antd';
const menu = (
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item key="2">Option 2</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
</Menu>
);
const CustomDropdown = () => {
const [placement, setPlacement] = useState('bottomLeft');
const handleVisibleChange = (visible) => {
if (visible) {
const triggerElement = document.querySelector('.ant-dropdown-link');
const rect = triggerElement.getBoundingClientRect();
const windowHeight = window.innerHeight;
const windowWidth = window.innerWidth;
// Calculate available space and set placement
if (rect.bottom + 200 > windowHeight) {
setPlacement('topLeft');
} else if (rect.right + 200 > windowWidth) {
setPlacement('bottomRight');
} else {
setPlacement('bottomLeft');
}
}
};
return (
<Dropdown
overlay={menu}
placement={placement}
onVisibleChange={handleVisibleChange}
>
<a className="ant-dropdown-link" onClick={e => e.preventDefault()}>
Hover me
</a>
</Dropdown>
);
};
export default CustomDropdown; In this example, the You can expand this logic to include more complex calculations or additional placements based on your specific layout requirements. For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me. |
To implement custom trigger logic for setting the dropdown position programmatically, you can use a combination of JavaScript to calculate the available space and dynamically adjust the dropdown's position. Here's a general approach you can take:
Here's a basic example of how you might implement this logic: import React, { useState, useRef, useEffect } from 'react';
import { Dropdown, Menu } from 'antd';
const menu = (
<Menu>
<Menu.Item key="1">Option 1</Menu.Item>
<Menu.Item key="2">Option 2</Menu.Item>
<Menu.Item key="3">Option 3</Menu.Item>
</Menu>
);
const CustomDropdown = () => {
const [placement, setPlacement] = useState('bottomLeft');
const triggerRef = useRef(null);
useEffect(() => {
const calculatePlacement = () => {
if (triggerRef.current) {
const rect = triggerRef.current.getBoundingClientRect();
const viewportHeight = window.innerHeight;
const viewportWidth = window.innerWidth;
// Example logic to determine placement
if (rect.bottom + 200 > viewportHeight) {
setPlacement('topLeft');
} else if (rect.right + 200 > viewportWidth) {
setPlacement('bottomRight');
} else {
setPlacement('bottomLeft');
}
}
};
calculatePlacement();
window.addEventListener('resize', calculatePlacement);
return () => {
window.removeEventListener('resize', calculatePlacement);
};
}, []);
return (
<Dropdown
overlay={menu}
placement={placement}
getPopupContainer={triggerNode => triggerNode.parentNode}
>
<a ref={triggerRef} className="ant-dropdown-link" onClick={e => e.preventDefault()}>
Hover me
</a>
</Dropdown>
);
};
export default CustomDropdown; In this example, the You can refine the logic to better suit your specific layout and requirements. For further assistance, please describe your question in the comments and @petercat-assistant to start a conversation with me. |
The easiest way to do this is to change the width of the Actions { title: "Actions",width: "120px"}, or move the position of the Actions around, guessing it's due to the columns not being wide enough.This doesn't look like a bug. |
@zhaohuaxuan Are you saying that, if you have a column with a dropdown, you need to set a static width to the column that is greater than the width of the dropdown contents? If so, I'd say that's either a bug or bad DX. That said, this issue occurs if you portal the dropdown to another element which, to me, nullifies your assertion. |
@afc163 @MadCcc @thinkasany @zombieJ I fixed the underlying cause of this issue in rc-trigger: react-component/trigger#510 |
Reproduction link
https://codesandbox.io/p/sandbox/8334zm
Steps to reproduce
What is expected?
The action menu to correctly determine where to open based on available space.
What is actually happening?
It endlessly "judders", causing a horrible user experience.
Additional information
This reproduction users a specific column width to easily replicate the issue, but I have seen this issue numerous other locations outside of a table.
This seems to be caused by an rc-trigger change.
react-component/trigger#496
react-component/trigger#419
ref: UIEN-6867
The text was updated successfully, but these errors were encountered: