Skip to content

Commit

Permalink
In <ContactPills>, use new "scrollToBottom" helper
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanHahn-Signal authored and josh-signal committed Mar 19, 2021
1 parent a2071d9 commit 934e0fa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
7 changes: 4 additions & 3 deletions ts/components/ContactPills.tsx
Expand Up @@ -9,6 +9,8 @@ import React, {
ReactNode,
} from 'react';

import { scrollToBottom } from '../util/scrollToBottom';

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

return (
Expand Down
47 changes: 47 additions & 0 deletions ts/test-electron/scrollToBottom_test.ts
@@ -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);
});
});
6 changes: 3 additions & 3 deletions ts/util/lint/exceptions.json
Expand Up @@ -14671,7 +14671,7 @@
"rule": "React-useRef",
"path": "ts/components/ContactPills.js",
"line": " const elRef = react_1.useRef(null);",
"lineNumber": 27,
"lineNumber": 28,
"reasonCategory": "usageTrusted",
"updated": "2021-03-01T18:34:36.638Z",
"reasonDetail": "Used for scrolling. Doesn't otherwise manipulate the DOM"
Expand All @@ -14680,7 +14680,7 @@
"rule": "React-useRef",
"path": "ts/components/ContactPills.js",
"line": " const previousChildCountRef = react_1.useRef(childCount);",
"lineNumber": 29,
"lineNumber": 30,
"reasonCategory": "usageTrusted",
"updated": "2021-03-01T18:34:36.638Z",
"reasonDetail": "Doesn't reference the DOM. Refers to a number"
Expand Down Expand Up @@ -15394,4 +15394,4 @@
"updated": "2021-01-08T15:46:32.143Z",
"reasonDetail": "Doesn't manipulate the DOM. This is just a function."
}
]
]
8 changes: 8 additions & 0 deletions ts/util/scrollToBottom.ts
@@ -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;
}

0 comments on commit 934e0fa

Please sign in to comment.