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 flickering on desktop modeler tab change #238

Merged
merged 3 commits into from
Apr 19, 2023
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
11 changes: 6 additions & 5 deletions src/PropertiesPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
useState,
useEffect,
useMemo,
useRef
useRef,
useLayoutEffect
} from 'preact/hooks';

import {
Expand Down Expand Up @@ -122,7 +123,7 @@ export default function PropertiesPanel(props) {
const [ layout, setLayout ] = useState(createLayout(layoutConfig));

// react to external changes in the layout config
useUpdateEffect(() => {
useUpdateLayoutEffect(() => {
const newLayout = createLayout(layoutConfig);

setLayout(newLayout);
Expand Down Expand Up @@ -258,15 +259,15 @@ function createDescriptionContext(overrides = {}) {
// hooks //////////////////

/**
* This hook behaves like useEffect, but does not trigger on the first render.
* This hook behaves like useLayoutEffect, but does not trigger on the first render.
*
* @param {Function} effect
* @param {Array} deps
*/
function useUpdateEffect(effect, deps) {
function useUpdateLayoutEffect(effect, deps) {
const isMounted = useRef(false);

useEffect(() => {
useLayoutEffect(() => {
if (isMounted.current) {
return effect();
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/useStickyIntersectionObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export function useStickyIntersectionObserver(ref, scrollContainerSelector, setS
const scrollContainer = domQuery(scrollContainerSelector);

observer = new Observer((entries) => {

// The ScrollContainer is unmounted, do not update sticky state
if (scrollContainer.scrollHeight === 0) {
return;
}

entries.forEach(entry => {
if (entry.intersectionRatio < 1) {
setSticky(true);
Expand Down
38 changes: 38 additions & 0 deletions test/spec/hooks/useStickyIntersectionObserver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import { useStickyIntersectionObserver } from 'src/hooks';

import { renderHook } from '@testing-library/preact-hooks';

import TestContainer from 'mocha-test-container-support';

describe('hooks/userStickyIntersectionObserver', function() {

const OriginalIntersectionObserver = global.IntersectionObserver;

let container;
beforeEach(function() {
container = TestContainer.get(this);
});

afterEach(function() {
global.IntersectionObserver = OriginalIntersectionObserver;
});
Expand Down Expand Up @@ -86,6 +92,38 @@ describe('hooks/userStickyIntersectionObserver', function() {
});


it('should not call when scrollContainer is unmounted', async function() {

// given
const callbackSpy = sinon.spy();

const triggerCallback = mockIntersectionObserver({});

const domObject = <div></div>;

const scrollContainer = document.createElement('div');
scrollContainer.setAttribute('id', 'scrollContainer');
container.appendChild(scrollContainer);

const ref = { current: domObject };

await renderHook(() => {
useStickyIntersectionObserver(ref, '#scrollContainer', callbackSpy);

return domObject;
});

// when
scrollContainer.remove();
triggerCallback([
{ intersectionRatio: 1 }
]);

// then
expect(callbackSpy).not.to.have.been.called;
});


it('should unobserve after unmount', async function() {

// given
Expand Down
Loading