Skip to content

Commit

Permalink
custom JSX example
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-barun committed Sep 10, 2024
1 parent 45c28e2 commit 65814eb
Show file tree
Hide file tree
Showing 55 changed files with 1,103 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
14 changes: 14 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BSD Zero Clause License

Copyright (c) 2024 by Barun Kharel

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Custom JSX Example

Example of using JSX without using framework like React.

There are 3 different ways:
- [./react-jsx--bare--node-modules/](./react-jsx--bare--node-modules/)
<br>using bare path to access `custom-jsx` through `node_modules` folder
- [./react-jsx--bare--internal/](./react-jsx--bare--internal/)
<br>using bare path to access `custom-jsx` internally
- [./react-jsx--relative/](./react-jsx--relative/)
<br>using relative path to access `custom-jsx`
<br>**Note**: This works in browser but shows errors when using `tsc`

## Prerequisities

1. Node and NPM

## Usage

0. `git clone` this repo
1. Make sure the folder is accessible through a web server.
<br>Lets assume this repo is available at http://localhost:8080
1. In the browser, go to:
- http://localhost:8080/react-jsx--bare--node-modules/
- http://localhost:8080/react-jsx--bare--internal/
- http://localhost:8080/react-jsx--relative/

To make modifications, go to the root folder and:
1. Install dependencies
```
npm ci
```
1. Make modifications
1. Run TypeScript Compiler after making modifications:
```
npx tsc
```
1. Refresh the browser
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"typescript": "5.6.2"
}
}
12 changes: 12 additions & 0 deletions react-jsx--bare--internal/App.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions react-jsx--bare--internal/App.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions react-jsx--bare--internal/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Bar } from "./bar/Bar.js";
import { Foo } from "./Foo.js";

function App()
{
return <>
<h1>App</h1>
<div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px;">
<Foo message={'Welcome'}/>
<Bar/>
</div>
</>
}

const elements = App();

const appContainer = document.getElementById('app-container')!;

for (const element of elements) {
appContainer.appendChild(element);
}
5 changes: 5 additions & 0 deletions react-jsx--bare--internal/Foo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions react-jsx--bare--internal/Foo.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions react-jsx--bare--internal/Foo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export
function Foo({ message }: { message: string})
{
return (
<div class="Foo">
<h2>Foo</h2>
<p>This is Foo.</p>
<p>
<b>Message: </b>
{message}
</p>
</div>
);
}
5 changes: 5 additions & 0 deletions react-jsx--bare--internal/bar/Bar.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions react-jsx--bare--internal/bar/Bar.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions react-jsx--bare--internal/bar/Bar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export
function Bar()
{
return (
<div class="Bar">
<h2>Bar</h2>
<p>
I am Bar.
</p>
</div>
);
}
44 changes: 44 additions & 0 deletions react-jsx--bare--internal/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<title>Custom JSX - Using "jsx": "react-jsx"</title>

<style>
body {
font-family: system-ui, sans-serif;
}
#app-container {
max-width: 800px;
margin-inline: auto;
border: 2px solid;
padding: 0 16px 16px;
}
.Foo {
border: 2px dashed;
padding: 16px;
}
.Bar {
border: 2px dashed;
padding: 16px;
}
</style>

</head>
<body>

<div id="app-container"></div>

<script type="importmap">
{
"imports": {
"custom-jsx/jsx-runtime": "./packages/custom-jsx/jsx-runtime.js"
}
}
</script>

<script src="App.js" type="module"></script>

</body>
</html>
37 changes: 37 additions & 0 deletions react-jsx--bare--internal/packages/custom-jsx/jsx-runtime.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions react-jsx--bare--internal/packages/custom-jsx/jsx-runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export type * from './jsx.d.js';

export
function jsxs(
tagNameOrFragment: string|((...attributes:any[])=>HTMLElement),
attributesWithChildren: {[attributeName: string]: any, children: Array<HTMLElement>},
_key?: number
) {
if (tagNameOrFragment === Fragment) {
return attributesWithChildren.children.flat();
} else {
const childElements = jsx.apply(null, arguments as any);
return Array.isArray(childElements)
? childElements
: [ childElements]
;
}
}

export
function jsx(
tagNameOrFunction: string|((...attributes:any[])=>HTMLElement),
attributesWithChildren: {[attributeName: string]: any, children?: string|HTMLElement|Array<string>},
_key?: number
) {
if (typeof tagNameOrFunction === 'string') {
const element = document.createElement(tagNameOrFunction);
for (const name in attributesWithChildren) {
if (name === 'children') {
const children = attributesWithChildren[name];
if (typeof children === 'string') {
element.appendChild(document.createTextNode(children));
} else if (Array.isArray(children)) {
element.append(...children.flat());
}
} else {
element.setAttribute(name, attributesWithChildren[name]);
}
}
return element;
} else {
return tagNameOrFunction(attributesWithChildren);
}
}

export
function Fragment()
{}

export
function jsxDEV()
{}
5 changes: 5 additions & 0 deletions react-jsx--bare--internal/packages/custom-jsx/jsx.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export namespace JSX {
interface IntrinsicElements {
[tagName: string]: any,
}
}
13 changes: 13 additions & 0 deletions react-jsx--bare--internal/packages/custom-jsx/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "module",
"exports": {
"./jsx-runtime": {
"types": "./jsx.d.ts",
"default": "./jsx-runtime.js"
},
"./jsx-dev-runtime": {
"types": "./jsx.d.ts",
"default": "./jsx-runtime.js"
}
}
}
Loading

0 comments on commit 65814eb

Please sign in to comment.