Skip to content
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

WRR-976: ContextualPopupDecorator: Modified to update position when screen orientation change #1722

Open
wants to merge 1 commit into
base: release/2.7.x.develop
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

The following is a curated list of changes in the Enact sandstone module, newest changes on the top.

## [unreleased]

### Fixed

- `sandstone/ContextualPopupDecorator` to update popup position properly when the screen orientation change

## [2.7.18] - 2024-09-05

### Added
Expand Down
26 changes: 26 additions & 0 deletions ContextualPopupDecorator/ContextualPopupDecorator.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global ResizeObserver */

/**
* A higher-order component to add a Sandstone styled popup to a component.
*
Expand Down Expand Up @@ -271,6 +273,7 @@
activator: null
};

this.resizeObserver = null;
this.overflow = {};
this.adjustedDirection = this.props.direction;
this.id = this.generateId();
Expand All @@ -290,6 +293,12 @@
on('keydown', this.handleKeyDown);
on('keyup', this.handleKeyUp);
}

if (typeof ResizeObserver === 'function') {
this.resizeObserver = new ResizeObserver(() => {
this.positionContextualPopup();

Check warning on line 299 in ContextualPopupDecorator/ContextualPopupDecorator.js

View check run for this annotation

Codecov / codecov/patch

ContextualPopupDecorator/ContextualPopupDecorator.js#L299

Added line #L299 was not covered by tests
});
}
}

getSnapshotBeforeUpdate (prevProps, prevState) {
Expand Down Expand Up @@ -339,6 +348,11 @@
off('keyup', this.handleKeyUp);
}
Spotlight.remove(this.state.containerId);

if (this.resizeObserver) {
this.resizeObserver.disconnect();
this.resizeObserver = null;
}
}

generateId = () => {
Expand Down Expand Up @@ -594,6 +608,18 @@

getClientNode = (node) => {
this.clientNode = ReactDOM.findDOMNode(node); // eslint-disable-line react/no-find-dom-node

if (this.resizeObserver) {
if (node) {
// It is not easy to trigger changed position of activator,
// so we chose to observe the `div` element's size that has the real size below the root of floatLayer.
// This implementation is dependent on the current structure of FloatingLayer,
// so if the structure have changed, below code needs to be changed accordingly.
this.resizeObserver.observe(node?.parentElement?.parentElement);
} else {
this.resizeObserver.disconnect();

Check warning on line 620 in ContextualPopupDecorator/ContextualPopupDecorator.js

View check run for this annotation

Codecov / codecov/patch

ContextualPopupDecorator/ContextualPopupDecorator.js#L618-L620

Added lines #L618 - L620 were not covered by tests
}
}
};

handle = handle.bind(this);
Expand Down
36 changes: 36 additions & 0 deletions ContextualPopupDecorator/tests/ContextualPopupDecorator-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,4 +505,40 @@ describe('ContextualPopupDecorator Specs', () => {
expect(scrimDivFirst).toHaveClass(expectedFirst);
expect(scrimDivSecond).toHaveClass(expectedSecond);
});

test('should create and observe with `ResizeObserver` when the popup opened and disconnect when the popup closed', () => {
const originalObserver = global.ResizeObserver;

const MockObserverInstance = {
observe: jest.fn(),
disconnect: jest.fn()
};
global.ResizeObserver = jest.fn().mockImplementation(() => MockObserverInstance);

const Root = FloatingLayerDecorator('div');
const {rerender} = render(
<Root>
<ContextualButton data-testid="contextualButton" open popupComponent={() => <div><Button>Button</Button></div>}>
Hello
</ContextualButton>
</Root>
);

const contextualButton = screen.getByTestId('contextualButton');

expect(contextualButton).toBeInTheDocument();
expect(MockObserverInstance.observe).toHaveBeenCalled();

rerender(
<Root>
<ContextualButton data-testid="contextualButton" popupComponent={() => <div><Button>Button</Button></div>}>
Hello
</ContextualButton>
</Root>
);

expect(MockObserverInstance.disconnect).toHaveBeenCalled();

global.ResizeObserver = originalObserver;
});
});