-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy path$libraryId.$version.docs.framework.$framework.examples.$.tsx
111 lines (100 loc) · 3.77 KB
/
$libraryId.$version.docs.framework.$framework.examples.$.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { createFileRoute } from '@tanstack/react-router'
import React from 'react'
import { FaExternalLinkAlt } from 'react-icons/fa'
import { DocTitle } from '~/components/DocTitle'
import { getBranch, getLibrary } from '~/libraries'
import { getInitialSandboxFileName } from '~/utils/sandbox'
import { seo } from '~/utils/seo'
import { capitalize, slugToTitle } from '~/utils/utils'
export const Route = createFileRoute(
'/$libraryId/$version/docs/framework/$framework/examples/$'
)({
meta: ({ params }) => {
const library = getLibrary(params.libraryId)
return seo({
title: `${capitalize(params.framework)} ${library.name} ${slugToTitle(
params._splat
)} Example | ${library.name} Docs`,
description: `An example showing how to implement ${slugToTitle(
params._splat
)} in ${capitalize(params.framework)} using ${library.name}.`,
})
},
component: Example,
})
export default function Example() {
const { version, framework, _splat, libraryId } = Route.useParams()
const library = getLibrary(libraryId)
const branch = getBranch(library, version)
const examplePath = [framework, _splat].join('/')
const [isDark, setIsDark] = React.useState(true)
React.useEffect(() => {
setIsDark(window.matchMedia?.(`(prefers-color-scheme: dark)`).matches)
}, [])
const sandboxFirstFileName = getInitialSandboxFileName(framework, libraryId)
const githubUrl = `https://github.com/${library.repo}/tree/${branch}/examples/${examplePath}`
// preset=node can be removed once Stackblitz runs Angular as webcontainer by default
// See https://github.com/stackblitz/core/issues/2957
const stackBlitzUrl = `https://stackblitz.com/github/${
library.repo
}/tree/${branch}/examples/${examplePath}?embed=1&theme=${
isDark ? 'dark' : 'light'
}&preset=node&file=${sandboxFirstFileName}`
const codesandboxUrl = `https://codesandbox.io/s/github/${
library.repo
}/tree/${branch}/examples/${examplePath}?embed=1&theme=${
isDark ? 'dark' : 'light'
}&file=${sandboxFirstFileName}`
const hideCodesandbox = library.hideCodesandboxUrl
const hideStackblitz = library.hideStackblitzUrl
return (
<div className="flex-1 flex flex-col min-h-0 overflow-auto">
<div className="p-4 lg:p-6">
<DocTitle>
<span>
{capitalize(framework)} Example: {slugToTitle(_splat)}
</span>
<div className="flex items-center gap-4 flex-wrap font-normal text-xs">
<a
href={githubUrl}
target="_blank"
className="flex gap-1 items-center"
rel="noreferrer"
>
<FaExternalLinkAlt /> Github
</a>
{!hideStackblitz ? (
<a
href={stackBlitzUrl}
target="_blank"
className="flex gap-1 items-center"
rel="noreferrer"
>
<FaExternalLinkAlt /> StackBlitz
</a>
) : null}
{!hideCodesandbox ? (
<a
href={codesandboxUrl}
target="_blank"
className="flex gap-1 items-center"
rel="noreferrer"
>
<FaExternalLinkAlt /> CodeSandbox
</a>
) : null}
</div>
</DocTitle>
</div>
<div className="flex-1 lg:px-6 flex flex-col min-h-0">
<iframe
src={stackBlitzUrl}
title={`${library.name} | ${examplePath}`}
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
className="flex-1 w-full overflow-hidden lg:rounded-lg shadow-xl shadow-gray-700/20 bg-white dark:bg-black"
/>
</div>
<div className="lg:h-16 lg:mt-2" />
</div>
)
}