Skip to content

Commit 9eeab1f

Browse files
fix: resolve NextPrev slider width measurement in modals
Replace react-d3-utils ResizeObserver with manual implementation using useLayoutEffect to fix incorrect `width` calculation when component is rendered inside modals. The previous implementation measured `width` before the modal was fully rendered
1 parent 0282d97 commit 9eeab1f

File tree

3 files changed

+52
-28
lines changed

3 files changed

+52
-28
lines changed

src/component/elements/NextPrev.tsx

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import styled from '@emotion/styled';
22
import type { CSSProperties, ReactElement } from 'react';
3-
import { Children } from 'react';
4-
import { useResizeObserver } from 'react-d3-utils';
3+
import { Children, useLayoutEffect, useRef, useState } from 'react';
54
import { FaAngleLeft } from 'react-icons/fa';
65

76
type Direction = 'right' | 'left';
@@ -90,10 +89,35 @@ interface NextPrevProps {
9089

9190
export function NextPrev(props: NextPrevProps) {
9291
const { children, index = 0, onChange = () => null, style = {} } = props;
93-
const [ref, { width } = { width: 0 }] = useResizeObserver();
9492
const slidersCount = Children.count(children);
9593
const lastIndex = slidersCount > 0 ? slidersCount - 1 : 0;
9694
const activeIndex = Math.min(index, lastIndex);
95+
const containerRef = useRef<HTMLDivElement>(null);
96+
97+
const [width, setWidth] = useState(0);
98+
99+
useLayoutEffect(() => {
100+
const container = containerRef.current;
101+
if (!container) return;
102+
103+
const observer = new ResizeObserver((entries) => {
104+
for (const entry of entries) {
105+
const newWidth = entry.contentRect.width;
106+
if (newWidth > 0) {
107+
setWidth(newWidth);
108+
}
109+
}
110+
});
111+
112+
observer.observe(container);
113+
114+
const rect = container.getBoundingClientRect();
115+
if (rect.width > 0) {
116+
setWidth(rect.width);
117+
}
118+
119+
return () => observer.disconnect();
120+
}, []);
97121

98122
function nextHandler() {
99123
if (index < lastIndex) {
@@ -113,7 +137,7 @@ export function NextPrev(props: NextPrevProps) {
113137
const slidersWidth = width * slidersCount;
114138

115139
return (
116-
<Container ref={ref}>
140+
<Container ref={containerRef}>
117141
<TransformController
118142
translation={translation}
119143
slidersWidth={slidersWidth}

src/component/modal/changeSum/ChangeSumModal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function ChangeSumModal(props: ChangeSumModalProps) {
3535
<Dialog
3636
isOpen
3737
onClose={closeDialog}
38-
style={{ width: 500 }}
38+
style={{ width: 500, minHeight: 450 }}
3939
title={
4040
currentSum
4141
? `Set new ${sumType} sum (Current: ${currentSum.toFixed(2)})`

src/component/modal/changeSum/SelectMolecule.tsx

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,29 +87,29 @@ export default function SelectMolecule<
8787
[setValue],
8888
);
8989

90-
return (
91-
<div>
92-
{element && molecules && molecules.length > 0 ? (
93-
<Container>
94-
<Title>Select a molecule as reference!</Title>
90+
if (element && molecules && molecules.length > 0) {
91+
return (
92+
<Container>
93+
<Title>Select a molecule as reference!</Title>
94+
95+
<SelectionContainer>
96+
<MoleculeSelection
97+
index={currentIndex}
98+
molecules={molecules}
99+
onChange={onChangeMoleculeSelectionHandler}
100+
/>
101+
<SumValue>
102+
New sum for {element} will be {newSum}!
103+
</SumValue>
104+
</SelectionContainer>
105+
</Container>
106+
);
107+
}
95108

96-
<SelectionContainer>
97-
<MoleculeSelection
98-
index={currentIndex}
99-
molecules={molecules}
100-
onChange={onChangeMoleculeSelectionHandler}
101-
/>
102-
<SumValue>
103-
New sum for {element} will be {newSum}!
104-
</SumValue>
105-
</SelectionContainer>
106-
</Container>
107-
) : (
108-
<EmptyText color={invalid ? 'red' : 'black'}>
109-
You have to Select a spectrum and Add a molecule from the Structure
110-
panel to select as a reference!
111-
</EmptyText>
112-
)}
113-
</div>
109+
return (
110+
<EmptyText color={invalid ? 'red' : 'black'}>
111+
You have to Select a spectrum and Add a molecule from the Structure panel
112+
to select as a reference!
113+
</EmptyText>
114114
);
115115
}

0 commit comments

Comments
 (0)