Scoop API

scoop:scoop.ui.component:0.1.0

src/disclosure/accordion.scoop

Selected production source · src/disclosure/accordion.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.surface.AnimationSpec
6import scoop.ui.*
7import scoop.ui.style.*
9public class AccordionProps<T: Equals<T>> public constructor(
10 public val entries: Array<AccordionEntry<T>>,
11 public val selected: AccordionSelection<T>,
12 public val enabled: Boolean = true,
13 public val loop: Boolean = true,
14 public val variant: AccordionVariant = AccordionVariant.Default,
15 public val retention: ContentRetention = ContentRetention.Dispose,
16 public val transition: StyleValue<AnimationSpec?> = StyleValue.Inherit,
17 public val fonts: FontCollection? = None,
18 public val theme: ThemeSource? = None,
19 public val style: StyleOverride = styleOverride(),
20 public val onOpenChange: (T, Boolean) -> Unit = { _, _ -> }
21) {
22 init {
23 validateChoices(Array<Choice<T>>(entries.size) { index -> entries[index].choice })
24 }
27public class Accordion<T: Equals<T>> public constructor(
28 private val props: Props<AccordionProps<T>>,
29 scope: ComponentScope
30): Component(scope) {
31 private val themes = ThemeAccess(scope)
32 private val handles = ChoiceHandles()
33 public override fun onMount() {
34 themes.mount(props.value.theme)
35 }
36 public override fun render(): View {
37 val input = props.value
38 val theme = themes.read(input.theme)
39 val density = theme.tokens.typography.body.fontSize / 14.0
40 val keys = Array<ViewKey>(input.entries.size) { index -> input.entries[index].choice.key }
41 val focus =
42 handles.resolve(
43 keys,
44 Array<FocusOptions>(keys.size) {
45 index -> input.entries[index].trigger?.focus ?: FocusOptions()
46 }
47 )
48 val children =
49 Array<View>(keys.size) {
50 index ->
51 val entry = input.entries[index]
52 val key = entry.choice.key
53 val trigger =
54 entry.trigger ?:
55 TriggerSpec(
56 ButtonContent.Label(
57 LabelProps(LabelText.Plain(entry.choice.label, None))
58 ),
59 variant = ButtonVariant.Link
60 )
61 val open =
62 binding(
63 {
64 val latest = props.value
65 when (accordionEntry(latest.entries, key)) {
66 case Some(val value) -> accordionOpen(
67 latest.selected,
68 value.choice.value
69 )
70 case None -> false
71 }
72 },
73 {
74 desired ->
75 val latest = props.value
76 when (accordionEntry(latest.entries, key)) {
77 case Some(val value) -> if (latest.enabled && value.choice.enabled)
78 setAccordionOpen(latest.selected, value.choice.value, desired)
79 case None -> {}
80 }
81 }
82 )
83 val frame =
84 accordionItemStyle(theme, input.variant, index == keys.size - 1L).then(
85 entry.style
86 )
87 panelFrame(
88 component(
89 CollapsibleProps(
90 open,
91 panelFrame(entry.content, Insets(0.0, 0.0, 0.0, 10.0 * density)),
92 Some(
93 TriggerSpec(
94 trigger.content,
95 trigger.enabled,
96 trigger.indicator,
97 trigger.variant,
98 Some(
99 trigger.style ?: accordionTriggerStyle(theme, input.variant)
100 ),
101 choiceFocus(trigger.focus, focus[index]),
102 trigger.fonts
103 )
104 ),
105 input.enabled && entry.choice.enabled,
106 entry.retention ?: input.retention,
107 0.0,
108 input.transition,
109 {
110 accepted ->
111 val latest = props.value
112 when (accordionEntry(latest.entries, key)) {
113 case Some(val value) -> latest.onOpenChange(
114 value.choice.value,
115 accepted
116 )
117 case None -> {}
118 }
119 },
120 input.fonts,
121 input.theme
122 ),
123 { current, mount -> Collapsible(current, scope = mount) }
124 ),
125 padding =
126 if (input.variant == AccordionVariant.Card)
127 Insets(12.0 * density, 0.0, 12.0 * density, 0.0)
128 else
129 Insets(),
130 visual = frame
131 )
132 }
133 return choiceLayout(
134 children,
135 keys,
136 focus,
137 FlexAxis.Vertical,
138 if (input.variant == AccordionVariant.Card)
139 12.0 * density
140 else
141 0.0,
142 input.loop,
143 input.enabled,
144 input.style,
145 roving = false
146 )
147 }
150private fun accordionTriggerStyle(theme: Theme, variant: AccordionVariant): ButtonStyle {
151 val density = theme.tokens.typography.body.fontSize / 14.0
152 return buttonStyle {
153 this.kind = SurfaceKind.Plain
154 surface {
155 this.suggestedSize = Some(Size(0.0, 0.0))
156 this.minimumSize = Some(Size(0.0, 0.0))
157 this.padding = Some(Insets(0.0, 10.0 * density, 0.0, 10.0 * density))
158 style { this.corners(Corners.all(theme.tokens.corners.large)) }
159 }
160 text {
161 this.fontSize = Some(14.0 * density)
162 this.lineHeight = Some(Some(20.0 / 14.0))
163 this.weight = Some(500)
164 this.color =
165 Some(
166 if (variant == AccordionVariant.Card)
167 theme.tokens.palette.card.foreground
168 else
169 theme.tokens.palette.background.foreground
170 )
171 }
172 }
175private fun accordionItemStyle(
176 theme: Theme,
177 variant: AccordionVariant,
178 last: Boolean
179): StyleOverride =
180 styleOverride {
181 if (variant == AccordionVariant.Card) {
182 this.background(Some(Paint(theme.tokens.palette.card.background)))
183 this.border(Some(Border.all(1.0, theme.tokens.palette.border)))
184 this.corners(Corners.all(theme.tokens.corners.large))
185 } else
186 if (!last)
187 this.border(Some(bottomBorder(theme.tokens.palette.border)))
188 }