|
1 | 1 | <script lang="ts"> |
2 | | - import { Listgroup } from '$lib'; |
3 | | - import { AdjustmentsHorizontalSolid, DownloadSolid, MessagesSolid, UserCircleSolid } from 'flowbite-svelte-icons'; |
4 | | - import type { ListGroupItemType } from '$lib/types'; |
5 | | - import HighlightCompo from '../../utils/HighlightCompo.svelte'; |
| 2 | + import { type Component } from 'svelte'; |
| 3 | + import { Label, Radio, uiHelpers } from '$lib'; |
| 4 | + import DynamicCodeBlockHighlight from '../../utils/DynamicCodeBlockHighlight.svelte'; |
6 | 5 | import CodeWrapper from '../../utils/CodeWrapper.svelte'; |
7 | 6 | import H1 from '../../utils/H1.svelte'; |
8 | 7 | import H2 from '../../utils/H2.svelte'; |
| 8 | + import { isSvelteOverflow, getExampleFileName } from '../../utils/helpers'; |
9 | 9 | // for Props table |
10 | 10 | import CompoAttributesViewer from '../../utils/CompoAttributesViewer.svelte'; |
11 | 11 | const dirName = 'list-group'; |
12 | | - const modules = import.meta.glob('./md/*.md', { |
| 12 | +
|
| 13 | + // for examples section that dynamically changes the svelte component and markdown content |
| 14 | + import * as ExampleComponents from './examples'; |
| 15 | + const exampleModules = import.meta.glob('./examples/*.svelte', { |
13 | 16 | query: '?raw', |
14 | 17 | import: 'default', |
15 | 18 | eager: true |
16 | 19 | }); |
17 | | - let simpleList = ['Profile', 'Settings', 'Messages', 'Download']; |
18 | | - let links: ListGroupItemType[] = [ |
19 | | - { name: 'Avatar', href: '/components/avatar' }, |
20 | | - { name: 'List group', href: '/components/list-group', current: true }, |
21 | | - { name: 'Banner', href: '/components/banner' }, |
22 | | - { name: 'Breadcrumbs', href: '/components/breadcrumb', target: '_blank' } |
23 | | - ]; |
24 | | - let buttons = [ |
25 | | - { name: 'Profile', mycustomfield: 'data1', current: true }, |
26 | | - { name: 'Settings', mycustomfield: 'data2' }, |
27 | | - { name: 'Messages', mycustomfield: 'data3' }, |
28 | | - { |
29 | | - name: 'Download', |
30 | | - mycustomfield: 'data4', |
31 | | - disabled: true, |
32 | | - attrs: { type: 'submit' } |
33 | | - } |
34 | | - ]; |
35 | 20 |
|
36 | | - let iconList = [ |
37 | | - { name: 'Profile', Icon: UserCircleSolid, mycustomfield: 'data1' }, |
38 | | - { |
39 | | - name: 'Settings', |
40 | | - Icon: AdjustmentsHorizontalSolid, |
41 | | - mycustomfield: 'data2' |
42 | | - }, |
43 | | - { name: 'Messages', Icon: MessagesSolid, mycustomfield: 'data3' }, |
44 | | - { name: 'Download', Icon: DownloadSolid, mycustomfield: 'data4' } |
| 21 | + const exampleArr = [ |
| 22 | + { name: 'Default', component: ExampleComponents.Default }, |
| 23 | + { name: 'With links', component: ExampleComponents.WithLinks }, |
| 24 | + { name: 'With buttons', component: ExampleComponents.WithButtons }, |
| 25 | + { name: 'With icons', component: ExampleComponents.WithIcons } |
45 | 26 | ]; |
46 | | - const handleClick = (e?: MouseEvent) => { |
47 | | - if (e?.target instanceof HTMLElement) { |
48 | | - alert('mycustomfield: ' + e.target.attributes.getNamedItem('mycustomfield')?.value); |
49 | | - } else { |
50 | | - console.warn('Unexpected event target type. Cannot access attributes.'); |
51 | | - } |
52 | | - }; |
53 | | - const handleClick2 = (e?: MouseEvent) => { |
54 | | - if (e?.target instanceof HTMLElement) { |
55 | | - console.log(e.target.attributes.getNamedItem('mycustomfield')?.value); |
56 | | - } else { |
57 | | - console.warn('Unexpected event target type. Cannot access attributes.'); |
58 | | - } |
| 27 | + let selectedExample = $state(exampleArr[0].name); |
| 28 | + let markdown = $derived(getExampleFileName(selectedExample, exampleArr)); |
| 29 | +
|
| 30 | + function findObject(arr: { name: string; component: Component }[], name: string) { |
| 31 | + const matchingObject = arr.find((obj) => obj.name === name); |
| 32 | + return matchingObject ? matchingObject.component : null; |
| 33 | + } |
| 34 | + const SelectedComponent = $derived(findObject(exampleArr, selectedExample)); |
| 35 | + // end of dynamic svelte component |
| 36 | +
|
| 37 | + // for examples DynamicCodeBlockHighlight |
| 38 | + let codeBlock = uiHelpers(); |
| 39 | + let exampleExpand = $state(false); |
| 40 | + let showExpandButton = $derived(isSvelteOverflow(markdown, exampleModules)); |
| 41 | + const handleExpandClick = () => { |
| 42 | + exampleExpand = !exampleExpand; |
59 | 43 | }; |
| 44 | + // end of DynamicCodeBlock setup |
| 45 | + $effect(() => { |
| 46 | + exampleExpand = codeBlock.isOpen; |
| 47 | + }); |
60 | 48 | </script> |
61 | 49 |
|
62 | 50 | <H1>List group</H1> |
63 | 51 |
|
64 | | -<H2>Setup</H2> |
65 | | - |
66 | | -<HighlightCompo code={modules['./md/setup.md'] as string} /> |
67 | | - |
68 | | -<H2>Default list group</H2> |
69 | 52 |
|
70 | | -<CodeWrapper innerClass="flex justify-center"> |
71 | | - <Listgroup items={simpleList} class="w-48" /> |
72 | | - {#snippet codeblock()} |
73 | | - <HighlightCompo code={modules['./md/default-list-group.md'] as string} /> |
74 | | - {/snippet} |
75 | | -</CodeWrapper> |
76 | | - |
77 | | -<H2>List group with links</H2> |
78 | | - |
79 | | -<CodeWrapper innerClass="flex justify-center"> |
80 | | - <Listgroup active items={links} class="w-48" /> |
81 | | - {#snippet codeblock()} |
82 | | - <HighlightCompo code={modules['./md/list-group-with-links.md'] as string} /> |
83 | | - {/snippet} |
84 | | -</CodeWrapper> |
85 | | - |
86 | | -<H2>List group with buttons</H2> |
87 | | -<CodeWrapper innerClass="flex justify-center"> |
88 | | - <Listgroup active items={buttons} class="w-48" onclick={handleClick} /> |
89 | | - {#snippet codeblock()} |
90 | | - <HighlightCompo code={modules['./md/list-group-with-buttons.md'] as string} /> |
91 | | - {/snippet} |
92 | | -</CodeWrapper> |
| 53 | +<H2>Examples</H2> |
93 | 54 |
|
94 | | -<H2>List group with icons</H2> |
95 | | -<CodeWrapper innerClass="flex justify-center"> |
96 | | - <Listgroup active items={iconList} class="w-48" onclick={handleClick2} /> |
| 55 | +<CodeWrapper> |
| 56 | + <div class="mb-12 flex flex-wrap"> |
| 57 | + <Label class="mb-4 w-full font-bold">Example:</Label> |
| 58 | + {#each exampleArr as style} |
| 59 | + <Radio labelClass="w-32 my-1" onclick={() => (exampleExpand = false)} name="block_style" bind:group={selectedExample} value={style.name}>{style.name}</Radio> |
| 60 | + {/each} |
| 61 | + </div> |
| 62 | + <SelectedComponent /> |
97 | 63 | {#snippet codeblock()} |
98 | | - <HighlightCompo code={modules['./md/list-group-with-icons.md'] as string} /> |
| 64 | + <DynamicCodeBlockHighlight replaceLib {handleExpandClick} expand={exampleExpand} {showExpandButton} code={exampleModules[`./examples/${markdown}`] as string} /> |
99 | 65 | {/snippet} |
100 | 66 | </CodeWrapper> |
101 | 67 |
|
|
0 commit comments