Skip to content

Commit 111c5b4

Browse files
authored
Merge pull request #762 from atls/feat/mobile-elevation
Feat/mobile elevation
2 parents 1e33c8e + e81f508 commit 111c5b4

13 files changed

Lines changed: 430 additions & 4 deletions

File tree

‎mobile/kmp/sample/src/commonMain/kotlin/com/atls/hyperion/sample/App.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.atls.hyperion.ui.fragments.datepicker.stories.DateRangePickerStory
2525
import com.atls.hyperion.ui.primitives.stories.LinkStory
2626
import com.atls.hyperion.ui.primitives.stories.TextStory
2727
import com.atls.hyperion.ui.theme.Theme
28+
import com.atls.hyperion.ui.theme.tokens.elevation.stories.ElevationStory
2829

2930
@Composable
3031
fun App() {
@@ -41,6 +42,7 @@ fun App() {
4142
DatePickerStory(),
4243
DateRangePickerStory(),
4344
DividerStory(),
45+
ElevationStory(),
4446
InputStory(),
4547
PaginationStory(),
4648
PlaceholderStory(),
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.atls.hyperion.ui.primitives
2+
3+
import androidx.compose.foundation.background
4+
import androidx.compose.ui.Modifier
5+
import androidx.compose.ui.draw.dropShadow
6+
import androidx.compose.ui.draw.innerShadow
7+
import androidx.compose.ui.graphics.Color
8+
import androidx.compose.ui.graphics.RectangleShape
9+
import androidx.compose.ui.graphics.Shape
10+
import androidx.compose.ui.graphics.shadow.Shadow
11+
import androidx.compose.ui.unit.DpOffset
12+
import com.atls.hyperion.ui.theme.tokens.elevation.Elevation
13+
import com.atls.hyperion.ui.theme.tokens.elevation.ShadowType
14+
15+
fun Modifier.elevation(
16+
elevation: Elevation,
17+
backgroundColor: Color,
18+
shape: Shape = RectangleShape
19+
): Modifier {
20+
val withDropShadows = elevation.shadows.fold(this) { modifier, shadow ->
21+
if (shadow.type == ShadowType.Drop) {
22+
modifier.dropShadow(
23+
shape = shape,
24+
shadow = shadow.toComposeShadow()
25+
)
26+
} else {
27+
modifier
28+
}
29+
}
30+
31+
val withBackground = withDropShadows.background(backgroundColor, shape)
32+
33+
return elevation.shadows.fold(withBackground) { modifier, shadow ->
34+
if (shadow.type == ShadowType.Inner) {
35+
modifier.innerShadow(
36+
shape = shape,
37+
shadow = shadow.toComposeShadow()
38+
)
39+
} else {
40+
modifier
41+
}
42+
}
43+
}
44+
45+
private fun com.atls.hyperion.ui.theme.tokens.elevation.Shadow.toComposeShadow() = Shadow(
46+
radius = blur,
47+
spread = spread,
48+
offset = DpOffset(offsetX, offsetY),
49+
color = color
50+
)

‎mobile/kmp/ui/src/commonMain/kotlin/com/atls/hyperion/ui/theme/Theme.kt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import com.atls.hyperion.ui.theme.tokens.borders.Borders
88
import com.atls.hyperion.ui.theme.tokens.colors.Colors
99
import com.atls.hyperion.ui.theme.tokens.colors.darkColors
1010
import com.atls.hyperion.ui.theme.tokens.colors.lightColors
11+
import com.atls.hyperion.ui.theme.tokens.elevation.Elevations
1112
import com.atls.hyperion.ui.theme.typography.TextStyles
1213
import com.atls.hyperion.ui.theme.typography.fontFamilies.ScienceGothicFontFamily
1314

1415
val LocalHyperionColors = staticCompositionLocalOf { lightColors }
1516
val LocalHyperionBorders = staticCompositionLocalOf { Borders(lightColors) }
17+
val LocalHyperionElevations = staticCompositionLocalOf { Elevations(lightColors) }
1618
val LocalHyperionTypography = staticCompositionLocalOf { TextStyles(fontFamily = FontFamily.Default) }
1719

1820
@Composable
@@ -22,11 +24,13 @@ fun Theme(
2224
content: @Composable () -> Unit
2325
) {
2426
val borders = Borders(colors)
27+
val elevations = Elevations(colors)
2528
val typography = TextStyles(fontFamily = ScienceGothicFontFamily())
2629

2730
CompositionLocalProvider(
2831
LocalHyperionColors provides colors,
2932
LocalHyperionBorders provides borders,
33+
LocalHyperionElevations provides elevations,
3034
LocalHyperionTypography provides typography
3135
) {
3236
content()

‎mobile/kmp/ui/src/commonMain/kotlin/com/atls/hyperion/ui/theme/tokens/colors/Dark.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ val darkColors = Colors(
88
hover = Color(0xFF7AA7FF),
99
pressed = Color(0xFF3F74FF),
1010
disabled = Color(0xFF5F8FFF),
11-
subtle = Color(0xFF394A6B)
11+
subtle = Color(0x4D5F8FFF)
1212
),
1313
surface = SurfaceColors(
1414
base = Color(0xFF111111),

‎mobile/kmp/ui/src/commonMain/kotlin/com/atls/hyperion/ui/theme/tokens/colors/Light.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ val lightColors = Colors(
88
hover = Color(0xFF1643A3),
99
pressed = Color(0xFF12357F),
1010
disabled = Color(0x401E56C7),
11-
subtle = Color(0xFF6B86C8)
11+
subtle = Color(0x401E56C7)
1212
),
1313
surface = SurfaceColors(
1414
base = Color(0xFFF8F9FF),
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.atls.hyperion.ui.theme.tokens.elevation
2+
3+
import androidx.compose.ui.unit.dp
4+
5+
object ShadowBlur {
6+
val xs5 = 1.dp
7+
val xs4 = 2.dp
8+
val xs3 = 3.dp
9+
val xs2 = 4.dp
10+
val xs = 5.dp
11+
val sm = 6.dp
12+
val md = 8.dp
13+
val lg = 12.dp
14+
val xl = 14.dp
15+
val xl2 = 16.dp
16+
val xl3 = 20.dp
17+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package com.atls.hyperion.ui.theme.tokens.elevation
2+
3+
import androidx.compose.ui.graphics.Color
4+
import androidx.compose.ui.unit.Dp
5+
import com.atls.hyperion.ui.theme.tokens.colors.Colors
6+
7+
class Elevations(colors: Colors) {
8+
val xs: PressableElevationStates = Xs(colors.elevation.xs)
9+
val sm: PressableElevationStates = Sm(colors.elevation.sm)
10+
val md: PressableElevationStates = Md(colors.elevation.md)
11+
val lg: PressableElevationStates = Lg(colors.elevation.lg)
12+
val modal: ElevationStates = Modal(colors.elevation.md)
13+
14+
private class Xs(color: Color) : PressableElevationStates {
15+
override val default = elevation(
16+
drop(color, offsetY = ShadowOffset.xs, blur = ShadowBlur.xs4)
17+
)
18+
override val pressed = elevation(
19+
inner(color, offsetY = ShadowOffset.xs, blur = ShadowBlur.xs5),
20+
drop(
21+
color,
22+
offsetY = ShadowOffset.xs,
23+
blur = ShadowBlur.xs5,
24+
spread = ShadowSpread.negativeXs
25+
)
26+
)
27+
override val disabled = elevation(
28+
drop(color, offsetY = ShadowOffset.xs, blur = ShadowBlur.xs5)
29+
)
30+
}
31+
32+
private class Sm(color: Color) : PressableElevationStates {
33+
override val default = elevation(
34+
drop(color, offsetY = ShadowOffset.sm, blur = ShadowBlur.sm)
35+
)
36+
override val pressed = elevation(
37+
inner(color, offsetY = ShadowOffset.xs, blur = ShadowBlur.xs4),
38+
drop(
39+
color,
40+
offsetY = ShadowOffset.xs,
41+
blur = ShadowBlur.xs3,
42+
spread = ShadowSpread.negativeSm
43+
)
44+
)
45+
override val disabled = elevation(
46+
drop(color, offsetY = ShadowOffset.xs, blur = ShadowBlur.xs4)
47+
)
48+
}
49+
50+
private class Md(color: Color) : PressableElevationStates {
51+
override val default = elevation(
52+
drop(color, offsetY = ShadowOffset.lg, blur = ShadowBlur.lg)
53+
)
54+
override val pressed = elevation(
55+
inner(color, offsetY = ShadowOffset.xs, blur = ShadowBlur.xs3),
56+
drop(
57+
color,
58+
offsetY = ShadowOffset.sm,
59+
blur = ShadowBlur.sm,
60+
spread = ShadowSpread.negativeXs
61+
)
62+
)
63+
override val disabled = elevation(
64+
drop(color, offsetY = ShadowOffset.sm, blur = ShadowBlur.xs)
65+
)
66+
}
67+
68+
private class Lg(color: Color) : PressableElevationStates {
69+
override val default = elevation(
70+
drop(
71+
color,
72+
offsetY = ShadowOffset.xl,
73+
blur = ShadowBlur.lg,
74+
spread = ShadowSpread.negativeLg
75+
)
76+
)
77+
override val pressed = elevation(
78+
inner(color, offsetY = ShadowOffset.sm, blur = ShadowBlur.xs2),
79+
drop(
80+
color,
81+
offsetY = ShadowOffset.md,
82+
blur = ShadowBlur.md,
83+
spread = ShadowSpread.negativeLg
84+
)
85+
)
86+
override val disabled = elevation(
87+
drop(
88+
color,
89+
offsetY = ShadowOffset.md,
90+
blur = ShadowBlur.sm,
91+
spread = ShadowSpread.negativeMd
92+
)
93+
)
94+
}
95+
96+
private class Modal(color: Color) : ElevationStates {
97+
override val default = elevation(
98+
drop(
99+
color,
100+
offsetY = ShadowOffset.xl3,
101+
blur = ShadowBlur.xl3,
102+
spread = ShadowSpread.negativeSm
103+
)
104+
)
105+
}
106+
}
107+
108+
private fun elevation(vararg shadows: Shadow) = Elevation(shadows.toList())
109+
110+
private fun drop(
111+
color: Color,
112+
offsetX: Dp = ShadowOffset.none,
113+
offsetY: Dp,
114+
blur: Dp,
115+
spread: Dp = ShadowSpread.none
116+
) = Shadow(
117+
type = ShadowType.Drop,
118+
offsetX = offsetX,
119+
offsetY = offsetY,
120+
blur = blur,
121+
spread = spread,
122+
color = color
123+
)
124+
125+
private fun inner(
126+
color: Color,
127+
offsetY: Dp,
128+
blur: Dp
129+
) = Shadow(
130+
type = ShadowType.Inner,
131+
offsetX = ShadowOffset.none,
132+
offsetY = offsetY,
133+
blur = blur,
134+
spread = ShadowSpread.none,
135+
color = color
136+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.atls.hyperion.ui.theme.tokens.elevation
2+
3+
import androidx.compose.runtime.Immutable
4+
import androidx.compose.ui.graphics.Color
5+
import androidx.compose.ui.unit.Dp
6+
7+
data class Elevation(
8+
val shadows: List<Shadow>
9+
)
10+
11+
@Immutable
12+
data class Shadow(
13+
val type: ShadowType,
14+
val offsetX: Dp,
15+
val offsetY: Dp,
16+
val blur: Dp,
17+
val spread: Dp,
18+
val color: Color
19+
)
20+
21+
enum class ShadowType {
22+
Drop,
23+
Inner
24+
}
25+
26+
interface ElevationStates {
27+
val default: Elevation
28+
}
29+
30+
interface PressableElevationStates : ElevationStates {
31+
val pressed: Elevation
32+
val disabled: Elevation
33+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.atls.hyperion.ui.theme.tokens.elevation
2+
3+
import androidx.compose.ui.unit.dp
4+
5+
object ShadowOffset {
6+
val none = 0.dp
7+
val xs = 1.dp
8+
val sm = 2.dp
9+
val md = 3.dp
10+
val lg = 4.dp
11+
val xl = 6.dp
12+
val xl2 = 7.dp
13+
val xl3 = 12.dp
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.atls.hyperion.ui.theme.tokens.elevation
2+
3+
import androidx.compose.ui.unit.dp
4+
5+
object ShadowSpread {
6+
val negativeLg = (-4).dp
7+
val negativeMd = (-3).dp
8+
val negativeSm = (-2).dp
9+
val negativeXs = (-1).dp
10+
val none = 0.dp
11+
val xs = 1.dp
12+
}

0 commit comments

Comments
 (0)