@daz4126/helium 0.3.0 → 0.3.1

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 (3) hide show
  1. package/README.md +54 -10
  2. package/helium.js +4 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -2,14 +2,16 @@
2
2
 
3
3
  The ultra-light library that makes HTML interactive!
4
4
 
5
- Here's a simple example of a button that counts clicks and turns red after more than 3 presses:
5
+ Here's a simple example of a button that counts the number of times it has been clicked and turns red after more than 3 clicks:
6
6
 
7
7
  ```html
8
- <button @helium @click="count++" :style="count > 3 && 'background: red'">
9
- clicked <span @react="count">0</span> times
8
+ <button @click="count++" :style="count > 3 && 'background: red'">
9
+ clicked <b @text="count">0</b> times
10
10
  </button>
11
11
  ```
12
12
 
13
+ It's really simple to use - just sprinkle the magic @attributes into your HTML and watch it come alive!
14
+
13
15
  [See more examples here](https://codepen.io/daz4126/pen/YPwwdBK)
14
16
 
15
17
  To use, just import from the CDN then call the `helium` funtion (no install or build step required!):
@@ -29,32 +31,36 @@ Then include it in your
29
31
 
30
32
  ```javascript
31
33
  import helium from "@daz4126/helium"
32
- helium
34
+ helium()
33
35
  ```
34
36
 
37
+ # Helium Attributes
38
+
39
+ Helium uses custom attributes to add interactivity to HTML elements. To identify them, they all start with `@`, although there are also `data` attribute aliases that can be used instead.
40
+
35
41
  ## `@helium`
36
42
 
37
43
  This attribute sets the root element. Helium attributes can only be used on this element and its children. If not set then it defaults to `document`.
38
44
 
39
45
  Alias: `data-helium`
40
46
 
41
- ## `@react`
47
+ ## `@text`
42
48
 
43
49
  Inserts the result of a JavaScript expression into the text-content of the element.
44
50
 
45
51
  This will update the textContent of the element with the value of the `count` variable:
46
52
 
47
53
  ```html
48
- <b @react="count">0</b>
54
+ <b @text="count">0</b>
49
55
  ```
50
56
 
51
57
  You can also use expresions. This will update the textContent of the element with the value of the `name` variable but in uppercase:
52
58
 
53
59
  ```html
54
- <span @react="name.toUpperCase()">0</span>
60
+ <span @text="name.toUpperCase()">0</span>
55
61
  ```
56
62
 
57
- Alias: `data-he-react`
63
+ Alias: `data-he-text`
58
64
 
59
65
  ## `@bind`
60
66
 
@@ -73,7 +79,7 @@ Alias: `data-he-bind`
73
79
  Makes the element hidden or visible depending on the result of a JavaScript expression.
74
80
 
75
81
  ```html
76
- <div @hidden="count > 3">Only visible if the count is greater than 3</div>
82
+ <div @visible="count > 3">Only visible if the count is greater than 3</div>
77
83
  ```
78
84
 
79
85
  Alias: `data-he-hidden` & `data-he-visible`
@@ -132,7 +138,17 @@ You can add modifiers of `prevent` to prevent the default behaviour, `outside` t
132
138
 
133
139
  Alias: prepend the event name with `data-he-on`, for example `data-he-onclick="count++"`
134
140
 
135
- ## Conditional Attributes
141
+ ## Dynamic Attributes
142
+
143
+ It's possible to dynamically update the attributes of elements. To do this, just prepend a `:` in front of the attribute name and write a JavaScript expression that evaulates to the desired attribute value. This will update whenever any of the Helium variables change value.
144
+
145
+ In the following example, the `<div>` element has a dynamic class attribute that will be 'normal' if the count is less than 10, but 'danger' if the count is 10 or more:
146
+
147
+ ```html
148
+ <div :class="count < 10 ? 'active' : 'danger'>
149
+ The count is <b @text=count></b>
150
+ </div>
151
+ ```
136
152
 
137
153
  ## Magic Attributes
138
154
 
@@ -145,3 +161,31 @@ Alias: prepend the event name with `data-he-on`, for example `data-he-onclick="c
145
161
  ## Default Variables and Functions
146
162
 
147
163
  The helium function accepts a single JavaScript object as an argument. This can include default variable values and functions that can then be called inside the JavaScript expressions.
164
+
165
+ For example, the following will set the `count` variable to an initial value of `29` and the `name` variable to "Helium":
166
+
167
+ ```javascript
168
+ helium({ count: 29, name: "Helium"})
169
+ ```
170
+
171
+ The following example shows how a function can be added into Helium and then used by even listeners:
172
+
173
+ ```javascript
174
+ helium({
175
+ appendTo(element){
176
+ const li = document.createElement("li")
177
+ li.textContent = "New Item"
178
+ element.append(li)
179
+ }
180
+ })
181
+
182
+ ```
183
+
184
+ This function can then be called from an event handler, such as `@click`:
185
+
186
+ ```html
187
+ <ul @ref="list"></ul>
188
+ <button @click="appendTo($list)>Append item to list</button>
189
+ ```
190
+
191
+ Note that Magic attributes and Helium variables are not available inside these functions and changing the value of a reactive variable will not trigger an update. They are best used for side effects (such as DOM manipulation) and to return data.
package/helium.js CHANGED
@@ -75,7 +75,10 @@ export default function helium(data = {}) {
75
75
  try {
76
76
  new Function(`let ${value} = 1`);
77
77
  state[value] ||=
78
- name == "@react" || name == "data-he-react"
78
+ name == "@react" ||
79
+ name == "data-he-react" ||
80
+ name == "@text" ||
81
+ name == "data-he-text"
79
82
  ? el.textContent
80
83
  : el.value;
81
84
  } catch (e) {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@daz4126/helium",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "main": "helium.js",
5
5
  "type": "module",
6
6
  "keywords": [