Skip to content

Commit

Permalink
fix(Select): handle pseudo-element bounds in mouseup detection
Browse files Browse the repository at this point in the history
- Add getPseudoElementPadding utility to measure ::before and ::after dimensions
- Update mouseup detection logic to include pseudo-element padding in clickable area
- Fix unexpected Select closing when clicking within extended pseudo-element bounds

Fixes mui#1232
  • Loading branch information
onehanddev committed Dec 31, 2024
1 parent 4d8f6a7 commit d1629c2
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions packages/react/src/select/trigger/useSelectTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,47 @@ export function useSelectTrigger(

const doc = ownerDocument(event.currentTarget);

function getPseudoElementPadding(triggerRefElement: HTMLElement) {
const beforeStyles = window.getComputedStyle(triggerRefElement, '::before');
const afterStyles = window.getComputedStyle(triggerRefElement, '::after');

const hasPseudoElements =
beforeStyles.content !== 'none' ||
afterStyles.content !== 'none';

const padding = hasPseudoElements ? Math.max(
parseInt(beforeStyles.width || '0', 10),
parseInt(afterStyles.width || '0', 10),
parseInt(beforeStyles.height || '0', 10),
parseInt(afterStyles.height || '0', 10)
) / 2 : 0;

return padding;
}

function handleMouseUp(mouseEvent: MouseEvent) {
if (!triggerRef.current) {
return;
}

const mouseUpTarget = mouseEvent.target as Element | null;

if (
contains(triggerRef.current, mouseUpTarget) ||
contains(positionerElement, mouseUpTarget) ||
mouseUpTarget === triggerRef.current
) {
return;
}

const padding = getPseudoElementPadding(triggerRef.current);
const triggerRect = triggerRef.current.getBoundingClientRect();

const isInsideTrigger =
mouseEvent.clientX >= triggerRect.left &&
mouseEvent.clientX <= triggerRect.right &&
mouseEvent.clientY >= triggerRect.top &&
mouseEvent.clientY <= triggerRect.bottom;

if (
isInsideTrigger ||
contains(positionerElement, mouseUpTarget) ||
contains(triggerRef.current, mouseUpTarget)
mouseEvent.clientX >= triggerRect.left - padding &&
mouseEvent.clientX <= triggerRect.right + padding &&
mouseEvent.clientY >= triggerRect.top - padding &&
mouseEvent.clientY <= triggerRect.bottom + padding
) {
return;
}
Expand Down

0 comments on commit d1629c2

Please sign in to comment.