KScript docs
EN

Components

Actions and browser events

Handlers contain KScript statements and can call imported functions. A handler can also be a function or one of the component's methods, which is called, so @on-click($save) runs fn save(). To flip a bool field, use the toggle: shorthand: @on-click(toggle: $open) is the same as @on-click($open = not $open). A bare bool such as @on-click($open) is an error with a quick fix to toggle:.

Handler shorthands

A handler can name what it does instead of spelling out the statement. Each shorthand is optional: the statement form keeps working. Several shorthands run left to right in one handler, such as @on-click(add: $draft, to: $tags, set: $draft, to: ''). A handler holds shorthands or statements, not both.

Shorthand Same as Field
toggle: $open open = not $open bool
call: $save fn save() of the component, or a function in a variable fn or method
call: $save, with: [$note.id] $this->save($note.id) fn or method
increment: $count count = $count + 1 number
increment: $count, by: 5 count = $count + 5 number
decrement: $count, by: count = $count - 1 number
set: $tab, to: 'notes' tab = 'notes' any
add: $draft, to: $tags tags = $tags->push($draft) list
remove: $tag, from: $tags tags = $tags->remove($tag) list
reset: $draft, reset: [$title, $done] each field back to the value it starts with (its default, or its <state> value) any
cycle: $view, through: ['list', 'grid'] the next value in the list, wrapping round (without through:, the next value of the field's enum) any
save: $note $note->save() KScript.Data record
delete: $note $note->delete() KScript.Data record
emit: 'notes-changed' @emit_event('notes-changed') none
go: '/notes' @live_redirect('/notes') none

Modifiers go with the actions and change when or how they run. The browser applies them before it sends anything.

A yes/no modifier is a flag, as it is in a call: once on its own turns it on, and not once turns it off. The same goes for @bind's trim and @drag's front and copy. Writing once: true still works, and the editor suggests the flag instead (KS6024).

Modifier What it does
confirm: 'Remove this note?' Asks first, and runs only if the person says yes.
key: enter, key: [ctrl, s] Runs only for that key, with exactly those modifier keys (ctrl, alt, shift, meta). Keys are enter, escape, space, tab, up, down, left, right, a letter or a digit.
prevent Stops the browser's own action, such as saving the page on ctrl+s.
debounce: 300ms Waits until the events stop for that long, then runs once.
throttle: 1s Runs at most once in that long.
once Runs the first time only.
self Runs only when the event starts on the element itself, not on something inside it.
disable Disables the element while its action runs.
HTML page
<input @bind($query, debounce: 300ms) @on-keydown(reset: $query, key: escape)>
<button @on-click(delete: $note, confirm: 'Delete this note?', emit: 'notes-changed')>Delete</button>
<form @on-keydown(save: $note, key: [ctrl, s], prevent)>…</form>

In a window, the actions go straight on a button or box: Button(toggle: $open) "Details" and Button(increment: $count, by: 5) "+5". Modifiers need a browser, so a window handler that has one stops with an error saying so.

decrement: never takes a field below its min:: with stock: int(min: 0), decrement: $stock, by: 2 at 1 leaves it at 0.

kscript check and the editor check each field's type, complete the names (and by:, to:, from: or with: after the action they go with), and show the statement a shorthand stands for on hover. After an action's name, completion lists only the fields it fits: toggle: offers the bool fields, increment: the numbers and to: after add: the lists.

Switching classes

@class(...) adds classes to a tag's own class while their conditions are true. Put the class first for one class per condition, or the condition first to switch several classes at once:

HTML page
<li class="item" @class(done: $todo.done, 'is-late': $todo.due < @today())>{$todo.title}</li>
<li @class({
  $todo.done: ['done', 'struck'],
  [$todo.done, $open]: 'expanded',
  $todo.done or $open: 'highlight',
})>{$todo.title}</li>

A condition is a bool, or a list of bools that must all be true. With the condition first, the value is a class name or a list of them. Browser details are available as $event. A key event includes its key, code and modifiers. Pointer events include coordinates, button, pointer ID and pointer type where applicable. $event.x and $event.y are where the pointer is inside the handler's own element, counting how far that element is scrolled, so @on-dblclick(call: $add, with: [$event.x, $event.y]) on a scrolling board gives the spot in the board's content.

Interaction Attributes
Buttons and forms on-click, on-dblclick, on-submit
Input changes on-input, on-change
Keyboard and focus on-keydown, on-keyup, on-enter, on-escape, on-focusin, on-focusout
Dialog and pointer events on-cancel, on-close, on-pointerdown, on-pointerup, on-pointercancel
Lifecycle on-init, on-visible, on-tick with every="1s"
Other components and client code on-event and a named on-action

Use prevent in a handler to cancel a browser default synchronously, and confirm: 'Remove this note?' to confirm a destructive action before posting it. @show($open) keeps a tag in the page and hides it while the value is false, so focus and scroll survive, and @live-disable(while: $saving) disables a control while a condition holds as well as while an action runs. An action can call @live_redirect('/notes') for a same-site redirect or @skip_render() to save state without updating the current HTML.

Events between components

Emit an event from an action with @emit_event('notes-changed', data: $note.id, target: 'summary'). Omit the target to broadcast. Each receiving block runs its first matching listener, such as on-event="$latest = $event.data" event-name="notes-changed". The handler can read $event.name, $event.data and $event.source.

Drag, drop and sorting

To drag one element onto another, put @drag(copy, value: $item.id) on the source and on-drop on the destination (see the next section). The action reads $event.drag_value, and $event.x and $event.y give the drop point inside the destination. @drop-class('over') on the destination adds the class while something it takes is dragged over it.

Move an element with @drag

@drag() lets the visitor move an absolutely positioned element with the pointer. When the drag ends, the element's on-dragend handler runs with where it landed, so the action can save it:

HTML page
<article class="note" style="left: {$note.x}px; top: {$note.y}px; z-index: {$note.z}"
  @drag(front, skip: '.thumb')
  @on-dragend(call: $move, with: [$event.x, $event.y, $event.z, $event.vx])>
  …
</article>
$event field Value
x, y The element's new left and top, in pixels inside its positioned parent.
vx, vy How fast it was moving when it was let go, in pixels per frame.
moved false when the press was a click. The handler still runs, so a click can bring an element to the front.
z With front, the z-index it was raised to above its siblings.
Option What it does
front Raises the element above the other @drag elements in its positioned parent.
scroll: '.surface' The container that scrolls when the pointer nears its edges. Defaults to the nearest scrolling ancestor.
skip: '.thumb' Presses on these belong to them, not to dragging. Fields, buttons, links and labels are always skipped.
threshold: 6 How far the pointer moves before a press becomes a drag. Defaults to 4 pixels, so a click stays a click.
copy Drags a ghost copy instead of the element, onto an on-drop target. A plain click still runs the element's on-click.
value: $color With copy, what the drop handler reads as $event.drag_value.
ghost: 'note-ghost' With copy, the classes of the ghost. Its top centre follows the pointer. Defaults to the element's own classes.

While it moves, the element (or the ghost) has the dragging class and the CSS variables --drag-vx, --drag-vy and --drag-speed, so a stylesheet can tilt it with the movement. After a drop it has drag-settled until its animation ends.

CSS
.note.dragging {
  --tilt-y: clamp(-28deg, calc(var(--drag-vx) * 1.6deg), 28deg);
  transform: perspective(900px) rotateY(var(--tilt-y));
}
.note.drag-settled { animation: settle 0.5s ease-out; }

A tray item that drops a new note on a board uses copy:

HTML page
<button type="button" class="pad" @drag(copy, value: $color, ghost: "note-ghost c-$color") @on-click($this->add($color))></button>
<div class="surface" @on-drop(call: $add, with: [$event.drag_value, $event.x, $event.y]) @drop-class('over')>…</div>

To take files dropped on an element, give it @drop-files('upload') and put a live-upload file input named upload inside it. Dropped files go through that input and its on-change action, the same as files picked by hand:

HTML page
<article class="note" @drop-files('upload') @drop-class('file-over')>
  <label>Attach <input type="file" name="upload" multiple hidden live-upload @on-change($this->attach($event.files.upload))></label>
</article>

kscript check reports a @drop-files name with no matching live-upload input inside the element.

For sortable siblings, give each item a unique sort-item="{$item.id}" and put on-sort on their container. The handler gets the item ID, the old and new zero-based indexes, and the proposed order. Users can drag an item or focus it and use Alt+Up or Alt+Down. Set sort-axis="horizontal" for left-to-right lists. Validate the item IDs before persisting a new order.