From 4c72a69b6cafa670e25181a5acba4771d3eab18d Mon Sep 17 00:00:00 2001 From: keyan Date: Thu, 20 Jan 2022 13:03:48 -0600 Subject: [PATCH] refine clickToContext handling for notifications/flat comments --- components/comment.js | 12 +++++++----- components/comments-flat.js | 6 +++++- components/notifications.js | 7 ++++++- components/upvote.js | 9 +-------- lib/clicks.js | 9 +++++++++ 5 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 lib/clicks.js diff --git a/components/comment.js b/components/comment.js index 2ef1b22a..8d4ac013 100644 --- a/components/comment.js +++ b/components/comment.js @@ -18,7 +18,7 @@ function Parent ({ item, rootText }) { <> \ - e.stopPropagation()} className='text-reset'>parent + parent ) @@ -32,7 +32,7 @@ function Parent ({ item, rootText }) { {Number(item.root.id) !== Number(item.parentId) && } \ - e.stopPropagation()} className='text-reset'>{rootText || 'on:'} {item.root.title} + {rootText || 'on:'} {item.root.title} ) @@ -87,11 +87,11 @@ export default function Comment ({ \ } - e.stopPropagation()} className='text-reset'>{item.ncomments} replies + {item.ncomments} replies \ - e.stopPropagation()}>@{item.user.name}{op && ' OP'} + @{item.user.name}{op && ' OP'} @@ -103,7 +103,9 @@ export default function Comment ({ \
setEdit(!edit)} + onClick={e => { + setEdit(!edit) + }} > {edit ? 'cancel' : 'edit'} { + onClick={e => { + if (ignoreClick(e)) { + return + } router.push({ pathname: '/items/[id]', query: { id: item.root.id, commentId: item.id } diff --git a/components/notifications.js b/components/notifications.js index 850237ae..b986c6fc 100644 --- a/components/notifications.js +++ b/components/notifications.js @@ -5,13 +5,18 @@ import { NOTIFICATIONS } from '../fragments/notifications' import { useRouter } from 'next/router' import MoreFooter from './more-footer' import Invite from './invite' +import { ignoreClick } from '../lib/clicks' function Notification ({ n }) { const router = useRouter() return (
{ + onClick={e => { + if (ignoreClick(e)) { + return + } + if (n.__typename === 'Invitification') { router.push('/invites') } else if (!n.item.title) { diff --git a/components/upvote.js b/components/upvote.js index cac3b4de..337c88cf 100644 --- a/components/upvote.js +++ b/components/upvote.js @@ -178,12 +178,10 @@ export default function UpVote ({ item, className }) { return ( {({ strike }) => -
+
{ - e.preventDefault() - e.stopPropagation() if (!item || voteLock) return // we can't tip ourselves @@ -198,8 +196,6 @@ export default function UpVote ({ item, className }) { onShortPress={ me ? async (e) => { - e.preventDefault() - e.stopPropagation() if (!item || voteLock) return // we can't tip ourselves @@ -256,9 +252,6 @@ export default function UpVote ({ item, className }) { filter: `drop-shadow(0 0 6px ${color}90)` } : undefined} - onClick={e => { - e.stopPropagation() - }} />
diff --git a/lib/clicks.js b/lib/clicks.js new file mode 100644 index 00000000..02432310 --- /dev/null +++ b/lib/clicks.js @@ -0,0 +1,9 @@ +export function ignoreClick (e) { + return e.target.onclick || // the target has a click handler + // the target has an interactive parent + e.target.matches(':where(.upvoteParent, form, textarea, button, a, input) :scope') || + // the target is an interactive element + ['TEXTAREA', 'BUTTON', 'A', 'INPUT', 'FORM'].includes(e.target.tagName.toUpperCase()) || + // the target is an interactive element + e.target.class === 'upvoteParent' +}