diff --git a/core/src/main/java/com/terning/core/designsystem/component/modifier/ShadowModifier.kt b/core/src/main/java/com/terning/core/designsystem/component/modifier/ShadowModifier.kt new file mode 100644 index 000000000..ad17c3c77 --- /dev/null +++ b/core/src/main/java/com/terning/core/designsystem/component/modifier/ShadowModifier.kt @@ -0,0 +1,68 @@ +package com.terning.core.designsystem.component.modifier + +import android.graphics.BlurMaskFilter +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.composed +import androidx.compose.ui.draw.drawBehind +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.Paint +import androidx.compose.ui.graphics.drawscope.drawIntoCanvas +import androidx.compose.ui.graphics.toArgb +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.unit.Dp +import androidx.compose.ui.unit.dp +import com.terning.core.designsystem.theme.Grey200 + +@Composable +fun Modifier.customShadow( + color: Color = Grey200, + shadowRadius: Dp = 0.dp, + shadowWidth: Dp = 2.dp, + offsetX: Dp = 0.dp, + offsetY: Dp = 0.dp, +) = composed { + val paint: Paint = remember { Paint() } + val density = LocalDensity.current + val radius = shadowRadius + shadowWidth + val blurRadiusPx = with(density) { radius.toPx() } + val maskFilter = remember { + BlurMaskFilter(blurRadiusPx, BlurMaskFilter.Blur.NORMAL) + } + drawBehind { + drawIntoCanvas { canvas -> + val frameworkPaint = paint.asFrameworkPaint() + if (radius != 0.dp) { + frameworkPaint.maskFilter = maskFilter + } + frameworkPaint.color = color.toArgb() + + val leftPixel = offsetX.toPx() + val topPixel = offsetY.toPx() + val rightPixel = size.width + leftPixel + val bottomPixel = size.height + topPixel + + if (radius > 0.dp) { + val radiusPx = radius.toPx() + canvas.drawRoundRect( + left = leftPixel, + top = topPixel, + right = rightPixel, + bottom = bottomPixel, + radiusX = radiusPx, + radiusY = radiusPx, + paint = paint, + ) + } else { + canvas.drawRect( + left = leftPixel, + top = topPixel, + right = rightPixel, + bottom = bottomPixel, + paint = paint, + ) + } + } + } +}