Skip to content

Commit 22d5b78

Browse files
committed
fix @orgajs/react
1 parent 897832e commit 22d5b78

File tree

6 files changed

+129
-66
lines changed

6 files changed

+129
-66
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module.exports = {
3737
'import/no-extraneous-dependencies': ['warn'],
3838
semi: ['error', 'never'],
3939
'react/react-in-jsx-scope': 'off',
40+
'react/prop-types': 'off',
4041
},
4142
overrides: [
4243
{

packages/react/lib/index.d.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Get current components from the MDX Context.
3+
*
4+
* @param {Components | MergeComponents | null | undefined} [components]
5+
* Additional components to use or a function that takes the current
6+
* components and filters/merges/changes them.
7+
* @returns {Components}
8+
* Current components.
9+
*/
10+
export function useOrgComponents(components?: Components | MergeComponents | null | undefined): Components;
11+
/**
12+
* Provider for MDX context
13+
*
14+
* @param {Props} props
15+
* @returns {JSX.Element}
16+
*/
17+
export function OrgProvider({ components, children, disableParentContext }: Props): JSX.Element;
18+
export type ReactNode = import('react').ReactNode;
19+
export type Components = import('@orgajs/orgx').OrgComponents;
20+
/**
21+
* Configuration.
22+
*/
23+
export type Props = {
24+
/**
25+
* Mapping of names for JSX components to React components.
26+
*/
27+
components?: Components | MergeComponents | null | undefined;
28+
/**
29+
* Turn off outer component context.
30+
*/
31+
disableParentContext?: boolean | null | undefined;
32+
/**
33+
* Children.
34+
*/
35+
children?: ReactNode | null | undefined;
36+
};
37+
/**
38+
* Custom merge function.
39+
*/
40+
export type MergeComponents = (currentComponents: Components) => Components;

packages/react/lib/index.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @typedef {import('react').ReactNode} ReactNode
3+
* @typedef {import('@orgajs/orgx').OrgComponents} Components
4+
*
5+
* @typedef Props
6+
* Configuration.
7+
* @property {Components | MergeComponents | null | undefined} [components]
8+
* Mapping of names for JSX components to React components.
9+
* @property {boolean | null | undefined} [disableParentContext=false]
10+
* Turn off outer component context.
11+
* @property {ReactNode | null | undefined} [children]
12+
* Children.
13+
*
14+
* @callback MergeComponents
15+
* Custom merge function.
16+
* @param {Components} currentComponents
17+
* Current components from the context.
18+
* @returns {Components}
19+
* Merged components.
20+
*/
21+
22+
import React from 'react'
23+
24+
/** @type {import('react').Context<Components>} */
25+
const OrgContext = React.createContext({})
26+
27+
/**
28+
* Get current components from the org context.
29+
*
30+
* @param {Components | MergeComponents | null | undefined} [components]
31+
* Additional components to use or a function that takes the current
32+
* components and filters/merges/changes them.
33+
* @returns {Components}
34+
* Current components.
35+
*/
36+
export function useOrgComponents(components) {
37+
const contextComponents = React.useContext(OrgContext)
38+
39+
// Memoize to avoid unnecessary top-level context changes
40+
return React.useMemo(() => {
41+
// Custom merge via a function prop
42+
if (typeof components === 'function') {
43+
return components(contextComponents)
44+
}
45+
46+
return { ...contextComponents, ...components }
47+
}, [contextComponents, components])
48+
}
49+
50+
/** @type {Components} */
51+
const emptyObject = {}
52+
53+
/**
54+
* Provider for org context
55+
*
56+
* @param {Props} props
57+
* @returns {JSX.Element}
58+
*/
59+
export function OrgProvider({ components, children, disableParentContext }) {
60+
/** @type {Components} */
61+
let allComponents
62+
63+
if (disableParentContext) {
64+
allComponents =
65+
typeof components === 'function'
66+
? components({})
67+
: components || emptyObject
68+
} else {
69+
allComponents = useOrgComponents(components)
70+
}
71+
72+
return React.createElement(
73+
OrgContext.Provider,
74+
{ value: allComponents },
75+
children
76+
)
77+
}

packages/react/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
"name": "@orgajs/react",
33
"version": "3.0.1",
44
"license": "MIT",
5+
"main": "index.js",
6+
"type": "module",
7+
"types": "index.d.ts",
58
"files": [
6-
"dist"
9+
"lib/",
10+
"index.d.ts",
11+
"index.js"
712
],
8-
"main": "dist/index.js",
9-
"types": "dist/index.d.ts",
10-
"type": "module",
1113
"scripts": {
12-
"build": "pnpm clean && pnpm compile",
13-
"clean": "del ./dist tsconfig.tsbuildinfo",
14-
"compile": "tsc -b"
14+
"build": "tsc --build --clean && tsc --build"
1515
},
1616
"peerDependencies": {
1717
"react": ">=16"

packages/react/src/index.ts

Lines changed: 0 additions & 53 deletions
This file was deleted.

packages/react/tsconfig.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
{
2-
"extends": "../../tsconfig.esm.json",
3-
"compilerOptions": {
4-
"rootDir": "./src",
5-
"outDir": "./dist"
6-
},
7-
"include": ["./src"]
2+
"extends": "../../tsconfig.types.json",
3+
"include": ["lib/**/*.js", "index.js"],
4+
"exclude": ["node_modules/"],
5+
"references": [{ "path": "../orgx" }]
86
}

0 commit comments

Comments
 (0)