Skip to content

Commit

Permalink
feat/refactor: reddit embeds, use dynamic instead of conditional rend…
Browse files Browse the repository at this point in the history
…er (#103)

* feat/refactor: reddit embeds, use dynamic instead of conditional render

* chore: format reddit jsx
  • Loading branch information
Nathen-Smith authored Mar 25, 2024
1 parent 31373a9 commit 233878c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 37 deletions.
27 changes: 0 additions & 27 deletions components/ConditionallyRender.tsx

This file was deleted.

16 changes: 7 additions & 9 deletions components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
/* eslint-disable jsx-a11y/label-has-associated-control */

import Link from 'next/link';
import dynamic from 'next/dynamic';
import { Bars3Icon } from '@heroicons/react/24/solid';

import ColorModeToggle from './ColorModeToggle';
import ConditionallyRender from './ConditionallyRender';
import NavLinks from './NavLinks';

const ColorModeToggle = dynamic(() => import('./ColorModeToggle'), {
ssr: false,
});

export default function NavBar() {
return (
<div className="navbar mx-auto w-full max-w-[65ch] px-0 transition-colors dark:text-white sm:bg-transparent sm:dark:bg-transparent">
Expand All @@ -28,16 +30,12 @@ export default function NavBar() {
<ul className="flex items-center space-x-4">
<NavLinks />
<li>
<ConditionallyRender client>
<ColorModeToggle />
</ConditionallyRender>
<ColorModeToggle />
</li>
</ul>
</div>
<div className="block flex-none sm:hidden">
<ConditionallyRender client>
<ColorModeToggle />
</ConditionallyRender>
<ColorModeToggle />
</div>
</div>
);
Expand Down
35 changes: 35 additions & 0 deletions components/RedditPost.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import useDarkMode from '../hooks/useDarkMode';
import useScript from '../hooks/useScript';

interface RedditEmbedProps {
url: string;
title: string;
author: string;
subreddit: string;
}

export default function RedditPost({
url,
title,
author,
subreddit,
}: RedditEmbedProps) {
const { isDark } = useDarkMode();
useScript('https://embed.reddit.com/widgets.js');

return (
<blockquote
className="reddit-embed-bq"
style={{ height: '316px' }}
data-embed-height="316"
data-embed-theme={isDark ? 'dark' : 'light'}
>
<a href={url}>{title}</a>
<br />
by
<a href={`https://www.reddit.com/user/${author}/`}>u/{author}</a>
in
<a href={`https://www.reddit.com/r/${subreddit}/`}>{subreddit}</a>
</blockquote>
);
}
19 changes: 19 additions & 0 deletions hooks/useScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect } from 'react';

// https://stackoverflow.com/questions/34424845/adding-script-tag-to-react-jsx
const useScript = (url: string) => {
useEffect(() => {
const script = document.createElement('script');

script.src = url;
script.async = true;

document.body.appendChild(script);

return () => {
document.body.removeChild(script);
};
}, [url]);
};

export default useScript;
6 changes: 5 additions & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
import '../styles/globals.css';
import type { AppProps } from 'next/app';
import { MDXProvider } from '@mdx-js/react';
import dynamic from 'next/dynamic';

import Tweet from '../components/Tweet';

const components = { Tweet };
const RedditPost = dynamic(() => import('../components/RedditPost'), {
ssr: false,
});
const components = { Tweet, RedditPost };

function MyApp({ Component, pageProps }: AppProps) {
return (
Expand Down

0 comments on commit 233878c

Please sign in to comment.