|
1 | 1 | import type { ReactElement } from "react"; |
| 2 | +import { useEffect } from "react"; |
2 | 3 | import cn from "classnames"; |
3 | 4 | import { defaults } from "lodash"; |
4 | | -import Head from "next/head"; |
5 | 5 |
|
6 | 6 | import { toId } from "utils/toTitle"; |
7 | 7 |
|
@@ -45,22 +45,40 @@ export default function DemoPage(props: DemoPageProps): ReactElement { |
45 | 45 | className, |
46 | 46 | fonts = EMPTY_LIST, |
47 | 47 | } = props; |
| 48 | + useEffect(() => { |
| 49 | + const head = document.querySelector("head"); |
| 50 | + if (!head || !fonts.length) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + const links: HTMLLinkElement[] = []; |
| 55 | + fonts.forEach((font) => { |
| 56 | + let href: string; |
| 57 | + if (font === "Font Awesome") { |
| 58 | + href = |
| 59 | + "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css"; |
| 60 | + } else { |
| 61 | + const name = font.replace(/\s+/g, "+"); |
| 62 | + href = `https://fonts.googleapis.com/css2?family=${name}&display=swap`; |
| 63 | + } |
| 64 | + const link = document.createElement("link"); |
| 65 | + |
| 66 | + link.rel = "stylesheet"; |
| 67 | + link.href = href; |
| 68 | + |
| 69 | + head.appendChild(link); |
| 70 | + links.push(link); |
| 71 | + }); |
| 72 | + |
| 73 | + return () => { |
| 74 | + links.forEach((link) => { |
| 75 | + head.removeChild(link); |
| 76 | + }); |
| 77 | + }; |
| 78 | + }, [fonts]); |
| 79 | + |
48 | 80 | return ( |
49 | 81 | <div id="demo-page-container" className={cn(styles.container, className)}> |
50 | | - <Head> |
51 | | - {fonts.map((font) => { |
52 | | - let href: string; |
53 | | - if (font === "Font Awesome") { |
54 | | - href = |
55 | | - "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css"; |
56 | | - } else { |
57 | | - const name = font.replace(/\s+/g, "+"); |
58 | | - href = `https://fonts.googleapis.com/css2?family=${name}&display=swap`; |
59 | | - } |
60 | | - |
61 | | - return <link key={font} rel="stylesheet" href={href} />; |
62 | | - })} |
63 | | - </Head> |
64 | 82 | <DemoPageHeader packageName={packageName}>{description}</DemoPageHeader> |
65 | 83 | {demos.map((demo, index) => ( |
66 | 84 | // eslint-disable-next-line react/jsx-key |
|
0 commit comments