Skip to content

Commit c765b63

Browse files
authored
feat: Add revealable() modifiers with multiple keys (#34)
1 parent 6d839f6 commit c765b63

File tree

1 file changed

+81
-9
lines changed

1 file changed

+81
-9
lines changed

reveal-core/src/main/kotlin/com/svenjacobs/reveal/RevealScope.kt

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,85 @@ public interface RevealScope {
3939
shape: RevealShape = RevealShape.RoundRect(4.dp),
4040
padding: PaddingValues = PaddingValues(8.dp),
4141
): Modifier
42+
43+
/**
44+
* Registers the element as a revealable item.
45+
*
46+
* Each key in [keys] must be unique in the current scope and should be used for
47+
* [RevealState.reveal]. Internally [Modifier.onGloballyPositioned] is used. Hence elements are
48+
* only registered after they have been laid out.
49+
*
50+
* If the element that this modifier is applied to leaves the composition while the reveal
51+
* effect is shown for the element, the effect is finished.
52+
*
53+
* @param keys Unique keys to identify the revealable content. Also see documentation of [Key].
54+
* @param shape Shape of the reveal effect around the element. Defaults to a rounded rect
55+
* with a corner size of 4 dp.
56+
* @param padding Additional padding around the reveal area. Positive values increase area while
57+
* negative values decrease it. Defaults to 8 dp on all sides.
58+
*
59+
* @see Key
60+
*/
61+
public fun Modifier.revealable(
62+
vararg keys: Key,
63+
shape: RevealShape = RevealShape.RoundRect(4.dp),
64+
padding: PaddingValues = PaddingValues(8.dp),
65+
): Modifier
66+
67+
/**
68+
* Registers the element as a revealable item.
69+
*
70+
* Each key specified in [keys] must be unique in the current scope and should be used for
71+
* [RevealState.reveal]. Internally [Modifier.onGloballyPositioned] is used. Hence elements are
72+
* only registered after they have been laid out.
73+
*
74+
* If the element that this modifier is applied to leaves the composition while the reveal
75+
* effect is shown for the element, the effect is finished.
76+
*
77+
* @param keys Unique keys to identify the revealable content. Also see documentation of [Key].
78+
* @param shape Shape of the reveal effect around the element. Defaults to a rounded rect
79+
* with a corner size of 4 dp.
80+
* @param padding Additional padding around the reveal area. Positive values increase area while
81+
* negative values decrease it. Defaults to 8 dp on all sides.
82+
*
83+
* @see Key
84+
*/
85+
public fun Modifier.revealable(
86+
keys: Iterable<Key>,
87+
shape: RevealShape = RevealShape.RoundRect(4.dp),
88+
padding: PaddingValues = PaddingValues(8.dp),
89+
): Modifier
4290
}
4391

4492
internal class RevealScopeInstance(
4593
private val revealState: RevealState,
4694
) : RevealScope {
4795

4896
override fun Modifier.revealable(key: Key, shape: RevealShape, padding: PaddingValues): Modifier =
49-
this.then(
50-
Modifier
51-
.onGloballyPositioned { layoutCoordinates ->
97+
revealable(
98+
keys = listOf(key),
99+
shape = shape,
100+
padding = padding,
101+
)
102+
103+
override fun Modifier.revealable(
104+
vararg keys: Key,
105+
shape: RevealShape,
106+
padding: PaddingValues,
107+
): Modifier = revealable(
108+
keys = keys.toList(),
109+
shape = shape,
110+
padding = padding,
111+
)
112+
113+
override fun Modifier.revealable(
114+
keys: Iterable<Key>,
115+
shape: RevealShape,
116+
padding: PaddingValues,
117+
): Modifier = this.then(
118+
Modifier
119+
.onGloballyPositioned { layoutCoordinates ->
120+
for (key in keys) {
52121
revealState.putRevealable(
53122
Revealable(
54123
key = key,
@@ -61,13 +130,16 @@ internal class RevealScopeInstance(
61130
),
62131
)
63132
}
64-
.composed {
65-
DisposableEffect(Unit) {
66-
onDispose {
133+
}
134+
.composed {
135+
DisposableEffect(Unit) {
136+
onDispose {
137+
for (key in keys) {
67138
revealState.removeRevealable(key)
68139
}
69140
}
70-
this
71-
},
72-
)
141+
}
142+
this
143+
},
144+
)
73145
}

0 commit comments

Comments
 (0)