Skip to content

Commit f979920

Browse files
committed
chore(showcase & codesnippet): refactor to enhance readability
1 parent 363d93b commit f979920

File tree

2 files changed

+33
-43
lines changed

2 files changed

+33
-43
lines changed

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Highlight } from '../highlight/highlight';
88
// /src/routes/docs/components/fluffy/modal/snippets/building-blocks.tsx
99
// /src/routes/docs/components/headless/modal/snippets/building-blocks.tsx
1010

11-
const rawCodeSnippets: any = import.meta.glob('/src/routes/docs/**/**/snippets/*', {
11+
const codeSnippets: any = import.meta.glob('/src/routes/docs/**/**/snippets/*', {
1212
as: 'raw',
1313
eager: isDev ? false : true,
1414
});
@@ -20,30 +20,23 @@ type CodeSnippetProps = QwikIntrinsicElements['div'] & {
2020
export const CodeSnippet = component$<CodeSnippetProps>(({ name }) => {
2121
const location = useLocation();
2222

23-
let lang = '.tsx';
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}`;
2427

25-
if (name.endsWith('.tsx')) lang = '';
26-
if (name.endsWith('.ts')) lang = '';
27-
if (name.endsWith('.css')) lang = '';
28-
29-
const snippetPath = `/src/routes${location.url.pathname}snippets/${name}${lang}`;
30-
31-
const CodeSnippetSig = useSignal<string>();
28+
const codeSnippetSig = useSignal<string>();
3229

3330
useTask$(async () => {
34-
// We need to call `await rawCodeSnippets[snippetPath]()` in development as it is `eager:false`
35-
if (isDev) {
36-
CodeSnippetSig.value = await rawCodeSnippets[snippetPath]();
37-
// We need to directly access the `components[componentPath]` expression in preview/production as it is `eager:true`
38-
} else {
39-
CodeSnippetSig.value = rawCodeSnippets[snippetPath];
40-
}
31+
const snippet = 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`
34+
codeSnippetSig.value = snippet;
4135
});
36+
4237
return (
43-
<div
44-
class={`shadow-3xl shadow-light-medium dark:shadow-dark-medium mb-6 rounded-xl border-2 border-slate-200 dark:border-slate-400`}
45-
>
46-
<Highlight code={CodeSnippetSig.value || ''} />
38+
<div class="shadow-3xl shadow-light-medium dark:shadow-dark-medium mb-6 rounded-xl border-2 border-slate-200 dark:border-slate-400">
39+
<Highlight code={codeSnippetSig.value || ''} />
4740
</div>
4841
);
4942
});

apps/website/src/components/showcase/showcase.tsx

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
useTask$,
88
} from '@builder.io/qwik';
99
import styles from './showcase.css?inline';
10-
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';
@@ -18,12 +17,15 @@ import { Highlight } from '../highlight/highlight';
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,27 +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 ComponentSig = useSignal<Component<any>>();
43-
const ComponentRawSig = useSignal<string>();
41+
const MetaGlobComponentSig = useSignal<Component<any>>();
42+
const componentCodeSig = useSignal<string>();
4443

4544
useTask$(async () => {
46-
// We need to call `await components[componentPath]()` in development as it is `eager:false`
47-
if (isDev) {
48-
ComponentSig.value = await components[componentPath]();
49-
ComponentRawSig.value = await componentsRaw[componentPath]();
50-
// We need to directly access the `components[componentPath]` expression in preview/production as it is `eager:true`
51-
} else {
52-
ComponentSig.value = components[componentPath];
53-
ComponentRawSig.value = componentsRaw[componentPath];
54-
}
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];
5551
});
5652

5753
useStyles$(styles);
54+
5855
return (
5956
<Tabs
6057
{...props}
@@ -69,15 +66,15 @@ export const Showcase = component$<ShowcaseProps>(({ name, ...props }) => {
6966
Code
7067
</Tab>
7168
</TabList>
72-
<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">
7370
<section class="flex flex-col items-center">
74-
{ComponentSig.value && <ComponentSig.value />}
71+
{MetaGlobComponentSig.value && <MetaGlobComponentSig.value />}
7572
</section>
7673
</TabPanel>
7774
<TabPanel class="border-qwikui-blue-300 dark:border-qwikui-purple-200 relative rounded-b-xl border-[1.5px]">
7875
<Highlight
7976
class="rounded-b-xl rounded-t-none"
80-
code={ComponentRawSig.value || ''}
77+
code={componentCodeSig.value || ''}
8178
/>
8279
</TabPanel>
8380
</Tabs>

0 commit comments

Comments
 (0)