|
| 1 | +package com.atls.hyperion.ui.theme.tokens.elevation.stories |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.clickable |
| 5 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 6 | +import androidx.compose.foundation.interaction.collectIsPressedAsState |
| 7 | +import androidx.compose.foundation.layout.* |
| 8 | +import androidx.compose.foundation.rememberScrollState |
| 9 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 10 | +import androidx.compose.foundation.verticalScroll |
| 11 | +import androidx.compose.material.Switch |
| 12 | +import androidx.compose.material.Text |
| 13 | +import androidx.compose.runtime.* |
| 14 | +import androidx.compose.ui.Alignment |
| 15 | +import androidx.compose.ui.Modifier |
| 16 | +import androidx.compose.ui.graphics.Color |
| 17 | +import androidx.compose.ui.graphics.Shape |
| 18 | +import com.atls.hyperion.storybook.shared.model.ComponentExample |
| 19 | +import com.atls.hyperion.ui.primitives.elevation |
| 20 | +import com.atls.hyperion.ui.theme.LocalHyperionColors |
| 21 | +import com.atls.hyperion.ui.theme.LocalHyperionElevations |
| 22 | +import com.atls.hyperion.ui.theme.Theme |
| 23 | +import com.atls.hyperion.ui.theme.tokens.elevation.Elevation |
| 24 | +import com.atls.hyperion.ui.theme.tokens.elevation.PressableElevationStates |
| 25 | +import com.atls.hyperion.ui.theme.tokens.layout.Radii |
| 26 | +import com.atls.hyperion.ui.theme.tokens.layout.Spacing |
| 27 | + |
| 28 | +class ElevationStory : ComponentExample { |
| 29 | + override val name: String = "Elevation" |
| 30 | + |
| 31 | + @Composable |
| 32 | + override fun Content() { |
| 33 | + var darkTheme by remember { mutableStateOf(false) } |
| 34 | + var focused by remember { mutableStateOf(false) } |
| 35 | + var enabled by remember { mutableStateOf(true) } |
| 36 | + |
| 37 | + Theme(darkTheme = darkTheme) { |
| 38 | + val colors = LocalHyperionColors.current |
| 39 | + val elevations = LocalHyperionElevations.current |
| 40 | + val pressableElevations = listOf( |
| 41 | + "xs" to elevations.xs, |
| 42 | + "sm" to elevations.sm, |
| 43 | + "md" to elevations.md, |
| 44 | + "lg" to elevations.lg |
| 45 | + ) |
| 46 | + |
| 47 | + Column( |
| 48 | + modifier = Modifier |
| 49 | + .fillMaxSize() |
| 50 | + .background(colors.surface.subtle) |
| 51 | + .verticalScroll(rememberScrollState()) |
| 52 | + .padding(Spacing.component.xl3), |
| 53 | + verticalArrangement = Arrangement.spacedBy(Spacing.component.xl3) |
| 54 | + ) { |
| 55 | + Toggle( |
| 56 | + label = "Dark theme", |
| 57 | + checked = darkTheme, |
| 58 | + textColor = colors.text.primary, |
| 59 | + onCheckedChange = { darkTheme = it } |
| 60 | + ) |
| 61 | + Toggle( |
| 62 | + label = "Focused", |
| 63 | + checked = focused, |
| 64 | + textColor = colors.text.primary, |
| 65 | + onCheckedChange = { focused = it } |
| 66 | + ) |
| 67 | + Toggle( |
| 68 | + label = "Enabled", |
| 69 | + checked = enabled, |
| 70 | + textColor = colors.text.primary, |
| 71 | + onCheckedChange = { enabled = it } |
| 72 | + ) |
| 73 | + |
| 74 | + pressableElevations.forEach { (label, states) -> |
| 75 | + ElevationCard( |
| 76 | + label = label, |
| 77 | + states = states, |
| 78 | + focused = focused, |
| 79 | + enabled = enabled, |
| 80 | + backgroundColor = colors.surface.base, |
| 81 | + textColor = colors.text.primary |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + StaticElevationCard( |
| 86 | + label = "modal · default", |
| 87 | + elevation = elevations.modal.default, |
| 88 | + backgroundColor = colors.surface.base, |
| 89 | + textColor = colors.text.primary |
| 90 | + ) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +@Composable |
| 97 | +private fun Toggle( |
| 98 | + label: String, |
| 99 | + checked: Boolean, |
| 100 | + textColor: Color, |
| 101 | + onCheckedChange: (Boolean) -> Unit |
| 102 | +) { |
| 103 | + Row( |
| 104 | + modifier = Modifier.fillMaxWidth(), |
| 105 | + verticalAlignment = Alignment.CenterVertically, |
| 106 | + horizontalArrangement = Arrangement.SpaceBetween |
| 107 | + ) { |
| 108 | + Text(label, color = textColor) |
| 109 | + Switch(checked = checked, onCheckedChange = onCheckedChange) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +@Composable |
| 114 | +private fun ElevationCard( |
| 115 | + label: String, |
| 116 | + states: PressableElevationStates, |
| 117 | + focused: Boolean, |
| 118 | + enabled: Boolean, |
| 119 | + backgroundColor: Color, |
| 120 | + textColor: Color |
| 121 | +) { |
| 122 | + val interactionSource = remember { MutableInteractionSource() } |
| 123 | + val pressed by interactionSource.collectIsPressedAsState() |
| 124 | + val state = when { |
| 125 | + !enabled -> "disabled" to states.disabled |
| 126 | + pressed -> "pressed" to states.pressed |
| 127 | + focused -> "focused" to states.focused |
| 128 | + else -> "default" to states.default |
| 129 | + } |
| 130 | + |
| 131 | + StaticElevationCard( |
| 132 | + label = "$label · ${state.first}", |
| 133 | + elevation = state.second, |
| 134 | + backgroundColor = backgroundColor, |
| 135 | + textColor = textColor, |
| 136 | + modifier = Modifier.clickable( |
| 137 | + interactionSource = interactionSource, |
| 138 | + indication = null, |
| 139 | + enabled = enabled, |
| 140 | + onClick = {} |
| 141 | + ) |
| 142 | + ) |
| 143 | +} |
| 144 | + |
| 145 | +@Composable |
| 146 | +private fun StaticElevationCard( |
| 147 | + label: String, |
| 148 | + elevation: Elevation, |
| 149 | + backgroundColor: Color, |
| 150 | + textColor: Color, |
| 151 | + modifier: Modifier = Modifier, |
| 152 | + shape: Shape = RoundedCornerShape(Radii.md) |
| 153 | +) { |
| 154 | + Box( |
| 155 | + modifier = Modifier |
| 156 | + .fillMaxWidth() |
| 157 | + .height(Spacing.layout.lg) |
| 158 | + .elevation( |
| 159 | + elevation = elevation, |
| 160 | + backgroundColor = backgroundColor, |
| 161 | + shape = shape |
| 162 | + ) |
| 163 | + .then(modifier), |
| 164 | + contentAlignment = Alignment.Center |
| 165 | + ) { |
| 166 | + Text(label, color = textColor) |
| 167 | + } |
| 168 | +} |
0 commit comments