In <ContactPills>, use new "scrollToBottom" helper

This commit is contained in:
Evan Hahn 2021-03-08 14:07:37 -06:00 committed by Josh Perez
parent a2071d9fa6
commit 934e0fa415
4 changed files with 62 additions and 6 deletions

View file

@ -9,6 +9,8 @@ import React, {
ReactNode, ReactNode,
} from 'react'; } from 'react';
import { scrollToBottom } from '../util/scrollToBottom';
type PropsType = { type PropsType = {
children?: ReactNode; children?: ReactNode;
}; };
@ -24,10 +26,9 @@ export const ContactPills: FunctionComponent<PropsType> = ({ children }) => {
useEffect(() => { useEffect(() => {
const hasAddedNewChild = childCount > previousChildCount; const hasAddedNewChild = childCount > previousChildCount;
const el = elRef.current; const el = elRef.current;
if (!hasAddedNewChild || !el) { if (hasAddedNewChild && el) {
return; scrollToBottom(el);
} }
el.scrollTop = el.scrollHeight;
}, [childCount, previousChildCount]); }, [childCount, previousChildCount]);
return ( return (

View file

@ -0,0 +1,47 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { scrollToBottom } from '../util/scrollToBottom';
describe('scrollToBottom', () => {
let sandbox: HTMLDivElement;
beforeEach(() => {
sandbox = document.createElement('div');
document.body.appendChild(sandbox);
});
afterEach(() => {
sandbox.remove();
});
it("sets the element's scrollTop to the element's scrollHeight", () => {
const el = document.createElement('div');
el.innerText = 'a'.repeat(50000);
Object.assign(el.style, {
height: '50px',
overflow: 'scroll',
whiteSpace: 'wrap',
width: '100px',
wordBreak: 'break-word',
});
sandbox.appendChild(el);
assert.strictEqual(
el.scrollTop,
0,
'Test is not set up correctly. Element is already scrolled'
);
assert.isAtLeast(
el.scrollHeight,
50,
'Test is not set up correctly. scrollHeight is too low'
);
scrollToBottom(el);
assert.isAtLeast(el.scrollTop, el.scrollHeight - 50);
});
});

View file

@ -14671,7 +14671,7 @@
"rule": "React-useRef", "rule": "React-useRef",
"path": "ts/components/ContactPills.js", "path": "ts/components/ContactPills.js",
"line": " const elRef = react_1.useRef(null);", "line": " const elRef = react_1.useRef(null);",
"lineNumber": 27, "lineNumber": 28,
"reasonCategory": "usageTrusted", "reasonCategory": "usageTrusted",
"updated": "2021-03-01T18:34:36.638Z", "updated": "2021-03-01T18:34:36.638Z",
"reasonDetail": "Used for scrolling. Doesn't otherwise manipulate the DOM" "reasonDetail": "Used for scrolling. Doesn't otherwise manipulate the DOM"
@ -14680,7 +14680,7 @@
"rule": "React-useRef", "rule": "React-useRef",
"path": "ts/components/ContactPills.js", "path": "ts/components/ContactPills.js",
"line": " const previousChildCountRef = react_1.useRef(childCount);", "line": " const previousChildCountRef = react_1.useRef(childCount);",
"lineNumber": 29, "lineNumber": 30,
"reasonCategory": "usageTrusted", "reasonCategory": "usageTrusted",
"updated": "2021-03-01T18:34:36.638Z", "updated": "2021-03-01T18:34:36.638Z",
"reasonDetail": "Doesn't reference the DOM. Refers to a number" "reasonDetail": "Doesn't reference the DOM. Refers to a number"

View file

@ -0,0 +1,8 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
export function scrollToBottom(el: HTMLElement): void {
// We want to mutate the parameter here.
// eslint-disable-next-line no-param-reassign
el.scrollTop = el.scrollHeight;
}