Skip to content

Commit de86f4a

Browse files
committed
reformat
1 parent 67a0547 commit de86f4a

1 file changed

Lines changed: 39 additions & 26 deletions

File tree

src/skills/EFFECTIVE_HYPERSCRIPT.md

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,28 @@ A practical reference for writing _hyperscript — a scripting language for the
44

55
## Core Concepts
66

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.
89

910
```html
11+
1012
<button _="on click add .active to me">Click Me</button>
1113
```
1214

1315
The language reads like English. Prefer natural phrasing over terse syntax.
1416

1517
## Magic Symbols
1618

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) |
23-
| `target` | `event.target` |
24-
| `detail` | `event.detail` |
25-
| `sender` | Element that sent a custom event |
26-
| `body` | `document.body` |
19+
| Symbol | Meaning |
20+
|---------------------------|---------------------------------------------|
21+
| `me`, `my`, `I` | The element the script is on |
22+
| `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` |
2729

2830
## Variable Scoping
2931

@@ -36,7 +38,8 @@ set global x to 1 -- same as $x
3638
set local x to 1 -- explicitly local
3739
```
3840

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.
4043

4144
## Features
4245

@@ -98,8 +101,9 @@ end
98101
```
99102

100103
```html
104+
101105
<div _="install Removable(removeButton: #btn)">
102-
<button id="btn">X</button>
106+
<button id="btn">X</button>
103107
</div>
104108
```
105109

@@ -473,7 +477,8 @@ add .loaded to me
473477
put data into #result
474478
```
475479

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:
477482

478483
```hyperscript
479484
put "hello" into me -- clears me, inserts "hello" as HTML
@@ -492,7 +497,8 @@ set :count to 0
492497
on click increment :count then put :count into me end
493498
```
494499

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:
496502

497503
```hyperscript
498504
fetch /api/data as json -- automatically waits for response
@@ -513,35 +519,40 @@ on click queue none -- ignore clicks while handling
513519
### Component State
514520

515521
```html
522+
516523
<div _="
517524
set :count to 0
518525
on click
519526
increment :count
520527
put :count into me
521528
end
522-
">0</div>
529+
">0
530+
</div>
523531
```
524532

525533
### Event Delegation
526534

527535
```html
536+
528537
<ul _="on click from <li/>
529538
take .selected from <li/> for target
530539
end">
531-
<li>Item 1</li>
532-
<li>Item 2</li>
540+
<li>Item 1</li>
541+
<li>Item 2</li>
533542
</ul>
534543
```
535544

536545
### Async Workflows
537546

538547
```html
548+
539549
<button _="on click
540550
add @disabled to me
541551
fetch /api/action as json
542552
put it.message into #result
543553
remove @disabled from me
544-
">Submit</button>
554+
">Submit
555+
</button>
545556
```
546557

547558
### Debounced Search
@@ -556,6 +567,7 @@ end">
556567
### Toggle with Cleanup
557568

558569
```html
570+
559571
<div _="on click
560572
toggle .expanded on #panel
561573
toggle between @aria-expanded='true' and @aria-expanded='false'
@@ -565,14 +577,15 @@ end">
565577
### Error Handling
566578

567579
```html
580+
568581
<script type="text/hyperscript">
569-
def safeFetch(url)
570-
fetch url as json
571-
return it
572-
catch e
573-
log "fetch failed: " + e
574-
return null
575-
end
582+
def safeFetch(url)
583+
fetch url as json
584+
return it
585+
catch e
586+
log "fetch failed: " + e
587+
return null
588+
end
576589
</script>
577590
```
578591

0 commit comments

Comments
 (0)