KScript docs
EN

Page directives and handlers

Every @ directive a tag takes in a live page or component, such as @on-click, @bind and @live-cloak, with the shorthands and modifiers a handler takes and the fields of $event. How live pages work is in Components.

A directive goes on the opening tag. It takes what is in its brackets: statements for a handler, a field and options for @bind, or nothing for a flag such as @live-loading.

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>
<p @live-loading>Saving...</p>

Handlers

A handler runs KScript on the server when its event happens in the browser. It holds statements, a function to call, or the shorthands below.

HandlerWhat it does
@on-actionRuns when client code calls the action named by @live-action with KScriptLive.call(). What it sent is in $event.data.
@on-cancelRuns when a <dialog> is cancelled, such as with Escape.
@on-changeRuns when a control's value is committed, such as a select being picked or a checkbox ticked.
@on-clickRuns when the element is clicked: @on-click(toggle: $open).
@on-closeRuns when a <dialog> closes.
@on-dblclickRuns when the element is double-clicked.
@on-dragendRuns when a drag of this element ends. After @drag, $event.x and $event.y are where it landed.
@on-dropRuns when something is dropped on the element: a @drag(copy: true) ghost, whose value is in $event.drag_value.
@on-enterRuns when Enter is pressed in the element: <input @on-enter(add: $draft, to: $tags)>.
@on-escapeRuns when Escape is pressed in the element.
@on-eventRuns when another live block on the page sends an event with @emit_event or emit:. Pick which with @event-name.
@on-focusinRuns when the element, or something inside it, gets focus.
@on-focusoutRuns when focus leaves the element and everything inside it.
@on-initRuns once, when the component is first drawn: load what it shows here.
@on-inputRuns each time a control's value changes as the person types. The new value is in $event.value.
@on-keydownRuns when a key is pressed. Pick the key with key:, as in @on-keydown(save: $note, key: [ctrl, s]).
@on-keyupRuns when a key is let go.
@on-pointercancelRuns when the browser takes over a pointer, such as to scroll the page.
@on-pointerdownRuns when a mouse button, pen or finger goes down on the element.
@on-pointerupRuns when the mouse button, pen or finger comes up.
@on-sortRuns when the person drags one of the @sort-item children to a new place. $event.order is the new order.
@on-submitRuns when a form is submitted, instead of the browser posting it: <form @on-submit($save)>.
@on-tickRuns again and again, as often as @every says: <p @on-tick(increment: $seconds) every="1s">.
@on-visibleRuns when the element scrolls into view, such as to load the next page of a list.

Other directives

Directives that bind controls, show and hide tags, give feedback while an action runs, and hand parts of the page to the browser.

DirectiveWhat it does
@autogrowMakes a <textarea> grow taller as its text grows.
@bindKeeps a control and a field in step: @bind($title). Options such as on: blur and trim: true go after the field.
@classSwitch classes on by conditions: @class(done: $todo.done) or @class({ $todo.done: ['done', 'struck'] }).
@dragMove this element with the pointer, and run its on-dragend handler where it lands: @drag(front: true). With copy: true, drag a ghost onto an on-drop target.
@drop-classAdd classes while something this element accepts is dragged over it: @drop-class('file-over').
@drop-filesSend files dropped on this element through the named live-upload input inside it: @drop-files('upload').
@eachDraws the tag once for each item: @each($tasks, as: task, at: i). Same as <for each="task" in="$tasks"> around it.
@event-nameRuns @on-event only for events with this name: event-name="notes-changed".
@everyHow often @on-tick runs: every="1s".
@focusGive this element focus when it appears, or when its condition turns true: @focus() or @focus($editing).
@ifDraws the tag only while the condition is true: @if($note). Same as <if $note> around it.
@live-actionNames the @on-action handler, so client code can call it with KScriptLive.call().
@live-clientRuns a client action registered with KScriptLive when the element is clicked, without a trip to the server.
@live-cloakHides the element until the page's live code has started, so nothing half-drawn shows.
@live-dirtyShows the element while a binding has edits not yet sent. Name a field to watch only that one: @live-dirty($title).
@live-disableDisable this control while an action is pending, or while a condition holds: @live-disable(while: $saving).
@live-errorShows the element only after an action fails.
@live-idGives this component a name that stays the same between updates, so two of the same component keep their own state.
@live-ignoreLeaves everything inside the element alone during updates, for a widget the browser or a script manages.
@live-keyGives each repeated element a key that stays the same between updates, so a moved row keeps its focus and state: live-key="{$task.id}".
@live-loadingShows the element only while an action is running, such as a spinner.
@live-navigateFollows the link without a full page load, swapping in the new page.
@live-navigate-onceRuns this <script> only once, even when in-page navigation comes back to the page.
@live-offlineShows the element only while the browser is offline.
@live-portalDraws this <template>'s content somewhere else in the page, at the CSS selector given: live-portal="#modals".
@live-replaceReplaces the element whole on each update instead of patching it.
@live-streamReceives what an action streams to this name, as it arrives.
@live-uploadSends the files picked in this file input with the next action. They are in $event.files.
@live-use-nameAttaches a client directive registered with KScriptLive, by its name: @live-use-autosize().
@pendingAdd classes while an action runs: @pending('saving'), or @pending('crumple', for: 'delete') for one method's actions.
@showShows the tag while the value is true and hides it (keeping it in the page) while false: @show($open).
@sort-axisSays which way the @sort-item children run: vertical (the default) or horizontal.
@sort-itemMarks a child of an @on-sort element as one that can be dragged to a new place, with the value it is known by: sort-item="{$task.id}".

Handler shorthands

A handler can name what it does instead of spelling out the statement: @on-click(toggle: $open) is @on-click(open = not $open). Several run left to right in one handler.

ShorthandWhat it does
toggle:Flips a bool field: toggle: $open is open = not $open.
call:, with:Calls a function or one of the component's methods: call: $save, with: [$note.id].
increment:, by:Adds 1 (or by:) to a number field: increment: $count, by: 5.
decrement:, by:Takes 1 (or by:) from a number field, never going below its min: (stock: int(min: 0)): decrement: $stock.
set:, to:Sets a field to a value: set: $tab, to: 'notes'.
add:, to:Adds a value to a list field: add: $draft, to: $tags.
remove:, from:Removes a value from a list field: remove: $tag, from: $tags.
reset:Puts a field (or a list of fields) back to its declared default: reset: $draft, or reset: [$title, $done].
cycle:, through:Moves a field to the next value of its enum, or of a list given with through:, wrapping round: cycle: $view, through: ['list', 'grid'].
save:Saves a KScript.Data record: save: $note is $note->save().
delete:Deletes a KScript.Data record: delete: $note is $note->delete().
emit:Sends an event to the page's live blocks: emit: 'notes-changed' is @emit_event('notes-changed').
go:Sends the browser to a path on this site: go: '/notes' is @live_redirect('/notes').

Handler modifiers

Modifiers go with a handler's shorthands and change when or how they run. The browser applies them before it sends anything: @on-keydown(save: $note, key: [ctrl, s], prevent: true).

ModifierWhat it does
confirm:Asks first, and runs only if the person says yes: confirm: 'Delete this note?'.
key:Runs only for this key, with any modifier keys: key: enter, key: [ctrl, s].
prevent:Stops the browser's own action for the event, such as saving the page on ctrl+s: prevent: true.
debounce:Waits until the events stop for this long, then runs once: debounce: 300ms.
throttle:Runs at most once in this long, however often the event fires: throttle: 1s.
once:Runs the first time only: once: true.
self:Runs only when the event starts on this element itself, not on something inside it: self: true.
disable:Disables the element while its action runs: disable: true.

@bind options

Options go after the field: @bind($title, on: blur, trim: true).

OptionWhat it does
on:When the value is sent: input (as you type, the default for text), change, blur (when focus leaves) or action (with the next action).
debounce:Sends the value once typing has stopped for this long: debounce: 300ms.
as:Reads the value as this type before it reaches the field, and refuses one that isn't: as: int.
trim:Trims spaces from both ends on the way in: trim: true.

$event

Inside a handler, $event is the browser event it runs for. Every handler gets these fields.

FieldWhat it holds
$event.typestring. The event's name: click, input, submit, component, action, sort...
$event.valuestring. The value of the element the event came from (an input, select or textarea), or null.
$event.checkedbool. For a checkbox: whether it is ticked. Null for other elements.
$event.target_idstring. The id of the element the event came from, or null.
$event.keystring. For a key event: the key pressed, such as Enter, Escape or a. Null otherwise.
$event.codestring. For a key event: the physical key, such as KeyA or Digit1. Null otherwise.
$event.altbool. True if Alt (Option) was held.
$event.ctrlbool. True if Ctrl was held.
$event.metabool. True if Cmd (Windows key) was held.
$event.shiftbool. True if Shift was held.
$event.repeatbool. True if a key event is a repeat from a key held down.
$event.client_xnumber. The pointer's x in the window, in pixels, or null.
$event.client_ynumber. The pointer's y in the window, in pixels, or null.
$event.page_xnumber. The pointer's x in the page, counting scroll, or null.
$event.page_ynumber. The pointer's y in the page, counting scroll, or null.
$event.xnumber. The pointer's x inside the handler's element, counting its scroll, or null.
$event.ynumber. The pointer's y inside the handler's element, counting its scroll, or null.
$event.buttonnumber. For a pointer event: the button (0 main, 1 middle, 2 secondary), or null.
$event.pointer_idnumber. For a pointer event: the pointer's id, or null.
$event.pointer_typestring. For a pointer event: mouse, pen or touch, or null.
$event.filesmap. Files uploaded with the event, by the name of their live-upload input.

In @on-action, $event also has:

FieldWhat it holds
$event.namestring. The action's name.
$event.dataany. What the caller sent with the action, or null.

In @on-dragend, $event also has:

FieldWhat it holds
$event.vxnumber. How fast the pointer moved across at the end, in pixels per frame.
$event.vynumber. How fast the pointer moved down at the end, in pixels per frame.
$event.movedbool. True if the element was dragged, false for a press without moving.
$event.znumber. With @drag(front: true): the z-index the element was given, or null.

In @on-drop, $event also has:

FieldWhat it holds
$event.drag_valuestring. The value of the @drag element dropped here, or null.

In @on-event, $event also has:

FieldWhat it holds
$event.namestring. The event's name, as given to @emit_event.
$event.dataany. What @emit_event sent with the event (its data: argument), or null.
$event.sourcestring. The id of the live block that emitted the event.

In @on-sort, $event also has:

FieldWhat it holds
$event.itemstring. The sort-item value of the item that moved.
$event.fromnumber. Where the item was, counting from 0.
$event.indexnumber. Where the item is now, counting from 0.
$event.orderstring[]. Every item's sort-item value, in the new order.

Retired spellings

These were replaced by a shorthand or an option. They are errors now, with a quick fix in the editor.

WasWrite instead
bind-onbind-on is now @bind's on: option, such as @bind($draft, on: blur).
live-confirmlive-confirm is now the handler's confirm: modifier, such as @on-click(delete: $note, confirm: 'Delete this note?').
prevent-defaultprevent-default is now the handler's prevent: modifier, such as @on-keydown(save: $note, key: [ctrl, s], prevent: true).