Skip to content

Commit

Permalink
feat(react-renderer): setup custom React renderer (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabertazimi authored Mar 24, 2022
1 parent 7d32907 commit 953c221
Show file tree
Hide file tree
Showing 28 changed files with 221 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ A brand new repository for web development prototypes.

## React Collection

- [React SandBox](https://sabertazimi.github.io/awesome-web/react-sandbox)
- [React Renderer](https://sabertazimi.github.io/awesome-web/react-renderer)

## Vue Collection

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"build:prepare": "rm -fr ./build && mkdir -p ./build",
"lint": "lerna run lint",
"release": "lerna version --force-publish --sign-git-commit --sign-git-tag",
"start": "yarn start:vue-design",
"start:react-sandbox": "yarn workspace @sabertazimi/react-sandbox start",
"start": "yarn start:react-renderer",
"start:react-renderer": "yarn workspace @sabertazimi/react-renderer start",
"start:vue-basis": "yarn workspace @sabertazimi/vue-basis serve",
"start:vue-design": "yarn workspace @sabertazimi/vue-design dev",
"start:vue-trello": "yarn workspace @sabertazimi/vue-trello dev",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# @sabertazimi/react-sandbox
# @sabertazimi/react-renderer

[![Author](https://img.shields.io/badge/author-sabertaz-lightgrey?style=for-the-badge)](https://github.com/sabertazimi)
[![LICENSE](https://img.shields.io/github/license/sabertazimi/bod?style=for-the-badge)](https://raw.githubusercontent.com/sabertazimi/bod/main/LICENSE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@sabertazimi/react-sandbox",
"name": "@sabertazimi/react-renderer",
"version": "1.6.1",
"private": true,
"homepage": "https://sabertazimi.github.io/awesome-web/react-sandbox",
"homepage": "https://sabertazimi.github.io/awesome-web/react-renderer",
"scripts": {
"build": "react-scripts build && yarn build:post",
"build:post": "mkdir -p ../../build/react-sandbox && cp -fr ./build/* ../../build/react-sandbox",
"build:post": "mkdir -p ../../build/react-renderer && cp -fr ./build/* ../../build/react-renderer",
"build:profile": "react-scripts build --profile",
"start": "react-scripts start",
"start:https": "HTTPS=true react-scripts start",
Expand All @@ -16,6 +16,7 @@
"dependencies": {
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-reconciler": "^0.26.2",
"web-vitals": "^2.1.4"
},
"devDependencies": {
Expand All @@ -28,6 +29,7 @@
"@types/node": "^16.11.26",
"@types/react": "^17.0.41",
"@types/react-dom": "^17.0.14",
"@types/react-reconciler": "^0.26.4",
"typescript": "^4.6.2"
},
"eslintConfig": {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Sandbox App"
content="React Renderer"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
Expand All @@ -24,7 +24,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `yarn build`.
-->
<title>Sandbox App</title>
<title>React Renderer</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
File renamed without changes
File renamed without changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"short_name": "Sandbox App",
"name": "Sandbox App",
"short_name": "React Renderer",
"name": "React Renderer",
"icons": [
{
"src": "favicon.ico",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.app {
text-align: center;
}

.app-logo {
height: 40vmin;
pointer-events: none;
Expand All @@ -14,19 +10,11 @@
}

.app-header {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
@apply flex flex-col items-center justify-center;
@apply bg-gray-900;

min-height: 100vh;
font-size: calc(10px + 2vmin);
color: white;
background-color: #282c34;
}

.app-link {
margin-bottom: 1rem;
color: #61dafb;
}

@keyframes app-logo-spin {
Expand Down
File renamed without changes.
32 changes: 32 additions & 0 deletions packages/react-renderer/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useCallback, useState } from 'react';
import Logo from './logo.svg';
import './App.css';

function App() {
const [count, setCount] = useState<number>(0);
const inc = useCallback(() => {
setCount(count => count + 1);
}, []);
const dec = useCallback(() => {
setCount(count => count - 1);
}, []);

return (
<div className="app">
<header className="app-header">
<Logo className="app-logo" title="logo" />
<div className="flex flex-row justify-center items-center">
<button className="p-4 text-white text-6xl" onClick={dec}>
-
</button>
<div className="p-4 text-white text-6xl">{count}</div>
<button className="p-4 text-white text-6xl" onClick={inc}>
+
</button>
</div>
</header>
</div>
);
}

export default App;
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
@import-normalize;
@tailwind base;
@tailwind components;
@tailwind utilities;

body {
margin: 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom';
import Renderer from './renderer';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

ReactDOM.render(
Renderer.render(
<React.StrictMode>
<App />
</React.StrictMode>,
Expand Down
File renamed without changes
18 changes: 18 additions & 0 deletions packages/react-renderer/src/renderer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { ReactElement } from 'react';
import type { Container } from './reconciler';
import Reconciler from './reconciler';

const Renderer = {
render: (
element: ReactElement,
container: Container | null,
callback?: Function
) => {
if (container) {
const root = Reconciler.createContainer(container, 0, false, null);
Reconciler.updateContainer(element, root, null);
}
},
};

export default Renderer;
124 changes: 124 additions & 0 deletions packages/react-renderer/src/renderer/reconciler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import type { HostConfig } from 'react-reconciler';
import ReactReconciler from 'react-reconciler';

type Type = string;
type Props = { [key: string]: any };
export type Container = Document | DocumentFragment | Element;
type Instance = Element;
type TextInstance = Text;

type SuspenseInstance = any;
type HydratableInstance = any;
type PublicInstance = any;
type HostContext = any;
type UpdatePayload = any;
type _ChildSet = any;
type TimeoutHandle = any;
type NoTimeout = number;

const hostConfig: HostConfig<
Type,
Props,
Container,
Instance,
TextInstance,
SuspenseInstance,
HydratableInstance,
PublicInstance,
HostContext,
UpdatePayload,
_ChildSet,
TimeoutHandle,
NoTimeout
> = {
supportsMutation: false,
supportsPersistence: false,
createInstance: function (
type: string,
props: Props,
rootContainer: Container,
hostContext: any,
internalHandle: any
): Element {
throw new Error('Function not implemented.');
},
createTextInstance: function (
text: string,
rootContainer: Container,
hostContext: any,
internalHandle: any
): Text {
throw new Error('Function not implemented.');
},
appendInitialChild: function (
parentInstance: Element,
child: Element | Text
): void {
throw new Error('Function not implemented.');
},
finalizeInitialChildren: function (
instance: Element,
type: string,
props: Props,
rootContainer: Container,
hostContext: any
): boolean {
throw new Error('Function not implemented.');
},
prepareUpdate: function (
instance: Element,
type: string,
oldProps: Props,
newProps: Props,
rootContainer: Container,
hostContext: any
) {
throw new Error('Function not implemented.');
},
shouldSetTextContent: function (type: string, props: Props): boolean {
throw new Error('Function not implemented.');
},
getRootHostContext: function (rootContainer: Container) {
throw new Error('Function not implemented.');
},
getChildHostContext: function (
parentHostContext: any,
type: string,
rootContainer: Container
) {
throw new Error('Function not implemented.');
},
getPublicInstance: function (instance: Element | Text) {
throw new Error('Function not implemented.');
},
prepareForCommit: function (
containerInfo: Container
): Record<string, any> | null {
throw new Error('Function not implemented.');
},
resetAfterCommit: function (containerInfo: Container): void {
throw new Error('Function not implemented.');
},
preparePortalMount: function (containerInfo: Container): void {
throw new Error('Function not implemented.');
},
now: function (): number {
throw new Error('Function not implemented.');
},
scheduleTimeout: function (
fn: (...args: unknown[]) => unknown,
delay?: number
) {
throw new Error('Function not implemented.');
},
cancelTimeout: function (id: any): void {
throw new Error('Function not implemented.');
},
noTimeout: 0,
isPrimaryRenderer: false,
supportsHydration: false,
};

const reconciler = ReactReconciler(hostConfig);

export default reconciler;
File renamed without changes.
File renamed without changes.
File renamed without changes.
34 changes: 0 additions & 34 deletions packages/react-sandbox/src/App.tsx

This file was deleted.

Loading

0 comments on commit 953c221

Please sign in to comment.