|
| 1 | +package com.dbsystel.designsystem.components.card |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.border |
| 5 | +import androidx.compose.foundation.layout.padding |
| 6 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 7 | +import androidx.compose.material3.Surface |
| 8 | +import androidx.compose.material3.Text |
| 9 | +import androidx.compose.runtime.Composable |
| 10 | +import androidx.compose.ui.Modifier |
| 11 | +import androidx.compose.ui.graphics.Color |
| 12 | +import androidx.compose.ui.tooling.preview.PreviewLightDark |
| 13 | +import androidx.compose.ui.tooling.preview.PreviewParameter |
| 14 | +import androidx.compose.ui.tooling.preview.PreviewParameterProvider |
| 15 | +import androidx.compose.ui.unit.Dp |
| 16 | +import androidx.compose.ui.unit.dp |
| 17 | +import com.dbsystel.designsystem.foundation.theme.DesignSystemTheme |
| 18 | + |
| 19 | +/** |
| 20 | + * The card components are shaded and rounded areas that give the interface depth. |
| 21 | + * Whether static or interactive - the cards are the basis for further components and modules and can display content and actions. |
| 22 | + * |
| 23 | + * @param modifier - custom modifier of the card |
| 24 | + * @param elevation - set the background color elevation |
| 25 | + * @param spacing - padding of the card |
| 26 | + * @param content - content composable |
| 27 | + */ |
| 28 | +@Composable |
| 29 | +fun DSCard( |
| 30 | + modifier: Modifier = Modifier, |
| 31 | + elevation: DSCardElevation = DSCardElevation.LEVEL_1, |
| 32 | + spacing: DSCardSpacing = DSCardSpacing.SMALL, |
| 33 | + content: @Composable () -> Unit, |
| 34 | +) { |
| 35 | + val shape = RoundedCornerShape(DesignSystemTheme.dimensions.border.radiusSm) |
| 36 | + |
| 37 | + Surface( |
| 38 | + color = elevation.value(), |
| 39 | + tonalElevation = 0.dp, |
| 40 | + modifier = Modifier |
| 41 | + .background(color = elevation.value(), shape = shape) |
| 42 | + .border( |
| 43 | + DesignSystemTheme.dimensions.border.height3xs, |
| 44 | + color = DesignSystemTheme.activeColor.Basic.Border.Default.Default, |
| 45 | + shape = shape |
| 46 | + ) |
| 47 | + .padding(spacing.value()) |
| 48 | + .then(modifier), |
| 49 | + content = content |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +enum class DSCardSpacing(val value: @Composable () -> Dp) { |
| 54 | + SMALL({ DesignSystemTheme.dimensions.spacing.fixedSm }), |
| 55 | + MEDIUM({ DesignSystemTheme.dimensions.spacing.fixedMd }), |
| 56 | + LARGE({ DesignSystemTheme.dimensions.spacing.fixedLg }) |
| 57 | +} |
| 58 | + |
| 59 | +enum class DSCardElevation(val value: @Composable () -> Color) { |
| 60 | + LEVEL_1({ DesignSystemTheme.activeColor.bgBasicLevel1Default }), |
| 61 | + LEVEL_2({ DesignSystemTheme.activeColor.bgBasicLevel2Default }), |
| 62 | + LEVEL_3({ DesignSystemTheme.activeColor.bgBasicLevel3Default }) |
| 63 | +} |
| 64 | + |
| 65 | +private class DSCardPreviewProvider : PreviewParameterProvider<DSCardPreviewParameterType> { |
| 66 | + override val values = DSCardElevation.entries.flatMap { elevation -> |
| 67 | + DSCardSpacing.entries.map { spacing -> |
| 68 | + DSCardPreviewParameterType(spacing = spacing, elevation = elevation) |
| 69 | + } |
| 70 | + }.asSequence() |
| 71 | +} |
| 72 | + |
| 73 | +private class DSCardPreviewParameterType(val spacing: DSCardSpacing, val elevation: DSCardElevation) |
| 74 | + |
| 75 | +@Composable |
| 76 | +@PreviewLightDark |
| 77 | +private fun DSCardPreview( |
| 78 | + @PreviewParameter(DSCardPreviewProvider::class) previewType: DSCardPreviewParameterType, |
| 79 | +) { |
| 80 | + DesignSystemTheme { |
| 81 | + DSCard(spacing = previewType.spacing, elevation = previewType.elevation) { |
| 82 | + Text("Card content") |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments