Skip to content

Commit 9a89669

Browse files
committed
docs: add detailed documents for usage and migration
1 parent 4ef7eb7 commit 9a89669

19 files changed

+738
-89
lines changed

README.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ yarn add @textea/json-viewer
4040
pnpm add @textea/json-viewer
4141
```
4242

43-
### Type Declaration
44-
45-
see [src/type.ts](src/type.ts)
46-
4743
### Basic Example
4844

4945
```jsx
@@ -70,11 +66,6 @@ const Component = () => (
7066
value={object}
7167
// just define it
7268
valueTypes={[
73-
{
74-
is: (value) => typeof value === 'string' && value.startsWith('https://i.imgur.com'),
75-
Component: (props) => <Image height={50} width={50} src={props.value} alt={props.value} />
76-
},
77-
// or
7869
defineDataType({
7970
is: (value) => typeof value === 'string' && value.startsWith('https://i.imgur.com'),
8071
Component: (props) => <Image height={50} width={50} src={props.value} alt={props.value} />

docs/components/JsonViewerPreview.tsx

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/examples/JsonViewerPreview.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { JsonViewerProps } from '@textea/json-viewer'
2+
import { JsonViewer } from '@textea/json-viewer'
3+
import type { FC } from 'react'
4+
5+
import { useNextraTheme } from '../hooks/useTheme'
6+
7+
export const JsonViewerPreview: FC<JsonViewerProps> = (props) => {
8+
const theme = useNextraTheme()
9+
return (
10+
<JsonViewer
11+
theme={theme}
12+
sx={{
13+
fontSize: 12
14+
}}
15+
value={props.value}
16+
/>
17+
)
18+
}

docs/examples/JsonViewerWithImage.tsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { defineDataType, JsonViewer } from '@textea/json-viewer'
2+
import type { FC } from 'react'
3+
4+
import { useNextraTheme } from '../hooks/useTheme'
5+
6+
const imageType = defineDataType<string>({
7+
is: (value) => {
8+
if (typeof value !== 'string') return false
9+
try {
10+
const url = new URL(value)
11+
return url.pathname.endsWith('.jpg')
12+
} catch (_) {
13+
return false
14+
}
15+
},
16+
Component: (props) => {
17+
return (
18+
<img
19+
height={48}
20+
width={48}
21+
src={props.value}
22+
alt={props.value}
23+
style={{ display: 'inline-block' }}
24+
/>
25+
)
26+
}
27+
})
28+
29+
const value = {
30+
image: 'https://i.imgur.com/1bX5QH6.jpg'
31+
}
32+
33+
const Example: FC = () => {
34+
const theme = useNextraTheme()
35+
return (
36+
<JsonViewer
37+
rootName={false}
38+
displaySize={false}
39+
theme={theme}
40+
value={value}
41+
valueTypes={[imageType]}
42+
/>
43+
)
44+
}
45+
46+
export default Example

docs/examples/JsonViewerWithURL.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { defineDataType, JsonViewer } from '@textea/json-viewer'
2+
import type { FC } from 'react'
3+
4+
import { useNextraTheme } from '../hooks/useTheme'
5+
6+
const urlType = defineDataType<URL>({
7+
is: (value) => value instanceof URL,
8+
Component: (props) => {
9+
const url = props.value.toString()
10+
return (
11+
<a
12+
href={url}
13+
target='_blank'
14+
rel='noopener noreferrer'
15+
style={{
16+
cursor: 'pointer',
17+
color: '#1976d2',
18+
textDecoration: 'underline'
19+
}}
20+
>
21+
{url}
22+
</a>
23+
)
24+
}
25+
})
26+
27+
const value = {
28+
string: 'this is a string',
29+
url: new URL('https://example.com')
30+
}
31+
32+
const Example: FC = () => {
33+
const theme = useNextraTheme()
34+
return (
35+
<JsonViewer
36+
rootName={false}
37+
displaySize={false}
38+
theme={theme}
39+
value={value}
40+
valueTypes={[urlType]}
41+
/>
42+
)
43+
}
44+
45+
export default Example
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import type { SvgIconProps } from '@mui/material'
2+
import { Button, SvgIcon } from '@mui/material'
3+
import { defineDataType, JsonViewer, stringType } from '@textea/json-viewer'
4+
import type { FC } from 'react'
5+
6+
import { useNextraTheme } from '../hooks/useTheme'
7+
8+
const LinkIcon = (props: SvgIconProps) => (
9+
// <svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 24 24' strokeWidth='2' stroke='currentColor' fill='none' strokeLinecap='round' strokeLinejoin='round'>
10+
<SvgIcon {...props}>
11+
<path stroke='none' d='M0 0h24v24H0z' fill='none'></path>
12+
<path stroke='currentcolor' d='M11 7h-5a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-5' fill='none'></path>
13+
<path stroke='currentcolor' d='M10 14l10 -10'></path>
14+
<path stroke='currentcolor' d='M15 4l5 0l0 5' fill='none'></path>
15+
</SvgIcon>
16+
)
17+
18+
const linkType = defineDataType<string>({
19+
...stringType,
20+
is (value) {
21+
return typeof value === 'string' && value.startsWith('http')
22+
},
23+
PostComponent: (props) => (
24+
<Button
25+
variant='contained'
26+
size='small'
27+
href={props.value}
28+
target='_blank'
29+
rel='noopener noreferrer'
30+
sx={{
31+
display: 'inline-block',
32+
marginLeft: 1
33+
}}
34+
>
35+
Open
36+
<LinkIcon sx={{ strokeWidth: 2 }} />
37+
</Button>
38+
)
39+
})
40+
41+
const value = {
42+
link: 'http://example.com'
43+
}
44+
45+
const Example: FC = () => {
46+
const theme = useNextraTheme()
47+
return (
48+
<JsonViewer
49+
rootName={false}
50+
displaySize={false}
51+
theme={theme}
52+
value={value}
53+
valueTypes={[linkType]}
54+
/>
55+
)
56+
}
57+
58+
export default Example

docs/hooks/useTheme.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { JsonViewerTheme } from '@textea/json-viewer'
2+
import { useTheme } from 'nextra-theme-docs'
3+
4+
export function useNextraTheme () {
5+
const { theme, systemTheme } = useTheme()
6+
const currentTheme = (theme === 'system' ? systemTheme : theme) as JsonViewerTheme
7+
return currentTheme
8+
}

docs/pages/_app.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'nextra-theme-docs/style.css'
2+
3+
export default function Nextra({ Component, pageProps }) {
4+
return <Component {...pageProps} />
5+
}

docs/pages/_app.tsx

Lines changed: 0 additions & 7 deletions
This file was deleted.

docs/pages/_meta.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"index": "Introduction"
2+
"index": "Introduction",
3+
"apis": "API Reference",
4+
"how-to": "How-to guides",
5+
"migration": "Migration"
36
}

0 commit comments

Comments
 (0)