diff --git a/src/components/comments.js b/src/components/comments.js index 5323f09..3500f00 100644 --- a/src/components/comments.js +++ b/src/components/comments.js @@ -207,16 +207,15 @@ export default function Comments({ ) : null} ) : null} -
{ - setCommentsShowing(true) - if (commentCount === 0) { - setAddingNewComment(true) - } - }} - > - +
+ { + setCommentsShowing(true) + if (commentCount === 0) { + setAddingNewComment(true) + } + }} + > {commentCount === 0 ? 'New comment' : `Show ${commentCount} comment${commentCount > 1 ? 's' : ''}`} diff --git a/src/components/reactButton.js b/src/components/reactButton.js index 68c61f3..44708a1 100644 --- a/src/components/reactButton.js +++ b/src/components/reactButton.js @@ -1,10 +1,25 @@ -import React, { useState } from 'react' +import React, { useState, useEffect, useRef } from 'react' import Tooltip from './tooltip.js' import styles from './reactButton.module.css' import reactions from '../data/reactions.json' +function useOutsideAlerter(ref, action) { + useEffect(() => { + function handleClickOutside(event) { + if (ref.current && !ref.current.contains(event.target)) { + action() + } + } + + document.addEventListener('mousedown', handleClickOutside) + return () => { + document.removeEventListener('mousedown', handleClickOutside) + } + }, [ref]) +} + function ExistingReacts({ existingReacts, onClick, uid }) { return (
@@ -69,6 +84,10 @@ function RenderEmojis({ onClick }) { export default function ReactButton({ onClick, existingReacts, uid }) { const [opened, setOpened] = useState(false) + const wrapperRef = useRef(null) + useOutsideAlerter(wrapperRef, () => { + setOpened(false) + }) return (
{ - setOpened(false) + // setOpened(false) }} > )} > -
+
{ // setOpened(false)