Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add hash to reply in article detail page #592

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion components/ArticleReplySummary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useRef } from 'react';
import { t, jt } from 'ttag';
import gql from 'graphql-tag';
import { ProfileTooltip } from 'components/ProfileLink';
Expand All @@ -25,11 +25,16 @@ const useStyles = makeStyles(theme => ({
'& > a': { textDecoration: 'none' },
'& > a:hover': { textDecoration: 'underline' },
},
hash: {
marginLeft: theme.spacing(1),
scrollMargin: `${theme.spacing(15)}px`,
},
ydcjeff marked this conversation as resolved.
Show resolved Hide resolved
}));

function ArticleReplySummary({ articleReply, className, ...props }) {
const { replyType, user } = articleReply;

const anchorRef = useRef();
const classes = useStyles({ replyType });

const authorElem = (
Expand All @@ -38,9 +43,23 @@ function ArticleReplySummary({ articleReply, className, ...props }) {
</ProfileTooltip>
);

useEffect(() => {
if (anchorRef.current.hash === location.hash) {
anchorRef.current.scrollIntoView({ behavior: 'smooth' });
}
}, []);

Comment on lines +46 to +50
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I prefer scroll-behavior CSS than imperative JS scrolling. I think it's OK to apply scroll-behavior globally

return (
<div className={cx(classes.replyType, className)} {...props}>
{jt`${authorElem} mark this message ${TYPE_NAME[replyType]}`}
<a
ref={anchorRef}
className={classes.hash}
href={`#${articleReply.replyId}`}
onClick={e => e.target.scrollIntoView({ behavior: 'smooth' })}
>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scrollIntoView is probably not needed if we define a global scroll-behavior in CSS.

However, it would be nice if clicking # copies the URL + href into clipboard. We are already using copy-to-clipboard package.

#
</a>
</div>
);
}
Expand Down
Loading