2021-03-03 20:09:58 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import React, {
|
|
|
|
useRef,
|
|
|
|
useEffect,
|
|
|
|
Children,
|
|
|
|
FunctionComponent,
|
|
|
|
ReactNode,
|
|
|
|
} from 'react';
|
|
|
|
|
2021-03-19 00:22:17 +00:00
|
|
|
import { usePrevious } from '../util/hooks';
|
2021-03-08 20:07:37 +00:00
|
|
|
import { scrollToBottom } from '../util/scrollToBottom';
|
|
|
|
|
2021-03-03 20:09:58 +00:00
|
|
|
type PropsType = {
|
|
|
|
children?: ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ContactPills: FunctionComponent<PropsType> = ({ children }) => {
|
|
|
|
const elRef = useRef<null | HTMLDivElement>(null);
|
|
|
|
|
|
|
|
const childCount = Children.count(children);
|
2021-03-19 00:22:17 +00:00
|
|
|
const previousChildCount = usePrevious(0, childCount);
|
2021-03-03 20:09:58 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const hasAddedNewChild = childCount > previousChildCount;
|
|
|
|
const el = elRef.current;
|
2021-03-08 20:07:37 +00:00
|
|
|
if (hasAddedNewChild && el) {
|
|
|
|
scrollToBottom(el);
|
2021-03-03 20:09:58 +00:00
|
|
|
}
|
|
|
|
}, [childCount, previousChildCount]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="module-ContactPills" ref={elRef}>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|