-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* types: mdx preact typings * docs: add preact to typescript documentation Co-authored-by: John Otander <johnotander@gmail.com>
- Loading branch information
1 parent
62931c2
commit 8c922e4
Showing
6 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "dtslint/dtslint.json", | ||
"rules": { | ||
"whitespace": false, | ||
"semicolon": false | ||
} | ||
} |