Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2025 Adevinta
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.adevinta.spark.placeholder

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.adevinta.spark.DefaultTestDevices
import com.adevinta.spark.components.placeholder.textPlaceholder
import com.adevinta.spark.components.text.Text
import com.adevinta.spark.paparazziRule
import com.adevinta.spark.sparkDocSnapshot
import com.android.ide.common.rendering.api.SessionParams.RenderingMode.SHRINK
import org.junit.Rule
import org.junit.Test

internal class PlaceholderDocumentationScreenshots {

@get:Rule
val paparazzi = paparazziRule(
renderingMode = SHRINK,
deviceConfig = DefaultTestDevices.DocPhone,
)

@Test
fun textPlaceholder() = paparazzi.sparkDocSnapshot {
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
Text(
text = "Short",
modifier = Modifier.textPlaceholder(visible = true),
)
Text(
text = "A longer placeholder line",
modifier = Modifier.textPlaceholder(visible = true),
)
Text(
text = "Another distinct width here",
modifier = Modifier.textPlaceholder(visible = true),
)
Text(
text = "A multi-line placeholder that wraps across several lines so each " +
"text line draws its own squiggle instead of one stroke for the whole block",
modifier = Modifier.textPlaceholder(visible = true),
)
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (c) 2023 Adevinta
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.adevinta.spark.components.placeholder

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.StrokeJoin
import androidx.compose.ui.graphics.drawscope.Stroke
import kotlin.math.max
import kotlin.math.roundToInt
import kotlin.random.Random

private const val STROKE_WIDTH_FRACTION = 0.42f
private const val AMPLITUDE_FRACTION = 0.14f
private const val MIN_STROKE_WIDTH_PX = 3f

/**
* Builds the squiggle points for each text line, one inner list per line.
*
* The placeholder box is split into [lineCount] equal horizontal bands, one per text line. Each
* band holds one squiggle centred on the band. The seed is derived purely from the integer-pixel
* dimensions and the line index, so two placeholders with identical width, height and line count
* always produce the same geometry with no per-frame randomness, and each line differs from the
* others.
*
* - The first and last points of a line sit on the band centre line.
* - Interior points are displaced vertically by a seeded random fraction of the amplitude.
* - Segment count grows with the band aspect ratio, so wider placeholders have more waves.
*/
internal fun buildSquigglePoints(widthPx: Float, heightPx: Float, lineCount: Int): List<List<Offset>> {
val lines = max(1, lineCount)
val band = heightPx / lines
val amplitude = band * AMPLITUDE_FRACTION
return List(lines) { line ->
val seed = widthPx.roundToInt().toLong() * 100_000L + heightPx.roundToInt().toLong() * 31L + line
val rng = Random(seed)
val centreY = band * (line + 0.5f)
val segmentCount = max(2, (widthPx / band).roundToInt())
List(segmentCount + 1) { i ->
val x = i * widthPx / segmentCount
val y = when (i) {
0, segmentCount -> centreY
else -> centreY + (rng.nextFloat() * 2f - 1f) * amplitude
}
Offset(x, y)
}
}
}

/**
* Builds a smooth [Path] through the squiggle points using quadratic Bézier midpoint subdivision.
*
* The path holds one squiggle sub-path per text line. Each point acts as a Bézier control point;
* the midpoint between it and its successor is the on-curve destination. Each line finishes with a
* straight segment to its last point, which sits on the band centre line.
*/
internal fun buildSquigglePath(widthPx: Float, heightPx: Float, lineCount: Int): Path {
val lines = buildSquigglePoints(widthPx, heightPx, lineCount)
return Path().apply {
for (points in lines) {
moveTo(points.first().x, points.first().y)
for (i in 0 until points.size - 1) {
val midX = (points[i].x + points[i + 1].x) / 2f
val midY = (points[i].y + points[i + 1].y) / 2f
quadraticTo(points[i].x, points[i].y, midX, midY)
}
lineTo(points.last().x, points.last().y)
}
}
}

/**
* Returns a [Stroke] proportional to one text line height, with rounded caps and joins to
* reinforce the hand-drawn character of the squiggle.
*/
internal fun buildSquiggleStroke(lineHeightPx: Float): Stroke = Stroke(
width = max(MIN_STROKE_WIDTH_PX, lineHeightPx * STROKE_WIDTH_FRACTION),
cap = StrokeCap.Round,
join = StrokeJoin.Round,
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.tooling.preview.PreviewFontScale
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.unit.dp
import com.adevinta.spark.PreviewTheme
Expand Down Expand Up @@ -59,7 +60,9 @@ public fun Modifier.placeholder(visible: Boolean): Modifier = basePlaceholder(vi
/**
* Draws some skeleton UI which is typically used whilst content is 'loading'.
*
* The shape is not customizable as it's meant to display the same style for each text placeholders.
* The modifier draws a hand-drawn squiggle stroke. The geometry is derived only from the placeholder
* size: two placeholders with the same integer pixel width and height draw the same squiggle, and
* any size difference draws a different squiggle.
*
* A cross-fade transition will be applied to the content and placeholder UI when the [visible]
* value changes.
Expand All @@ -76,7 +79,7 @@ public fun Modifier.placeholder(visible: Boolean): Modifier = basePlaceholder(vi
*/
public fun Modifier.textPlaceholder(visible: Boolean): Modifier = basePlaceholder(
visible = visible,
useFullShape = true,
handDrawnText = true,
)

/**
Expand Down Expand Up @@ -109,6 +112,7 @@ public fun Modifier.illustrationPlaceholder(
)

@PreviewLightDark
@PreviewFontScale
@Composable
internal fun PreviewPlaceHolder() {
PreviewTheme {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ Box(

#### `Modifier.textPlaceholder`

Skeleton for text content. Uses `SparkTheme.shapes.full` (pill shape) to mirror the rounded
profile of text runs. Animation is a **fade**.
Skeleton for text content. Draws a hand-drawn squiggle stroke using `SparkTheme` colour tokens.
The geometry is derived deterministically from the placeholder size: two placeholders with the
same integer pixel width and height always draw the same stroke, and any size difference draws a
different stroke. Animation is a **fade**.

```kotlin
Text(
Expand Down
Loading
Loading