soapbox/app/soapbox/components/hover-ref-wrapper.tsx

61 wiersze
1.7 KiB
TypeScript
Czysty Zwykły widok Historia

import classNames from 'clsx';
import debounce from 'lodash/debounce';
import React, { useRef } from 'react';
import { fetchAccount } from 'soapbox/actions/accounts';
import {
openProfileHoverCard,
closeProfileHoverCard,
2022-11-16 13:32:32 +00:00
} from 'soapbox/actions/profile-hover-card';
import { useAppDispatch } from 'soapbox/hooks';
2022-11-15 20:48:54 +00:00
import { isMobile } from 'soapbox/is-mobile';
const showProfileHoverCard = debounce((dispatch, ref, accountId) => {
dispatch(openProfileHoverCard(ref, accountId));
2022-03-21 18:09:01 +00:00
}, 600);
2022-04-28 01:01:31 +00:00
interface IHoverRefWrapper {
accountId: string,
2022-06-17 19:39:53 +00:00
inline?: boolean,
className?: string,
2023-01-10 23:03:15 +00:00
children: React.ReactNode,
2022-04-28 01:01:31 +00:00
}
/** Makes a profile hover card appear when the wrapped element is hovered. */
export const HoverRefWrapper: React.FC<IHoverRefWrapper> = ({ accountId, children, inline = false, className }) => {
const dispatch = useAppDispatch();
2022-05-01 18:11:20 +00:00
const ref = useRef<HTMLDivElement>(null);
2022-04-28 01:01:31 +00:00
const Elem: keyof JSX.IntrinsicElements = inline ? 'span' : 'div';
2022-03-21 18:09:01 +00:00
const handleMouseEnter = () => {
if (!isMobile(window.innerWidth)) {
dispatch(fetchAccount(accountId));
showProfileHoverCard(dispatch, ref, accountId);
2022-03-21 18:09:01 +00:00
}
};
2022-03-21 18:09:01 +00:00
const handleMouseLeave = () => {
showProfileHoverCard.cancel();
setTimeout(() => dispatch(closeProfileHoverCard()), 300);
};
2022-03-21 18:09:01 +00:00
const handleClick = () => {
2020-09-11 20:30:37 +00:00
showProfileHoverCard.cancel();
dispatch(closeProfileHoverCard(true));
};
return (
<Elem
ref={ref}
className={classNames('hover-ref-wrapper', className)}
2022-03-21 18:09:01 +00:00
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onClick={handleClick}
>
{children}
</Elem>
);
};
2022-03-21 18:09:01 +00:00
export { HoverRefWrapper as default, showProfileHoverCard };