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

fix(reply): Fix at mention list misaligned after window resize #678

Merged
merged 2 commits into from
Jan 29, 2021
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
8 changes: 8 additions & 0 deletions src/components/ReplyField/ReplyField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ export default class ReplyField extends React.Component<Props, State> {
fetchCollaborators(trimmedQuery);
}, DEFAULT_COLLAB_DEBOUNCE);

handleResize = debounce(() => this.updatePopupReference(), 100);

componentDidMount(): void {
// The browser’s native zoom doesn’t always trigger Preview’s resize
window.addEventListener('resize', this.handleResize);
}

componentDidUpdate({ editorState: prevEditorState }: Props): void {
const { editorState } = this.props;

Expand All @@ -67,6 +74,7 @@ export default class ReplyField extends React.Component<Props, State> {
}

componentWillUnmount(): void {
window.removeEventListener('resize', this.handleResize);
this.saveCursorPosition();
}

Expand Down
28 changes: 28 additions & 0 deletions src/components/ReplyField/__tests__/ReplyField-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,34 @@ describe('ReplyField', () => {
});
});

describe('componentDidMount()', () => {
test('should add resize window event listener', () => {
const instance = getWrapper().instance();

jest.spyOn(window, 'addEventListener');
jest.spyOn(instance, 'handleResize');

instance.componentDidMount();

expect(window.addEventListener).toHaveBeenCalledWith('resize', instance.handleResize);
});
});

describe('componentWillUnmount()', () => {
test('should remove resize event listener and save cursor position', () => {
const instance = getWrapper().instance();

jest.spyOn(window, 'removeEventListener');
jest.spyOn(instance, 'handleResize');
jest.spyOn(instance, 'saveCursorPosition');

instance.componentWillUnmount();

expect(window.removeEventListener).toHaveBeenCalledWith('resize', instance.handleResize);
expect(instance.saveCursorPosition).toHaveBeenCalled();
});
});

describe('event handlers', () => {
test('should handle the editor change event', () => {
const wrapper = getWrapper();
Expand Down