xref: /aosp_15_r20/external/accompanist/docs/placeholder.md (revision fa44fe6ae8e729aa3cfe5c03eedbbf98fb44e2c6)
1*fa44fe6aSInna Palant# Placeholder
2*fa44fe6aSInna Palant
3*fa44fe6aSInna Palant[![Maven Central](https://img.shields.io/maven-central/v/com.google.accompanist/accompanist-placeholder)](https://search.maven.org/search?q=g:com.google.accompanist)
4*fa44fe6aSInna Palant
5*fa44fe6aSInna Palant!!! warning
6*fa44fe6aSInna Palant    **This library is deprecated, and the API is no longer maintained. We recommend forking the implementation and customising it to your needs.** The original documentation is below.
7*fa44fe6aSInna Palant
8*fa44fe6aSInna PalantA library which provides a [modifier][modifier] for display 'placeholder' UI while content is loading.
9*fa44fe6aSInna Palant
10*fa44fe6aSInna PalantMore information on the UX provided by this library can be found on the Material Theming [Placeholder UI](https://material.io/design/communication/launch-screen.html#placeholder-ui) guidelines.
11*fa44fe6aSInna Palant
12*fa44fe6aSInna PalantThere are actually two versions of the library available:
13*fa44fe6aSInna Palant
14*fa44fe6aSInna Palant* *Placeholder Foundation*: Provides the base functionality and depends on Jetpack Compose Foundation. This version requires the app to provide all of the colors to display.
15*fa44fe6aSInna Palant* *Placeholder Material*. This uses the foundation library above, but also provides sensible default colors using your app's Material color palette.
16*fa44fe6aSInna Palant
17*fa44fe6aSInna Palant!!! tip
18*fa44fe6aSInna Palant    You only need to use one of the libraries, and most apps should use **Placeholder Material**. The APIs of the libraries are (mostly) equivalent with only the imports being different. Where possible we have provided equivalent code samples below.
19*fa44fe6aSInna Palant
20*fa44fe6aSInna Palant## Basic usage
21*fa44fe6aSInna Palant
22*fa44fe6aSInna PalantAt the most basic usage, the modifier will draw a shape over your composable content, filled with the provided color.
23*fa44fe6aSInna Palant
24*fa44fe6aSInna Palant![Basic Placeholder demo](basic.jpg)
25*fa44fe6aSInna Palant
26*fa44fe6aSInna Palant=== "Placeholder Material"
27*fa44fe6aSInna Palant
28*fa44fe6aSInna Palant    ``` kotlin
29*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.material.placeholder
30*fa44fe6aSInna Palant
31*fa44fe6aSInna Palant    Text(
32*fa44fe6aSInna Palant        text = "Content to display after content has loaded",
33*fa44fe6aSInna Palant        modifier = Modifier
34*fa44fe6aSInna Palant            .padding(16.dp)
35*fa44fe6aSInna Palant            .placeholder(visible = true)
36*fa44fe6aSInna Palant    )
37*fa44fe6aSInna Palant    ```
38*fa44fe6aSInna Palant
39*fa44fe6aSInna Palant=== "Placeholder Foundation"
40*fa44fe6aSInna Palant
41*fa44fe6aSInna Palant    ``` kotlin
42*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.placeholder
43*fa44fe6aSInna Palant
44*fa44fe6aSInna Palant    Text(
45*fa44fe6aSInna Palant        text = "Content to display after content has loaded",
46*fa44fe6aSInna Palant        modifier = Modifier
47*fa44fe6aSInna Palant            .padding(16.dp)
48*fa44fe6aSInna Palant            .placeholder(
49*fa44fe6aSInna Palant                visible = true,
50*fa44fe6aSInna Palant                color = Color.Gray,
51*fa44fe6aSInna Palant                // optional, defaults to RectangleShape
52*fa44fe6aSInna Palant                shape = RoundedCornerShape(4.dp),
53*fa44fe6aSInna Palant            )
54*fa44fe6aSInna Palant    )
55*fa44fe6aSInna Palant    ```
56*fa44fe6aSInna Palant
57*fa44fe6aSInna Palant## Placeholder highlights
58*fa44fe6aSInna Palant
59*fa44fe6aSInna PalantThe library also provides some 'highlight' animations to entertain the user while they are waiting. There are two provided by the library, but you can also provide your own.
60*fa44fe6aSInna Palant
61*fa44fe6aSInna Palant### Fade
62*fa44fe6aSInna Palant
63*fa44fe6aSInna PalantThis highlight fades a color over the entire placeholder in and out.
64*fa44fe6aSInna Palant
65*fa44fe6aSInna Palant<figure>
66*fa44fe6aSInna Palant    <video width="400" controls loop>
67*fa44fe6aSInna Palant    <source src="fade.mp4" type="video/mp4">
68*fa44fe6aSInna Palant        Your browser does not support the video tag.
69*fa44fe6aSInna Palant    </video>
70*fa44fe6aSInna Palant    <figcaption>Placeholder Fade demo</figcaption>
71*fa44fe6aSInna Palant</figure>
72*fa44fe6aSInna Palant
73*fa44fe6aSInna Palant=== "Placeholder Material"
74*fa44fe6aSInna Palant
75*fa44fe6aSInna Palant    ``` kotlin
76*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.PlaceholderHighlight
77*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.material.placeholder
78*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.material.fade
79*fa44fe6aSInna Palant
80*fa44fe6aSInna Palant    Text(
81*fa44fe6aSInna Palant        text = "Content to display after content has loaded",
82*fa44fe6aSInna Palant        modifier = Modifier
83*fa44fe6aSInna Palant            .padding(16.dp)
84*fa44fe6aSInna Palant            .placeholder(
85*fa44fe6aSInna Palant                visible = true,
86*fa44fe6aSInna Palant                highlight = PlaceholderHighlight.fade(),
87*fa44fe6aSInna Palant            )
88*fa44fe6aSInna Palant    )
89*fa44fe6aSInna Palant    ```
90*fa44fe6aSInna Palant
91*fa44fe6aSInna Palant=== "Placeholder Foundation"
92*fa44fe6aSInna Palant
93*fa44fe6aSInna Palant    ``` kotlin
94*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.PlaceholderHighlight
95*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.placeholder
96*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.fade
97*fa44fe6aSInna Palant
98*fa44fe6aSInna Palant    Text(
99*fa44fe6aSInna Palant        text = "Content to display after content has loaded",
100*fa44fe6aSInna Palant        modifier = Modifier
101*fa44fe6aSInna Palant            .padding(16.dp)
102*fa44fe6aSInna Palant            .placeholder(
103*fa44fe6aSInna Palant                visible = true,
104*fa44fe6aSInna Palant                color = Color.Gray,
105*fa44fe6aSInna Palant                // optional, defaults to RectangleShape
106*fa44fe6aSInna Palant                shape = RoundedCornerShape(4.dp),
107*fa44fe6aSInna Palant                highlight = PlaceholderHighlight.fade(
108*fa44fe6aSInna Palant                    highlightColor = Color.White,
109*fa44fe6aSInna Palant                ),
110*fa44fe6aSInna Palant            )
111*fa44fe6aSInna Palant    )
112*fa44fe6aSInna Palant    ```
113*fa44fe6aSInna Palant
114*fa44fe6aSInna Palant### Shimmer
115*fa44fe6aSInna Palant
116*fa44fe6aSInna PalantThis displays a gradient shimmer effect which emanates from the top-start corner.
117*fa44fe6aSInna Palant
118*fa44fe6aSInna Palant<figure>
119*fa44fe6aSInna Palant    <video width="400" controls loop>
120*fa44fe6aSInna Palant    <source src="shimmer.mp4" type="video/mp4">
121*fa44fe6aSInna Palant        Your browser does not support the video tag.
122*fa44fe6aSInna Palant    </video>
123*fa44fe6aSInna Palant    <figcaption>Placeholder Shimmer demo</figcaption>
124*fa44fe6aSInna Palant</figure>
125*fa44fe6aSInna Palant
126*fa44fe6aSInna Palant=== "Placeholder Material"
127*fa44fe6aSInna Palant
128*fa44fe6aSInna Palant    ``` kotlin
129*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.PlaceholderHighlight
130*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.material.placeholder
131*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.material.shimmer
132*fa44fe6aSInna Palant
133*fa44fe6aSInna Palant    Text(
134*fa44fe6aSInna Palant        text = "Content to display after content has loaded",
135*fa44fe6aSInna Palant        modifier = Modifier
136*fa44fe6aSInna Palant            .padding(16.dp)
137*fa44fe6aSInna Palant            .placeholder(
138*fa44fe6aSInna Palant                visible = true,
139*fa44fe6aSInna Palant                highlight = PlaceholderHighlight.shimmer(),
140*fa44fe6aSInna Palant            )
141*fa44fe6aSInna Palant    )
142*fa44fe6aSInna Palant    ```
143*fa44fe6aSInna Palant
144*fa44fe6aSInna Palant=== "Placeholder Foundation"
145*fa44fe6aSInna Palant
146*fa44fe6aSInna Palant    ``` kotlin
147*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.PlaceholderHighlight
148*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.placeholder
149*fa44fe6aSInna Palant    import com.google.accompanist.placeholder.shimmer
150*fa44fe6aSInna Palant
151*fa44fe6aSInna Palant    Text(
152*fa44fe6aSInna Palant        text = "Content to display after content has loaded",
153*fa44fe6aSInna Palant        modifier = Modifier
154*fa44fe6aSInna Palant            .padding(16.dp)
155*fa44fe6aSInna Palant            .placeholder(
156*fa44fe6aSInna Palant                visible = true,
157*fa44fe6aSInna Palant                color = Color.Gray,
158*fa44fe6aSInna Palant                // optional, defaults to RectangleShape
159*fa44fe6aSInna Palant                shape = RoundedCornerShape(4.dp),
160*fa44fe6aSInna Palant                highlight = PlaceholderHighlight.shimmer(
161*fa44fe6aSInna Palant                    highlightColor = Color.White,
162*fa44fe6aSInna Palant                ),
163*fa44fe6aSInna Palant            )
164*fa44fe6aSInna Palant    )
165*fa44fe6aSInna Palant    ```
166*fa44fe6aSInna Palant
167*fa44fe6aSInna Palant## Usage
168*fa44fe6aSInna Palant
169*fa44fe6aSInna Palant``` groovy
170*fa44fe6aSInna Palantrepositories {
171*fa44fe6aSInna Palant    mavenCentral()
172*fa44fe6aSInna Palant}
173*fa44fe6aSInna Palant
174*fa44fe6aSInna Palantdependencies {
175*fa44fe6aSInna Palant    // If you're using Material, use accompanist-placeholder-material
176*fa44fe6aSInna Palant    implementation "com.google.accompanist:accompanist-placeholder-material:<version>"
177*fa44fe6aSInna Palant
178*fa44fe6aSInna Palant    // Otherwise use the foundation version
179*fa44fe6aSInna Palant    implementation "com.google.accompanist:accompanist-placeholder:<version>"
180*fa44fe6aSInna Palant}
181*fa44fe6aSInna Palant```
182*fa44fe6aSInna Palant
183*fa44fe6aSInna Palant### Library Snapshots
184*fa44fe6aSInna Palant
185*fa44fe6aSInna PalantSnapshots of the current development version of this library are available, which track the latest commit. See [here](../using-snapshot-version) for more information on how to use them.
186*fa44fe6aSInna Palant
187*fa44fe6aSInna Palant---
188*fa44fe6aSInna Palant
189*fa44fe6aSInna Palant## Contributions
190*fa44fe6aSInna Palant
191*fa44fe6aSInna PalantPlease contribute! We will gladly review any pull requests.
192*fa44fe6aSInna PalantMake sure to read the [Contributing](../contributing) page first though.
193*fa44fe6aSInna Palant
194*fa44fe6aSInna Palant## License
195*fa44fe6aSInna Palant
196*fa44fe6aSInna Palant```
197*fa44fe6aSInna PalantCopyright 2021 The Android Open Source Project
198*fa44fe6aSInna Palant
199*fa44fe6aSInna PalantLicensed under the Apache License, Version 2.0 (the "License");
200*fa44fe6aSInna Palantyou may not use this file except in compliance with the License.
201*fa44fe6aSInna PalantYou may obtain a copy of the License at
202*fa44fe6aSInna Palant
203*fa44fe6aSInna Palant    https://www.apache.org/licenses/LICENSE-2.0
204*fa44fe6aSInna Palant
205*fa44fe6aSInna PalantUnless required by applicable law or agreed to in writing, software
206*fa44fe6aSInna Palantdistributed under the License is distributed on an "AS IS" BASIS,
207*fa44fe6aSInna PalantWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
208*fa44fe6aSInna PalantSee the License for the specific language governing permissions and
209*fa44fe6aSInna Palantlimitations under the License.
210*fa44fe6aSInna Palant```
211*fa44fe6aSInna Palant
212*fa44fe6aSInna Palant  [modifier]: https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier
213