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.
<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.
| Handler | What it does |
|---|---|
@on-action | Runs when client code calls the action named by @live-action with KScriptLive.call(). What it sent is in $event.data. |
@on-cancel | Runs when a <dialog> is cancelled, such as with Escape. |
@on-change | Runs when a control's value is committed, such as a select being picked or a checkbox ticked. |
@on-click | Runs when the element is clicked: @on-click(toggle: $open). |
@on-close | Runs when a <dialog> closes. |
@on-dblclick | Runs when the element is double-clicked. |
@on-dragend | Runs when a drag of this element ends. After @drag, $event.x and $event.y are where it landed. |
@on-drop | Runs when something is dropped on the element: a @drag(copy: true) ghost, whose value is in $event.drag_value. |
@on-enter | Runs when Enter is pressed in the element: <input @on-enter(add: $draft, to: $tags)>. |
@on-escape | Runs when Escape is pressed in the element. |
@on-event | Runs when another live block on the page sends an event with @emit_event or emit:. Pick which with @event-name. |
@on-focusin | Runs when the element, or something inside it, gets focus. |
@on-focusout | Runs when focus leaves the element and everything inside it. |
@on-init | Runs once, when the component is first drawn: load what it shows here. |
@on-input | Runs each time a control's value changes as the person types. The new value is in $event.value. |
@on-keydown | Runs when a key is pressed. Pick the key with key:, as in @on-keydown(save: $note, key: [ctrl, s]). |
@on-keyup | Runs when a key is let go. |
@on-pointercancel | Runs when the browser takes over a pointer, such as to scroll the page. |
@on-pointerdown | Runs when a mouse button, pen or finger goes down on the element. |
@on-pointerup | Runs when the mouse button, pen or finger comes up. |
@on-sort | Runs when the person drags one of the @sort-item children to a new place. $event.order is the new order. |
@on-submit | Runs when a form is submitted, instead of the browser posting it: <form @on-submit($save)>. |
@on-tick | Runs again and again, as often as @every says: <p @on-tick(increment: $seconds) every="1s">. |
@on-visible | Runs 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.
| Directive | What it does |
|---|---|
@autogrow | Makes a <textarea> grow taller as its text grows. |
@bind | Keeps a control and a field in step: @bind($title). Options such as on: blur and trim: true go after the field. |
@class | Switch classes on by conditions: @class(done: $todo.done) or @class({ $todo.done: ['done', 'struck'] }). |
@drag | Move 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-class | Add classes while something this element accepts is dragged over it: @drop-class('file-over'). |
@drop-files | Send files dropped on this element through the named live-upload input inside it: @drop-files('upload'). |
@each | Draws the tag once for each item: @each($tasks, as: task, at: i). Same as <for each="task" in="$tasks"> around it. |
@event-name | Runs @on-event only for events with this name: event-name="notes-changed". |
@every | How often @on-tick runs: every="1s". |
@focus | Give this element focus when it appears, or when its condition turns true: @focus() or @focus($editing). |
@if | Draws the tag only while the condition is true: @if($note). Same as <if $note> around it. |
@live-action | Names the @on-action handler, so client code can call it with KScriptLive.call(). |
@live-client | Runs a client action registered with KScriptLive when the element is clicked, without a trip to the server. |
@live-cloak | Hides the element until the page's live code has started, so nothing half-drawn shows. |
@live-dirty | Shows the element while a binding has edits not yet sent. Name a field to watch only that one: @live-dirty($title). |
@live-disable | Disable this control while an action is pending, or while a condition holds: @live-disable(while: $saving). |
@live-error | Shows the element only after an action fails. |
@live-id | Gives this component a name that stays the same between updates, so two of the same component keep their own state. |
@live-ignore | Leaves everything inside the element alone during updates, for a widget the browser or a script manages. |
@live-key | Gives 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-loading | Shows the element only while an action is running, such as a spinner. |
@live-navigate | Follows the link without a full page load, swapping in the new page. |
@live-navigate-once | Runs this <script> only once, even when in-page navigation comes back to the page. |
@live-offline | Shows the element only while the browser is offline. |
@live-portal | Draws this <template>'s content somewhere else in the page, at the CSS selector given: live-portal="#modals". |
@live-replace | Replaces the element whole on each update instead of patching it. |
@live-stream | Receives what an action streams to this name, as it arrives. |
@live-upload | Sends the files picked in this file input with the next action. They are in $event.files. |
@live-use-name | Attaches a client directive registered with KScriptLive, by its name: @live-use-autosize(). |
@pending | Add classes while an action runs: @pending('saving'), or @pending('crumple', for: 'delete') for one method's actions. |
@show | Shows the tag while the value is true and hides it (keeping it in the page) while false: @show($open). |
@sort-axis | Says which way the @sort-item children run: vertical (the default) or horizontal. |
@sort-item | Marks 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.
| Shorthand | What 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).
| Modifier | What 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).
| Option | What 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.
| Field | What it holds |
|---|---|
$event.type | string. The event's name: click, input, submit, component, action, sort... |
$event.value | string. The value of the element the event came from (an input, select or textarea), or null. |
$event.checked | bool. For a checkbox: whether it is ticked. Null for other elements. |
$event.target_id | string. The id of the element the event came from, or null. |
$event.key | string. For a key event: the key pressed, such as Enter, Escape or a. Null otherwise. |
$event.code | string. For a key event: the physical key, such as KeyA or Digit1. Null otherwise. |
$event.alt | bool. True if Alt (Option) was held. |
$event.ctrl | bool. True if Ctrl was held. |
$event.meta | bool. True if Cmd (Windows key) was held. |
$event.shift | bool. True if Shift was held. |
$event.repeat | bool. True if a key event is a repeat from a key held down. |
$event.client_x | number. The pointer's x in the window, in pixels, or null. |
$event.client_y | number. The pointer's y in the window, in pixels, or null. |
$event.page_x | number. The pointer's x in the page, counting scroll, or null. |
$event.page_y | number. The pointer's y in the page, counting scroll, or null. |
$event.x | number. The pointer's x inside the handler's element, counting its scroll, or null. |
$event.y | number. The pointer's y inside the handler's element, counting its scroll, or null. |
$event.button | number. For a pointer event: the button (0 main, 1 middle, 2 secondary), or null. |
$event.pointer_id | number. For a pointer event: the pointer's id, or null. |
$event.pointer_type | string. For a pointer event: mouse, pen or touch, or null. |
$event.files | map. Files uploaded with the event, by the name of their live-upload input. |
In @on-action, $event also has:
| Field | What it holds |
|---|---|
$event.name | string. The action's name. |
$event.data | any. What the caller sent with the action, or null. |
In @on-dragend, $event also has:
| Field | What it holds |
|---|---|
$event.vx | number. How fast the pointer moved across at the end, in pixels per frame. |
$event.vy | number. How fast the pointer moved down at the end, in pixels per frame. |
$event.moved | bool. True if the element was dragged, false for a press without moving. |
$event.z | number. With @drag(front: true): the z-index the element was given, or null. |
In @on-drop, $event also has:
| Field | What it holds |
|---|---|
$event.drag_value | string. The value of the @drag element dropped here, or null. |
In @on-event, $event also has:
| Field | What it holds |
|---|---|
$event.name | string. The event's name, as given to @emit_event. |
$event.data | any. What @emit_event sent with the event (its data: argument), or null. |
$event.source | string. The id of the live block that emitted the event. |
In @on-sort, $event also has:
| Field | What it holds |
|---|---|
$event.item | string. The sort-item value of the item that moved. |
$event.from | number. Where the item was, counting from 0. |
$event.index | number. Where the item is now, counting from 0. |
$event.order | string[]. 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.
| Was | Write instead |
|---|---|
bind-on | bind-on is now @bind's on: option, such as @bind($draft, on: blur). |
live-confirm | live-confirm is now the handler's confirm: modifier, such as @on-click(delete: $note, confirm: 'Delete this note?'). |
prevent-default | prevent-default is now the handler's prevent: modifier, such as @on-keydown(save: $note, key: [ctrl, s], prevent: true). |