2022-03-03 20:23:10 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-03-03 20:09:58 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { FunctionComponent, ReactNode } from 'react';
|
|
|
|
import React, { useRef, useEffect, Children } from 'react';
|
2022-08-31 16:18:18 +00:00
|
|
|
import classNames from 'classnames';
|
2021-03-03 20:09:58 +00:00
|
|
|
|
2021-09-17 22:24:21 +00:00
|
|
|
import { usePrevious } from '../hooks/usePrevious';
|
2022-03-03 20:23:10 +00:00
|
|
|
import { scrollToBottom } from '../util/scrollUtil';
|
2021-03-08 20:07:37 +00:00
|
|
|
|
2021-03-03 20:09:58 +00:00
|
|
|
type PropsType = {
|
2022-08-31 16:18:18 +00:00
|
|
|
moduleClassName?: string;
|
2021-03-03 20:09:58 +00:00
|
|
|
children?: ReactNode;
|
|
|
|
};
|
|
|
|
|
2022-08-31 16:18:18 +00:00
|
|
|
export const ContactPills: FunctionComponent<PropsType> = ({
|
|
|
|
moduleClassName,
|
|
|
|
children,
|
|
|
|
}) => {
|
2021-03-03 20:09:58 +00:00
|
|
|
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 (
|
2022-08-31 16:18:18 +00:00
|
|
|
<div
|
|
|
|
className={classNames('module-ContactPills', moduleClassName)}
|
|
|
|
ref={elRef}
|
|
|
|
>
|
2021-03-03 20:09:58 +00:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|