Skip to content

Commit 7c9f409

Browse files
authored
feat: reverse order of event log entries (#178)
1 parent dcc799e commit 7c9f409

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

src/components/DomEvents.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import usePlayground from '../hooks/usePlayground';
66
import state from '../lib/state';
77
import { eventMap } from '@testing-library/dom/dist/event-map';
88
import { VirtualScrollable } from './Scrollable';
9-
import { FixedSizeList as List } from 'react-window';
109
import throttle from 'lodash.throttle';
1110
import AutoSizer from 'react-virtualized-auto-sizer';
1211
import IconButton from './IconButton';
1312
import TrashcanIcon from './TrashcanIcon';
1413
import EmptyStreetImg from '../images/EmptyStreetImg';
14+
import StickyList from './StickyList';
1515

1616
function onStateChange({ markup, query, result }) {
1717
state.save({ markup, query });
@@ -137,9 +137,8 @@ function DomEvents() {
137137
if (node) {
138138
previewRef.current = node;
139139
const eventListeners = addLoggingEvents(node, (event) => {
140-
// insert at index 0
141140
event.id = buffer.current.length;
142-
buffer.current.splice(0, 0, event);
141+
buffer.current.push(event);
143142
setTimeout(flush, 0);
144143
});
145144
setEventListeners(eventListeners);
@@ -196,7 +195,9 @@ function DomEvents() {
196195
) : (
197196
<AutoSizer>
198197
{({ width, height }) => (
199-
<List
198+
<StickyList
199+
follow={true}
200+
mode="bottom"
200201
ref={listRef}
201202
height={height}
202203
itemCount={eventCount}
@@ -206,7 +207,7 @@ function DomEvents() {
206207
outerElementType={VirtualScrollable}
207208
>
208209
{EventRecord}
209-
</List>
210+
</StickyList>
210211
)}
211212
</AutoSizer>
212213
)}

src/components/StickyList.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React, { forwardRef, useEffect, useRef } from 'react';
2+
import { FixedSizeList as List } from 'react-window';
3+
4+
function StickyList(
5+
{
6+
follow = false,
7+
mode = 'bottom',
8+
height,
9+
itemCount,
10+
itemData,
11+
itemSize,
12+
width,
13+
outerElementType,
14+
children,
15+
},
16+
ref,
17+
) {
18+
const innerRef = useRef();
19+
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+
}
29+
}
30+
}, [itemCount, follow]);
31+
32+
return (
33+
<List
34+
ref={ref}
35+
height={height}
36+
itemCount={itemCount}
37+
itemData={itemData}
38+
itemSize={itemSize}
39+
width={width}
40+
innerRef={innerRef}
41+
outerElementType={outerElementType}
42+
>
43+
{children}
44+
</List>
45+
);
46+
}
47+
48+
export default forwardRef(StickyList);

0 commit comments

Comments
 (0)