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: Recalculate height of ZoomElement when child element updates #15472

Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
1c6f503
remove duplicated function browserSupportsCssZoom
Tomastomaslol Jul 3, 2021
1a3f03b
update zoomElement heigth when a child element mutate using a Mutatio…
Tomastomaslol Dec 7, 2021
5b681d1
Merge branch 'next' of github.com:storybookjs/storybook into issue-15…
Tomastomaslol Dec 7, 2021
cd25d9a
handle scenario no MutationObserver, clean up ZoomElement
Tomastomaslol Dec 19, 2021
60cad0c
Merge branch 'next' of github.com:storybookjs/storybook into issue-15…
Tomastomaslol Jan 18, 2022
8d0d673
merged next into issue 15368, only add ref with observer if browser d…
Tomastomaslol Mar 15, 2022
926ada3
Merge branch 'next' of github.com:storybookjs/storybook into issue-15…
Tomastomaslol Mar 16, 2022
6686b6f
merged with remote origin next
Tomastomaslol May 10, 2022
849c7fe
Merge branch 'next' of github.com:storybookjs/storybook into issue-15…
Tomastomaslol May 10, 2022
cd01b77
Merge branch 'next' of github.com:storybookjs/storybook into issue-15…
Tomastomaslol May 15, 2022
cd7bce9
Merge branch 'next' into issue-15368-ZoomElement-do-not-resize-on-chi…
ndelangen Jun 30, 2022
413c6df
feedback from code review
Tomastomaslol Jul 8, 2022
f7739fb
Merge branch 'next' of github.com:storybookjs/storybook into issue-15…
Tomastomaslol Jul 8, 2022
d35ed61
Merge branch 'next' into issue-15368-ZoomElement-do-not-resize-on-chi…
ndelangen Sep 13, 2022
d06ab1c
remove unnecessary check if observer is defined
Tomastomaslol Sep 14, 2022
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
13 changes: 0 additions & 13 deletions lib/components/src/Zoom/Zoom.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import global from 'global';
import { ZoomElement } from './ZoomElement';
import { ZoomIFrame } from './ZoomIFrame';

const { window: globalWindow } = global;

export const browserSupportsCssZoom = (): boolean => {
Tomastomaslol marked this conversation as resolved.
Show resolved Hide resolved
try {
return (
globalWindow.document.implementation.createHTMLDocument('').body.style.zoom !== undefined
);
} catch (error) {
return false;
}
};

export const Zoom = {
Element: ZoomElement,
IFrame: ZoomIFrame,
Expand Down
55 changes: 53 additions & 2 deletions lib/components/src/Zoom/ZoomElement.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import React, { ReactElement, useEffect, useRef, useState } from 'react';
/* global MutationObserver */
import React, {
ReactElement,
useEffect,
useRef,
useState,
useMemo,
RefObject,
useCallback,
} from 'react';
import { styled } from '@storybook/theming';
import { browserSupportsCssZoom } from './browserSupportsCssZoom';

Expand All @@ -15,6 +24,35 @@ const ZoomElementWrapper = styled.div<{ scale: number; height: number }>(({ scal
transform: `scale(${1 / scale})`,
}
);

const useMutationObserver = ({
Tomastomaslol marked this conversation as resolved.
Show resolved Hide resolved
element,
options = {},
callback,
}: {
element: RefObject<Element>;
options: MutationObserverInit;
callback: MutationCallback;
}): void => {
const observer = useMemo(
() =>
typeof MutationObserver === 'function'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this check, MutationObserver has been available in all browsers since 2014. Save for Opera Mini which frankly nobody is going to be using to view their Storybook in anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I strongly agree with this, Storybook 7.0 will target chrome100. If it's supported in there, don't bother downgrading, transpiling, of feature detecting!

? new MutationObserver((mutationRecord, mutationObserver) => {
callback?.(mutationRecord, mutationObserver);
})
: undefined,
[callback]
);

useEffect(() => {
if (observer && element?.current) {
observer.observe(element.current, options);
}

return () => observer?.disconnect();
}, [element, observer, options]);
};

type ZoomProps = {
scale: number;
children: ReactElement | ReactElement[];
Expand All @@ -24,15 +62,28 @@ export function ZoomElement({ scale, children }: ZoomProps) {
const componentWrapperRef = useRef<HTMLDivElement>(null);
const [height, setHeight] = useState(0);

const handleMutations = useCallback(() => {
setHeight(componentWrapperRef.current.getBoundingClientRect().height);
}, []);

useEffect(() => {
if (componentWrapperRef.current) {
setHeight(componentWrapperRef.current.getBoundingClientRect().height);
}
}, [scale, componentWrapperRef.current]);

useMutationObserver({
element: componentWrapperRef,
options: { subtree: true, childList: true },
Copy link
Member

@ghengeveld ghengeveld Jul 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options should be defined outside the component render function (or memoized, but that seems silly), otherwise we'll have a new object with each render, which will cause the useEffect above to disconnect and re-observe each time.

callback: handleMutations,
});

return (
<ZoomElementWrapper scale={scale} height={height}>
<div ref={componentWrapperRef} className="innerZoomElementWrapper">
<div
ref={browserSupportsCssZoom() ? null : componentWrapperRef}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just call browserSupportsCssZoom once, outside the component render function, put it in a variable and use that here. Otherwise we'll be running this check over and over again.

className="innerZoomElementWrapper"
>
{children}
</div>
</ZoomElementWrapper>
Expand Down