State Management
Ink provides several ways to manage properties and states in your components.
Props
The props function can be used to access the properties of a component.
Signals
Ink provides a reactive state management system that allows you to manage states in your components. The system is based on signals, which are reactive variables that can be used to store and update data. Signals can be used to store any type of data, including numbers, strings, objects, arrays, and even functions.
To create a signal, you can use the signal() function, which takes an initial value as an argument. Signals can be read and updated using the value property. Setting the value will trigger a re-render of the component.
Signals can be used in your components to manage states and trigger updates when the state changes. You can use signals to store data that needs to be shared between components, or to trigger side effects when the state changes. Signals can also be used to store data that needs to be persisted across page reloads, such as form data or user preferences.
Events
For example, you can use the click attribute assigned to a function to trigger a function when the element is clicked. In combination with updating a signal, can trigger a re-render of the component. The following event attributes are supported.
Mouse Events
click dblclick mousedown mouseup mousemove mouseover mouseout wheel
Keyboard Events
keydown keypress keyup
Form Events
blur change contextmenu focus input submit invalid reset search select
Clipboard Events
copy cut paste
Transition Events
transitionend
Drag Events
drag dragstart dragend dragover dragenter dragleave drop scroll
Media Events
durationchange ended error loadeddata loadedmetadata loadstart pause play playing progress ratechange seeked seeking stalled suspend timeupdate volumechange waiting
Animation Events
animationstart animationend animationiteration
Class Names
The classnames function can be used to generate a list of class names based on the properties of an object.
Children
The children function can be used to render child components in a parent component.
Component
For other edge cases, the component function can be used to get raw access to the component's functionality.
Environment Variables
The env function can be used to access environment variables in a component.
this
this refers to the InkComponent that extends HTMLElement. This means all components in Ink are in fact are HTML elements and has access to the common functionality like innerHTML and querySelector() to name a few. InkComponent has the additional following properties and methods that you can access using this.
API: InkComponent
Property | Returns | Description |
---|---|---|
attr | Hash |
Returns only the valid HTML tag attributes (string and true). Example |
props | Hash |
Returns all the attributes assigned to the component. Example |
metadata | [ string, string ] |
Returns the tag and class name of the component. Example |
styles( ) | string |
Returns the css styles used in the component. Example |
template( ) | Node[] |
Returns a function that returns an array of elements, text nodes and web components Example |
render( ) | string |
Renders the children and returns the final HTML markup. Example |
HTMLElement
class on the
MDN Web Docs
.