### Install Dependencies and Serve Website
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/README.md
Run these commands in your terminal to install project dependencies and start the local development server for the hyperscript.org website. Ensure you have Node.js 15 installed.
```bash
npm install
```
```bash
npx eleventy --serve
```
--------------------------------
### Run Manual Test Server
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/test/manual/README.md
Execute this command to start the Node.js server required for testing WebSockets and EventSource. Ensure the `ws` package is installed (`npm install ws`).
```sh
# requires the `ws` package: npm install ws
node test/manual/server.js
# then open http://localhost:3000/test/manual/connections.html
```
--------------------------------
### 2 Second Wait Example
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/test/commands/fetch/scratch.html
A basic example demonstrating a 2-second delay before executing an action. No specific setup is required beyond Hyperscript initialization.
```html
```
--------------------------------
### Install Namespaced Behavior
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/features/install.md
Demonstrates installing a behavior defined under a namespace.
```hyperscript
behavior My.UI.Removable
on click remove me
end
```
```html
...
```
--------------------------------
### Load Example from Dropdown
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/playground.html
Handles the change event for the examples dropdown. Loads selected example code into the editor and runs it.
```javascript
document.getElementById('examples').addEventListener('change', function () {
var val = this.value;
if (!val) return;
this.value = '';
if (BUILTIN_EXAMPLES[val]) {
cm.setValue(BUILTIN_EXAMPLES[val]);
run();
return;
}
// Fet
```
--------------------------------
### Hello World Example in ///_hyperscript
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/docs/getting-started.md
A basic 'Hello World' example demonstrating how to put text into an element on click using ///_hyperscript embedded in an HTML attribute.
```html
```
--------------------------------
### Install Draggable Behavior
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/reference.md
Use the 'install' feature to apply a predefined behavior, such as 'Draggable', to the current element.
```hyperscript
install Draggable
```
--------------------------------
### Basic transition example
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/commands/transition.md
A simple example of using the `transition` command to fade out an element and then remove it from the DOM upon clicking.
```html
Fade then remove me
```
--------------------------------
### Start View Transition
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/_includes/commands_table.md
Wraps DOM mutations in a View Transition for animated state changes. Example: `start a view transition using "fade" ... end`.
```hyperscript
start a view transition using "fade" ... end
```
--------------------------------
### Fetch and Load Example Code
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/playground.html
Fetches example code from the /playground/ directory and updates the CodeMirror editor. Includes error handling for fetch failures.
```javascript
fetch('/playground/' + val) .then(function (r) { return r.text(); }) .then(function (text) { cm.setValue(text); run(); }) .catch(function () { document.getElementById('status').innerHTML = 'Failed to load example'; });
```
--------------------------------
### Install Behavior with Named Arguments
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/features/install.md
Installs a 'Removable' behavior that uses a specific button to trigger removal.
```html
Banner content
```
--------------------------------
### Install Tool Dependencies
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/tools/README.md
Installs all necessary dependencies for the hyperscript tools. This command should be run from the `tools/` directory.
```sh
just install
```
--------------------------------
### Install Multiple Behaviors
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/features/install.md
Installs both 'Removable' and 'Draggable' behaviors on a single element.
```html
...
```
--------------------------------
### Hyperscript Component State Example
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/src/skills/EFFECTIVE_HYPERSCRIPT.md
A basic Hyperscript example demonstrating component state management using element-scoped variables and event handling within an HTML element.
```html
0
```
--------------------------------
### String Prefix Check
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/expressions/comparison-operator.md
The 'starts with' operator checks if a string begins with a specified prefix. This example adds a class if the input starts with 'http'.
```html
```
--------------------------------
### DOM Reference Examples
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/reference.md
Examples of how to reference DOM elements using IDs, classes, queries, attributes, styles, and relative positioning.
```hyperscript
#main-div
```
```hyperscript
.active
```
```hyperscript
```
```hyperscript
<:focused/>
```
```hyperscript
@selected
```
```hyperscript
*color
```
```hyperscript
*computed-fontSize
```
```hyperscript
closest
```
```hyperscript
next from me
```
```hyperscript
first from
```
--------------------------------
### Example Table with Master Checkbox
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/patterns/components/select-all-component.md
An example of how to integrate the `` component into a table's header. This setup allows the master checkbox to control all other checkboxes in the table body.
```html
Task
Status
Write documentation
In progress
Fix login bug
Open
Deploy to staging
Pending
Review pull request
Open
Update dependencies
Open
```
--------------------------------
### Behavior Installation Syntax
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/features/behavior.md
The EBNF syntax for installing a hyperscript behavior on an element, including named arguments.
```ebnf
install ()
```
--------------------------------
### Simple Greeting Component
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/features/components.md
A minimal component example that displays static text.
```html
```
--------------------------------
### Hyperscript Behaviors
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/src/skills/EFFECTIVE_HYPERSCRIPT.md
Illustrates how to define reusable bundles of features using `behavior` and how to install them onto elements using the `install` attribute.
```hyperscript
behavior Removable(removeButton)
on click from removeButton
remove me
end
end
```
```html
```
--------------------------------
### Use Custom Hyperscript Command
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/docs/extensions.md
Example of using the custom 'foo' command after it has been registered.
```hyperscript
def testFoo()
set str to "foo"
foo str
end
```
--------------------------------
### Test Runner Setup
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/test/manual/connections.html
Initializes test counters and defines helper functions for reporting test results and displaying a summary.
```JavaScript
var passCount = 0, failCount = 0;
function report(container, name, passed, detail) {
var div = document.createElement('div');
div.className = 'test ' + (passed ? 'pass' : 'fail');
div.textContent = (passed ? 'PASS' : 'FAIL') + ': ' + name;
if (detail) {
var pre = document.createElement('pre');
pre.textContent = detail;
div.appendChild(pre);
}
document.getElementById(container).appendChild(div);
if (passed) passCount++;
else failCount++;
}
function showSummary() {
document.getElementById('summary').textContent = passCount + ' passed, ' + failCount + ' failed';
}
```
--------------------------------
### Install ///_hyperscript via NPM
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/docs/getting-started.md
Install the ///_hyperscript package using npm. This is the recommended approach for projects using a bundler.
```bash
npm install hyperscript.org
```
--------------------------------
### Basic Click Event Handling
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/features/on.md
A simple example demonstrating how to attach a click event listener to an element.
```html
Click Me!
```
--------------------------------
### Start a View Transition
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/docs/dom.md
Initiate a DOM transition using the 'start a view transition' command, which wraps DOM mutations in the browser's View Transition API. Animation is controlled via CSS.
```hyperscript
start a view transition
put newContent into #container
end
```
```hyperscript
start a view transition using "slide-left"
remove .active from .tab
add .active to me
put content into #panel
end
```
--------------------------------
### Built-in Hyperscript Examples
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/playground.html
These examples demonstrate common Hyperscript patterns for interactive web components. They include counters, input handling, and dynamic styling.
```html
Counter
\n\n
```
```html
Reactive Counter
\n
\n \n \n \n
```
```html
Hello
\n\n
```
```html
Color Cycle
\n\n
Watch me change!
```
--------------------------------
### hs-include Specifiers
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/docs/conclusion.md
Examples demonstrating different specifiers for hs-include to select variables from various scopes.
```html
```
```html
```
```html
```
```html
```
--------------------------------
### DOM Manipulation Examples
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/llms.txt
Provides examples of common DOM manipulation tasks such as adding/removing classes, attributes, styles, showing/hiding elements, and managing dialogs.
```hyperscript
add .active to me
add @disabled to me
add {color: red} to me -- inline styles
remove .active from #target
remove me -- removes element
toggle .active on me
toggle between .on and .off
take .active from .tabs for me -- move class
show me
hide me
focus #input
empty #container
open #dialog
close #dialog
```
--------------------------------
### Install a Basic Behavior
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/features/install.md
Attaches a 'Removable' behavior to a div, allowing it to be removed on click.
```html
Click to remove me
```
--------------------------------
### Hyperscript Async Workflow Example
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/src/skills/EFFECTIVE_HYPERSCRIPT.md
An example of an asynchronous workflow in Hyperscript, handling a button click to fetch data, update a result element, and manage a disabled state.
```html
```
--------------------------------
### Operator Examples
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/reference.md
Illustrates various operators including math, comparison, logical, existence checks, type conversion, and string manipulation.
```hyperscript
x + 1
```
```hyperscript
y * 2
```
```hyperscript
z mod 3
```
```hyperscript
x == "foo"
```
```hyperscript
I match <:active/>
```
```hyperscript
x and y
```
```hyperscript
not z
```
```hyperscript
a or false
```
```hyperscript
no element.children
```
```hyperscript
some <.results/>
```
```hyperscript
"10" as Int
```
```hyperscript
x as Values | JSONString
```
```hyperscript
x:String
```
```hyperscript
x:Int!
```
```hyperscript
"foo" in myArray
```
```hyperscript
url starts with "https"
```
```hyperscript
x is between 1 and 10
```
```hyperscript
#a precedes #b
```
```hyperscript
x contains "hi" ignoring case
```
--------------------------------
### Install ///_hyperscript via CDN
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/docs/getting-started.md
Include this script tag in your HTML to install ///_hyperscript from a CDN. This is a quick way to add the library to your project without a build process.
```html
```
--------------------------------
### Property Access Examples
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/reference.md
Demonstrates dot notation, possessive, and 'of' expressions for accessing object properties, as well as array indexing.
```hyperscript
event.target
```
```hyperscript
the window's location
```
```hyperscript
the location of window
```
```hyperscript
items[0]
```
```hyperscript
obj['key']
```
--------------------------------
### Basic Cookie Example
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/expressions/cookies.md
A simple HTML button demonstrating how to set a cookie using an `on click` event.
```APIDOC
## Basic Cookie Example
### Description
This example shows a basic HTML button that sets a cookie named 'hello' to the value 'world' when clicked.
### HTML
```html
```
```
--------------------------------
### Load Remote Behaviors
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/features/behavior.md
Demonstrates how to load behaviors from an external file before loading the hyperscript library. Ensure behaviors are defined before installation.
```html
```
--------------------------------
### Format Pizza Toppings using make and Intl.ListFormat
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/commands/make.md
This example shows how to format a list of pizza toppings using `Intl.ListFormat` and the `make` command to create DOM elements for each topping. It iterates through the formatted parts and appends them to a target element.
```hyperscript
def formatPizzaToppings(toppings)
make an Intl.ListFormat from "en", { type: "conjunction" }
called listFmt
for part in listFmt.formatToParts(toppings)
if the part's type is "element"
make a
put the part's value into its textContent
append it to #toppings
else
append the part's value to #toppings
end
end
end
```
--------------------------------
### Open Dialogs, Details, Popovers, or Fullscreen
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/_includes/commands_table.md
Opens various UI elements. Examples: `open #my-dialog`, `open fullscreen`.
```hyperscript
open #my-dialog
```
```hyperscript
open fullscreen
```
--------------------------------
### Event-Driven Loop Example
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/docs/async.md
This example shows an event-driven loop that pulses a button until a 'stop' event is received. The loop continues as long as the 'stop' event is not detected at the start of each iteration. The cancel button sends the 'stop' event to terminate the loop.
```html
```
--------------------------------
### Initialize and Run
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/test/scratch.html
Performs an initial run of the code after a short delay. Useful for setting up the initial state.
```javascript
setTimeout(run, 100);
```
--------------------------------
### Access Selected Text
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/reference.md
Get the currently selected text on the page using the 'selection' magic value. Example: `put selection into #out`.
```hyperscript
put selection into #out
```
--------------------------------
### Add class to closest div relative to parent on click
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/expressions/closest.md
This example adds a class to the nearest ancestor `
` element, starting the search from the parent element of the clicked element.
```html
...
```
--------------------------------
### Set Attribute within a `behavior` using `set`
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/features/set.md
When defining a `behavior`, use `set` to apply an attribute to every element the behavior is installed onto. This example sets the `data-linked-scroll` attribute.
```html
```
--------------------------------
### Accessing and Setting DOM Attributes with Possessive Expression
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/expressions/possessive.md
This example shows how to use the possessive expression to get a DOM element's attribute (`@data-demo`) and set it as the content of another element. Ensure the target element has the desired attribute.
```html
```
--------------------------------
### Event-driven approach with `on` handlers
Source: https://github.com/bigskysoftware/_hyperscript/blob/master/www/features/live.md
This example shows a basic event-driven approach using `on` handlers for simple interactions. It is suitable for straightforward cases but can become unwieldy with multiple dependencies.
```html