scoop:scoop.ui.component:0.1.0
src/overlays/modal.scoop
Selected production source · src/overlays/modal.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.surface.AnimationSpec6
import scoop.surface.Easing7
import scoop.time.Duration8
import scoop.ui.*9
import scoop.ui.style.*10
import scoop.window.accessibility.*11
import scoop.ui.component.column as modalColumn13
public class Dialog public constructor(14
private val props: Props<DialogProps>,15
scope: ComponentScope16
): Component(scope) {17
private val modal = ModalContent({ props.value.input }, scope)18
public override fun onMount() {19
modal.mount()20
}21
public override fun render(): View = modal.render()22
}24
public class AlertDialog public constructor(25
private val props: Props<AlertDialogProps>,26
scope: ComponentScope27
): Component(scope) {28
private val modal = ModalContent({ props.value.input }, scope)29
public override fun onMount() {30
modal.mount()31
}32
public override fun render(): View = modal.render()33
}35
public class Sheet public constructor(36
private val props: Props<SheetProps>,37
scope: ComponentScope38
): Component(scope) {39
private val modal = ModalContent({ props.value.input }, scope)40
public override fun onMount() {41
modal.mount()42
}43
public override fun render(): View = modal.render()44
}46
/** The business binding and Portal own the session; this composition only supplies its content. */47
internal class ModalContent(private val read: () -> ModalInput, scope: ComponentScope) {48
private val themes = ThemeAccess(scope)49
private val localHandle = OverlayHandle()50
private val semanticReference = SemanticsReference()51
private val cancelFocus = FocusHandle()52
private val titles = ActionTextStyle()53
private val descriptions = ActionTextStyle()54
fun mount() {55
themes.mount(read().options.theme)56
}57
private fun close() {58
(read().options.handle ?: localHandle).requestClose()59
}60
private fun dismissSemantically(): SemanticActionResult {61
if (!read().open.value)62
return SemanticActionResult.Unavailable63
close()64
return if (!read().open.value)65
SemanticActionResult.Completed66
else67
SemanticActionResult.Rejected68
}69
private fun setOpen(next: Boolean) {70
val previous = read().open.value71
if (previous == next)72
return73
read().open.value = next74
val accepted = read().open.value75
if (previous != accepted)76
read().options.onOpenChange(accepted)77
}78
private fun activate() {79
val input = read()80
if (!input.options.enabled || !(input.options.trigger?.enabled ?: false))81
return82
if (input.open.value)83
close()84
else85
setOpen(true)86
}87
fun render(): View {88
val input = read()89
val options = input.options90
val theme = themes.read(options.theme)91
val palette = theme.tokens.palette92
val density = theme.tokens.typography.body.fontSize / 14.093
val sheet =94
when (input.kind) {95
case Sheet(_) -> true;96
case Drawer(_, _) -> true;97
else -> false98
}99
val alert =100
when (input.kind) {101
case Alert(_) -> true;102
else -> false103
}104
val small =105
when (input.kind) {106
case Alert(AlertDialogSize.Sm) -> true107
else -> false108
}109
val drawer =110
when (input.kind) {111
case Drawer(_, _) -> true;112
else -> false113
}114
val overlayHandle = options.handle ?: localHandle115
val titleStyle =116
titles.resolve(117
theme.tokens.typography.body.copy {118
fontSize = 16.0 * density;119
weight = 500;120
lineHeight = Some(1.0)121
},122
palette.popover.foreground,123
None124
)125
val descriptionStyle =126
descriptions.resolve(127
theme.tokens.typography.body,128
componentForeground(theme, palette.muted.foreground, palette.popover.background),129
None130
)131
val headerPresent =132
panelPresent(options.header) ||133
panelPresent(options.title) ||134
panelPresent(options.description) ||135
panelPresent(input.media)136
val header =137
options.header ?:138
if (!headerPresent)139
empty()140
else141
if (alert) {142
val media =143
when (input.media) {144
case Some(val view) -> frame(view) {145
width = Some(40.0 * density);146
height = Some(40.0 * density)147
style {148
background(Some(Paint(palette.muted.background)));149
corners(Corners.all(6.0 * density))150
}151
}152
case None -> empty()153
}154
element(155
AlertHeaderProps(156
density,157
small,158
panelPresent(input.media),159
panelPresent(options.title),160
panelPresent(options.description)161
),162
{ current, mount -> AlertHeader(current, scope = mount) },163
[164
media,165
modalCaption(options.title, titleStyle, options.fonts, true),166
modalCaption(167
options.description,168
descriptionStyle,169
options.fonts,170
true171
)172
]173
)174
} else175
modalColumn {176
when (options.title) {177
case Some(val value) -> add(178
panelSlot(panelCaption(value, titleStyle, options.fonts), 0L)179
)180
case None -> {}181
}182
when (options.description) {183
case Some(val value) -> add(184
panelSlot(185
panelCaption(value, descriptionStyle, options.fonts),186
1L187
)188
)189
case None -> {}190
}191
spacing =192
if (sheet)193
2.0 * density194
else195
8.0 * density196
alignment = CrossAlignment.Stretch197
}198
val cancelHandle =199
when (input.cancel) {200
case Some(val value) -> value.focus.handle ?: cancelFocus201
case None -> cancelFocus202
}203
val footer =204
if (alert) {205
val cancel = input.cancel!!206
val focus = cancel.focus.copy { handle = Some(cancelHandle) }207
val cancelView = modalButton(cancel, { close() }, options, Some(focus))208
val actions =209
when (input.action) {210
case Some(val view) -> [panelSlot(cancelView, 0L), panelSlot(view, 1L)]211
case None -> [panelSlot(cancelView, 0L)]212
}213
Some(modalActions(actions, density, small))214
} else215
input.footer216
val footerView =217
when (footer) {218
case Some(val view) -> panelFrame(219
view,220
Insets.all(options.contentPadding * density),221
styleOverride {222
if (!sheet) {223
background(Some(Paint(componentPanelSection(theme, palette.popover))))224
border(Some(topBorder(componentPanelBorder(theme, palette.popover))))225
corners(226
Corners.Logical(227
bottomStart = Size(12.0 * density, 12.0 * density),228
bottomEnd = Size(12.0 * density, 12.0 * density)229
)230
)231
}232
}233
)234
case None -> empty()235
}236
val closeView =237
if (input.showClose) {238
val spec =239
input.close ?:240
PopupTriggerSpec(241
ButtonContent.Custom(242
modalCloseIcon(16.0 * density, palette.popover.foreground)243
),244
variant = ButtonVariant.Ghost,245
size = ButtonSize.IconSm,246
semantics = ComponentSemantics(name = Some("Close"))247
)248
modalButton(spec, { close() }, options)249
} else250
empty()251
val bodyScroll =252
when (input.kind) {253
case Drawer(_, val owner) -> Some(options.scrollState ?: owner.scroll)254
else -> options.scrollState255
}256
val bodyContent =257
element(258
ModalBodyProps(259
options,260
theme,261
bodyScroll,262
when (input.kind) {263
case Drawer(val value, _) ->264
value.side ==265
SheetSide.Start ||266
value.side ==267
SheetSide.End ||268
value.height !=269
None ||270
value.snapPoints.size >271
0L272
else -> true273
},274
when (input.kind) {275
case Drawer(val value, _) -> TouchScrollOptions(276
threshold = value.dragThreshold,277
claimAtBoundary = false278
)279
else -> TouchScrollOptions()280
}281
),282
{ current, mount -> ModalBody(current, scope = mount) },283
[input.content]284
)285
val body =286
when (input.kind) {287
case Drawer(_, val owner) -> element(288
owner,289
{ current, mount -> DrawerBodyMarker(current, scope = mount) },290
[bodyContent]291
)292
else -> bodyContent293
}294
val panelContent =295
element(296
ModalPanelProps(density, headerPresent, options.contentPadding),297
{ current, mount -> ModalPanel(current, scope = mount) },298
[header, body, footerView, closeView]299
)300
val panel =301
panelFrame(302
when (input.kind) {303
case Drawer(val props, val owner) -> drawerPanel(304
DrawerInput(props, theme, owner, overlayHandle),305
panelContent306
)307
else -> panelContent308
},309
visual =310
styleOverride {311
background(Some(Paint(palette.popover.background)))312
border(313
Some(314
modalPanelBorder(315
input.kind,316
componentPanelBorder(theme, palette.popover)317
)318
)319
)320
corners(321
when (input.kind) {322
case Drawer(val props, _) -> drawerCorners(323
props.side,324
12.0 * density325
)326
else -> Corners.all(327
if (sheet)328
0.0329
else330
12.0 * density331
)332
}333
)334
if (sheet)335
boxShadows(theme.tokens.effects.floating)336
}.then(options.style)337
)338
val motion =339
theme.tokens.motion.resolve(340
options.transition.resolve(341
Some(342
AnimationSpec.Tween(343
Duration.milliseconds(344
if (drawer)345
450L346
else347
if (sheet)348
200L349
else350
100L351
),352
if (drawer)353
Easing.cubicBezier(0.22, 1.0, 0.36, 1.0)354
else355
Easing.Linear356
)357
)358
)359
)360
val overlayFocus =361
if (alert && !panelPresent(options.focus.initialFocus))362
options.focus.copy { initialFocus = Some(cancelHandle) }363
else364
options.focus365
val current = read366
val exitMotion =367
if (drawer)368
theme.tokens.motion.resolve(369
options.transition.resolve(370
Some(371
AnimationSpec.Tween(372
Duration.milliseconds(400L),373
Easing.cubicBezier(0.22, 1.0, 0.36, 1.0)374
)375
)376
)377
)378
else379
motion380
val surface =381
when (input.kind) {382
case Drawer(val props, val owner) -> element(383
DrawerInput(props, theme, owner, overlayHandle),384
{ value, mount -> DrawerSurface(value, scope = mount) },385
[panel]386
)387
else -> element(388
ModalSurfaceProps(input, density),389
{ value, mount -> ModalSurface(value, scope = mount) },390
[panel]391
)392
}393
val overlayMode =394
when (input.kind) {395
case Drawer(val props, _) -> props.mode396
else -> OverlayMode.Modal397
}398
val semantics =399
options.semantics.copy {400
if (name == None && relations.labelledBy.size == 0L)401
name = semanticCaption(options.title)402
if (description == None)403
description = semanticCaption(options.description)404
reference =405
Some(options.semantics.reference ?: this @ModalContent.semanticReference)406
}407
val semanticSurface =408
semanticContent(409
surface,410
SemanticRole.Dialog,411
semantics.properties(412
enabled = options.enabled,413
modal = overlayMode == OverlayMode.Modal414
),415
actions = SemanticActions(dismiss = Some { dismissSemantically() }),416
relations = semantics.relations,417
mergeLabels = false,418
reference = semantics.reference419
)420
val popup =421
portal(binding({ read().open.value }, { next -> setOpen(next) }), semanticSurface) {422
placement = OverlayPlacement.Viewport(viewportPadding = 0.0)423
mode = overlayMode424
handle = Some(overlayHandle)425
focus = Some(overlayFocus)426
dismiss = input.dismiss427
transition = PresenceOptions(motion, exitMotion)428
onCloseRequest = { reason -> current().options.onCloseRequest(reason) }429
onClosed = { reason -> current().options.onClosed(reason) }430
}431
val trigger =432
when (options.trigger) {433
case Some(val spec) -> modalButton(434
spec,435
{ activate() },436
options,437
semantics =438
Some(439
spec.semantics.copy {440
relations =441
semanticControls(442
relations,443
(444
options.semantics.reference ?:445
this @ModalContent.semanticReference446
).id447
)448
}449
)450
)451
case None -> empty()452
}453
return element({ mount -> ModalTriggerLayout(scope = mount) }, [trigger, popup])454
}455
}457
private fun modalCaption(458
content: PanelContent?,459
style: TextStyle,460
fonts: FontCollection?,461
centered: Boolean = false462
): View =463
when (content) {464
case Some(val value) -> panelCaption(value, style, fonts, centered);465
case None -> empty()466
}468
private fun modalButton(469
input: PopupTriggerSpec,470
action: () -> Unit,471
options: ModalOptions,472
focus: FocusOptions? = None,473
semantics: ComponentSemantics? = None474
): View =475
component(476
ButtonProps(477
navigationLabel(478
input.content,479
if (panelPresent(input.fonts))480
input.fonts481
else482
options.fonts,483
TextAlignment.Center484
),485
action,486
input.enabled && options.enabled,487
input.style,488
options.theme,489
focus ?: input.focus,490
variant = input.variant,491
size = input.size,492
semantics = semantics ?: input.semantics493
),494
{ current, mount -> Button(current, scope = mount) }495
)497
private fun modalPanelBorder(kind: ModalKind, color: Color): Border =498
when (kind) {499
case Sheet(val side) -> modalSideBorder(side, color)500
case Drawer(val props, _) -> modalSideBorder(props.side, color)501
else -> Border.all(1.0, color)502
}504
private fun modalSideBorder(side: SheetSide, color: Color): Border {505
val edge = BorderEdge(1.0, color)506
return when (side) {507
case Top -> Border.Physical(BorderEdges(bottom = edge))508
case Bottom -> Border.Physical(BorderEdges(top = edge))509
case Start -> Border.Logical(end = edge)510
case End -> Border.Logical(start = edge)511
}512
}