xref: /aosp_15_r20/external/accompanist/docs/placeholder.md (revision fa44fe6ae8e729aa3cfe5c03eedbbf98fb44e2c6)
1# Placeholder
2
3[![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
5!!! warning
6    **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
8A library which provides a [modifier][modifier] for display 'placeholder' UI while content is loading.
9
10More 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
12There are actually two versions of the library available:
13
14* *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* *Placeholder Material*. This uses the foundation library above, but also provides sensible default colors using your app's Material color palette.
16
17!!! tip
18    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
20## Basic usage
21
22At the most basic usage, the modifier will draw a shape over your composable content, filled with the provided color.
23
24![Basic Placeholder demo](basic.jpg)
25
26=== "Placeholder Material"
27
28    ``` kotlin
29    import com.google.accompanist.placeholder.material.placeholder
30
31    Text(
32        text = "Content to display after content has loaded",
33        modifier = Modifier
34            .padding(16.dp)
35            .placeholder(visible = true)
36    )
37    ```
38
39=== "Placeholder Foundation"
40
41    ``` kotlin
42    import com.google.accompanist.placeholder.placeholder
43
44    Text(
45        text = "Content to display after content has loaded",
46        modifier = Modifier
47            .padding(16.dp)
48            .placeholder(
49                visible = true,
50                color = Color.Gray,
51                // optional, defaults to RectangleShape
52                shape = RoundedCornerShape(4.dp),
53            )
54    )
55    ```
56
57## Placeholder highlights
58
59The 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
61### Fade
62
63This highlight fades a color over the entire placeholder in and out.
64
65<figure>
66    <video width="400" controls loop>
67    <source src="fade.mp4" type="video/mp4">
68        Your browser does not support the video tag.
69    </video>
70    <figcaption>Placeholder Fade demo</figcaption>
71</figure>
72
73=== "Placeholder Material"
74
75    ``` kotlin
76    import com.google.accompanist.placeholder.PlaceholderHighlight
77    import com.google.accompanist.placeholder.material.placeholder
78    import com.google.accompanist.placeholder.material.fade
79
80    Text(
81        text = "Content to display after content has loaded",
82        modifier = Modifier
83            .padding(16.dp)
84            .placeholder(
85                visible = true,
86                highlight = PlaceholderHighlight.fade(),
87            )
88    )
89    ```
90
91=== "Placeholder Foundation"
92
93    ``` kotlin
94    import com.google.accompanist.placeholder.PlaceholderHighlight
95    import com.google.accompanist.placeholder.placeholder
96    import com.google.accompanist.placeholder.fade
97
98    Text(
99        text = "Content to display after content has loaded",
100        modifier = Modifier
101            .padding(16.dp)
102            .placeholder(
103                visible = true,
104                color = Color.Gray,
105                // optional, defaults to RectangleShape
106                shape = RoundedCornerShape(4.dp),
107                highlight = PlaceholderHighlight.fade(
108                    highlightColor = Color.White,
109                ),
110            )
111    )
112    ```
113
114### Shimmer
115
116This displays a gradient shimmer effect which emanates from the top-start corner.
117
118<figure>
119    <video width="400" controls loop>
120    <source src="shimmer.mp4" type="video/mp4">
121        Your browser does not support the video tag.
122    </video>
123    <figcaption>Placeholder Shimmer demo</figcaption>
124</figure>
125
126=== "Placeholder Material"
127
128    ``` kotlin
129    import com.google.accompanist.placeholder.PlaceholderHighlight
130    import com.google.accompanist.placeholder.material.placeholder
131    import com.google.accompanist.placeholder.material.shimmer
132
133    Text(
134        text = "Content to display after content has loaded",
135        modifier = Modifier
136            .padding(16.dp)
137            .placeholder(
138                visible = true,
139                highlight = PlaceholderHighlight.shimmer(),
140            )
141    )
142    ```
143
144=== "Placeholder Foundation"
145
146    ``` kotlin
147    import com.google.accompanist.placeholder.PlaceholderHighlight
148    import com.google.accompanist.placeholder.placeholder
149    import com.google.accompanist.placeholder.shimmer
150
151    Text(
152        text = "Content to display after content has loaded",
153        modifier = Modifier
154            .padding(16.dp)
155            .placeholder(
156                visible = true,
157                color = Color.Gray,
158                // optional, defaults to RectangleShape
159                shape = RoundedCornerShape(4.dp),
160                highlight = PlaceholderHighlight.shimmer(
161                    highlightColor = Color.White,
162                ),
163            )
164    )
165    ```
166
167## Usage
168
169``` groovy
170repositories {
171    mavenCentral()
172}
173
174dependencies {
175    // If you're using Material, use accompanist-placeholder-material
176    implementation "com.google.accompanist:accompanist-placeholder-material:<version>"
177
178    // Otherwise use the foundation version
179    implementation "com.google.accompanist:accompanist-placeholder:<version>"
180}
181```
182
183### Library Snapshots
184
185Snapshots 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
187---
188
189## Contributions
190
191Please contribute! We will gladly review any pull requests.
192Make sure to read the [Contributing](../contributing) page first though.
193
194## License
195
196```
197Copyright 2021 The Android Open Source Project
198
199Licensed under the Apache License, Version 2.0 (the "License");
200you may not use this file except in compliance with the License.
201You may obtain a copy of the License at
202
203    https://www.apache.org/licenses/LICENSE-2.0
204
205Unless required by applicable law or agreed to in writing, software
206distributed under the License is distributed on an "AS IS" BASIS,
207WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
208See the License for the specific language governing permissions and
209limitations under the License.
210```
211
212  [modifier]: https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier
213