bigbuffet-rw/app/soapbox/components/hover_ref_wrapper.tsx

58 lines
1.5 KiB
TypeScript
Raw Normal View History

import classNames from 'classnames';
import debounce from 'lodash/debounce';
import React, { useRef } from 'react';
2022-01-10 14:01:24 -08:00
import { useDispatch } from 'react-redux';
import {
openProfileHoverCard,
closeProfileHoverCard,
} from 'soapbox/actions/profile_hover_card';
import { isMobile } from 'soapbox/is_mobile';
const showProfileHoverCard = debounce((dispatch, ref, accountId) => {
dispatch(openProfileHoverCard(ref, accountId));
2022-03-21 11:09:01 -07:00
}, 600);
2022-04-27 18:01:31 -07:00
interface IHoverRefWrapper {
accountId: string,
inline: boolean,
className?: string,
2022-04-27 18:01:31 -07:00
}
/** Makes a profile hover card appear when the wrapped element is hovered. */
export const HoverRefWrapper: React.FC<IHoverRefWrapper> = ({ accountId, children, inline = false, className }) => {
2022-03-21 11:09:01 -07:00
const dispatch = useDispatch();
2022-05-01 11:11:20 -07:00
const ref = useRef<HTMLDivElement>(null);
2022-04-27 18:01:31 -07:00
const Elem: keyof JSX.IntrinsicElements = inline ? 'span' : 'div';
2022-03-21 11:09:01 -07:00
const handleMouseEnter = () => {
if (!isMobile(window.innerWidth)) {
showProfileHoverCard(dispatch, ref, accountId);
2022-03-21 11:09:01 -07:00
}
};
2022-03-21 11:09:01 -07:00
const handleMouseLeave = () => {
showProfileHoverCard.cancel();
setTimeout(() => dispatch(closeProfileHoverCard()), 300);
};
2022-03-21 11:09:01 -07:00
const handleClick = () => {
2020-09-11 13:30:37 -07:00
showProfileHoverCard.cancel();
dispatch(closeProfileHoverCard(true));
};
return (
<Elem
ref={ref}
className={classNames('hover-ref-wrapper', className)}
2022-03-21 11:09:01 -07:00
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onClick={handleClick}
>
{children}
</Elem>
);
};
2022-03-21 11:09:01 -07:00
export { HoverRefWrapper as default, showProfileHoverCard };