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

61 lines
1.4 KiB
JavaScript
Raw Normal View History

import { debounce } from 'lodash';
import PropTypes from 'prop-types';
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-03-21 11:09:01 -07:00
export const HoverRefWrapper = ({ accountId, children, inline }) => {
const dispatch = useDispatch();
const ref = useRef();
const Elem = inline ? 'span' : 'div';
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='hover-ref-wrapper'
2022-03-21 11:09:01 -07:00
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
onClick={handleClick}
>
{children}
</Elem>
);
};
HoverRefWrapper.propTypes = {
accountId: PropTypes.string,
children: PropTypes.node,
inline: PropTypes.bool,
};
HoverRefWrapper.defaultProps = {
inline: false,
};
2022-03-21 11:09:01 -07:00
export { HoverRefWrapper as default, showProfileHoverCard };