Skip to content

Commit 63c2940

Browse files
authored
fix: move target node for event logger a few nodes down (#190)
1 parent a6719ed commit 63c2940

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

src/components/DomEvents.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ function DomEvents() {
157157
<MarkupEditor markup={markup} dispatch={dispatch} />
158158
</div>
159159

160-
<div className="flex-auto h-56 md:h-full" ref={setPreviewRef}>
160+
<div className="flex-auto h-56 md:h-full">
161161
<Preview
162+
forwardedRef={setPreviewRef}
162163
markup={markup}
163164
elements={result.elements}
164165
accessibleRoles={result.accessibleRoles}
@@ -196,7 +197,6 @@ function DomEvents() {
196197
<AutoSizer>
197198
{({ width, height }) => (
198199
<StickyList
199-
follow={true}
200200
mode="bottom"
201201
ref={listRef}
202202
height={height}

src/components/Preview.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import React, { useState, useEffect, useRef, useMemo } from 'react';
1+
import React, {
2+
useState,
3+
useEffect,
4+
useRef,
5+
useMemo,
6+
useCallback,
7+
} from 'react';
28
import Scrollable from './Scrollable';
39
import PreviewHint from './PreviewHint';
410
import AddHtml from './AddHtml';
@@ -8,7 +14,14 @@ function selectByCssPath(rootNode, cssPath) {
814
return rootNode?.querySelector(cssPath.toString().replace(/^body > /, ''));
915
}
1016

11-
function Preview({ markup, accessibleRoles, elements, dispatch, variant }) {
17+
function Preview({
18+
markup,
19+
accessibleRoles,
20+
elements,
21+
dispatch,
22+
variant,
23+
forwardedRef,
24+
}) {
1225
// Okay, listen up. `highlighted` can be a number of things, as I wanted to
1326
// keep a single variable to represent the state. This to reduce bug count
1427
// by creating out-of-sync states.
@@ -33,7 +46,13 @@ function Preview({ markup, accessibleRoles, elements, dispatch, variant }) {
3346
element: highlighted,
3447
});
3548

36-
// TestingLibraryDom?.getSuggestedQuery(highlighted, 'get').toString() : null
49+
const refSetter = useCallback((node) => {
50+
if (typeof forwardedRef === 'function') {
51+
forwardedRef(node || null);
52+
}
53+
54+
htmlRoot.current = node;
55+
}, []);
3756

3857
useEffect(() => {
3958
const container = document.createElement('div');
@@ -154,7 +173,7 @@ function Preview({ markup, accessibleRoles, elements, dispatch, variant }) {
154173
className="preview"
155174
onClick={handleClick}
156175
onMouseMove={handleMove}
157-
ref={htmlRoot}
176+
ref={refSetter}
158177
dangerouslySetInnerHTML={{
159178
__html: actualMarkup,
160179
}}

src/components/StickyList.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { FixedSizeList as List } from 'react-window';
33

44
function StickyList(
55
{
6-
follow = false,
76
mode = 'bottom',
87
height,
98
itemCount,
@@ -17,17 +16,20 @@ function StickyList(
1716
) {
1817
const innerRef = useRef();
1918
useEffect(() => {
20-
if (ref.current && follow && innerRef.current) {
21-
if (
22-
mode === 'bottom' &&
23-
innerRef.current.offsetHeight > ref.current.props.height
24-
) {
25-
ref.current.scrollTo(innerRef.current.offsetHeight);
26-
} else if (mode === 'top') {
27-
ref.current.scrollTo(0);
28-
}
19+
if (!ref.current || !innerRef.current) {
20+
return;
2921
}
30-
}, [itemCount, follow]);
22+
23+
if (innerRef.current.offsetHeight <= ref.current.props.height) {
24+
return;
25+
}
26+
27+
if (mode === 'bottom') {
28+
ref.current.scrollTo(innerRef.current.offsetHeight);
29+
} else {
30+
ref.current.scrollTo(0);
31+
}
32+
}, [itemCount]);
3133

3234
return (
3335
<List

0 commit comments

Comments
 (0)