Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/fluentui/e2e/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ Cypress.Commands.add('clickOn', selector => {
cy.get(selector).realClick();
});

Cypress.Commands.add('mouseDownOn', selector => {
cy.get(selector).trigger('mousedown');
});

Cypress.Commands.add('focusOn', selector => {
cy.get(selector).focus();
});
Expand Down
1 change: 1 addition & 0 deletions packages/fluentui/e2e/cypress/support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare namespace Cypress {
nothingIsFocused(): Chainable<Element>;
simulatePageMove(): Chainable<Element>;
clickOn(selector: string): Chainable<Element>;
mouseDownOn(selector: string): Chainable<Element>;
focusOn(selector: string): Chainable<Element>;
hover(selector: string): Chainable<Element>;
resizeViewport(width: number): Chainable<Element>;
Expand Down
37 changes: 37 additions & 0 deletions packages/fluentui/e2e/tests/popupMouseDownSelecting-example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as React from 'react';
import { Popup, Button, buttonClassName, popupContentClassName } from '@fluentui/react-northstar';

export const selectors = {
popupContent: popupContentClassName,
button: buttonClassName,
secondDiv: 'second-div',
};

const PopupWithoutTriggerExample = () => {
return (
<div
style={{
width: 500,
height: 500,
}}
>
<Popup
content={{
content: 'Test Content',
styles: { margin: '20px' },
}}
trigger={<Button content="Test button" />}
/>
<div
id={selectors.secondDiv}
style={{
width: 50,
height: 50,
margin: 50,
}}
/>
</div>
);
};

export default PopupWithoutTriggerExample;
15 changes: 15 additions & 0 deletions packages/fluentui/e2e/tests/popupMouseDownSelecting.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
describe('Popup without `trigger`', () => {
const selectors = { popupContent: 'ui-popup__content', button: 'ui-button' };
const button = `.${selectors.button}`;
const popupContent = `.${selectors.popupContent}`;

beforeEach(() => {
cy.gotoTestCase(__filename, button);
});

it('Popup still open on mousedown event', () => {
cy.clickOn(button);
cy.get(popupContent).trigger('mousedown', { which: 1 }).trigger('mousemove', { clientX: 300, clientY: 300 });
cy.visible(popupContent);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export const Popup: React.FC<PopupProps> &
const [isOpenedByRightClick, setIsOpenedByRightClick] = React.useState(false);

const closeTimeoutId = React.useRef<number | undefined>();

const mouseDownEventRef = React.useRef<MouseEvent | null>();
const popupContentRef = React.useRef<HTMLElement>();
const pointerTargetRef = React.useRef<HTMLElement>();
const triggerRef = React.useRef<HTMLElement>();
Expand Down Expand Up @@ -230,6 +230,13 @@ export const Popup: React.FC<PopupProps> &
});

const handleDocumentClick = (getRefs: Function) => (e: MouseEvent) => {
const currentMouseDownEvent = mouseDownEventRef.current;
mouseDownEventRef.current = null;

if (currentMouseDownEvent && !isOutsidePopupElement(getRefs(), currentMouseDownEvent)) {
return;
}

if (isOpenedByRightClick && isOutsidePopupElement(getRefs(), e)) {
trySetOpen(false, e);
rightClickReferenceObject.current = null;
Expand All @@ -241,6 +248,10 @@ export const Popup: React.FC<PopupProps> &
}
};

const handleMouseDown = (e: MouseEvent) => {
mouseDownEventRef.current = e;
};

const handleDocumentKeyDown = (getRefs: Function) => (e: KeyboardEvent) => {
const keyCode = getCode(e);
const isMatchingKey = keyCode === keyboardKey.Enter || keyCode === SpacebarKey;
Expand Down Expand Up @@ -439,6 +450,7 @@ export const Popup: React.FC<PopupProps> &

{context.target && (
<>
<EventListener listener={handleMouseDown} target={context.target} type="mousedown" />
<EventListener listener={handleDocumentClick(getRefs)} target={context.target} type="click" capture />
<EventListener
listener={handleDocumentClick(getRefs)}
Expand Down