Skip to content

Commit 56c1fb9

Browse files
WIP examples from sanity data (#1086)
* WIP examples from sanity data * Organize inputs * Fix styles and scope * Fix component preview * Change title to caption
1 parent 0092439 commit 56c1fb9

File tree

4 files changed

+48
-50
lines changed

4 files changed

+48
-50
lines changed

apps/docs/app/routes/_docs/komponenter/$slug.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as badgeExamples from '@/examples/badge';
22
import * as buttonExamples from '@/examples/button';
33
import { sanityFetch } from '@/lib/sanity';
4-
import { ComponentPreview } from '@/ui/component-preview';
54
import { Content } from '@/ui/content';
65
import { PropsTable } from '@/ui/props-table';
76
import { createFileRoute, notFound } from '@tanstack/react-router';
@@ -46,10 +45,6 @@ function Page() {
4645

4746
<Content content={data.content ?? []} />
4847

49-
{examples.map(({ title, code }) => (
50-
<ComponentPreview scope={scope} key={title} title={title} code={code} />
51-
))}
52-
5348
<PropsTable props={componentProps} />
5449
</>
5550
);

apps/docs/app/ui/component-preview.tsx

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,56 @@ import reactElementToJSXString from 'react-element-to-jsx-string';
77
import { LiveEditor, LivePreview, LiveProvider } from 'react-live';
88

99
type ComponentPreviewProps = {
10-
title: string;
10+
caption: string;
1111
/** @alpha - Passing a React.ReactNode is currently not compatible with React 19, pass a string to make it work with React 19 until react-element-to-jsx-string supports React 19 */
1212
code: React.ReactNode | string;
13-
/** All custom components that are rendered must be present in the scope */
14-
scope: Partial<typeof GrunnmurenIconsScope & typeof GrunnmurenScope>;
1513
};
1614

17-
export const ComponentPreview = ({
18-
title,
19-
code,
20-
scope,
21-
}: ComponentPreviewProps) => {
15+
export const ComponentPreview = ({ caption, code }: ComponentPreviewProps) => {
2216
// Keep of the code string in state to be able to copy it
2317
const [codeString, setCodeString] = useState(
2418
typeof code === 'string' ? code : reactElementToJSXString(code),
2519
);
2620

2721
const [hasCopied, setHasCopied] = useState(false);
22+
2823
return (
29-
<LiveProvider code={codeString} scope={scope} theme={themes.vsDark}>
30-
<div className="my-8 grid gap-y-4">
31-
<h2 className="heading-m">{title}</h2>
32-
<hr />
33-
<LivePreview className="flex gap-x-4" />
34-
<div className="grid grid-cols-[1fr,auto] grid-rows-[auto,1fr] overflow-hidden rounded-lg bg-[#1e1e1e]">
35-
<LiveEditor
36-
tabMode="focus"
37-
// Use the same black color as the background on the editor
38-
className="row-span-2 font-mono"
39-
onChange={(newCode) => setCodeString(newCode)}
40-
/>
41-
<div className="relative text-mint">
42-
<GrunnmurenScope.Button
43-
onPress={() =>
44-
navigator.clipboard.writeText(codeString).then(() => {
45-
setHasCopied(true);
46-
setTimeout(() => setHasCopied(false), 2000); // Reset after 2 seconds
47-
})
48-
}
49-
variant="tertiary"
50-
>
51-
<GrunnmurenIconsScope.Documents />
52-
</GrunnmurenScope.Button>
53-
<span
54-
className={cx(
55-
'absolute right-2 top-full transition-opacity duration-100',
56-
hasCopied ? 'opacity-100' : 'opacity-0',
57-
)}
58-
aria-hidden={!hasCopied}
59-
role="alert"
60-
>
61-
Kopiert!
62-
</span>
63-
</div>
24+
<LiveProvider
25+
code={codeString}
26+
scope={{ ...GrunnmurenIconsScope, ...GrunnmurenScope }}
27+
theme={themes.vsDark}
28+
>
29+
<p>{caption}</p>
30+
<LivePreview className="my-4 flex gap-x-4" />
31+
<div className="grid grid-cols-[1fr,auto] grid-rows-[auto,1fr] overflow-hidden rounded-lg bg-[#1e1e1e]">
32+
<LiveEditor
33+
tabMode="focus"
34+
// Use the same black color as the background on the editor
35+
className="row-span-2 font-mono"
36+
onChange={(newCode) => setCodeString(newCode)}
37+
/>
38+
<div className="relative text-mint">
39+
<GrunnmurenScope.Button
40+
onPress={() =>
41+
navigator.clipboard.writeText(codeString).then(() => {
42+
setHasCopied(true);
43+
setTimeout(() => setHasCopied(false), 2000); // Reset after 2 seconds
44+
})
45+
}
46+
variant="tertiary"
47+
>
48+
<GrunnmurenIconsScope.Documents />
49+
</GrunnmurenScope.Button>
50+
<span
51+
className={cx(
52+
'absolute right-2 top-full transition-opacity duration-100',
53+
hasCopied ? 'opacity-100' : 'opacity-0',
54+
)}
55+
aria-hidden={!hasCopied}
56+
role="alert"
57+
>
58+
Kopiert!
59+
</span>
6460
</div>
6561
</div>
6662
</LiveProvider>

apps/docs/app/ui/content.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import { PortableText } from '@portabletext/react';
22
import type { Content as IContent } from 'sanity.types';
3+
import { ComponentPreview } from './component-preview';
34

4-
const components = {};
5+
const components = {
6+
types: {
7+
'live-code-block': ({ value }) => (
8+
<ComponentPreview caption={value.caption} code={value.code.code} />
9+
),
10+
},
11+
};
512

613
export function Content({ content }: { content: IContent }) {
714
return (

apps/docs/app/ui/props-table.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface PropsTableProps {
1212
}
1313

1414
export const PropsTable = ({ props }: PropsTableProps) => (
15-
<table className="w-full">
15+
<table className="my-12 w-full">
1616
<caption className="heading-m mb-2 text-left">Props</caption>
1717
<thead>
1818
<tr className="bg-sky-lightest text-left align-baseline *:px-3 *:py-2">

0 commit comments

Comments
 (0)