Skip to content

Commit

Permalink
Add types to @mdx-js/preact (#1233)
Browse files Browse the repository at this point in the history
* types: mdx preact typings

* docs: add preact to typescript documentation

Co-authored-by: John Otander <johnotander@gmail.com>
  • Loading branch information
ChristianMurphy and johno committed Dec 1, 2020
1 parent 62931c2 commit 8c922e4
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/advanced/typescript.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ improve the developer experience for TypeScript users.
## Typings for Components and Utilities

- `@mdx-js/mdx`
- `@mdx-js/preact`
- `@mdx-js/react`
- `@mdx-js/runtime`
- `@mdx-js/vue`
Expand Down
13 changes: 10 additions & 3 deletions packages/preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@
"Matija Marohnić <matija.marohnic@gmail.com>",
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
"JounQin <admin@1stg.me> (https://www.1stg.me)",
"Chris Biscardi <chris@christopherbiscardi.com> (https://www.christopherbiscardi.com)"
"Chris Biscardi <chris@christopherbiscardi.com> (https://www.christopherbiscardi.com)",
"Christian Murphy <christian.murphy.42@gmail.com>"
],
"license": "MIT",
"main": "dist/cjs.js",
"module": "dist/esm.js",
"types": "types/index.d.ts",
"license": "MIT",
"main": "dist/mdx-preact.js",
"module": "dist/mdx-preact.es.js",
"exports": {
"import": "./dist/mdx-preact.modern.js",
"require": "./dist/mdx-preact.js"
},
"files": [
"dist"
"dist",
"types/index.d.ts"
],
"keywords": [
"mdx",
Expand All @@ -38,7 +44,8 @@
],
"scripts": {
"build": "microbundle -f modern,es,cjs src/index.js",
"test": "jest test --coverage"
"test": "jest test --coverage",
"test-types": "dtslint types"
},
"peerDependencies": {
"preact": "^10.4.6"
Expand Down
69 changes: 69 additions & 0 deletions packages/preact/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// TypeScript Version: 3.4

import {
h,
Context,
AnyComponent,
FunctionComponent
} from 'preact'

/**
* Mapping of names for JSX components to React components
*/
interface ComponentDictionary {
[name: string]: AnyComponent<any>
}

/**
* Prop type that includes a component dictionary
*/
interface ComponentsProp {
/**
* Mapping of names for JSX components to React components
*/
components?: ComponentDictionary,
}

/**
* Direct access to the MDX React Context
*/
declare const MDXContext: Context<ComponentsProp>

/**
* Provider for MDX context
*/
declare const MDXProvider: FunctionComponent<ComponentsProp>

/**
* Gets components from the MDX Context
*
* @param components additional components to include
* @returns all components from context with overrides from components parameter
*/
declare function useMDXComponents(
components: ComponentDictionary | (() => ComponentDictionary)
): ComponentDictionary

/**
* High order component that passes components prop to child
*
* @param child Component being wrapped
*/
declare function withMDXComponents(
child: AnyComponent<ComponentsProp>
): ReactElement | null

/**
* Preact hyperscript function wrapped with handler for MDX content
*/
declare const mdx: typeof h

export {
ComponentDictionary,
ComponentsProp,
MDXContext,
MDXProvider,
useMDXComponents,
withMDXComponents,
mdx
}
36 changes: 36 additions & 0 deletions packages/preact/types/mdx-preact-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {h, ComponentChildren} from 'preact'
import {useContext} from 'preact/hooks'
import {
MDXProvider,
useMDXComponents,
withMDXComponents,
ComponentsProp,
MDXContext,
mdx
} from '@mdx-js/preact'

const H1 = ({children}: {children: ComponentChildren}) => <h1>{children}</h1>

const MDXProvideExample = () => (
<MDXProvider components={{h1: H1}}>
<h1>Hello, world!</h1>
</MDXProvider>
)

const WithMDXComponentsExample = () =>
withMDXComponents(({components}: ComponentsProp) => {
components // $ExpectType ComponentDictionary | undefined
return <div />
})

const UseMDXComponentsExample = () => {
useMDXComponents({h1: H1}) // $ExpectType ComponentDictionary
useMDXComponents(() => ({h1: H1})) // $ExpectType ComponentDictionary
}

const UseMDXContextExample = () => {
const {components} = useContext(MDXContext)
components // $ExpectType ComponentDictionary | undefined
}

const MDXCreateElementExample = () => mdx('mdx', {title: 'example'}, [])
14 changes: 14 additions & 0 deletions packages/preact/types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["dom", "es6"],
"strict": true,
"skipLibCheck": true,
"baseUrl": ".",
"jsx": "react",
"jsxFactory": "h",
"paths": {
"@mdx-js/preact": ["index.d.ts"]
}
}
}
7 changes: 7 additions & 0 deletions packages/preact/types/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"whitespace": false,
"semicolon": false
}
}

0 comments on commit 8c922e4

Please sign in to comment.