Skip to content

Commit 35c6a47

Browse files
Merge pull request #529 from maiieul/cleanup-mdx-pr
refactor(showcase): cleanup Showcase & CodeSnippet
2 parents 7a5b14e + 3f8248b commit 35c6a47

File tree

4 files changed

+46
-45
lines changed

4 files changed

+46
-45
lines changed

apps/website/src/components/code-snippet/code-snippet.tsx

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,39 @@ import { useLocation } from '@builder.io/qwik-city';
33
import { isDev } from '@builder.io/qwik/build';
44
import { Highlight } from '../highlight/highlight';
55

6-
type CodeSnippetProps = QwikIntrinsicElements['div'] & {
7-
name: string;
8-
};
6+
// The below `/src/routes/docs/**/**/snippets/*.tsx` pattern is here so that import.meta.glob works both for fluffy and headless routes.
7+
// For example:
8+
// /src/routes/docs/components/fluffy/modal/snippets/building-blocks.tsx
9+
// /src/routes/docs/components/headless/modal/snippets/building-blocks.tsx
910

10-
const rawCodeSnippets: any = import.meta.glob('/src/routes/docs/**/**/snippets/*', {
11+
const codeSnippets: any = import.meta.glob('/src/routes/docs/**/**/snippets/*', {
1112
as: 'raw',
1213
eager: isDev ? false : true,
1314
});
1415

16+
type CodeSnippetProps = QwikIntrinsicElements['div'] & {
17+
name: string;
18+
};
19+
1520
export const CodeSnippet = component$<CodeSnippetProps>(({ name }) => {
1621
const location = useLocation();
1722

18-
let lang = '.tsx';
19-
20-
if (name.endsWith('.tsx')) lang = '';
21-
if (name.endsWith('.ts')) lang = '';
22-
if (name.endsWith('.css')) lang = '';
23+
// Determine the file extension if not specified
24+
const fileExtension =
25+
name.endsWith('.tsx') || name.endsWith('.ts') || name.endsWith('.css') ? '' : '.tsx';
26+
const snippetPath = `/src/routes${location.url.pathname}snippets/${name}${fileExtension}`;
2327

24-
const snippetPath = `/src/routes${location.url.pathname}snippets/${name}${lang}`;
25-
26-
const CodeSnippet = useSignal<string>();
28+
const codeSnippetSig = useSignal<string>();
2729

2830
useTask$(async () => {
29-
if (isDev) {
30-
CodeSnippet.value = await rawCodeSnippets[snippetPath]();
31-
} else {
32-
CodeSnippet.value = rawCodeSnippets[snippetPath];
33-
}
31+
codeSnippetSig.value = isDev
32+
? await codeSnippets[snippetPath]() // We need to call `await codeSnippets[snippetPath]()` in development as it is `eager:false`
33+
: codeSnippets[snippetPath]; // We need to directly access the `codeSnippets[snippetPath]` expression in preview/production as it is `eager:true`
3434
});
35+
3536
return (
36-
<div
37-
class={`shadow-3xl shadow-light-medium dark:shadow-dark-medium mb-6 rounded-xl border-2 border-slate-200 dark:border-slate-400`}
38-
>
39-
<Highlight code={CodeSnippet.value || ''} />
37+
<div class="shadow-3xl shadow-light-medium dark:shadow-dark-medium mb-6 rounded-xl border-2 border-slate-200 dark:border-slate-400">
38+
<Highlight code={codeSnippetSig.value || ''} />
4039
</div>
4140
);
4241
});

apps/website/src/components/mdx-components/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AnatomyTable } from '../anatomy-table/anatomy-table';
55
import { APITable } from '../api-table/api-table';
66
import { KeyboardInteractionTable } from '../keyboard-interaction-table/keyboard-interaction-table';
77
import { CodeCopy } from '../code-copy/code-copy';
8-
import { Showcase } from '../show-example/show-example';
8+
import { Showcase } from '../showcase/showcase';
99
import { CodeSnippet } from '../code-snippet/code-snippet';
1010

1111
export const components: Record<string, any> = {

apps/website/src/components/show-example/show-example.tsx renamed to apps/website/src/components/showcase/showcase.tsx

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,26 @@ import {
66
useStyles$,
77
useTask$,
88
} from '@builder.io/qwik';
9-
import styles from './show-example.css?inline';
10-
9+
import styles from './showcase.css?inline';
1110
import { Tab, TabList, TabPanel, Tabs } from '@qwik-ui/headless';
1211
import { useLocation } from '@builder.io/qwik-city';
1312
import { isDev } from '@builder.io/qwik/build';
1413
import { Highlight } from '../highlight/highlight';
1514

16-
// The below patterns are here so that import.meta.glob works both for fluffy and headless routes.
15+
// The below `/src/routes/docs/**/**/examples/*.tsx` patterns are here so that import.meta.glob works both for fluffy and headless routes.
1716
// For example:
1817
// /src/routes/docs/components/fluffy/modal/examples/hero.tsx
1918
// /src/routes/docs/components/headless/modal/examples/hero.tsx
2019

21-
const components: any = import.meta.glob('/src/routes/docs/**/**/examples/*.tsx', {
22-
import: 'default',
23-
eager: isDev ? false : true,
24-
});
20+
const metaGlobComponents: any = import.meta.glob(
21+
'/src/routes/docs/**/**/examples/*.tsx',
22+
{
23+
import: 'default',
24+
eager: isDev ? false : true,
25+
},
26+
);
2527

26-
const componentsRaw: any = import.meta.glob('/src/routes/docs/**/**/examples/*.tsx', {
28+
const rawComponents: any = import.meta.glob('/src/routes/docs/**/**/examples/*.tsx', {
2729
as: 'raw',
2830
eager: isDev ? false : true,
2931
});
@@ -34,25 +36,22 @@ type ShowcaseProps = QwikIntrinsicElements['div'] & {
3436

3537
export const Showcase = component$<ShowcaseProps>(({ name, ...props }) => {
3638
const location = useLocation();
37-
3839
const componentPath = `/src/routes${location.url.pathname}examples/${name}.tsx`;
3940

40-
console.log('componentPath', componentPath);
41-
42-
const Component = useSignal<Component<any>>();
43-
const ComponentRaw = useSignal<string>();
41+
const MetaGlobComponentSig = useSignal<Component<any>>();
42+
const componentCodeSig = useSignal<string>();
4443

4544
useTask$(async () => {
46-
if (isDev) {
47-
Component.value = await components[componentPath]();
48-
ComponentRaw.value = await componentsRaw[componentPath]();
49-
} else {
50-
Component.value = components[componentPath];
51-
ComponentRaw.value = componentsRaw[componentPath];
52-
}
45+
MetaGlobComponentSig.value = isDev
46+
? await metaGlobComponents[componentPath]() // We need to call `await metaGlobComponents[componentPath]()` in development as it is `eager:false`
47+
: metaGlobComponents[componentPath]; // We need to directly access the `metaGlobComponents[componentPath]` expression in preview/production as it is `eager:true`
48+
componentCodeSig.value = isDev
49+
? await rawComponents[componentPath]()
50+
: rawComponents[componentPath];
5351
});
5452

5553
useStyles$(styles);
54+
5655
return (
5756
<Tabs
5857
{...props}
@@ -67,13 +66,16 @@ export const Showcase = component$<ShowcaseProps>(({ name, ...props }) => {
6766
Code
6867
</Tab>
6968
</TabList>
70-
<TabPanel class="shadow-light-medium dark:shadow-dark-medium border-qwikui-blue-300 dark:border-qwikui-purple-200 rounded-b-xl border-[1.5px] bg-slate-200 bg-slate-800 p-4 dark:bg-slate-900 md:p-12">
69+
<TabPanel class="shadow-light-medium dark:shadow-dark-medium border-qwikui-blue-300 dark:border-qwikui-purple-200 rounded-b-xl border-[1.5px] bg-slate-200 bg-slate-800 p-4 dark:bg-slate-900 md:p-12">
7170
<section class="flex flex-col items-center">
72-
{Component.value && <Component.value />}
71+
{MetaGlobComponentSig.value && <MetaGlobComponentSig.value />}
7372
</section>
7473
</TabPanel>
7574
<TabPanel class="border-qwikui-blue-300 dark:border-qwikui-purple-200 relative rounded-b-xl border-[1.5px]">
76-
<Highlight class="rounded-b-xl rounded-t-none" code={ComponentRaw.value || ''} />
75+
<Highlight
76+
class="rounded-b-xl rounded-t-none"
77+
code={componentCodeSig.value || ''}
78+
/>
7779
</TabPanel>
7880
</Tabs>
7981
);

0 commit comments

Comments
 (0)