Silk UI
.
Write an app as a model, an update function and a view. Run it in a browser tab or in a native window, from the same code, with no browser engine inside.
Get startedSource on GitHubProductsCan I use
Try it
Drag a card to another column. Focus one and move it with the arrow keys. Filter by tag, add a card, then undo it all. This board is one model, one
update
and a few view functions, and the same file runs in a native window. Read how it is built.
Live: this is the program below, running
All
Design
Code
Docs
Undo
To do
3
Sketch settings
Design
×
Upgrade guide
Docs
×
Cache font atlas
Code
×
+ Add a card
Doing
2
Dark charts
Design
×
Fix resize scroll
Code
×
+ Add a card
Done
2
Release notes
Docs
×
Keyboard moves
Code
×
+ Add a card
This is a whole program
The board is built from the same four parts as this counter. A model holds the state. Messages say what happened.
update
is the only place the state changes, and
view
draws it. The frame below is this exact file running.
Live: this is the program below, running
-
0
+
Reset
examples/src/core/counter.silk
Copy
//! A counter: the smallest complete Silk UI program.
import
loom.css
import
loom.css { px,
Align
,
BorderStyle
}
import
loom.lib {
Document
,
Node
,
NoCommand
,
NoOutput
,
Program
,
Update
}
import
silk.color.rgba {
Rgba
}
import
silk.mem.allocator {
Allocator
,
OutOfMemoryError
}
/// Everything the program remembers.
pub
struct
Counter
{
pub
count: i64, }
/// Everything that can happen.
pub
union
Message
{
ClickedIncrement
,
ClickedDecrement
,
ClickedReset
, }
type
Next
=
Update
<
Counter
,
NoCommand
,
NoOutput
>
impl
Program
<
Message
,
NoCommand
>
for
Counter
{
/// The model the program starts with.
effect
fn
init
() ->
Next
!
OutOfMemoryError
? &
mut
Allocator
{
return
Next
.
of
(
Counter
{ count:
0
}) }
/// The model after a message. Nothing else changes the model.
effect
fn
update
(
mut
model:
Counter
, message:
Message
) ->
Next
!
OutOfMemoryError
? &
mut
Allocator
{
match
move
message {
Message
.
ClickedIncrement
=> { model.count = model.count +
1
}
Message
.
ClickedDecrement
=> { model.count = model.count -
1
}
Message
.
ClickedReset
=> { model.count =
0
} }
return
Next
.
of
(
move
model) }
/// What the screen shows for a model.
fn
view
(model: &
Counter
, h: &
Document
<
Message
>) ->
Node
{
return
h.
row
( &[h.
style
(&[css.
alignItems
(
Align
.
Center
), css.
gap
(
px
(
12.0
)), css.
padding
(
px
(
16.0
))])], &[
button
(h,
"-"
,
Message
.
ClickedDecrement
), h.
row
( &[ h.
testId
(
"count"
), h.
style
( &[css.
fontSize
(
px
(
24.0
)), css.
minWidth
(
px
(
48.0
)), css.
justifyContent
(
Align
.
Center
)], ), ], &[h.
number
(model.count)], ),
button
(h,
"+"
,
Message
.
ClickedIncrement
),
button
(h,
"Reset"
,
Message
.
ClickedReset
), ], ) } }
/// A button is a function: it takes what to show and the message a click sends.
fn
button
(h: &
Document
<
Message
>, label: string, message:
Message
) ->
Node
{
let
gray =
Rgba
<f32> { r:
0.82
, g:
0.84
, b:
0.86
, a:
1.0
}
let
hovered =
Rgba
<f32> { r:
0.95
, g:
0.96
, b:
0.96
, a:
1.0
}
return
h.
button
( &[ h.
onClick
(
move
message), h.
style
( &[ css.
padding
(
px
(
6.0
)), css.
paddingLeft
(
px
(
14.0
)), css.
paddingRight
(
px
(
14.0
)), css.
borderWidth
(
px
(
1.0
)), css.
borderStyle
(
BorderStyle
.
Solid
), css.
borderColor
(gray), css.
borderRadius
(
px
(
8.0
)), ], ), h.
hover
(&[css.
backgroundColor
(hovered)]), ], &[h.
text
(label)], ) }
Three guides, one stack
Each guide stands on its own. Read the first to build apps, the second to ship them as desktop programs, and the third to use the engine underneath without the framework.
Guide 1
Silk UI
The framework. Models, messages, views, styling, commands, subscriptions, routing, composition and tests, each with a program you can click.
Read the guide
Guide 2
Native apps
The same programs in real windows: menus, file dialogs, several windows, worker threads, the built-in devtools, and tests that need no display.
Open a window
Guide 3
The UI tree
The engine underneath: nodes, CSS layout, text, input and paint as a library. Use it directly to host your own framework or to drive a UI from another process.
Meet the tree
How the parts stack
Your program never talks to a window or a DOM. It returns a view, and a host turns that view into something on screen. Swap the host and the same program runs somewhere else.
Your program
A model, an update function and a view. Plain data and pure functions.
Silk UI
Runs the loop: delivers messages, performs commands, keeps subscriptions, and diffs each view into patches.
A host
In a browser, a small script applies the patches to the DOM. In a native app, the patches go to the UI tree.
The UI tree
Lays out with CSS, shapes text, routes pointer and keyboard input the way the web does, and paints with the GPU.
A window
One OS window per root, on macOS, Windows and Linux.
This site is a Silk UI program. Every demo on it is the code shown beside it.