Skip to content

Commit

Permalink
feat: Add revealable() modifiers with multiple keys (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenjacobs authored Mar 10, 2023
1 parent 6d839f6 commit c765b63
Showing 1 changed file with 81 additions and 9 deletions.
90 changes: 81 additions & 9 deletions reveal-core/src/main/kotlin/com/svenjacobs/reveal/RevealScope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,85 @@ public interface RevealScope {
shape: RevealShape = RevealShape.RoundRect(4.dp),
padding: PaddingValues = PaddingValues(8.dp),
): Modifier

/**
* Registers the element as a revealable item.
*
* Each key in [keys] must be unique in the current scope and should be used for
* [RevealState.reveal]. Internally [Modifier.onGloballyPositioned] is used. Hence elements are
* only registered after they have been laid out.
*
* If the element that this modifier is applied to leaves the composition while the reveal
* effect is shown for the element, the effect is finished.
*
* @param keys Unique keys to identify the revealable content. Also see documentation of [Key].
* @param shape Shape of the reveal effect around the element. Defaults to a rounded rect
* with a corner size of 4 dp.
* @param padding Additional padding around the reveal area. Positive values increase area while
* negative values decrease it. Defaults to 8 dp on all sides.
*
* @see Key
*/
public fun Modifier.revealable(
vararg keys: Key,
shape: RevealShape = RevealShape.RoundRect(4.dp),
padding: PaddingValues = PaddingValues(8.dp),
): Modifier

/**
* Registers the element as a revealable item.
*
* Each key specified in [keys] must be unique in the current scope and should be used for
* [RevealState.reveal]. Internally [Modifier.onGloballyPositioned] is used. Hence elements are
* only registered after they have been laid out.
*
* If the element that this modifier is applied to leaves the composition while the reveal
* effect is shown for the element, the effect is finished.
*
* @param keys Unique keys to identify the revealable content. Also see documentation of [Key].
* @param shape Shape of the reveal effect around the element. Defaults to a rounded rect
* with a corner size of 4 dp.
* @param padding Additional padding around the reveal area. Positive values increase area while
* negative values decrease it. Defaults to 8 dp on all sides.
*
* @see Key
*/
public fun Modifier.revealable(
keys: Iterable<Key>,
shape: RevealShape = RevealShape.RoundRect(4.dp),
padding: PaddingValues = PaddingValues(8.dp),
): Modifier
}

internal class RevealScopeInstance(
private val revealState: RevealState,
) : RevealScope {

override fun Modifier.revealable(key: Key, shape: RevealShape, padding: PaddingValues): Modifier =
this.then(
Modifier
.onGloballyPositioned { layoutCoordinates ->
revealable(
keys = listOf(key),
shape = shape,
padding = padding,
)

override fun Modifier.revealable(
vararg keys: Key,
shape: RevealShape,
padding: PaddingValues,
): Modifier = revealable(
keys = keys.toList(),
shape = shape,
padding = padding,
)

override fun Modifier.revealable(
keys: Iterable<Key>,
shape: RevealShape,
padding: PaddingValues,
): Modifier = this.then(
Modifier
.onGloballyPositioned { layoutCoordinates ->
for (key in keys) {
revealState.putRevealable(
Revealable(
key = key,
Expand All @@ -61,13 +130,16 @@ internal class RevealScopeInstance(
),
)
}
.composed {
DisposableEffect(Unit) {
onDispose {
}
.composed {
DisposableEffect(Unit) {
onDispose {
for (key in keys) {
revealState.removeRevealable(key)
}
}
this
},
)
}
this
},
)
}

0 comments on commit c765b63

Please sign in to comment.