You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/skills/EFFECTIVE_HYPERSCRIPT.md
+39-26Lines changed: 39 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,26 +4,28 @@ A practical reference for writing _hyperscript — a scripting language for the
4
4
5
5
## Core Concepts
6
6
7
-
_hyperscript code lives in `_` attributes on HTML elements. Each element's script defines **features** (event handlers, functions, variables) that operate on that element.
7
+
_hyperscript code lives in `_` attributes on HTML elements. Each element's script defines **features** (event handlers,
8
+
functions, variables) that operate on that element.
8
9
9
10
```html
11
+
10
12
<button_="on click add .active to me">Click Me</button>
11
13
```
12
14
13
15
The language reads like English. Prefer natural phrasing over terse syntax.
14
16
15
17
## Magic Symbols
16
18
17
-
| Symbol | Meaning |
18
-
|--------|---------|
19
-
|`me`, `my`, `I`| The element the script is on |
20
-
|`it`, `its`, `result`| Result of the last command |
21
-
|`you`, `your`, `yourself`| Current target in a `tell` block |
22
-
|`event`| The current event object (in `on` handlers) |
|`it`, `its`, `result`| Result of the last command|
23
+
|`you`, `your`, `yourself`| Current target in a `tell` block |
24
+
|`event`| The current event object (in `on` handlers) |
25
+
|`target`|`event.target`|
26
+
|`detail`|`event.detail`|
27
+
|`sender`| Element that sent a custom event|
28
+
|`body`|`document.body`|
27
29
28
30
## Variable Scoping
29
31
@@ -36,7 +38,8 @@ set global x to 1 -- same as $x
36
38
set local x to 1 -- explicitly local
37
39
```
38
40
39
-
Element-scoped variables (`:x`) persist between event handler invocations on the same element. Use them for component state.
41
+
Element-scoped variables (`:x`) persist between event handler invocations on the same element. Use them for component
42
+
state.
40
43
41
44
## Features
42
45
@@ -98,8 +101,9 @@ end
98
101
```
99
102
100
103
```html
104
+
101
105
<div_="install Removable(removeButton: #btn)">
102
-
<buttonid="btn">X</button>
106
+
<buttonid="btn">X</button>
103
107
</div>
104
108
```
105
109
@@ -473,7 +477,8 @@ add .loaded to me
473
477
put data into #result
474
478
```
475
479
476
-
**`set` vs `put` for DOM elements.**`put X into el` clears the element's children and inserts X as HTML. `set el to X` overwrites the variable. Use `put` for DOM content, `set` for property/variable assignment:
480
+
**`set` vs `put` for DOM elements.**`put X into el` clears the element's children and inserts X as HTML. `set el to X`
481
+
overwrites the variable. Use `put` for DOM content, `set` for property/variable assignment:
477
482
478
483
```hyperscript
479
484
put "hello" into me -- clears me, inserts "hello" as HTML
@@ -492,7 +497,8 @@ set :count to 0
492
497
on click increment :count then put :count into me end
493
498
```
494
499
495
-
**Promises auto-resolve.** Any command that returns a Promise automatically waits. No `await` keyword exists or is needed:
500
+
**Promises auto-resolve.** Any command that returns a Promise automatically waits. No `await` keyword exists or is
501
+
needed:
496
502
497
503
```hyperscript
498
504
fetch /api/data as json -- automatically waits for response
@@ -513,35 +519,40 @@ on click queue none -- ignore clicks while handling
513
519
### Component State
514
520
515
521
```html
522
+
516
523
<div_="
517
524
set :count to 0
518
525
on click
519
526
increment :count
520
527
put :count into me
521
528
end
522
-
">0</div>
529
+
">0
530
+
</div>
523
531
```
524
532
525
533
### Event Delegation
526
534
527
535
```html
536
+
528
537
<ul_="on click from <li/>
529
538
take .selected from <li/> for target
530
539
end">
531
-
<li>Item 1</li>
532
-
<li>Item 2</li>
540
+
<li>Item 1</li>
541
+
<li>Item 2</li>
533
542
</ul>
534
543
```
535
544
536
545
### Async Workflows
537
546
538
547
```html
548
+
539
549
<button_="on click
540
550
add @disabled to me
541
551
fetch /api/action as json
542
552
put it.message into #result
543
553
remove @disabled from me
544
-
">Submit</button>
554
+
">Submit
555
+
</button>
545
556
```
546
557
547
558
### Debounced Search
@@ -556,6 +567,7 @@ end">
556
567
### Toggle with Cleanup
557
568
558
569
```html
570
+
559
571
<div_="on click
560
572
toggle .expanded on #panel
561
573
toggle between @aria-expanded='true' and @aria-expanded='false'
0 commit comments