Scoop API

scoop:scoop.ui.component:0.1.0

src/navigation_menu/link.scoop

Selected production source · src/navigation_menu/link.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.ui.style.Theme
8import scoop.window.accessibility.*
10public class NavigationMenuLink public constructor(
11 private val props: Props<NavigationMenuLinkProps>,
12 scope: ComponentScope
13): Component(scope) {
14 private val themes = ThemeAccess(scope)
15 public override fun onMount() {
16 themes.mount(props.value.button.theme)
17 }
18 public override fun render(): View {
19 val input = props.value
20 val button = input.button
21 val theme = themes.read(button.theme)
22 return semanticButton(
23 ButtonProps(
24 button.content,
25 button.onClick,
26 button.enabled,
27 navMenuStyle(theme, button.style, input.active, input.inPanel, false),
28 button.theme,
29 button.focus,
30 button.invalid,
31 button.variant,
32 button.size,
33 button.group,
34 button.labelTarget,
35 button.semantics
36 ),
37 SemanticRole.Link,
38 button.semantics.properties(
39 enabled = button.enabled,
40 invalid = button.invalid,
41 selected = Some(input.active)
42 ),
43 SemanticActions(
44 invoke =
45 Some {
46 if (!props.value.button.enabled)
47 SemanticActionResult.Rejected
48 else {
49 props.value.button.onClick()
50 SemanticActionResult.Completed
51 }
52 }
53 )
54 )
55 }
58internal fun navMenuStyle(
59 theme: Theme,
60 local: ButtonStyle,
61 active: Boolean,
62 inPanel: Boolean,
63 trigger: Boolean
64): ButtonStyle {
65 val d = theme.tokens.typography.body.fontSize / 14.0
66 val palette = theme.tokens.palette
67 val hover = styleOverride { background(Some(Paint(palette.muted.background))) }
68 return local.copy {
69 surface {
70 if (suggestedSize == None)
71 suggestedSize = Some(Size(0.0, 36.0 * d))
72 if (minimumSize == None)
73 minimumSize = Some(Size(0.0, 0.0))
74 if (padding == None)
75 padding =
76 Some(
77 if (inPanel)
78 Insets.all(8.0 * d)
79 else
80 Insets(10.0 * d, 6.0 * d, 10.0 * d, 6.0 * d)
81 )
82 style =
83 styleOverride {
84 border(None);
85 boxShadows([]);
86 corners(
87 Corners.all(
88 (
89 if (inPanel)
90 6.0
91 else
92 8.0
93 ) *
94 d
95 )
96 )
97 if (active)
98 background(Some(Paint(palette.muted.background.withAlpha(0.5))))
99 }.then(local.surface.style)
100 }
101 text =
102 Some(
103 (local.text ?: spanStyle()).copy {
104 if (weight == None)
105 weight =
106 Some(
107 if (trigger)
108 500
109 else
110 400
111 )
112 }
113 )
114 states {
115 hovered = hover.then(local.surface.style).then(local.states.hovered)
116 pressed = hover.then(local.surface.style).then(local.states.pressed)
117 focused = hover.then(local.surface.style).then(local.states.focused)
118 }
119 }