Scoop API

scoop:scoop.ui.component:0.1.0

src/typography/roles.scoop

Selected production source · src/typography/roles.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.style.*
7public enum TypographyRole {
8 H1, H2, H3, H4, P, Blockquote, InlineCode, Lead, Large, Small, Muted
9}
11internal fun typographyStyle(role: TypographyRole, theme: Theme): TextStyle {
12 val body = theme.tokens.typography.body
13 val density = body.fontSize / 14.0
14 val(size, height, weight) =
15 when (role) {
16 case H1 -> (36.0, 40.0, 800)
17 case H2 -> (30.0, 36.0, 600)
18 case H3 -> (24.0, 32.0, 600)
19 case H4 -> (20.0, 28.0, 600)
20 case P -> (16.0, 28.0, 400)
21 case Blockquote -> (16.0, 24.0, 400)
22 case InlineCode -> (14.0, 20.0, 600)
23 case Lead -> (20.0, 28.0, 400)
24 case Large -> (18.0, 28.0, 600)
25 case Small -> (14.0, 14.0, 500)
26 case Muted -> (14.0, 20.0, 400)
27 }
28 return body.copy {
29 this.fontSize = size * density
30 this.lineHeight = Some(height / size)
31 this.weight = weight
32 this.color =
33 if (role == TypographyRole.Lead || role == TypographyRole.Muted)
34 theme.tokens.palette.muted.foreground
35 else
36 theme.tokens.palette.background.foreground
37 this.letterSpacing =
38 when (role) {
39 case H1 -> -0.025 * size * density
40 case H2 -> -0.025 * size * density
41 case H3 -> -0.025 * size * density
42 case H4 -> -0.025 * size * density
43 else -> 0.0
44 }
45 if (role == TypographyRole.Blockquote)
46 this.slant = FontSlant.Italic
47 if (role == TypographyRole.InlineCode)
48 this.families = ["monospace"]
49 }
52internal fun typographyDecoration(role: TypographyRole, theme: Theme): StyleOverride {
53 val density = theme.tokens.typography.body.fontSize / 14.0
54 return styleOverride {
55 when (role) {
56 case H2 -> this.border(Some(bottomBorder(theme.tokens.palette.border)))
57 case Blockquote -> this.border(
58 Some(Border.Logical(start = BorderEdge(2.0 * density, theme.tokens.palette.border)))
59 )
60 case InlineCode -> {
61 this.background(Some(Paint(theme.tokens.palette.muted.background)))
62 this.corners(Corners.all(theme.tokens.corners.small))
63 }
64 else -> {}
65 }
66 }
69internal fun typographyPadding(role: TypographyRole, density: Double): LogicalInsets =
70 when (role) {
71 case H2 -> LogicalInsets(bottom = 8.0 * density)
72 case Blockquote -> LogicalInsets(start = 24.0 * density)
73 case InlineCode -> LogicalInsets(
74 start = 4.8 * density,
75 end = 4.8 * density,
76 top = 3.2 * density,
77 bottom = 3.2 * density
78 )
79 else -> LogicalInsets()
80 }