@daz4126/helium 0.2.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.
package/LLM_GUIDE.md ADDED
@@ -0,0 +1,138 @@
1
+ # Helium LLM Guide
2
+
3
+ This guide explains how to use **Helium** to build interactive UIs.
4
+ It is designed for **AI assistants** and **developers** to have a clear playbook for generating Helium code.
5
+
6
+ ---
7
+
8
+ ## Core Ideas
9
+
10
+ - Helium uses **HTML attributes** to connect state and DOM.
11
+ - Attributes can use either `@` or `data-he-*` prefixes. Both are valid.
12
+ - State lives in a JavaScript object (via `helium({...})`) or in HTML with `@data`.
13
+ - Changes to state automatically update the DOM where it is bound.
14
+
15
+ ---
16
+
17
+ ## Directives
18
+
19
+ ### State
20
+ - `@data` / `data-he` → Initialize state.
21
+ ```html
22
+ <div @data="{ count: 0, open: false }"></div>
23
+ ```
24
+
25
+ ### Reactive values
26
+ @react / data-he-react → Bind text content to state.
27
+
28
+ ```html
29
+ <span @react="count"></span>
30
+ ```
31
+
32
+ ### Two-way binding
33
+ @bind / data-he-bind → Sync input values with state.
34
+
35
+ ```html
36
+ <input @bind="name" placeholder="Enter your name">
37
+ ```
38
+
39
+ ### Visibility
40
+ @hidden / data-he-hidden → Hide element if expression is true.
41
+ @visible / data-he-visible → Show element if expression is true.
42
+
43
+ ```html
44
+ <div @hidden="!open">Only visible if `open` is true</div>
45
+ ```
46
+
47
+ ### Attribute binding
48
+ :attribute → Dynamically bind attributes.
49
+
50
+ ```html
51
+ <p :class="count > 3 ? 'danger' : 'safe'">Hello</p>
52
+ ```
53
+
54
+ ###Events
55
+
56
+ @event / data-he-on* → Attach event listeners.
57
+ .prevent → preventDefault
58
+ .once → run once then remove
59
+ .outside → listen outside the element
60
+
61
+ ```html
62
+ <button @click="count++">Increment</button>
63
+ <button @click.prevent="submitForm()">Save</button>
64
+ ```
65
+
66
+ ### Refs
67
+
68
+ @ref / data-he-ref → Create named references to elements.
69
+
70
+ ```html
71
+ <div @ref="box"></div>
72
+ ```
73
+
74
+
75
+ ### Initialization
76
+
77
+ @init / data-he-init → Run code once on init.
78
+
79
+ ```html
80
+ <div @init="console.log('ready')"></div>
81
+ ```
82
+
83
+ ### Example Patterns
84
+
85
+ #### Counter
86
+
87
+ ```html
88
+ <div @data="{ count: 0 }">
89
+ <h1 @react="count"></h1>
90
+ <button @click="count++">+</button>
91
+ <button @click="count--">-</button>
92
+ </div>
93
+ ```
94
+
95
+ #### Modal
96
+
97
+ ```html
98
+ <div @data="{ open: false }">
99
+ <button @click="open = true">Open Modal</button>
100
+
101
+ <div @visible="open" class="modal">
102
+ <p>Hello Helium!</p>
103
+ <button @click="open = false">Close</button>
104
+ </div>
105
+ </div>
106
+ ```
107
+
108
+ #### Form Handling
109
+
110
+ ```html
111
+ <div @data="{ name: '', email: '' }">
112
+ <input @bind="name" placeholder="Name">
113
+ <input @bind="email" placeholder="Email">
114
+ <button @click.prevent="console.log(name, email)">
115
+ Submit
116
+ </button>
117
+ </div>
118
+ ```
119
+
120
+ ## Tips for LLMs
121
+
122
+ Always start with @data to define state.
123
+
124
+ * Use @react for dynamic text.
125
+ * Use @bind for inputs.
126
+ * Use @click (and modifiers) for actions.
127
+ * Use @hidden/@visible for conditionals.
128
+ * Use :attribute="expression" for dynamic attributes.
129
+ * Use @ref if you need to manipulate elements directly.
130
+ * Use @init for setup logic.
131
+
132
+ When in Doubt ...
133
+
134
+ * Think in terms of state → DOM binding.
135
+ * If something should display, use @react.
136
+ * If it should toggle, use @hidden/@visible.
137
+ * If it should update on input, use @bind.
138
+ * If it should respond to a click, use @click.
package/README.md CHANGED
@@ -2,10 +2,190 @@
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 the number of times it has been clicked and turns red after more than 3 clicks:
6
+
7
+ ```html
8
+ <button @click="count++" :style="count > 3 && 'background: red'">
9
+ clicked <b @text="count">0</b> times
10
+ </button>
11
+ ```
12
+
13
+ It's really simple to use - just sprinkle the magic @attributes into your HTML and watch it come alive!
14
+
15
+ [See more examples here](https://codepen.io/daz4126/pen/YPwwdBK)
16
+
17
+ To use, just import from the CDN then call the `helium` funtion (no install or build step required!):
18
+
19
+ ```javascript
20
+ import helium from "https://cdn.jsdelivr.net/gh/daz-codes/helium/helium.js"
21
+ helium()
22
+ ```
23
+
24
+ Alernatively you can install from NPM:
25
+
26
+ ```bash
27
+ npm install @daz4126/helium
28
+ ```
29
+
30
+ Then include it in your
31
+
32
+ ```javascript
33
+ import helium from "@daz4126/helium"
34
+ helium()
35
+ ```
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
+
41
+ ## `@helium`
42
+
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`.
44
+
45
+ Alias: `data-helium`
46
+
47
+ ## `@text`
48
+
49
+ Inserts the result of a JavaScript expression into the text-content of the element.
50
+
51
+ This will update the textContent of the element with the value of the `count` variable:
52
+
53
+ ```html
54
+ <b @text="count">0</b>
55
+ ```
56
+
57
+ You can also use expresions. This will update the textContent of the element with the value of the `name` variable but in uppercase:
58
+
59
+ ```html
60
+ <span @text="name.toUpperCase()">0</span>
61
+ ```
62
+
63
+ Alias: `data-he-text`
64
+
65
+ ## `@bind`
66
+
67
+ Creates a 2-way binding between an input or textcontent element's value attribute and a variable.
68
+
69
+ Whatever is entered in the following input field will be stored as a variable called `name`:
70
+
71
+ ```html
72
+ <input @bind="name" placeholder="Enter your name">
73
+ ```
74
+
75
+ Alias: `data-he-bind`
76
+
77
+ ## `@hidden` & `@visible`
78
+
79
+ Makes the element hidden or visible depending on the result of a JavaScript expression.
80
+
81
+ ```html
82
+ <div @visible="count > 3">Only visible if the count is greater than 3</div>
83
+ ```
84
+
85
+ Alias: `data-he-hidden` & `data-he-visible`
86
+
87
+ ## `@data`
88
+
89
+ Initializes variables that can be used in JacaScript expressions.
90
+
91
+ ```html
92
+ <div @data="{ count: 0, open: false }"></div>
93
+ ```
94
+
95
+ Alias: `data-he-data`
96
+
97
+ ## `@ref`
98
+
99
+ Creates a reference to the element that can be used in JavaScript expressions.
100
+
101
+ For example, this will create a reference called `$list` to this element:
102
+
5
103
  ```html
6
- <main @helium>
7
- <button @click="count++" :style="count > 3 && 'background: red'">
8
- clicked <span @react="count">0</span> times
9
- </button>
10
- </main>
104
+ <ul @ref="list"></ul>
11
105
  ```
106
+
107
+ This element can then be accessed in other JavaScript expressions as `$list`, for example:
108
+
109
+ ```html
110
+ <button @click="appendTo($list)">Add Task</button>
111
+ ```
112
+
113
+ Alias: `data-he-ref`
114
+
115
+ ## `@init`
116
+
117
+ A JavaScript expression that will run once when Helium initializes.
118
+
119
+ ```html
120
+ <div @init="timestamp = Date.now()"></div>
121
+ ```
122
+
123
+ Alias: `data-he-init`
124
+
125
+ ## Event Listeners & Handlers
126
+
127
+ Event listeners and handlers can be created by prepending `@` before the event name, for example `@click="count++"` will run the cound `count++` when the element is clicked on.
128
+
129
+ ```html
130
+ <button @click="count++">Increment</button>
131
+ ```
132
+
133
+ You can add modifiers of `prevent` to prevent the default behaviour, `outside` to only fire when the event happens outside the element and `once` to only run the event handler once.
134
+
135
+ ```html
136
+ <button @click.prevent="submitForm()">Save</button>
137
+ ```
138
+
139
+ Alias: prepend the event name with `data-he-on`, for example `data-he-onclick="count++"`
140
+
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
+ ```
152
+
153
+ ## Magic Attributes
154
+
155
+ `$` is an alias for `document.querySelector`
156
+
157
+ `$el` is an alias for the element
158
+
159
+ `$event` is an alias for the event object of an event handler
160
+
161
+ ## Default Variables and Functions
162
+
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
@@ -67,13 +67,18 @@ export default function helium(data = {}) {
67
67
  if (
68
68
  name == "@react" ||
69
69
  name == "data-he-react" ||
70
+ name == "@text" ||
71
+ name == "data-he-text" ||
70
72
  name == "@bind" ||
71
73
  name == "data-he-bind"
72
74
  ) {
73
75
  try {
74
76
  new Function(`let ${value} = 1`);
75
77
  state[value] ||=
76
- name == "@react" || name == "data-he-react"
78
+ name == "@react" ||
79
+ name == "data-he-react" ||
80
+ name == "@text" ||
81
+ name == "data-he-text"
77
82
  ? el.textContent
78
83
  : el.value;
79
84
  } catch (e) {}
@@ -95,7 +100,12 @@ export default function helium(data = {}) {
95
100
  ),
96
101
  );
97
102
  if (name == "@ref" || name == "data-he-ref") refs.set("$" + value, el);
98
- if (name == "@react" || name == "data-he-react")
103
+ if (
104
+ name == "@react" ||
105
+ name == "data-he-react" ||
106
+ name == "@text" ||
107
+ name == "data-he-text"
108
+ )
99
109
  Object.keys(state)
100
110
  .filter((key) => value.includes(key))
101
111
  .forEach((val) =>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@daz4126/helium",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "main": "helium.js",
5
5
  "type": "module",
6
6
  "keywords": [