arche 0.1.5 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/README.md +55 -44
  2. package/es.js +34 -21
  3. package/index.js +30 -17
  4. package/index.mjs +251 -0
  5. package/package.json +1 -1
package/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Arche
2
+ ![arche-logo](https://raw.githubusercontent.com/a-synchronous/assets/master/arche-logo-226x226.png)
2
3
  > Arche (/ˈɑːrki/; Ancient Greek: ἀρχή) is a Greek word with primary senses "beginning", "origin" or "source of action" (ἐξ' ἀρχῆς: from the beginning, οr ἐξ' ἀρχῆς λόγος: the original argument), and later "first principle" or "element". ([wikipedia](https://en.wikipedia.org/wiki/Arche))
3
4
 
4
5
  ![Node.js CI](https://github.com/richytong/arche/workflows/Node.js%20CI/badge.svg?branch=master)
@@ -49,7 +50,7 @@ render(UserCard({ firstName: 'George', lastName: 'Henry', age: 32 }))
49
50
  // </div>
50
51
  ```
51
52
 
52
- Complete interop with React hooks (converted from [this example](https://reactjs.org/docs/hooks-intro.html)):
53
+ Complete interoperability with React hooks (converted from [this example](https://reactjs.org/docs/hooks-intro.html)):
53
54
  ```javascript [playground]
54
55
  const ReactElement = Arche(React)
55
56
  const { Div, P, Button } = ReactElement
@@ -75,8 +76,6 @@ render(Example())
75
76
  // </div>
76
77
  ```
77
78
 
78
- Proof of concept: [rubico.land](https://rubico.land/) ([github](https://github.com/a-synchronous/rubico.land))
79
-
80
79
  # Installation
81
80
  with `npm`
82
81
  ```bash
@@ -95,46 +94,58 @@ import Arche from 'https://unpkg.com/arche/es.js'
95
94
 
96
95
  # Syntax
97
96
  ```coffeescript [specscript]
98
- Element = Object
99
-
100
- var type string|function,
101
- props Object,
102
- children string|Element|Array<string|Element>,
103
- element Element,
104
- creator { createElement: (type, props?, children?)=>element },
105
- rootElement type=>((props, children?)|children)=>element {
106
- Script: ((props, children?)|children)=>element,
107
- Html: ((props, children?)|children)=>element,
108
- Body: (props, children?)|children)=>element,
109
- Section: (props, children?)|children)=>element,
110
- Article: (props, children?)|children)=>element,
111
- Span: (props, children?)|children)=>element,
112
- Div: (props, children?)|children)=>element,
113
- Img: (props, children?)|children)=>element,
114
- H1: (props, children?)|children)=>element,
115
- H2: (props, children?)|children)=>element,
116
- H3: (props, children?)|children)=>element,
117
- H4: (props, children?)|children)=>element,
118
- H5: (props, children?)|children)=>element,
119
- H6: (props, children?)|children)=>element,
120
-
121
- A: (props, children?)|children)=>element,
122
- P: (props, children?)|children)=>element,
123
- B: (props, children?)|children)=>element,
124
- Q: (props, children?)|children)=>element,
125
- I: (props, children?)|children)=>element,
126
- Ul: (props, children?)|children)=>element,
127
- Ol: (props, children?)|children)=>element,
128
- Li: (props, children?)|children)=>element,
129
- Textarea: (props, children?)|children)=>element,
130
- Button: (props, children?)|children)=>element,
131
- Iframe: (props, children?)|children)=>element,
132
- Blockquote: (props, children?)|children)=>element,
133
- Br: (props, children?)|children)=>element,
134
- Code: (props, children?)|children)=>element,
135
- Pre: (props, children?)|children)=>element,
136
- }
137
-
138
- Arche(creator) -> rootElement
97
+ Arche(React {
98
+ createElement: (type, props?, children?)=>ReactElement,
99
+ }) -> ReactElement
100
+ ```
101
+
102
+ # Usage
103
+ ```javascript
104
+ const ReactElement = Arche(React)
105
+
106
+ const {
107
+ A, P, B, Q, I, Ul, Ol, Li,
108
+ H1, H2, H3, H4, H5, H6, Hr, Br,
109
+ Script, Html, Body, Nav, Section, Article, Footer, Span, Div, Img, Video,
110
+ Form, Fieldset, Input, Label, Textarea, Select, Option,
111
+ Button, Iframe, Blockquote, Code, Pre,
112
+ } = ReactElement
139
113
  ```
140
114
 
115
+ Don't see an element you need? Just create it!
116
+ ```javascript [playground]
117
+ const ReactElement = Arche(React)
118
+
119
+ const Aside = ReactElement('aside')
120
+ ```
121
+
122
+ ## Using React Context
123
+ To use React Context with Arche, wrap `YourContext.Provider` with `ReactElement` and supply `value` as a prop, specifying children in the next argument.
124
+
125
+ JSX example:
126
+ ```javascript
127
+ function ArticleWrapper () {
128
+ const [theme, setTheme] = React.useState(themes[0])
129
+
130
+ return (
131
+ <ThemeContext.Provider value={{
132
+ theme,
133
+ changeTheme: setTheme
134
+ }}>
135
+ <ThemeSwitcher />
136
+ <Article />
137
+ </ThemeContext.Provider>
138
+ )
139
+ }
140
+ ```
141
+
142
+ Translates to the following with Arche:
143
+ ```javascript
144
+ const ArticleWrapper = ReactElement(() => {
145
+ const [theme, setTheme] = React.useState(themes[0])
146
+
147
+ return ReactElement(ThemeContext.Provider)({
148
+ value: { theme, changeTheme: setTheme },
149
+ }, [ThemeSwitcher(), Article()])
150
+ })
151
+ ```
package/es.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Arche v0.1.3
2
+ * Arche v0.2.0
3
3
  * https://github.com/richytong/arche
4
- * (c) 2020 Richard Tong
4
+ * (c) 2020-2021 Richard Tong
5
5
  * Arche may be freely distributed under the MIT license.
6
6
  */
7
7
 
@@ -22,10 +22,7 @@ const isArray = Array.isArray
22
22
  * ```
23
23
  */
24
24
  const elementSetAttribute = function (element, key, value) {
25
- if (value == null) {
26
- return element
27
- }
28
- if (value.constructor == Object) { // style
25
+ if (value != null && value.constructor == Object) { // style
29
26
  for (const subKey in value) {
30
27
  element[key][subKey] = value[subKey]
31
28
  }
@@ -198,37 +195,53 @@ const Arche = function (creator) {
198
195
  if (isArray(arg1)) {
199
196
  return creatorCreateElement(creator, type, arg0, arg1)
200
197
  }
198
+ if (arg1 == null) {
199
+ return creatorCreateElement(creator, type, arg0, [])
200
+ }
201
201
  return creatorCreateElement(creator, type, arg0, [arg1])
202
202
  }
203
203
 
204
+ rootElement.A = rootElement('a')
205
+ rootElement.P = rootElement('p')
206
+ rootElement.B = rootElement('b')
207
+ rootElement.Q = rootElement('q')
208
+ rootElement.I = rootElement('i')
209
+ rootElement.Ul = rootElement('ul')
210
+ rootElement.Ol = rootElement('ol')
211
+ rootElement.Li = rootElement('li')
212
+
213
+ rootElement.H1 = rootElement('h1')
214
+ rootElement.H2 = rootElement('h2')
215
+ rootElement.H3 = rootElement('h3')
216
+ rootElement.H4 = rootElement('h4')
217
+ rootElement.H5 = rootElement('h5')
218
+ rootElement.H6 = rootElement('h6')
219
+ rootElement.Hr = rootElement('hr')
220
+ rootElement.Br = rootElement('br')
221
+
204
222
  rootElement.Script = rootElement('script')
205
223
  rootElement.Html = rootElement('html')
206
224
  rootElement.Body = rootElement('body')
225
+ rootElement.Nav = rootElement('nav')
207
226
  rootElement.Section = rootElement('section')
208
227
  rootElement.Article = rootElement('article')
228
+ rootElement.Footer = rootElement('footer')
209
229
  rootElement.Span = rootElement('span')
210
230
  rootElement.Div = rootElement('div')
211
231
  rootElement.Img = rootElement('img')
212
- rootElement.H1 = rootElement('h1')
213
- rootElement.H2 = rootElement('h2')
214
- rootElement.H3 = rootElement('h3')
215
- rootElement.H4 = rootElement('h4')
216
- rootElement.H5 = rootElement('h5')
217
- rootElement.H6 = rootElement('h6')
232
+ rootElement.Video = rootElement('video')
218
233
 
219
- rootElement.A = rootElement('a')
220
- rootElement.P = rootElement('p')
221
- rootElement.B = rootElement('b')
222
- rootElement.Q = rootElement('q')
223
- rootElement.I = rootElement('i')
224
- rootElement.Ul = rootElement('ul')
225
- rootElement.Ol = rootElement('ol')
226
- rootElement.Li = rootElement('li')
234
+ rootElement.Form = rootElement('form')
235
+ rootElement.Fieldset = rootElement('fieldset')
236
+ rootElement.Input = rootElement('input')
237
+ rootElement.Label = rootElement('label')
227
238
  rootElement.Textarea = rootElement('textarea')
239
+ rootElement.Select = rootElement('select')
240
+ rootElement.Option = rootElement('option')
241
+
228
242
  rootElement.Button = rootElement('button')
229
243
  rootElement.Iframe = rootElement('iframe')
230
244
  rootElement.Blockquote = rootElement('blockquote')
231
- rootElement.Br = rootElement('br')
232
245
  rootElement.Code = rootElement('code')
233
246
  rootElement.Pre = rootElement('pre')
234
247
 
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Arche v0.1.3
2
+ * Arche v0.2.0
3
3
  * https://github.com/richytong/arche
4
- * (c) 2020 Richard Tong
4
+ * (c) 2020-2021 Richard Tong
5
5
  * Arche may be freely distributed under the MIT license.
6
6
  */
7
7
 
@@ -207,34 +207,47 @@ const Arche = function (creator) {
207
207
  return creatorCreateElement(creator, type, arg0, [arg1])
208
208
  }
209
209
 
210
+ rootElement.A = rootElement('a')
211
+ rootElement.P = rootElement('p')
212
+ rootElement.B = rootElement('b')
213
+ rootElement.Q = rootElement('q')
214
+ rootElement.I = rootElement('i')
215
+ rootElement.Ul = rootElement('ul')
216
+ rootElement.Ol = rootElement('ol')
217
+ rootElement.Li = rootElement('li')
218
+
219
+ rootElement.H1 = rootElement('h1')
220
+ rootElement.H2 = rootElement('h2')
221
+ rootElement.H3 = rootElement('h3')
222
+ rootElement.H4 = rootElement('h4')
223
+ rootElement.H5 = rootElement('h5')
224
+ rootElement.H6 = rootElement('h6')
225
+ rootElement.Hr = rootElement('hr')
226
+ rootElement.Br = rootElement('br')
227
+
210
228
  rootElement.Script = rootElement('script')
211
229
  rootElement.Html = rootElement('html')
212
230
  rootElement.Body = rootElement('body')
231
+ rootElement.Nav = rootElement('nav')
213
232
  rootElement.Section = rootElement('section')
214
233
  rootElement.Article = rootElement('article')
234
+ rootElement.Footer = rootElement('footer')
215
235
  rootElement.Span = rootElement('span')
216
236
  rootElement.Div = rootElement('div')
217
237
  rootElement.Img = rootElement('img')
218
- rootElement.H1 = rootElement('h1')
219
- rootElement.H2 = rootElement('h2')
220
- rootElement.H3 = rootElement('h3')
221
- rootElement.H4 = rootElement('h4')
222
- rootElement.H5 = rootElement('h5')
223
- rootElement.H6 = rootElement('h6')
238
+ rootElement.Video = rootElement('video')
224
239
 
225
- rootElement.A = rootElement('a')
226
- rootElement.P = rootElement('p')
227
- rootElement.B = rootElement('b')
228
- rootElement.Q = rootElement('q')
229
- rootElement.I = rootElement('i')
230
- rootElement.Ul = rootElement('ul')
231
- rootElement.Ol = rootElement('ol')
232
- rootElement.Li = rootElement('li')
240
+ rootElement.Form = rootElement('form')
241
+ rootElement.Fieldset = rootElement('fieldset')
242
+ rootElement.Input = rootElement('input')
243
+ rootElement.Label = rootElement('label')
233
244
  rootElement.Textarea = rootElement('textarea')
245
+ rootElement.Select = rootElement('select')
246
+ rootElement.Option = rootElement('option')
247
+
234
248
  rootElement.Button = rootElement('button')
235
249
  rootElement.Iframe = rootElement('iframe')
236
250
  rootElement.Blockquote = rootElement('blockquote')
237
- rootElement.Br = rootElement('br')
238
251
  rootElement.Code = rootElement('code')
239
252
  rootElement.Pre = rootElement('pre')
240
253
 
package/index.mjs ADDED
@@ -0,0 +1,251 @@
1
+ /**
2
+ * Arche v0.2.0
3
+ * https://github.com/richytong/arche
4
+ * (c) 2020-2021 Richard Tong
5
+ * Arche may be freely distributed under the MIT license.
6
+ */
7
+
8
+ const isArray = Array.isArray
9
+
10
+ /**
11
+ * @name elementSetAttribute
12
+ *
13
+ * @synopsis
14
+ * ```coffeescript [specscript]
15
+ * Element = Object
16
+ *
17
+ * var element Element,
18
+ * key string,
19
+ * value string|number|Object,
20
+ *
21
+ * elementSetAttribute(element, key, value) -> element
22
+ * ```
23
+ */
24
+ const elementSetAttribute = function (element, key, value) {
25
+ if (value != null && value.constructor == Object) { // style
26
+ for (const subKey in value) {
27
+ element[key][subKey] = value[subKey]
28
+ }
29
+ } else {
30
+ element.setAttribute(key, value)
31
+ }
32
+ return element
33
+ }
34
+
35
+ /**
36
+ * @name creatorCreateElement
37
+ *
38
+ * @synopsis
39
+ * ```coffeescript [specscript]
40
+ * Element = Object
41
+ *
42
+ * var type string|function,
43
+ * props Object,
44
+ * children string|Object|Array<string|Object>,
45
+ * element Element,
46
+ * creator { createElement: (type, props?, children?)=>element },
47
+ *
48
+ * creatorCreateElement(creator, type, props, children) -> element
49
+ * ```
50
+ */
51
+ const creatorCreateElement = function (creator, type, props, children) {
52
+ if (creator.createElement.length == 1) {
53
+ const element = creator.createElement(type) // document.createElement
54
+ for (const key in props) {
55
+ elementSetAttribute(element, key, props[key])
56
+ }
57
+ const childrenLength = children.length
58
+ let childrenIndex = -1
59
+ while (++childrenIndex < childrenLength) {
60
+ const child = children[childrenIndex]
61
+ if (typeof child == 'string') {
62
+ element.appendChild(creator.createTextNode(child))
63
+ } else {
64
+ element.appendChild(child)
65
+ }
66
+ }
67
+ return element
68
+ }
69
+ return creator.createElement(type, props, ...children) // React.createElement
70
+ }
71
+
72
+ /**
73
+ * @name Arche
74
+ *
75
+ * @synopsis
76
+ * ```coffeescript [specscript]
77
+ * Element = Object
78
+ *
79
+ * var type string|function,
80
+ * props Object,
81
+ * children string|Object|Array<string|Object>,
82
+ * element Element,
83
+ * creator { createElement: (type, props?, children?)=>element },
84
+ * rootElement type=>((props, children?)|children)=>element {
85
+ * Script: ((props, children?)|children)=>element,
86
+ * Html: ((props, children?)|children)=>element,
87
+ * Body: (props, children?)|children)=>element,
88
+ * Section: (props, children?)|children)=>element,
89
+ * Article: (props, children?)|children)=>element,
90
+ * Span: (props, children?)|children)=>element,
91
+ * Div: (props, children?)|children)=>element,
92
+ * Img: (props, children?)|children)=>element,
93
+ * H1: (props, children?)|children)=>element,
94
+ * H2: (props, children?)|children)=>element,
95
+ * H3: (props, children?)|children)=>element,
96
+ * H4: (props, children?)|children)=>element,
97
+ * H5: (props, children?)|children)=>element,
98
+ * H6: (props, children?)|children)=>element,
99
+ *
100
+ * A: (props, children?)|children)=>element,
101
+ * P: (props, children?)|children)=>element,
102
+ * B: (props, children?)|children)=>element,
103
+ * Q: (props, children?)|children)=>element,
104
+ * I: (props, children?)|children)=>element,
105
+ * Ul: (props, children?)|children)=>element,
106
+ * Ol: (props, children?)|children)=>element,
107
+ * Li: (props, children?)|children)=>element,
108
+ * Textarea: (props, children?)|children)=>element,
109
+ * Button: (props, children?)|children)=>element,
110
+ * Iframe: (props, children?)|children)=>element,
111
+ * Blockquote: (props, children?)|children)=>element,
112
+ * Br: (props, children?)|children)=>element,
113
+ * Code: (props, children?)|children)=>element,
114
+ * Pre: (props, children?)|children)=>element,
115
+ * }
116
+ *
117
+ * Arche(creator) -> rootElement
118
+ * ```
119
+ *
120
+ * @description
121
+ * > Arche (/ˈɑːrki/; Ancient Greek: ἀρχή) is a Greek word with primary senses "beginning", "origin" or "source of action" (ἐξ' ἀρχῆς: from the beginning, οr ἐξ' ἀρχῆς λόγος: the original argument), and later "first principle" or "element". ([wikipedia](https://en.wikipedia.org/wiki/Arche))
122
+ *
123
+ * HTML as JavaScript.
124
+ *
125
+ * ```javascript [playground]
126
+ * const ReactElement = Arche(React)
127
+ * // supply the React library
128
+ *
129
+ * const { Div, H1, P } = ReactElement
130
+ * // some common building blocks are provided on ReactElement
131
+ * // as property functions.
132
+ *
133
+ * const myElement = Div([
134
+ * H1('I am a heading'),
135
+ * P('heyo'),
136
+ * P('lorem ipsum'),
137
+ * ])
138
+ *
139
+ * console.log(myElement)
140
+ * // {
141
+ * // $$typeof: Symbol(react.element),
142
+ * // type: 'div',
143
+ * // props: {
144
+ * // children: [
145
+ * // { $$typeof: Symbol(react.element), type: 'h1', props: { children: 'I am a heading' } },
146
+ * // { $$typeof: Symbol(react.element), type: 'p', props: { children: 'heyo' } },
147
+ * // { $$typeof: Symbol(react.element), type: 'p', props: { children: 'heyo' } },
148
+ * // ],
149
+ * // },
150
+ * // }
151
+ * ```
152
+ *
153
+ * Create dynamic components with props:
154
+ * ```javascript [playground]
155
+ * const ReactElement = Arche(React)
156
+ * const { Div, P, Button } = ReactElement
157
+ *
158
+ * const UserCard = ReactElement(({
159
+ * firstName, lastName, age,
160
+ * }) => Div([
161
+ * H1(`${firstName} ${lastName}`),
162
+ * P({ style: { color: 'lightgrey' } }, [age]),
163
+ * ]))
164
+ * ```
165
+ *
166
+ * Complete interop with React hooks (converted from [this example](https://reactjs.org/docs/hooks-intro.html)):
167
+ * ```javascript [playground]
168
+ * const ReactElement = Arche(React)
169
+ * const { Div, P, Button } = ReactElement
170
+ * const { useState } = React
171
+ *
172
+ * const Example = ReactElement(() => {
173
+ * const [count, setCount] = useState(0)
174
+ *
175
+ * return Div([
176
+ * P(`You clicked ${count} times`),
177
+ * Button({
178
+ * onClick() {
179
+ * setCount(count + 1)
180
+ * },
181
+ * }, 'Click me'),
182
+ * ])
183
+ * })
184
+ * ```
185
+ */
186
+
187
+ const Arche = function (creator) {
188
+ const rootElement = type => function creatingElement(arg0, arg1) {
189
+ if (isArray(arg0)) {
190
+ return creatorCreateElement(creator, type, {}, arg0)
191
+ }
192
+ if (typeof arg0 == 'string') {
193
+ return creatorCreateElement(creator, type, {}, [arg0])
194
+ }
195
+ if (isArray(arg1)) {
196
+ return creatorCreateElement(creator, type, arg0, arg1)
197
+ }
198
+ if (arg1 == null) {
199
+ return creatorCreateElement(creator, type, arg0, [])
200
+ }
201
+ return creatorCreateElement(creator, type, arg0, [arg1])
202
+ }
203
+
204
+ rootElement.A = rootElement('a')
205
+ rootElement.P = rootElement('p')
206
+ rootElement.B = rootElement('b')
207
+ rootElement.Q = rootElement('q')
208
+ rootElement.I = rootElement('i')
209
+ rootElement.Ul = rootElement('ul')
210
+ rootElement.Ol = rootElement('ol')
211
+ rootElement.Li = rootElement('li')
212
+
213
+ rootElement.H1 = rootElement('h1')
214
+ rootElement.H2 = rootElement('h2')
215
+ rootElement.H3 = rootElement('h3')
216
+ rootElement.H4 = rootElement('h4')
217
+ rootElement.H5 = rootElement('h5')
218
+ rootElement.H6 = rootElement('h6')
219
+ rootElement.Hr = rootElement('hr')
220
+ rootElement.Br = rootElement('br')
221
+
222
+ rootElement.Script = rootElement('script')
223
+ rootElement.Html = rootElement('html')
224
+ rootElement.Body = rootElement('body')
225
+ rootElement.Nav = rootElement('nav')
226
+ rootElement.Section = rootElement('section')
227
+ rootElement.Article = rootElement('article')
228
+ rootElement.Footer = rootElement('footer')
229
+ rootElement.Span = rootElement('span')
230
+ rootElement.Div = rootElement('div')
231
+ rootElement.Img = rootElement('img')
232
+ rootElement.Video = rootElement('video')
233
+
234
+ rootElement.Form = rootElement('form')
235
+ rootElement.Fieldset = rootElement('fieldset')
236
+ rootElement.Input = rootElement('input')
237
+ rootElement.Label = rootElement('label')
238
+ rootElement.Textarea = rootElement('textarea')
239
+ rootElement.Select = rootElement('select')
240
+ rootElement.Option = rootElement('option')
241
+
242
+ rootElement.Button = rootElement('button')
243
+ rootElement.Iframe = rootElement('iframe')
244
+ rootElement.Blockquote = rootElement('blockquote')
245
+ rootElement.Code = rootElement('code')
246
+ rootElement.Pre = rootElement('pre')
247
+
248
+ return rootElement
249
+ }
250
+
251
+ export default Arche
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arche",
3
- "version": "0.1.5",
3
+ "version": "0.2.5",
4
4
  "description": "HTML as JavaScript",
5
5
  "author": "Richard Tong",
6
6
  "license": "MIT",