Skip to content

Commit 36cdff4

Browse files
committed
build: introduce TypeScript to the project
1 parent c61cc6f commit 36cdff4

15 files changed

+2018
-3343
lines changed

.babelrc

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

.npmignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
src
2-
.babelrc
32
webpack.config.js
43
assets

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,5 @@ But for implementation purposed use it like a regular child component.
159159
# TODO
160160

161161
- ~Live Demos~
162-
- Maybe Typescript this?
162+
- ~Maybe Typescript this?~
163163
- Test cases

dist/build.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import React from 'react';
2+
declare type Mode = 'open' | 'element';
3+
export declare const registerAsWebComponent: (component: React.ElementType, customElementName: string, mode?: Mode) => void;
4+
export {};

dist/prop-bridge.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import React from 'react';
2+
export declare const renderReact2Node: (RComponent: React.ElementType, initialProps: {}, targetDomNode: Element | DocumentFragment, onRender: (ref: React.RefObject<any>) => void) => void;
3+
export declare const sendPropsToReact: (propBridgeRef: React.RefObject<any>, props: any) => void;
4+
export declare const getPropsFromNode: (node: HTMLElement) => {
5+
[key: string]: string | JSX.Element;
6+
};

dist/react-dom-child.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
import 'child-replace-with-polyfill';
3+
export declare class ReactDomChild extends React.Component {
4+
ref: React.RefObject<HTMLDivElement>;
5+
componentDidMount(): void;
6+
render(): JSX.Element;
7+
}

package-lock.json

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

package.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"name": "react-webcomponentify",
33
"version": "1.2.2",
44
"description": "Build and export React Components as Web Components without any extra effort.",
5-
"main": "dist/build.js",
5+
"main": "dist/build",
6+
"typings": "dist/index",
67
"scripts": {
78
"test": "echo \"Error: no test specified\" && exit 1",
89
"build": "webpack -p"
@@ -26,18 +27,18 @@
2627
},
2728
"homepage": "https://github.com/master-atul/react-webcomponentify#readme",
2829
"dependencies": {
29-
"child-replace-with-polyfill": "^1.0.1",
30-
"react-create-ref": "^1.0.1"
30+
"child-replace-with-polyfill": "1.0.1",
31+
"react-create-ref": "1.0.1"
3132
},
3233
"devDependencies": {
33-
"@babel/core": "^7.2.2",
34-
"@babel/plugin-proposal-class-properties": "^7.3.0",
35-
"@babel/plugin-syntax-class-properties": "^7.2.0",
36-
"@babel/plugin-transform-react-jsx": "^7.3.0",
37-
"@babel/preset-env": "^7.3.1",
38-
"babel-loader": "^8.0.5",
39-
"webpack": "^4.29.0",
40-
"webpack-cli": "^3.2.1"
34+
"@types/react": "17.0.4",
35+
"@types/react-dom": "17.0.3",
36+
"react": "17.0.2",
37+
"react-dom": "17.0.2",
38+
"ts-loader": "8.2.0",
39+
"typescript": "4.2.4",
40+
"webpack": "4.29.0",
41+
"webpack-cli": "3.2.1"
4142
},
4243
"peerDependencies": {
4344
"react": "*",

src/index.js renamed to src/index.ts

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
import React from 'react';
12
import {
23
renderReact2Node,
34
getPropsFromNode,
4-
sendPropsToReact
5-
} from "./prop-bridge";
5+
sendPropsToReact,
6+
} from './prop-bridge';
67

7-
const getCustomElementFromReactComponent = (RComponent, mode) => {
8+
type Mode = 'open' | 'element';
9+
10+
const getCustomElementFromReactComponent = (
11+
RComponent: React.ElementType,
12+
mode?: Mode
13+
) => {
814
return class ReactAsCustomElement extends HTMLElement {
9-
targetNode = null;
10-
propBridgeRef = null;
15+
targetNode: this | ShadowRoot = null;
16+
propBridgeRef: any = null;
1117
props = {};
12-
observer = null;
18+
observer: MutationObserver = null;
1319

1420
constructor() {
1521
super();
@@ -28,27 +34,38 @@ const getCustomElementFromReactComponent = (RComponent, mode) => {
2834
break;
2935
}
3036

31-
renderReact2Node(RComponent, this.props, this.targetNode, this._onReactMount);
37+
renderReact2Node(
38+
RComponent,
39+
this.props,
40+
this.targetNode,
41+
this._onReactMount
42+
);
3243
}
3344

34-
setProps = newProps => {
45+
setProps = (newProps: {}) => {
3546
this.props = { ...this.props, ...newProps };
3647
sendPropsToReact(this.propBridgeRef, this.props);
3748
};
3849

39-
_onReactMount = propBridgeRef => {
50+
_onReactMount = (propBridgeRef: React.RefObject<any>) => {
4051
this.propBridgeRef = propBridgeRef;
4152
this.setProps(this.props);
4253
};
4354

44-
_onMutation = mutationsList => {
45-
const newProps = mutationsList.reduce((props, mutation) => {
46-
if (mutation.type === "attributes") {
47-
const propKey = mutation.attributeName;
48-
props[propKey] = this.getAttribute(propKey);
49-
}
50-
return props;
51-
}, {});
55+
_onMutation = (mutationsList: any[]) => {
56+
const newProps = mutationsList.reduce(
57+
(
58+
props: { [x: string]: string },
59+
mutation: { type: string; attributeName: string }
60+
) => {
61+
if (mutation.type === 'attributes') {
62+
const propKey = mutation.attributeName;
63+
props[propKey] = this.getAttribute(propKey);
64+
}
65+
return props;
66+
},
67+
{}
68+
);
5269
this.setProps(newProps);
5370
};
5471
/*
@@ -73,7 +90,11 @@ const getCustomElementFromReactComponent = (RComponent, mode) => {
7390
- "open" - open shadow DOM
7491
- "element" - no shadow DOM
7592
*/
76-
export const registerAsWebComponent = (component, customElementName, mode) => {
93+
export const registerAsWebComponent = (
94+
component: React.ElementType,
95+
customElementName: string,
96+
mode?: Mode
97+
) => {
7798
const ReactCustomElement = getCustomElementFromReactComponent(
7899
component,
79100
mode

0 commit comments

Comments
 (0)