Skip to content

Commit

Permalink
Add README
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenybai committed Sep 16, 2021
1 parent 3333409 commit 2f5a695
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 27 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
dist
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# `tiny-vdom`

Smallest possible virtual DOM implementation.

## Installation

```sh
npm install tiny-vdom
```

## Usage

`index.js`

```js
import { h, createElement, patch } from 'tiny-vdom';

const el = createElement(h('div'));

patch(el, h('div', null, 'Hello World!'), h('div'));
```

## License

`tiny-vdom` is [MIT-licensed](LICENSE) open-source software by [Aiden Bai](https://github.com/aidenybai).
10 changes: 0 additions & 10 deletions index.html

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "mini-vdom",
"version": "1.0.0",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"dev": "vite",
"build": "vite build"
"build": "esbuild src/vdom.ts --minify --outfile=dist/vdom.js"
},
"repository": {
"type": "git",
Expand All @@ -18,6 +17,7 @@
},
"homepage": "https://github.com/aidenybai/mini-vdom#readme",
"devDependencies": {
"esbuild": "^0.12.28",
"vite": "^2.5.7"
}
}
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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

9 changes: 0 additions & 9 deletions src/index.js

This file was deleted.

9 changes: 5 additions & 4 deletions src/vdom.js → src/vdom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const m = (tag, props = {}, children = []) => ({ tag, props, children });
export const h = (tag, props = {}, ...children) => ({ tag, props, children });

export const createElement = (vnode) => {
if (typeof vnode === 'string') return document.createTextNode(vnode);
Expand All @@ -17,12 +17,13 @@ export const createElement = (vnode) => {
};

export const patch = (el, newVNode, oldVNode) => {
const replace = () => el.replaceWith(createElement(newVNode));
if (!newVNode) el.remove();
if (typeof oldVNode === 'string' || typeof newVNode === 'string') {
if (oldVNode !== newVNode) return replace();
if (oldVNode !== newVNode) return el.replaceWith(createElement(newVNode));
} else {
if (oldVNode.tag !== newVNode.tag) return replace();
if (oldVNode.tag !== newVNode.tag) {
return el.replaceWith(createElement(newVNode));
}

for (const prop in { ...oldVNode.props, ...newVNode.props }) {
if (newVNode.props[prop] === undefined) {
Expand Down

0 comments on commit 2f5a695

Please sign in to comment.