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(controls): Prevent controls from hiding when already unmounted #1316

Merged
merged 2 commits into from
Jan 12, 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
10 changes: 8 additions & 2 deletions src/lib/viewers/controls/controls-layer/ControlsLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ export default function ControlsLayer({ children, onMount = noop }: Props): JSX.

// Visibility helpers
const helpersRef = React.useRef({
hide() {
clean() {
window.clearTimeout(hideTimeoutRef.current);
},
hide() {
helpersRef.current.clean();

hideTimeoutRef.current = window.setTimeout(() => {
if (hasCursorRef.current || hasFocusRef.current) {
Expand All @@ -40,7 +43,7 @@ export default function ControlsLayer({ children, onMount = noop }: Props): JSX.
hasFocusRef.current = false;
},
show() {
window.clearTimeout(hideTimeoutRef.current);
helpersRef.current.clean();
setIsShown(true);
},
});
Expand Down Expand Up @@ -71,6 +74,9 @@ export default function ControlsLayer({ children, onMount = noop }: Props): JSX.
onMount(helpersRef.current);
}, [onMount]);

// Destroy timeouts on unmount
React.useEffect(() => helpersRef.current.clean, []);

return (
<div
className={`bp-ControlsLayer ${isShown ? SHOW_CLASSNAME : ''}`}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { act } from 'react-dom/test-utils';
import { mount, ReactWrapper } from 'enzyme';
import ControlsLayer, { HIDE_DELAY_MS, SHOW_CLASSNAME } from '../ControlsLayer';
import ControlsLayer, { Helpers, HIDE_DELAY_MS, SHOW_CLASSNAME } from '../ControlsLayer';

describe('ControlsLayer', () => {
const children = <div className="TestControls">Controls</div>;
Expand Down Expand Up @@ -88,6 +88,21 @@ describe('ControlsLayer', () => {
});
});

describe('unmount', () => {
test('should destroy any existing hide timeout', () => {
jest.spyOn(window, 'clearTimeout');

const onMount = (helpers: Helpers): void => {
helpers.hide(); // Kick off the hide timeout
};
const wrapper = getWrapper({ onMount });

wrapper.unmount();

expect(window.clearTimeout).toBeCalledTimes(2); // Once on hide, once on unmount
});
});

describe('render', () => {
test('should invoke the onMount callback once with the visibility helpers', () => {
const onMount = jest.fn();
Expand All @@ -99,6 +114,7 @@ describe('ControlsLayer', () => {

expect(onMount).toBeCalledTimes(1);
expect(onMount).toBeCalledWith({
clean: expect.any(Function),
hide: expect.any(Function),
reset: expect.any(Function),
show: expect.any(Function),
Expand Down