scoop:scoop.ui.component:0.1.0
src/disclosure/accordion.scoop
Selected production source · src/disclosure/accordion.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.surface.AnimationSpec6
import scoop.ui.*7
import scoop.ui.style.*9
public 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
}25
}27
public class Accordion<T: Equals<T>> public constructor(28
private val props: Props<AccordionProps<T>>,29
scope: ComponentScope30
): 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.value38
val theme = themes.read(input.theme)39
val density = theme.tokens.typography.body.fontSize / 14.040
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.key53
val trigger =54
entry.trigger ?:55
TriggerSpec(56
ButtonContent.Label(57
LabelProps(LabelText.Plain(entry.choice.label, None))58
),59
variant = ButtonVariant.Link60
)61
val open =62
binding(63
{64
val latest = props.value65
when (accordionEntry(latest.entries, key)) {66
case Some(val value) -> accordionOpen(67
latest.selected,68
value.choice.value69
)70
case None -> false71
}72
},73
{74
desired ->75
val latest = props.value76
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.style86
)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.fonts103
)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.value112
when (accordionEntry(latest.entries, key)) {113
case Some(val value) -> latest.onOpenChange(114
value.choice.value,115
accepted116
)117
case None -> {}118
}119
},120
input.fonts,121
input.theme122
),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
else129
Insets(),130
visual = frame131
)132
}133
return choiceLayout(134
children,135
keys,136
focus,137
FlexAxis.Vertical,138
if (input.variant == AccordionVariant.Card)139
12.0 * density140
else141
0.0,142
input.loop,143
input.enabled,144
input.style,145
roving = false146
)147
}148
}150
private fun accordionTriggerStyle(theme: Theme, variant: AccordionVariant): ButtonStyle {151
val density = theme.tokens.typography.body.fontSize / 14.0152
return buttonStyle {153
this.kind = SurfaceKind.Plain154
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.foreground168
else169
theme.tokens.palette.background.foreground170
)171
}172
}173
}175
private fun accordionItemStyle(176
theme: Theme,177
variant: AccordionVariant,178
last: Boolean179
): 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
} else186
if (!last)187
this.border(Some(bottomBorder(theme.tokens.palette.border)))188
}