halfcab 15.0.5 → 15.0.9
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.
- package/README.md +14 -42
- package/halfcab.mjs +2 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ Halfcab is no longer built as a common js distribution.
|
|
|
25
25
|
halfcab exposes a bunch of functions and objects that you import from the halfcab module. If you want to grab them all at once ( you don't ), it'd look like this:
|
|
26
26
|
|
|
27
27
|
```js
|
|
28
|
-
import halfcab, { html, css, injectHTML, injectMarkdown, geb, eventEmitter, updateState, rerender, formField, formIsValid, fieldIsTouched, resetTouched, ssr, defineRoute, gotoRoute, http, getRouteComponent, nextTick
|
|
28
|
+
import halfcab, { html, css, injectHTML, injectMarkdown, geb, eventEmitter, updateState, rerender, formField, formIsValid, fieldIsTouched, resetTouched, ssr, defineRoute, gotoRoute, http, getRouteComponent, nextTick } from 'halfcab'
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
## Installation
|
|
@@ -55,21 +55,17 @@ halfcab({
|
|
|
55
55
|
#### Components
|
|
56
56
|
- `html` - creates dom elements from template literals
|
|
57
57
|
- `css` - injects css into html component's class property
|
|
58
|
-
- `
|
|
59
|
-
- `LRU` - nanolru: https://www.npmjs.com/package/nanolru
|
|
60
|
-
- `PureComponent` - Extends Component and only rerenders when arguments change
|
|
61
|
-
- `cachedComponent` - pair with PureComponent to automatically cache components using LRU and only rerender when arguments change
|
|
62
|
-
- `injectHTML` - injects html from a string, much like a triple mustache or React's dangerouslySetInnerHTML
|
|
58
|
+
- `injectHTML` - injects html from a string, much like a triple mustache or React's dangerouslySetInnerHTML. Uses lit-html's unsafeHTML
|
|
63
59
|
- `injectMarkdown` - the same as `injectHTML` but first converts markdown into HTML, making sure HTML entities are not double encoded.
|
|
64
60
|
|
|
65
61
|
Both injectHTML and injectMarkdown have a second argument for options. Currently there's just a single option:
|
|
66
62
|
|
|
67
63
|
```{wrapper: false}``` (default is true)
|
|
68
64
|
|
|
69
|
-
By default injected html will have a wrapping `<div>`. This is to ensure an html element can successfully be made
|
|
65
|
+
By default injected html will have a wrapping `<div>`. This is to ensure an html element can successfully be made.
|
|
70
66
|
If you know that your HTML already has a wrapping element, or it's just a single element, you can set the wrapper to false. Particularly useful when dealing with SVGs.
|
|
71
67
|
|
|
72
|
-
Under the hood, halfcab uses
|
|
68
|
+
Under the hood, halfcab uses lit-html, which turns tagged template literals into elements, and updates the DOM.
|
|
73
69
|
|
|
74
70
|
Here's an example of a simple component:
|
|
75
71
|
```js
|
|
@@ -82,7 +78,7 @@ export default args => html`
|
|
|
82
78
|
<img src="${args.company.logo.url}" />
|
|
83
79
|
</div>
|
|
84
80
|
|
|
85
|
-
<div style="width: 216px; text-align: center;"
|
|
81
|
+
<div style="width: 216px; text-align: center;" ?disabled=${args.disabled}>
|
|
86
82
|
<button onclick=${e => {
|
|
87
83
|
alert('I am a button')}}
|
|
88
84
|
>Log in <i class="material-icons" style="vertical-align: inherit">account_circle</i>
|
|
@@ -93,9 +89,10 @@ export default args => html`
|
|
|
93
89
|
`
|
|
94
90
|
|
|
95
91
|
```
|
|
96
|
-
|
|
97
92
|
This is just regular HTML with one twist - Using event handlers like onclick will use the scope of your component, not the global scope. Just don't use quotation marks around it. Put it within ${ } instead.
|
|
98
93
|
|
|
94
|
+
Note: As of version 15, to conditionally include html flags like `disabled`, halfcab now uses the lit-html syntax starting with a question mark, so like the example above, you'd have: `?disabled=${args.disabled}`, where we used to have the nanohtml syntax like this: `${args.disabled ? {disabled} : ''}`
|
|
95
|
+
|
|
99
96
|
halfcab uses csjs for inline css, like so:
|
|
100
97
|
```js
|
|
101
98
|
import { html, css } from 'halfcab'
|
|
@@ -118,7 +115,7 @@ let styles = css`
|
|
|
118
115
|
`
|
|
119
116
|
|
|
120
117
|
export default args => html`
|
|
121
|
-
<header class
|
|
118
|
+
<header class="${styles.header}">
|
|
122
119
|
<nav>
|
|
123
120
|
<div style="width: 280px;">
|
|
124
121
|
<img src="${args.company.logo.url}" />
|
|
@@ -138,24 +135,7 @@ Notice how you can use media queries, and inject variables using JavaScript! The
|
|
|
138
135
|
|
|
139
136
|
|
|
140
137
|
#### Performance
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
For convenience if you want to write pure components that are cached you can use `PureComponent` and not have to have an update method and return cached components like so:
|
|
144
|
-
|
|
145
|
-
```js
|
|
146
|
-
class DateTimePicker extends PureComponent {
|
|
147
|
-
createElement(args) {
|
|
148
|
-
this.myFunction = args.myFunction
|
|
149
|
-
return html`<div>Datepicker ${args.something}</div>`
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
export default args => cachedComponent(DateTimePicker, args, args.uniqueKey)
|
|
153
|
-
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
Make sure you have a uniqueKey (id) so that the component is properly cached and not referenced by any other call to cachedComponent
|
|
157
|
-
|
|
158
|
-
In the example above, the component that matches the uniqueKey will be extracted from the cache, the new args will be compared against the previous args, and if there's a difference, it will rerender, and if not, you'll just get the existing element from the cache. Note that this does a deep compare of objects and for functions it just copies them across. So make sure that all your functions are copied to `this`. If you see the line with `this.myFunction = args.myFunction` - this is done when the element is created, but it will also automatically be run for anything argument that is a function when performing an update. This is so that if your function argument has closed over any other variables from elsewhere, it always gets the latest function, even if it's not having to rerender the element.
|
|
138
|
+
From version 15 onwards, lit-html is used under the hood (replacing nanohtml and nanomorph) so the old caching system functions have been removed as lit-html is efficient out of the box. This keeps things really simple as you no longer need to provide uniqueKeys for component caching and can just use functions as there's no need for extending classes or managing a cache.
|
|
159
139
|
|
|
160
140
|
#### Events
|
|
161
141
|
- `geb` - global event bus
|
|
@@ -489,8 +469,6 @@ htmlTemplate.mjs
|
|
|
489
469
|
import pack from '../../../package'
|
|
490
470
|
import components from '../../../components'
|
|
491
471
|
import { ssr } from 'halfcab'
|
|
492
|
-
import { minify } from 'html-minifier'
|
|
493
|
-
|
|
494
472
|
|
|
495
473
|
function htmlOutput(data){
|
|
496
474
|
let apiData = data[0]
|
|
@@ -510,7 +488,7 @@ function htmlOutput(data){
|
|
|
510
488
|
</head>
|
|
511
489
|
<body style="padding: 0px; margin: 0px;">
|
|
512
490
|
|
|
513
|
-
|
|
491
|
+
<div id="root">${componentsString}</div>
|
|
514
492
|
|
|
515
493
|
</body>
|
|
516
494
|
</html>
|
|
@@ -518,10 +496,7 @@ function htmlOutput(data){
|
|
|
518
496
|
}
|
|
519
497
|
|
|
520
498
|
|
|
521
|
-
export default data =>
|
|
522
|
-
collapseWhitespace: true,
|
|
523
|
-
minifyCSS: true
|
|
524
|
-
})
|
|
499
|
+
export default data => htmlOutput(data)
|
|
525
500
|
```
|
|
526
501
|
|
|
527
502
|
###### Browser JS structure
|
|
@@ -550,9 +525,6 @@ halfcab({
|
|
|
550
525
|
})
|
|
551
526
|
|
|
552
527
|
```
|
|
553
|
-
Notice:
|
|
554
|
-
1. This browser code is also creating an mock function to add to the cd object, but this time, it's actually importing the real someBrowserOnlyLib library and using it before returning the element.
|
|
555
|
-
2. The halfcab function returns a promise that returns our root element ready for us to use.
|
|
556
528
|
|
|
557
529
|
###### The common file between server and browser - components.mjs
|
|
558
530
|
|
|
@@ -570,8 +542,8 @@ function products(products){
|
|
|
570
542
|
}
|
|
571
543
|
}
|
|
572
544
|
|
|
573
|
-
export default args =>
|
|
574
|
-
<div
|
|
545
|
+
export default args => html`
|
|
546
|
+
<div style="margin-top: 10px; text-align: center;">
|
|
575
547
|
${topNav({
|
|
576
548
|
company: args.company,
|
|
577
549
|
products: products(args.products)
|
|
@@ -584,7 +556,7 @@ export default args => cd.mock(html`
|
|
|
584
556
|
${footer()}
|
|
585
557
|
${injectHTML(args.safeHTMLFromServer)}
|
|
586
558
|
</div>
|
|
587
|
-
`
|
|
559
|
+
`
|
|
588
560
|
```
|
|
589
561
|
|
|
590
562
|
This is our top level component, from here we're also pulling in three other components - topNav, body, and footer. This is the start of the tree-like component structure.
|
package/halfcab.mjs
CHANGED
|
@@ -359,9 +359,9 @@ function emptySSRVideos (c) {
|
|
|
359
359
|
|
|
360
360
|
function injectHTML (htmlString, options) {
|
|
361
361
|
if (options && options.wrapper === false) {
|
|
362
|
-
return
|
|
362
|
+
return unsafeHTML(htmlString)
|
|
363
363
|
}
|
|
364
|
-
return html
|
|
364
|
+
return html`<div>${unsafeHTML(htmlString)}</div>`
|
|
365
365
|
}
|
|
366
366
|
|
|
367
367
|
function injectMarkdown (mdString, options) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "halfcab",
|
|
3
|
-
"version": "15.0.
|
|
3
|
+
"version": "15.0.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A simple universal JavaScript framework focused on making use of es2015 template strings to build components.",
|
|
6
6
|
"main": "halfcab.mjs",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"versionbump:fix": "npm version patch --no-git-tag-version",
|
|
14
14
|
"versionbump:feature": "npm version major --no-git-tag-version",
|
|
15
15
|
"versionbump:breakingchanges": "npm version major --no-git-tag-version",
|
|
16
|
-
"npm-publish": "npm publish
|
|
16
|
+
"npm-publish": "npm publish",
|
|
17
17
|
"start:example": "npx --yes serve . -l 5173"
|
|
18
18
|
},
|
|
19
19
|
"repository": {
|