Skip to content

Instantly share code, notes, and snippets.

@ctjlewis
Last active July 25, 2022 06:15
Show Gist options
  • Save ctjlewis/1a9ee9cac89b8b87fc97c02c28f0f2d0 to your computer and use it in GitHub Desktop.
Save ctjlewis/1a9ee9cac89b8b87fc97c02c28f0f2d0 to your computer and use it in GitHub Desktop.
<GoogleFont> demo
import { FC } from "react";
interface GoogleFontProps {
family: string;
weights?: (100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900)[];
}
/**
* Automatically generate <link> elements for the specified Google Fonts.
*/
export const GoogleFont: FC<GoogleFontProps> =
({ family, weights = [400, 800] }) => {
const href =
`https://fonts.googleapis.com/css2?family=${
family.replace(/ /g, "+")
}${
weights.length
? `:wght@${weights.join(";")}`
: ""
}&display=swap`;
return (
<link rel="stylesheet" href={href} />
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment