Skip to content

Commit 8c922e4

Browse files
Add types to @mdx-js/preact (#1233)
* types: mdx preact typings * docs: add preact to typescript documentation Co-authored-by: John Otander <[email protected]>
1 parent 62931c2 commit 8c922e4

File tree

6 files changed

+137
-3
lines changed

6 files changed

+137
-3
lines changed

docs/advanced/typescript.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ improve the developer experience for TypeScript users.
1010
## Typings for Components and Utilities
1111

1212
- `@mdx-js/mdx`
13+
- `@mdx-js/preact`
1314
- `@mdx-js/react`
1415
- `@mdx-js/runtime`
1516
- `@mdx-js/vue`

packages/preact/package.json

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,23 @@
1616
"Matija Marohnić <[email protected]>",
1717
"Titus Wormer <[email protected]> (https://wooorm.com)",
1818
"JounQin <[email protected]> (https://www.1stg.me)",
19-
"Chris Biscardi <[email protected]> (https://www.christopherbiscardi.com)"
19+
"Chris Biscardi <[email protected]> (https://www.christopherbiscardi.com)",
20+
"Christian Murphy <[email protected]>"
2021
],
2122
"license": "MIT",
23+
"main": "dist/cjs.js",
24+
"module": "dist/esm.js",
25+
"types": "types/index.d.ts",
26+
"license": "MIT",
2227
"main": "dist/mdx-preact.js",
2328
"module": "dist/mdx-preact.es.js",
2429
"exports": {
2530
"import": "./dist/mdx-preact.modern.js",
2631
"require": "./dist/mdx-preact.js"
2732
},
2833
"files": [
29-
"dist"
34+
"dist",
35+
"types/index.d.ts"
3036
],
3137
"keywords": [
3238
"mdx",
@@ -38,7 +44,8 @@
3844
],
3945
"scripts": {
4046
"build": "microbundle -f modern,es,cjs src/index.js",
41-
"test": "jest test --coverage"
47+
"test": "jest test --coverage",
48+
"test-types": "dtslint types"
4249
},
4350
"peerDependencies": {
4451
"preact": "^10.4.6"

packages/preact/types/index.d.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// TypeScript Version: 3.4
2+
3+
import {
4+
h,
5+
Context,
6+
AnyComponent,
7+
FunctionComponent
8+
} from 'preact'
9+
10+
/**
11+
* Mapping of names for JSX components to React components
12+
*/
13+
interface ComponentDictionary {
14+
[name: string]: AnyComponent<any>
15+
}
16+
17+
/**
18+
* Prop type that includes a component dictionary
19+
*/
20+
interface ComponentsProp {
21+
/**
22+
* Mapping of names for JSX components to React components
23+
*/
24+
components?: ComponentDictionary,
25+
}
26+
27+
/**
28+
* Direct access to the MDX React Context
29+
*/
30+
declare const MDXContext: Context<ComponentsProp>
31+
32+
/**
33+
* Provider for MDX context
34+
*/
35+
declare const MDXProvider: FunctionComponent<ComponentsProp>
36+
37+
/**
38+
* Gets components from the MDX Context
39+
*
40+
* @param components additional components to include
41+
* @returns all components from context with overrides from components parameter
42+
*/
43+
declare function useMDXComponents(
44+
components: ComponentDictionary | (() => ComponentDictionary)
45+
): ComponentDictionary
46+
47+
/**
48+
* High order component that passes components prop to child
49+
*
50+
* @param child Component being wrapped
51+
*/
52+
declare function withMDXComponents(
53+
child: AnyComponent<ComponentsProp>
54+
): ReactElement | null
55+
56+
/**
57+
* Preact hyperscript function wrapped with handler for MDX content
58+
*/
59+
declare const mdx: typeof h
60+
61+
export {
62+
ComponentDictionary,
63+
ComponentsProp,
64+
MDXContext,
65+
MDXProvider,
66+
useMDXComponents,
67+
withMDXComponents,
68+
mdx
69+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {h, ComponentChildren} from 'preact'
2+
import {useContext} from 'preact/hooks'
3+
import {
4+
MDXProvider,
5+
useMDXComponents,
6+
withMDXComponents,
7+
ComponentsProp,
8+
MDXContext,
9+
mdx
10+
} from '@mdx-js/preact'
11+
12+
const H1 = ({children}: {children: ComponentChildren}) => <h1>{children}</h1>
13+
14+
const MDXProvideExample = () => (
15+
<MDXProvider components={{h1: H1}}>
16+
<h1>Hello, world!</h1>
17+
</MDXProvider>
18+
)
19+
20+
const WithMDXComponentsExample = () =>
21+
withMDXComponents(({components}: ComponentsProp) => {
22+
components // $ExpectType ComponentDictionary | undefined
23+
return <div />
24+
})
25+
26+
const UseMDXComponentsExample = () => {
27+
useMDXComponents({h1: H1}) // $ExpectType ComponentDictionary
28+
useMDXComponents(() => ({h1: H1})) // $ExpectType ComponentDictionary
29+
}
30+
31+
const UseMDXContextExample = () => {
32+
const {components} = useContext(MDXContext)
33+
components // $ExpectType ComponentDictionary | undefined
34+
}
35+
36+
const MDXCreateElementExample = () => mdx('mdx', {title: 'example'}, [])
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": ["dom", "es6"],
5+
"strict": true,
6+
"skipLibCheck": true,
7+
"baseUrl": ".",
8+
"jsx": "react",
9+
"jsxFactory": "h",
10+
"paths": {
11+
"@mdx-js/preact": ["index.d.ts"]
12+
}
13+
}
14+
}

packages/preact/types/tslint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"whitespace": false,
5+
"semicolon": false
6+
}
7+
}

0 commit comments

Comments
 (0)