Styling
.
Styles are CSS, written as Silk values. If you know CSS you know the names already:
css.padding
,
css.borderRadius
,
css.backgroundColor
.
Note:
Styling is part of the view: a look is a value computed from the model, like the nodes are. Hover, press and focus are tracked by the runtime, so they stay out of your model.
A plan picker
Live: this is the program below, running
Solo
One person
$0
Team
Up to ten people
$12
Company
Everyone
$40
Click a plan, then move the pointer over the others. Use Tab and the focus ring shows.
examples/src/core/plans.silk
Copy
//! A plan picker: styles that come from the model, and styles that follow the pointer.
import
core.look {
ACCENT
,
INK
,
LINE
,
MUTED
, rgb }
import
loom.css
import
loom.css { px,
Align
,
BorderStyle
,
Property
}
import
loom.lib {
Document
,
Node
,
NoCommand
,
NoOutput
,
Program
,
Role
,
Update
}
import
silk.mem.allocator {
Allocator
,
OutOfMemoryError
}
pub
struct
Plans
{
/// The plan that is chosen: 0, 1 or 2.
pub
chosen: u32, }
pub
union
Message
{
ClickedPlan
{
pub
plan: u32 }, }
type
Next
=
Update
<
Plans
,
NoCommand
,
NoOutput
>
impl
Program
<
Message
,
NoCommand
>
for
Plans
{
effect
fn
init
() ->
Next
!
OutOfMemoryError
? &
mut
Allocator
{
return
Next
.
of
(
Plans
{ chosen:
1
}) }
effect
fn
update
(
mut
model:
Plans
, message:
Message
) ->
Next
!
OutOfMemoryError
? &
mut
Allocator
{
match
move
message {
Message
.
ClickedPlan
{ plan } => { model.chosen = plan } }
return
Next
.
of
(
move
model) }
fn
view
(model: &
Plans
, h: &
Document
<
Message
>) ->
Node
{
return
h.
row
( &[ h.
accessRole
(
Role
.
Group
), h.
label
(
"Plan"
), h.
style
(&[css.
padding
(
px
(
16.0
)), css.
columnGap
(
px
(
12.0
)), css.
color
(
rgb
(
INK
))]), ], &[
plan
(h, model,
0
,
"Solo"
,
"One person"
,
0
),
plan
(h, model,
1
,
"Team"
,
"Up to ten people"
,
12
),
plan
(h, model,
2
,
"Company"
,
"Everyone"
,
40
), ], ) } }
/// One plan, as a card that can be chosen.
fn
plan
( h: &
Document
<
Message
>, model: &
Plans
, index: u32, name: string, about: string, dollars: i64, ) ->
Node
{
let
chosen = model.chosen == index
return
h.
button
( &[ h.
accessRole
(
Role
.
Radio
), h.
checked
(chosen), h.
onClick
(
Message
.
ClickedPlan
{ plan: index }),
// The base style. The last declaration of a property wins.
h.
style
( &[ css.
flexGrow
(
1.0
), css.
flexBasis
(
px
(
0.0
)), css.
padding
(
px
(
14.0
)), css.
borderWidth
(
px
(
1.0
)), css.
borderStyle
(
BorderStyle
.
Solid
), css.
borderRadius
(
px
(
12.0
)), css.
borderColor
(
rgb
(
LINE
)), css.
backgroundColor
(
rgb
(
0xFFFFFF
)), ], ),
// A style that depends on the model is an `if`.
if
chosen { h.
style
(&[css.
borderColor
(
rgb
(
ACCENT
)), css.
backgroundColor
(
rgb
(
0xF0FDFA
))]) }
else
{
// A style that depends on the pointer is a variant. The model never hears of it.
h.
hover
(&[css.
borderColor
(
rgb
(
MUTED
))]) }, h.
active
(&[css.
raw
(
Property
.
Translate
,
"0 1px"
)]), h.
focusVisible
(&[css.
borderColor
(
rgb
(
INK
))]), h.
transition
(
"border-color 150ms ease, background-color 150ms ease"
), ], &[
// A button's content flows inline, like text. A column stacks it.
h.
column
( &[h.
style
(&[css.
rowGap
(
px
(
4.0
)), css.
alignItems
(
Align
.
Start
)])], &[ h.
box
(&[h.
style
(&[css.
fontWeight
(
700.0
)])], &[h.
text
(name)]), h.
box
(&[h.
style
(&[css.
color
(
rgb
(
MUTED
)), css.
fontSize
(
px
(
13.0
))])], &[h.
text
(about)]), h.
row
( &[h.
style
(&[css.
marginTop
(
px
(
8.0
)), css.
fontSize
(
px
(
20.0
)), css.
fontWeight
(
700.0
)])], &[h.
text
(
"$"
), h.
number
(dollars)], ), ], ), ], ) }
Declarations
A style is a list of declarations. Each one is a function named after its CSS property.
Silk
Copy
h.
style
( &[ css.
padding
(
px
(
14.0
)), css.
borderWidth
(
px
(
1.0
)), css.
borderStyle
(
BorderStyle
.
Solid
), css.
borderRadius
(
px
(
12.0
)), css.
backgroundColor
(
rgb
(
0xFFFFFF
)), ], )
Lengths are built with
px(14.0)
and
percent(100.0)
. Keywords are enums, such as
BorderStyle.Solid
and
Align.Center
, so a misspelled value does not compile.
A node may have several
h.style
attributes. They apply in order, and the last declaration of a property wins. That is how the card gets a base look first and its chosen look after.
Styles that depend on the model
There are no class names to toggle. A style that depends on the model is an
if
.
examples/src/core/plans.silk
Copy
/// One plan, as a card that can be chosen.
fn
plan
(
chosen
comes from the model, so the chosen card's border and background are part of what
view
returns for that model.
Styles that follow the pointer
Hover, press and focus are not in your model, and should not be. Nobody wants a message for every pointer move.
Attribute
The look while
h.hover
The pointer is over the node
h.active
The node is being pressed
h.focused
The node has focus
h.focusVisible
The node has focus from the keyboard
The node switches between these looks by itself. The model never hears of it, and
view
is not called.
h.transition("border-color 150ms ease")
eases the change, with the syntax of the CSS
transition
property. A style can also set
transition
and
animation
as CSS does.
h.starting
is the look a node transitions from when it first shows, as CSS
@starting-style
, which is how a popup fades in.
transform
,
translate
,
rotate
,
scale
and
transform-origin
move, turn and scale a node as CSS does, in 2D or 3D with
perspective
. Layout does not change: the node draws, takes pointers and reports
getBoundingClientRect
where it shows. They transition and animate like any other property, and a
linear()
easing can take stops, such as
linear(0, 1.2 60%, 1)
for an overshoot.
Colors
A color is an
Rgba<f32>
: four numbers from 0 to 1. Most programs want to write hex codes, so the examples share a small helper.
examples/src/core/look.silk
Copy
/// A color from its hex code: `rgb(0x0F766E)`.
pub
fn
rgb
(hex: u32) ->
Rgba
<f32> {
let
r = u32.
toF32
(u32.
bitAnd
(u32.
shiftRight
(hex,
16
),
255
)) /
255.0
let
g = u32.
toF32
(u32.
bitAnd
(u32.
shiftRight
(hex,
8
),
255
)) /
255.0
let
b = u32.
toF32
(u32.
bitAnd
(hex,
255
)) /
255.0
return
Rgba
<f32> { r: r, g: g, b: b, a:
1.0
} }
Anything else CSS has
Every CSS property has a name in
Property
, and
css.raw
takes its value as text:
Silk
Copy
css.
raw
(
Property
.
Translate
,
"0 1px"
)
In a page,
css.raw
passes the text to the browser, so
var(--accent)
,
clamp()
and
calc()
all work. This site's light and dark themes are CSS variables set that way. In a native window, the tree applies the properties it implements.
A look of your own
A design system in Silk UI is a module of functions. This is the one the examples in these docs share: a button, a primary button, and the look of a text field.
examples/src/core/look.silk
Copy
//! The look the examples share: a few colors, a button and a text field. Each is a plain
//! function that works for any message type.
import
loom.css
import
loom.css { px,
BorderStyle
}
import
loom.lib {
Attr
,
Document
,
Node
}
import
silk.color.rgba {
Rgba
}
import
silk.num.u32
/// A color from its hex code: `rgb(0x0F766E)`.
pub
fn
rgb
(hex: u32) ->
Rgba
<f32> {
let
r = u32.
toF32
(u32.
bitAnd
(u32.
shiftRight
(hex,
16
),
255
)) /
255.0
let
g = u32.
toF32
(u32.
bitAnd
(u32.
shiftRight
(hex,
8
),
255
)) /
255.0
let
b = u32.
toF32
(u32.
bitAnd
(hex,
255
)) /
255.0
return
Rgba
<f32> { r: r, g: g, b: b, a:
1.0
} }
pub
const
INK
: u32 =
0x1F2937
pub
const
MUTED
: u32 =
0x6B7280
pub
const
LINE
: u32 =
0xD1D5DB
pub
const
ACCENT
: u32 =
0x0F766E
/// The style every button starts from. `filled` is for the one action that matters most.
pub
fn
buttonLook<
M
>(h: &
Document
<
M
>, filled: bool) ->
Attr
{
let
base = h.
style
( &[ css.
paddingTop
(
px
(
6.0
)), css.
paddingBottom
(
px
(
6.0
)), css.
paddingLeft
(
px
(
14.0
)), css.
paddingRight
(
px
(
14.0
)), css.
borderWidth
(
px
(
1.0
)), css.
borderStyle
(
BorderStyle
.
Solid
), css.
borderRadius
(
px
(
8.0
)), css.
borderColor
(
rgb
(
LINE
)), css.
backgroundColor
(
rgb
(
0xFFFFFF
)), css.
color
(
rgb
(
INK
)), ], )
if
filled {
return
h.
attrs
( &[ base, h.
style
( &[ css.
backgroundColor
(
rgb
(
ACCENT
)), css.
borderColor
(
rgb
(
ACCENT
)), css.
color
(
rgb
(
0xFFFFFF
)), ], ), h.
hover
(&[css.
backgroundColor
(
rgb
(
0x0D5F58
))]), h.
focusVisible
(&[css.
borderColor
(
rgb
(
INK
))]), ], ) }
return
h.
attrs
( &[ base, h.
hover
(&[css.
backgroundColor
(
rgb
(
0xF3F4F6
))]), h.
active
(&[css.
backgroundColor
(
rgb
(
0xE5E7EB
))]), h.
focusVisible
(&[css.
borderColor
(
rgb
(
ACCENT
))]), ], ) }
/// A button that sends `message` when it is clicked.
pub
fn
button<
M
>(h: &
Document
<
M
>, label: string, message:
M
) ->
Node
{
return
h.
button
(&[h.
onClick
(
move
message), buttonLook<
M
>(h,
false
)], &[h.
text
(label)]) }
/// The button for the main action of a view.
pub
fn
primary<
M
>(h: &
Document
<
M
>, label: string, message:
M
) ->
Node
{
return
h.
button
(&[h.
onClick
(
move
message), buttonLook<
M
>(h,
true
)], &[h.
text
(label)]) }
/// The border and padding of a text field.
pub
fn
fieldLook<
M
>(h: &
Document
<
M
>) ->
Attr
{
return
h.
attrs
( &[ h.
style
( &[ css.
minWidth
(
px
(
0.0
)), css.
paddingTop
(
px
(
6.0
)), css.
paddingBottom
(
px
(
6.0
)), css.
paddingLeft
(
px
(
10.0
)), css.
paddingRight
(
px
(
10.0
)), css.
borderWidth
(
px
(
1.0
)), css.
borderStyle
(
BorderStyle
.
Solid
), css.
borderRadius
(
px
(
8.0
)), css.
borderColor
(
rgb
(
LINE
)), css.
backgroundColor
(
rgb
(
0xFFFFFF
)), css.
color
(
rgb
(
INK
)), ], ), h.
focused
(&[css.
borderColor
(
rgb
(
ACCENT
))]), ], ) }
buttonLook
returns an
Attr
.
h.attrs
joins several attributes into one, so a look can be a base style plus its hover, press and focus variants, and still be one value to pass around.
The functions are generic over the message type
M
, which is why any program can use them.
Recap
•
Styles are CSS properties written as values, in
h.style
.
•
A style that depends on your data is an
if
. A style that depends on the pointer or focus is a variant:
h.hover
,
h.active
,
h.focusVisible
.
•
Share a look by writing a function that returns an
Attr
.
•
css.raw
passes any CSS through when there is no helper for it.