Links inside news articles open in a new tab

This commit is contained in:
Michael Fatemi 2020-12-16 22:51:00 -05:00
parent f53b64edab
commit b9f611698a

View File

@ -14,7 +14,7 @@ export default function NewsArticle() {
sanity.fetch('*[_id == $articleId] [0]', { articleId }).then(setArticle);
}, [articleId]);
let thumbUrl: string | null = null;
let thumbUrl: string;
if (article?.thumbnail) {
thumbUrl = imageUrl(article.thumbnail).url();
} else {
@ -36,7 +36,21 @@ export default function NewsArticle() {
</i>
<br />
{/* Wrap the BlockContent in a div because it expands to <></> */}
<div className='article-paragraphs'>
<div
id='article-content'
className='article-paragraphs'
ref={(ref) => {
/*
When this element loads, convert all the links to have target="_blank."
This ensures that the links open in a new tab
*/
if (ref) {
ref.querySelectorAll('a').forEach((link) => {
link.target = '_blank';
});
}
}}
>
<BlockContent blocks={article.content} />
</div>
<br />