├── .babelrc
├── .gitignore
├── README.md
├── examples
├── button.js
├── counter.js
├── greeting.js
├── index.html
├── index.js
├── list-with-styles.js
├── lists.js
├── post.js
└── title.js
├── package-lock.json
├── package.json
└── src
└── index.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["env"],
3 | "plugins": ["transform-react-jsx"]
4 | }
5 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | dist/
3 | .cache/
4 | lib/
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # joltik
2 |
3 | A micro VDOM library for learning purposes
4 |
5 | ## Description
6 |
7 | I'm just learning how a VDOM (Virtual Document Object Model) library works by building one and documenting my learnings in medium. I don't pretend this to be nothing serious, so please don't use it in production.
8 |
9 | ## Installation
10 |
11 | ```
12 | $ npm install joltik --save
13 | ```
14 |
15 | ### Configuring JSX pragma
16 |
17 | By default, [Babel] transforms your JSX code into a call to `React.createElement`. You can customize this behaviour by adding a **jsx pragma** at the top of your files, with the following syntax:
18 |
19 | ```js
20 | /** @jsx j */
21 | ```
22 |
23 | Wher `j` here is joltik's replacement for `React.createElement`. You can read more about this behaviour in [@developit](https://github.com/developit)'s blog post: [WTF Is JSX](https://jasonformat.com/wtf-is-jsx/).
24 |
25 | ### Configuring Babel
26 |
27 | You will need to install Babel's [transform react jsx plugin](https://babeljs.io/docs/en/babel-plugin-transform-react-jsx) in order to support JSX syntax only, instead of the full preset you normally would use for React.
28 |
29 | Here's a sample of the bare minimum `.babelrc` config you will need:
30 |
31 | ```json
32 | {
33 | "presets": ["env"],
34 | "plugins": ["transform-react-jsx"]
35 | }
36 | ```
37 |
38 | It is very convenient to replace your pragma everywhere by defoult, to avoid adding it as a comment at the top of your files. In order to do so, add the following config to the plugin:
39 |
40 | ```
41 | {
42 | ...
43 | "plugins": [
44 | ["@babel/plugin-transform-react-jsx", {
45 | "pragma": "j"
46 | }]
47 | ]
48 | }
49 | ```
50 |
51 | ### Creating your first component
52 |
53 | The syntax is similar to a usual React component, with the only difference of importing `j` from joltik.
54 |
55 | ```jsx
56 | // HelloWorld.js
57 | import { j } from "joltik";
58 | import "./styles.css";
59 |
60 | export const HelloWorld = ({ text }) =>
{text}
;
61 | ```
62 |
63 | To render an element, you would do:
64 |
65 | ```jsx
66 | // index.js
67 | import { j, createElement } from "joltik";
68 | import { HelloWolrd } from "./HelloWorld";
69 |
70 | document
71 | .getElementById("app")
72 | .appendChild(createElement());
73 | ```
74 |
75 | ## Demo
76 |
77 | You can see a working demo in [this codesandbox](https://codesandbox.io/s/93474k06xr) and also in the [examples folder](https://github.com/d4nidev/joltik/tree/master/examples).
78 |
79 | ## Why Joltik?
80 |
81 | ![joltik]
82 |
83 | [Joltik]() is the smallest Pokémon from all the current editions.
84 |
85 | ## References
86 |
87 | - [How to write your own Virtual DOM](https://medium.com/@deathmood/how-to-write-your-own-virtual-dom-ee74acc13060)
88 | - [WTF is JSX](https://jasonformat.com/wtf-is-jsx/)
89 |
90 | [joltik]: https://cdn.bulbagarden.net/upload/e/e7/Spr_5b_595.png
91 |
--------------------------------------------------------------------------------
/examples/button.js:
--------------------------------------------------------------------------------
1 | /** @jsx j */
2 | import { j } from "../src";
3 |
4 | /**
5 | * A basic JSX component, non parameterized, with attributes
6 | */
7 | const Button = ;
8 |
9 | function handleClick() {
10 | alert("joltik works!");
11 | }
12 |
13 | export { Button };
14 |
--------------------------------------------------------------------------------
/examples/counter.js:
--------------------------------------------------------------------------------
1 | /** @jsx j */
2 | import { j } from "../src";
3 |
4 | export function Counter({ count = 0, onClick = () => {} } = {}) {
5 | const increment = () => {
6 | onClick(+1);
7 | };
8 |
9 | const decrement = () => {
10 | onClick(-1);
11 | };
12 |
13 | return (
14 |