-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathcode-snippet.tsx
39 lines (31 loc) · 1.44 KB
/
code-snippet.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
import { PropsOf, component$, useSignal, useTask$ } from '@qwik.dev/core';
import { useLocation } from '@qwik.dev/router';
import { Highlight } from '../highlight/highlight';
// The below `/src/routes/docs/**/**/snippets/*.tsx` pattern is here so that import.meta.glob works both for styled and headless routes.
// For example:
// /src/routes/docs/components/styled/modal/snippets/building-blocks.tsx
// /src/routes/docs/components/headless/modal/snippets/building-blocks.tsx
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const codeSnippets: any = import.meta.glob('/src/routes/docs/**/**/snippets/*', {
query: '?raw',
import: 'default',
});
type CodeSnippetProps = PropsOf<'div'> & {
name: string;
};
export const CodeSnippet = component$<CodeSnippetProps>(({ name }) => {
const location = useLocation();
// Determine the file extension if not specified
const fileExtension =
name.endsWith('.tsx') || name.endsWith('.ts') || name.endsWith('.css') ? '' : '.tsx';
const snippetPath = `/src/routes${location.url.pathname}snippets/${name}${fileExtension}`;
const codeSnippetSig = useSignal<string>();
useTask$(async () => {
codeSnippetSig.value = await codeSnippets[snippetPath](); // We need to call `await codeSnippets[snippetPath]()` in development as it is `eager:false`
});
return (
<div class="shadow-3xl mb-6 rounded-md border shadow-lg">
<Highlight code={codeSnippetSig.value || ''} />
</div>
);
});