Skip to content

Commit 9dd23c2

Browse files
authored
Merge pull request #17 from jason89521/add-example
2 parents 183e272 + 01c0b9d commit 9dd23c2

13 files changed

Lines changed: 178 additions & 25 deletions

File tree

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"preinstall": "npx only-allow pnpm",
1414
"commit": "cz",
1515
"test": "pnpm -r --filter ./packages/* run test",
16-
"dev:website": "pnpm --filter ./website dev",
17-
"build:website": "pnpm --filter ./website run build",
16+
"dev:website": "pnpm build:packages && pnpm --filter ./website dev",
17+
"build:website": "pnpm build:packages && pnpm --filter ./website run build",
1818
"build:packages": "pnpm -r --filter ./packages/* run build:package",
1919
"prepublishOnly": "pnpm build:lib"
2020
},
@@ -47,5 +47,8 @@
4747
"@vitejs/plugin-react": "^2.1.0",
4848
"typescript": "^4.8.3"
4949
}
50+
},
51+
"dependencies": {
52+
"next": "12.3.1"
5053
}
5154
}

pnpm-lock.yaml

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
import { allDocs, Doc } from 'contentlayer/generated';
22
import type { GetStaticPaths, GetStaticProps } from 'next';
33
import { useMDXComponent } from 'next-contentlayer/hooks';
4+
import Head from 'next/head';
45
import { useRouter } from 'next/router';
56

7+
import components from '~/modules/docs/components';
8+
69
interface Props {
710
docs: Doc[];
811
}
912

1013
export default function Component({ docs }: Props) {
1114
const { query } = useRouter();
12-
const doc = docs.find(d => d.title === query.component);
15+
const doc = docs.find(d => d.name === query.component);
16+
1317
const MDXComponent = useMDXComponent(doc?.body.code || '');
1418

1519
if (!doc) return null;
1620

1721
return (
1822
<div className='p-10'>
23+
<Head>
24+
<title>{doc.title}</title>
25+
<meta name='description' content={doc.intro} />
26+
</Head>
1927
<article className='prose max-w-none'>
20-
<MDXComponent />
28+
<h1>{doc.title}</h1>
29+
<p>{doc.intro}</p>
30+
<MDXComponent components={components[doc.name]} />
2131
</article>
2232
</div>
2333
);
@@ -34,7 +44,7 @@ export const getStaticProps: GetStaticProps<
3444

3545
export const getStaticPaths: GetStaticPaths = () => {
3646
const paths = allDocs.map(doc => ({
37-
params: { component: doc.title },
47+
params: { component: doc.name },
3848
}));
3949

4050
return {

0 commit comments

Comments
 (0)