Skip to content

Commit 97bbb92

Browse files
committed
feat(website): add Typist example
1 parent 183e272 commit 97bbb92

9 files changed

Lines changed: 96 additions & 21 deletions

File tree

website/contentlayer.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ export const Doc = defineDocumentType(() => ({
66
filePathPattern: 'docs/**/*.mdx',
77
contentType: 'mdx',
88
fields: {
9+
name: {
10+
type: 'enum',
11+
options: ['react-typist-component'],
12+
description: `The package's name`,
13+
required: true,
14+
},
915
title: {
1016
type: 'string',
1117
description: 'The title of the document',

website/contents/docs/react-typist-component.mdx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
---
2-
title: react-typist-component
2+
name: react-typist-component
3+
title: React Typist Component
34
intro: A react component for creating typewritter effect by setting up its children directy.
45
---
56

6-
# React Typist Component
7-
8-
Create typewriter effect by setting up a component's children directly.
9-
107
## Install
118

129
```bash
@@ -15,7 +12,17 @@ npm install react-typist-component
1512
yarn add react-typist-component
1613
```
1714

18-
## Example
15+
## Features
16+
17+
### Support Looping
18+
19+
<Loop />
20+
21+
### Support Pausing
22+
23+
<Pause />
24+
25+
## Examples
1926

2027
```jsx
2128
import Typist from 'react-typist-component';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import type { ReactNode } from 'react';
2+
3+
interface Props {
4+
children: ReactNode;
5+
}
6+
7+
export default function NotProse({ children }: Props) {
8+
return <div className='not-prose'>{children}</div>;
9+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { useState } from 'react';
2+
import Typist from 'react-typist-component';
3+
4+
import NotProse from '~/modules/common/components/NotProse';
5+
6+
export function Loop() {
7+
return (
8+
<NotProse>
9+
<Typist loop={true} cursor='|'>
10+
The typing animation can be looped infinitely.
11+
</Typist>
12+
</NotProse>
13+
);
14+
}
15+
16+
export function Pause() {
17+
const [pause, setPause] = useState(false);
18+
19+
return (
20+
<NotProse>
21+
<div className='flex gap-2'>
22+
<input
23+
type='checkbox'
24+
checked={pause}
25+
onChange={e => setPause(e.target.checked)}
26+
/>
27+
Pause
28+
</div>
29+
<Typist pause={pause} loop={true} cursor='_'>
30+
You can pause the typing animation whenever you want.
31+
</Typist>
32+
</NotProse>
33+
);
34+
}
35+
36+
const TypistExamples = {
37+
Loop,
38+
Pause,
39+
};
40+
41+
export default TypistExamples;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { ComponentName, ExamplesType } from '../types';
2+
import TypistExamples from './TypistExamples';
3+
4+
const components: Record<ComponentName, ExamplesType> = {
5+
'react-typist-component': TypistExamples,
6+
};
7+
8+
export default components;

website/modules/docs/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import type { ReactNode } from 'react';
2+
3+
export type ComponentName = 'react-typist-component';
4+
export type ExamplesType = Record<string, () => ReactNode>;

website/modules/layout/components/Aside.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ interface Props {
66
}
77

88
export default function Aside({ docs }: Props) {
9-
const titles = docs?.map(doc => doc.title) || [];
10-
119
return (
1210
<aside
1311
style={{ height: 'calc(100vh - 5rem)' }}
1412
className='sticky top-20 flex-shrink-0 border-r border-r-black py-5 pr-5'>
1513
<ul>
16-
{titles.map(title => {
14+
{docs?.map(doc => {
1715
return (
18-
<li key={title} className='text-lg text-primary hover:underline'>
19-
<Link href={`/${title}`}>{title}</Link>
16+
<li key={doc._id} className='text-lg text-primary hover:underline'>
17+
<Link href={`/${doc.name}`}>{doc.title}</Link>
2018
</li>
2119
);
2220
})}

website/pages/[component].tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@ import type { GetStaticPaths, GetStaticProps } from 'next';
33
import { useMDXComponent } from 'next-contentlayer/hooks';
44
import { useRouter } from 'next/router';
55

6+
import components from '~/modules/docs/components';
7+
68
interface Props {
79
docs: Doc[];
810
}
911

1012
export default function Component({ docs }: Props) {
1113
const { query } = useRouter();
12-
const doc = docs.find(d => d.title === query.component);
14+
const doc = docs.find(d => d.name === query.component);
15+
1316
const MDXComponent = useMDXComponent(doc?.body.code || '');
1417

1518
if (!doc) return null;
1619

1720
return (
1821
<div className='p-10'>
1922
<article className='prose max-w-none'>
20-
<MDXComponent />
23+
<h1>{doc.title}</h1>
24+
<p>{doc.intro}</p>
25+
<MDXComponent components={components[doc.name]} />
2126
</article>
2227
</div>
2328
);
@@ -34,7 +39,7 @@ export const getStaticProps: GetStaticProps<
3439

3540
export const getStaticPaths: GetStaticPaths = () => {
3641
const paths = allDocs.map(doc => ({
37-
params: { component: doc.title },
42+
params: { component: doc.name },
3843
}));
3944

4045
return {

website/pages/index.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { allDocs, Doc } from 'contentlayer/generated';
22
import { GetStaticProps } from 'next';
33
import Head from 'next/head';
4-
import Link from 'next/link';
54

65
interface Props {
76
docs: Doc[];
@@ -20,12 +19,10 @@ export default function Home({ docs }: Props) {
2019
<h1 className='text-4xl text-primary mb-10'>React Components</h1>
2120
{docs.map(doc => {
2221
return (
23-
<Link key={doc._id} href={`/${doc.title}`}>
24-
<div className='mb-6 last:mb-0'>
25-
<h2 className='text-2xl mb-2'>{doc.title}</h2>
26-
<p>{doc.intro}</p>
27-
</div>
28-
</Link>
22+
<div key={doc._id} className='mb-6 last:mb-0'>
23+
<h2 className='text-2xl mb-2'>{doc.title}</h2>
24+
<p>{doc.intro}</p>
25+
</div>
2926
);
3027
})}
3128
</main>

0 commit comments

Comments
 (0)