Scoop API

scoop:scoop.ui.component:0.1.0

src/actions/semantic_button.scoop

Selected production source · src/actions/semantic_button.scoop

1package scoop.ui.component
3import scoop.ui.*
4import scoop.window.accessibility.*
6private class SemanticButtonProps(
7 val input: ButtonProps,
8 val role: SemanticRole,
9 val state: SemanticProperties,
10 val actions: SemanticActions,
11 val fallbackName: String?
14private class SemanticButton(
15 private val props: Props<SemanticButtonProps>,
16 scope: ComponentScope
17): Component(scope) {
18 private val button =
19 ButtonControl(
20 { props.value.input },
21 scope,
22 props.value.role,
23 { Some(props.value.state) },
24 { Some(props.value.actions) },
25 { props.value.fallbackName }
26 )
27 public override fun onMount() {
28 button.mount()
29 }
30 public override fun render(): View = button.render()
33/** Uses the ordinary button's layout, press, focus and visual state for semantic variants. */
34internal fun semanticButton(
35 input: ButtonProps,
36 role: SemanticRole,
37 state: SemanticProperties,
38 actions: SemanticActions,
39 fallbackName: String? = None
40): View =
41 component(
42 SemanticButtonProps(input, role, state, actions, fallbackName),
43 { props, mount -> SemanticButton(props, scope = mount) },
44 // A role change retires its identity even when the surrounding component is reused.
45 key =
46 ViewKey.Number(
47 when (role) {
48 case Button -> 2L
49 case Link -> 13L
50 case MenuItem -> 18L
51 case Cell -> 23L
52 case Disclosure -> 26L
53 else -> throw IllegalArgumentException(
54 Some("unsupported semantic button variant")
55 )
56 }
57 )
58 )
60internal fun popupSemanticButton(
61 input: ButtonProps,
62 expanded: Boolean,
63 change: (Boolean) -> SemanticActionResult,
64 value: String? = None,
65 fallbackName: String? = None
66): View =
67 semanticButton(
68 input,
69 SemanticRole.Button,
70 input.semantics.properties(
71 value = value,
72 enabled = input.enabled,
73 invalid = input.invalid,
74 expanded = Some(expanded)
75 ),
76 SemanticActions(invoke = Some { change(!expanded) }, setExpanded = Some(change)),
77 fallbackName
78 )