1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-27 07:58:26 -04:00
jarv.is/components/TweetEmbed/TweetEmbed.tsx
2022-07-25 13:11:40 -04:00

44 lines
1.2 KiB
TypeScript

import { useState } from "react";
import { TwitterTweetEmbed } from "react-twitter-embed";
import useUpdateEffect from "../../hooks/useUpdateEffect";
import useTheme from "../../hooks/useTheme";
import { styled } from "../../lib/styles/stitches.config";
const Wrapper = styled("div", {
// reserve a moderate amount of space for the widget, it takes a while to load...
minHeight: "300px",
});
export type TweetEmbedProps = {
id: string;
options?: Record<string, unknown>;
className?: string;
};
const TweetEmbed = ({ id, options, className }: TweetEmbedProps) => {
const { activeTheme } = useTheme();
const [widgetTheme, setWidgetTheme] = useState(activeTheme);
useUpdateEffect(() => {
setWidgetTheme(activeTheme === "dark" ? activeTheme : "light");
}, [activeTheme]);
return (
<Wrapper className={className}>
<TwitterTweetEmbed
tweetId={id}
options={{
dnt: true,
align: "center",
theme: widgetTheme,
...options,
}}
// changing this key forces the iframe URL to reformulate itself and update the theme:
key={`tweet-${id}-${widgetTheme}`}
/>
</Wrapper>
);
};
export default TweetEmbed;