This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChip.kt
2291 lines (2178 loc) · 98.5 KB
/
Chip.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.compose.material3
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.VectorConverter
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.interaction.DragInteraction
import androidx.compose.foundation.interaction.FocusInteraction
import androidx.compose.foundation.interaction.HoverInteraction
import androidx.compose.foundation.interaction.Interaction
import androidx.compose.foundation.interaction.InteractionSource
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.PressInteraction
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.tokens.AssistChipTokens
import androidx.compose.material3.tokens.FilterChipTokens
import androidx.compose.material3.tokens.InputChipTokens
import androidx.compose.material3.tokens.SuggestionChipTokens
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
/**
* <a href="https://m3.material.io/components/chips/overview" class="external" target="_blank">Material Design assist chip</a>.
*
* Chips help people enter information, make selections, filter content, or trigger actions. Chips
* can show multiple interactive elements together in the same area, such as a list of selectable
* movie times, or a series of email contacts.
*
* Assist chips represent smart or automated actions that can span multiple apps, such as opening a
* calendar event from the home screen. Assist chips function as though the user asked an assistant
* to complete the action. They should appear dynamically and contextually in a UI.
*
* ![Assist chip image](https://developer.android.com/images/reference/androidx/compose/material3/assist-chip.png)
*
* This assist chip is applied with a flat style. If you want an elevated style, use the
* [ElevatedAssistChip].
*
* Example of a flat AssistChip:
* @sample androidx.compose.material3.samples.AssistChipSample
*
* @param onClick called when this chip is clicked
* @param label text label for this chip
* @param modifier the [Modifier] to be applied to this chip
* @param enabled controls the enabled state of this chip. When `false`, this component will not
* respond to user input, and it will appear visually disabled and disabled to accessibility
* services.
* @param leadingIcon optional icon at the start of the chip, preceding the [label] text
* @param trailingIcon optional icon at the end of the chip
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this chip. You can create and pass in your own `remember`ed instance to observe
* [Interaction]s and customize the appearance / behavior of this chip in different states.
* @param elevation [ChipElevation] used to resolve the elevation for this chip in different states.
* This controls the size of the shadow below the chip. Additionally, when the container color is
* [ColorScheme.surface], this controls the amount of primary color applied as an overlay. See
* [AssistChipDefaults.assistChipElevation].
* @param shape defines the shape of this chip's container, border (when [border] is not null), and
* shadow (when using [elevation])
* @param border the border to draw around the container of this chip. Pass `null` for no border.
* See [AssistChipDefaults.assistChipBorder].
* @param colors [ChipColors] that will be used to resolve the colors used for this chip in
* different states. See [AssistChipDefaults.assistChipColors].
*/
@ExperimentalMaterial3Api
@Composable
fun AssistChip(
onClick: () -> Unit,
label: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
leadingIcon: @Composable (() -> Unit)? = null,
trailingIcon: @Composable (() -> Unit)? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
elevation: ChipElevation? = AssistChipDefaults.assistChipElevation(),
shape: Shape = AssistChipDefaults.Shape,
border: ChipBorder? = AssistChipDefaults.assistChipBorder(),
colors: ChipColors = AssistChipDefaults.assistChipColors()
) = Chip(
modifier = modifier,
onClick = onClick,
enabled = enabled,
label = label,
labelTextStyle = MaterialTheme.typography.fromToken(AssistChipTokens.LabelTextFont),
labelColor = colors.labelColor(enabled).value,
leadingIcon = leadingIcon,
trailingIcon = trailingIcon,
elevation = elevation,
colors = colors,
minHeight = AssistChipDefaults.Height,
paddingValues = AssistChipPadding,
shape = shape,
border = border?.borderStroke(enabled)?.value,
interactionSource = interactionSource
)
/**
* <a href="https://m3.material.io/components/chips/overview" class="external" target="_blank">Material Design elevated assist chip</a>.
*
* Chips help people enter information, make selections, filter content, or trigger actions. Chips
* can show multiple interactive elements together in the same area, such as a list of selectable
* movie times, or a series of email contacts.
*
* Assist chips represent smart or automated actions that can span multiple apps, such as opening a
* calendar event from the home screen. Assist chips function as though the user asked an assistant
* to complete the action. They should appear dynamically and contextually in a UI.
*
* ![Assist chip image](https://developer.android.com/images/reference/androidx/compose/material3/elevated-assist-chip.png)
*
* This assist chip is applied with an elevated style. If you want a flat style, use the
* [AssistChip].
*
* Example of an elevated AssistChip with a trailing icon:
* @sample androidx.compose.material3.samples.ElevatedAssistChipSample
*
* @param onClick called when this chip is clicked
* @param label text label for this chip
* @param modifier the [Modifier] to be applied to this chip
* @param enabled controls the enabled state of this chip. When `false`, this component will not
* respond to user input, and it will appear visually disabled and disabled to accessibility
* services.
* @param leadingIcon optional icon at the start of the chip, preceding the [label] text
* @param trailingIcon optional icon at the end of the chip
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this chip. You can create and pass in your own `remember`ed instance to observe
* [Interaction]s and customize the appearance / behavior of this chip in different states.
* @param elevation [ChipElevation] used to resolve the elevation for this chip in different states.
* This controls the size of the shadow below the chip. Additionally, when the container color is
* [ColorScheme.surface], this controls the amount of primary color applied as an overlay. See
* [AssistChipDefaults.elevatedAssistChipElevation].
* @param shape defines the shape of this chip's container, border (when [border] is not null), and
* shadow (when using [elevation])
* @param border the border to draw around the container of this chip
* @param colors [ChipColors] that will be used to resolve the colors used for this chip in
* different states. See [AssistChipDefaults.elevatedAssistChipColors].
*/
@ExperimentalMaterial3Api
@Composable
fun ElevatedAssistChip(
onClick: () -> Unit,
label: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
leadingIcon: @Composable (() -> Unit)? = null,
trailingIcon: @Composable (() -> Unit)? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
elevation: ChipElevation? = AssistChipDefaults.elevatedAssistChipElevation(),
shape: Shape = AssistChipDefaults.Shape,
border: ChipBorder? = null,
colors: ChipColors = AssistChipDefaults.elevatedAssistChipColors()
) = Chip(
modifier = modifier,
onClick = onClick,
enabled = enabled,
label = label,
labelTextStyle = MaterialTheme.typography.fromToken(AssistChipTokens.LabelTextFont),
labelColor = colors.labelColor(enabled).value,
leadingIcon = leadingIcon,
trailingIcon = trailingIcon,
elevation = elevation,
colors = colors,
minHeight = AssistChipDefaults.Height,
paddingValues = AssistChipPadding,
shape = shape,
border = border?.borderStroke(enabled)?.value,
interactionSource = interactionSource
)
/**
* <a href="https://m3.material.io/components/chips/overview" class="external" target="_blank">Material Design filter chip</a>.
*
* Chips help people enter information, make selections, filter content, or trigger actions. Chips
* can show multiple interactive elements together in the same area, such as a list of selectable
* movie times, or a series of email contacts.
*
* Filter chips use tags or descriptive words to filter content. They can be a good alternative to
* toggle buttons or checkboxes.
*
* ![Filter chip image](https://developer.android.com/images/reference/androidx/compose/material3/filter-chip.png)
*
* This filter chip is applied with a flat style. If you want an elevated style, use the
* [ElevatedFilterChip].
*
* Tapping on a filter chip selects it, and in case a [selectedIcon] is provided (e.g. a checkmark),
* it's appended to the starting edge of the chip's label, drawn instead of any given [leadingIcon].
*
* Example of a flat FilterChip with a trailing icon:
* @sample androidx.compose.material3.samples.FilterChipSample
*
* Example of a FilterChip with both a leading icon and a selected icon:
* @sample androidx.compose.material3.samples.FilterChipWithLeadingIconSample
*
* @param selected whether this chip is selected or not
* @param onClick called when this chip is clicked
* @param label text label for this chip
* @param modifier the [Modifier] to be applied to this chip
* @param enabled controls the enabled state of this chip. When `false`, this component will not
* respond to user input, and it will appear visually disabled and disabled to accessibility
* services.
* @param leadingIcon optional icon at the start of the chip, preceding the [label] text
* @param selectedIcon optional icon at the start of the chip, preceding the [label] text, which is
* displayed when the chip is selected, instead of any given [leadingIcon]
* @param trailingIcon optional icon at the end of the chip
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this chip. You can create and pass in your own `remember`ed instance to observe
* [Interaction]s and customize the appearance / behavior of this chip in different states.
* @param elevation [SelectableChipElevation] used to resolve the elevation for this chip in
* different states. This controls the size of the shadow below the chip. Additionally, when the
* container color is [ColorScheme.surface], this controls the amount of primary color applied as an
* overlay. See [FilterChipDefaults.filterChipElevation].
* @param shape defines the shape of this chip's container, border (when [border] is not null), and
* shadow (when using [elevation])
* @param border the border to draw around the container of this chip. Pass `null` for no border.
* See [FilterChipDefaults.filterChipBorder].
* @param colors [SelectableChipColors] that will be used to resolve the colors used for this chip
* in different states. See [FilterChipDefaults.filterChipColors].
*/
@ExperimentalMaterial3Api
@Composable
fun FilterChip(
selected: Boolean,
onClick: () -> Unit,
label: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
leadingIcon: @Composable (() -> Unit)? = null,
selectedIcon: @Composable (() -> Unit)? = null,
trailingIcon: @Composable (() -> Unit)? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
elevation: SelectableChipElevation? = FilterChipDefaults.filterChipElevation(),
shape: Shape = FilterChipDefaults.Shape,
border: SelectableChipBorder? = FilterChipDefaults.filterChipBorder(),
colors: SelectableChipColors = FilterChipDefaults.filterChipColors()
) = SelectableChip(
selected = selected,
modifier = modifier,
onClick = onClick,
enabled = enabled,
label = label,
labelTextStyle = MaterialTheme.typography.fromToken(FilterChipTokens.LabelTextFont),
leadingIcon = if (selected) selectedIcon else leadingIcon,
avatar = null,
trailingIcon = trailingIcon,
elevation = elevation,
colors = colors,
minHeight = FilterChipDefaults.Height,
paddingValues = FilterChipPadding,
shape = shape,
border = border?.borderStroke(enabled, selected)?.value,
interactionSource = interactionSource
)
/**
* <a href="https://m3.material.io/components/chips/overview" class="external" target="_blank">Material Design elevated filter chip</a>.
*
* Chips help people enter information, make selections, filter content, or trigger actions. Chips
* can show multiple interactive elements together in the same area, such as a list of selectable
* movie times, or a series of email contacts.
*
* Filter chips use tags or descriptive words to filter content. They can be a good alternative to
* toggle buttons or checkboxes.
*
* ![Filter chip image](https://developer.android.com/images/reference/androidx/compose/material3/elevated-filter-chip.png)
*
* This filter chip is applied with an elevated style. If you want a flat style, use the
* [FilterChip].
*
* Tapping on a filter chip selects it, and in case a [selectedIcon] is provided (e.g. a checkmark),
* it's appended to the starting edge of the chip's label, drawn instead of any given [leadingIcon].
*
* Example of an elevated FilterChip with a trailing icon:
* @sample androidx.compose.material3.samples.ElevatedFilterChipSample
*
* @param selected whether this chip is selected or not
* @param onClick called when this chip is clicked
* @param label text label for this chip
* @param modifier the [Modifier] to be applied to this chip
* @param enabled controls the enabled state of this chip. When `false`, this component will not
* respond to user input, and it will appear visually disabled and disabled to accessibility
* services.
* @param leadingIcon optional icon at the start of the chip, preceding the [label] text
* @param selectedIcon optional icon at the start of the chip, preceding the [label] text, which is
* displayed when the chip is selected, instead of any given [leadingIcon]
* @param trailingIcon optional icon at the end of the chip
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this chip. You can create and pass in your own `remember`ed instance to observe
* [Interaction]s and customize the appearance / behavior of this chip in different states.
* @param elevation [SelectableChipElevation] used to resolve the elevation for this chip in
* different states. This controls the size of the shadow below the chip. Additionally, when the
* container color is [ColorScheme.surface], this controls the amount of primary color applied as an
* overlay. See [FilterChipDefaults.filterChipElevation].
* @param shape defines the shape of this chip's container, border (when [border] is not null), and
* shadow (when using [elevation])
* @param border the border to draw around the container of this chip. Pass `null` for no border.
* See [FilterChipDefaults.filterChipBorder].
* @param colors [SelectableChipColors] that will be used to resolve the colors used for this chip
* in different states. See [FilterChipDefaults.elevatedFilterChipColors].
*/
@ExperimentalMaterial3Api
@Composable
fun ElevatedFilterChip(
selected: Boolean,
onClick: () -> Unit,
label: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
leadingIcon: @Composable (() -> Unit)? = null,
selectedIcon: @Composable (() -> Unit)? = null,
trailingIcon: @Composable (() -> Unit)? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
elevation: SelectableChipElevation? = FilterChipDefaults.elevatedFilterChipElevation(),
shape: Shape = FilterChipDefaults.Shape,
border: SelectableChipBorder? = null,
colors: SelectableChipColors = FilterChipDefaults.elevatedFilterChipColors()
) = SelectableChip(
selected = selected,
modifier = modifier,
onClick = onClick,
enabled = enabled,
label = label,
labelTextStyle = MaterialTheme.typography.fromToken(FilterChipTokens.LabelTextFont),
leadingIcon = if (selected) selectedIcon else leadingIcon,
avatar = null,
trailingIcon = trailingIcon,
elevation = elevation,
colors = colors,
minHeight = FilterChipDefaults.Height,
paddingValues = FilterChipPadding,
shape = shape,
border = border?.borderStroke(enabled, selected)?.value,
interactionSource = interactionSource
)
/**
* <a href="https://m3.material.io/components/chips/overview" class="external" target="_blank">Material Design input chip</a>.
*
* Chips help people enter information, make selections, filter content, or trigger actions. Chips
* can show multiple interactive elements together in the same area, such as a list of selectable
* movie times, or a series of email contacts.
*
* Input chips represent discrete pieces of information entered by a user.
*
* ![Input chip image](https://developer.android.com/images/reference/androidx/compose/material3/input-chip.png)
*
* An Input Chip can have a leading icon or an avatar at its start. In case both are provided, the
* avatar will take precedence and will be displayed.
*
* Example of an InputChip with a trailing icon:
* @sample androidx.compose.material3.samples.InputChipSample
*
* Example of an InputChip with an avatar and a trailing icon:
* @sample androidx.compose.material3.samples.InputChipWithAvatarSample
*
* Input chips should appear in a set and can be horizontally scrollable:
* @sample androidx.compose.material3.samples.ChipGroupSingleLineSample
*
* Alternatively, use Accompanist's [Flow Layouts](https://google.github.io/accompanist/flowlayout/)
* to wrap chips to a new line.
*
* @param selected whether this chip is selected or not
* @param onClick called when this chip is clicked
* @param label text label for this chip
* @param modifier the [Modifier] to be applied to this chip
* @param enabled controls the enabled state of this chip. When `false`, this component will not
* respond to user input, and it will appear visually disabled and disabled to accessibility
* services.
* @param leadingIcon optional icon at the start of the chip, preceding the [label] text
* @param avatar optional avatar at the start of the chip, preceding the [label] text
* @param trailingIcon optional icon at the end of the chip
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this chip. You can create and pass in your own `remember`ed instance to observe
* [Interaction]s and customize the appearance / behavior of this chip in different states.
* @param elevation [ChipElevation] used to resolve the elevation for this chip in different states.
* This controls the size of the shadow below the chip. Additionally, when the container color is
* [ColorScheme.surface], this controls the amount of primary color applied as an overlay. See
* [InputChipDefaults.inputChipElevation].
* @param shape defines the shape of this chip's container, border (when [border] is not null), and
* shadow (when using [elevation])
* @param border the border to draw around the container of this chip. Pass `null` for no border.
* See [InputChipDefaults.inputChipBorder].
* @param colors [ChipColors] that will be used to resolve the colors used for this chip in
* different states. See [InputChipDefaults.inputChipColors].
*/
@ExperimentalMaterial3Api
@Composable
fun InputChip(
selected: Boolean,
onClick: () -> Unit,
label: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
leadingIcon: @Composable (() -> Unit)? = null,
avatar: @Composable (() -> Unit)? = null,
trailingIcon: @Composable (() -> Unit)? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
elevation: SelectableChipElevation? = InputChipDefaults.inputChipElevation(),
shape: Shape = InputChipDefaults.Shape,
border: SelectableChipBorder? = InputChipDefaults.inputChipBorder(),
colors: SelectableChipColors = InputChipDefaults.inputChipColors()
) {
// If given, place the avatar in an InputChipTokens.AvatarShape shape before passing it into the
// Chip function.
var shapedAvatar: @Composable (() -> Unit)? = null
if (avatar != null) {
val avatarOpacity = if (enabled) 1f else InputChipTokens.DisabledAvatarOpacity
val avatarShape = InputChipTokens.AvatarShape.toShape()
shapedAvatar = @Composable {
Box(
modifier = Modifier.graphicsLayer {
this.alpha = avatarOpacity
this.shape = avatarShape
this.clip = true
},
contentAlignment = Alignment.Center
) {
avatar()
}
}
}
SelectableChip(
selected = selected,
modifier = modifier,
onClick = onClick,
enabled = enabled,
label = label,
labelTextStyle = MaterialTheme.typography.fromToken(InputChipTokens.LabelTextFont),
leadingIcon = leadingIcon,
avatar = shapedAvatar,
trailingIcon = trailingIcon,
elevation = elevation,
colors = colors,
minHeight = InputChipDefaults.Height,
paddingValues = inputChipPadding(
hasAvatar = shapedAvatar != null,
hasLeadingIcon = leadingIcon != null,
hasTrailingIcon = trailingIcon != null
),
shape = shape,
border = border?.borderStroke(enabled, selected)?.value,
interactionSource = interactionSource
)
}
/**
* <a href="https://m3.material.io/components/chips/overview" class="external" target="_blank">Material Design suggestion chip</a>.
*
* Chips help people enter information, make selections, filter content, or trigger actions. Chips
* can show multiple interactive elements together in the same area, such as a list of selectable
* movie times, or a series of email contacts.
*
* Suggestion chips help narrow a user's intent by presenting dynamically generated suggestions,
* such as possible responses or search filters.
*
* ![Suggestion chip image](https://developer.android.com/images/reference/androidx/compose/material3/suggestion-chip.png)
*
* This suggestion chip is applied with a flat style. If you want an elevated style, use the
* [ElevatedSuggestionChip].
*
* Example of a flat SuggestionChip with a trailing icon:
* @sample androidx.compose.material3.samples.SuggestionChipSample
*
* @param onClick called when this chip is clicked
* @param label text label for this chip
* @param modifier the [Modifier] to be applied to this chip
* @param enabled controls the enabled state of this chip. When `false`, this component will not
* respond to user input, and it will appear visually disabled and disabled to accessibility
* services.
* @param icon optional icon at the start of the chip, preceding the [label] text
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this chip. You can create and pass in your own `remember`ed instance to observe
* [Interaction]s and customize the appearance / behavior of this chip in different states.
* @param elevation [ChipElevation] used to resolve the elevation for this chip in different states.
* This controls the size of the shadow below the chip. Additionally, when the container color is
* [ColorScheme.surface], this controls the amount of primary color applied as an overlay. See
* [SuggestionChipDefaults.suggestionChipElevation].
* @param shape defines the shape of this chip's container, border (when [border] is not null), and
* shadow (when using [elevation])
* @param border the border to draw around the container of this chip. Pass `null` for no border.
* See [SuggestionChipDefaults.suggestionChipBorder].
* @param colors [ChipColors] that will be used to resolve the colors used for this chip in
* different states. See [SuggestionChipDefaults.suggestionChipColors].
*/
@ExperimentalMaterial3Api
@Composable
fun SuggestionChip(
onClick: () -> Unit,
label: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
icon: @Composable (() -> Unit)? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
elevation: ChipElevation? = SuggestionChipDefaults.suggestionChipElevation(),
shape: Shape = SuggestionChipDefaults.Shape,
border: ChipBorder? = SuggestionChipDefaults.suggestionChipBorder(),
colors: ChipColors = SuggestionChipDefaults.suggestionChipColors()
) = Chip(
modifier = modifier,
onClick = onClick,
enabled = enabled,
label = label,
labelTextStyle = MaterialTheme.typography.fromToken(SuggestionChipTokens.LabelTextFont),
labelColor = colors.labelColor(enabled).value,
leadingIcon = icon,
trailingIcon = null,
elevation = elevation,
colors = colors,
minHeight = SuggestionChipDefaults.Height,
paddingValues = SuggestionChipPadding,
shape = shape,
border = border?.borderStroke(enabled)?.value,
interactionSource = interactionSource
)
/**
* <a href="https://m3.material.io/components/chips/overview" class="external" target="_blank">Material Design elevated suggestion chip</a>.
*
* Chips help people enter information, make selections, filter content, or trigger actions. Chips
* can show multiple interactive elements together in the same area, such as a list of selectable
* movie times, or a series of email contacts.
*
* Suggestion chips help narrow a user's intent by presenting dynamically generated suggestions,
* such as possible responses or search filters.
*
* ![Suggestion chip image](https://developer.android.com/images/reference/androidx/compose/material3/elevated-suggestion-chip.png)
*
* This suggestion chip is applied with an elevated style. If you want a flat style, use the
* [SuggestionChip].
*
* Example of an elevated SuggestionChip with a trailing icon:
* @sample androidx.compose.material3.samples.ElevatedSuggestionChipSample
*
* @param onClick called when this chip is clicked
* @param label text label for this chip
* @param modifier the [Modifier] to be applied to this chip
* @param enabled controls the enabled state of this chip. When `false`, this component will not
* respond to user input, and it will appear visually disabled and disabled to accessibility
* services.
* @param icon optional icon at the start of the chip, preceding the [label] text
* @param interactionSource the [MutableInteractionSource] representing the stream of [Interaction]s
* for this chip. You can create and pass in your own `remember`ed instance to observe
* [Interaction]s and customize the appearance / behavior of this chip in different states.
* @param elevation [ChipElevation] used to resolve the elevation for this chip in different states.
* This controls the size of the shadow below the chip. Additionally, when the container color is
* [ColorScheme.surface], this controls the amount of primary color applied as an overlay. See
* [Surface] and [SuggestionChipDefaults.elevatedSuggestionChipElevation].
* @param shape defines the shape of this chip's container, border (when [border] is not null), and
* shadow (when using [elevation])
* @param border the border to draw around the container of this chip
* @param colors [ChipColors] that will be used to resolve the colors used for this chip in
* different states. See [SuggestionChipDefaults.elevatedSuggestionChipColors].
*/
@ExperimentalMaterial3Api
@Composable
fun ElevatedSuggestionChip(
onClick: () -> Unit,
label: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
icon: @Composable (() -> Unit)? = null,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
elevation: ChipElevation? = SuggestionChipDefaults.elevatedSuggestionChipElevation(),
shape: Shape = SuggestionChipDefaults.Shape,
border: ChipBorder? = null,
colors: ChipColors = SuggestionChipDefaults.elevatedSuggestionChipColors()
) = Chip(
modifier = modifier,
onClick = onClick,
enabled = enabled,
label = label,
labelTextStyle = MaterialTheme.typography.fromToken(SuggestionChipTokens.LabelTextFont),
labelColor = colors.labelColor(enabled).value,
leadingIcon = icon,
trailingIcon = null,
elevation = elevation,
colors = colors,
minHeight = SuggestionChipDefaults.Height,
paddingValues = SuggestionChipPadding,
shape = shape,
border = border?.borderStroke(enabled)?.value,
interactionSource = interactionSource
)
/**
* Represents the elevation for a chip in different states.
*/
@Stable
@ExperimentalMaterial3Api
interface ChipElevation {
/**
* Represents the tonal elevation used in a chip, depending on its [enabled] state and
* [interactionSource]. This should typically be the same value as the [shadowElevation].
*
* Tonal elevation is used to apply a color shift to the surface to give the it higher emphasis.
* When surface's color is [ColorScheme.surface], a higher elevation will result in a darker
* color in light theme and lighter color in dark theme.
*
* See [shadowElevation] which controls the elevation of the shadow drawn around the chip.
*
* @param enabled whether the chip is enabled
* @param interactionSource the [InteractionSource] for this chip
*/
@Composable
fun tonalElevation(enabled: Boolean, interactionSource: InteractionSource): State<Dp>
/**
* Represents the shadow elevation used in a chip, depending on its [enabled] state and
* [interactionSource]. This should typically be the same value as the [tonalElevation].
*
* Shadow elevation is used to apply a shadow around the chip to give it higher emphasis.
*
* See [tonalElevation] which controls the elevation with a color shift to the surface.
*
* @param enabled whether the chip is enabled
* @param interactionSource the [InteractionSource] for this chip
*/
@Composable
fun shadowElevation(enabled: Boolean, interactionSource: InteractionSource): State<Dp>
}
/**
* Represents the elevation used in a selectable chip in different states.
*/
@Stable
@ExperimentalMaterial3Api
interface SelectableChipElevation {
/**
* Represents the tonal elevation used in a chip, depending on [enabled], [selected], and
* [interactionSource]. This should typically be the same value as the [shadowElevation].
*
* Tonal elevation is used to apply a color shift to the surface to give the it higher emphasis.
* When surface's color is [ColorScheme.surface], a higher elevation will result in a darker
* color in light theme and lighter color in dark theme.
*
* See [shadowElevation] which controls the elevation of the shadow drawn around the Chip.
*
* @param enabled whether the chip is enabled
* @param selected whether the chip is selected
* @param interactionSource the [InteractionSource] for this chip
*/
@Composable
fun tonalElevation(
enabled: Boolean,
selected: Boolean,
interactionSource: InteractionSource
): State<Dp>
/**
* Represents the shadow elevation used in a chip, depending on [enabled], [selected], and
* [interactionSource]. This should typically be the same value as the [tonalElevation].
*
* Shadow elevation is used to apply a shadow around the surface to give it higher emphasis.
*
* See [tonalElevation] which controls the elevation with a color shift to the surface.
*
* @param enabled whether the chip is enabled
* @param selected whether the chip is selected
* @param interactionSource the [InteractionSource] for this chip
*/
@Composable
fun shadowElevation(
enabled: Boolean,
selected: Boolean,
interactionSource: InteractionSource
): State<Dp>
}
/**
* Represents the container and content colors used in a clickable chip in different states.
*
* See [AssistChipDefaults], [InputChipDefaults], and [SuggestionChipDefaults] for the default
* colors used in the various Chip configurations.
*/
@Stable
@ExperimentalMaterial3Api
interface ChipColors {
/**
* Represents the container color for this chip, depending on [enabled].
*
* @param enabled whether the chip is enabled
*/
@Composable
fun containerColor(enabled: Boolean): State<Color>
/**
* Represents the label color for this chip, depending on [enabled].
*
* @param enabled whether the chip is enabled
*/
@Composable
fun labelColor(enabled: Boolean): State<Color>
/**
* Represents the leading icon's content color for this chip, depending on [enabled].
*
* @param enabled whether the chip is enabled
*/
@Composable
fun leadingIconContentColor(enabled: Boolean): State<Color>
/**
* Represents the trailing icon's content color for this chip, depending on [enabled].
*
* @param enabled whether the chip is enabled
*/
@Composable
fun trailingIconContentColor(enabled: Boolean): State<Color>
}
/**
* Represents the container and content colors used in a selectable chip in different states.
*
* See [FilterChipDefaults.filterChipColors] and [FilterChipDefaults.elevatedFilterChipColors] for
* the default colors used in [FilterChip].
*/
@Stable
@ExperimentalMaterial3Api
interface SelectableChipColors {
/**
* Represents the container color for this chip, depending on [enabled] and [selected].
*
* @param enabled whether the chip is enabled
* @param selected whether the chip is selected
*/
@Composable
fun containerColor(enabled: Boolean, selected: Boolean): State<Color>
/**
* Represents the label color for this chip, depending on [enabled] and [selected].
*
* @param enabled whether the chip is enabled
* @param selected whether the chip is selected
*/
@Composable
fun labelColor(enabled: Boolean, selected: Boolean): State<Color>
/**
* Represents the leading icon color for this chip, depending on [enabled] and [selected].
*
* @param enabled whether the chip is enabled
* @param selected whether the chip is selected
*/
@Composable
fun leadingIconContentColor(enabled: Boolean, selected: Boolean): State<Color>
/**
* Represents the trailing icon color for this chip, depending on [enabled] and [selected].
*
* @param enabled whether the chip is enabled
* @param selected whether the chip is selected
*/
@Composable
fun trailingIconContentColor(enabled: Boolean, selected: Boolean): State<Color>
}
/**
* Represents the border stroke used in a chip in different states.
*/
@Stable
@ExperimentalMaterial3Api
interface ChipBorder {
/**
* Represents the [BorderStroke] for this chip, depending on [enabled].
*
* @param enabled whether the chip is enabled
*/
@Composable
fun borderStroke(enabled: Boolean): State<BorderStroke?>
}
/**
* Represents the border stroke used used in a selectable chip in different states.
*/
@Stable
@ExperimentalMaterial3Api
interface SelectableChipBorder {
/**
* Represents the [BorderStroke] stroke used for this chip, depending on [enabled] and
* [selected].
*
* @param enabled whether the chip is enabled
* @param selected whether the chip is selected
*/
@Composable
fun borderStroke(enabled: Boolean, selected: Boolean): State<BorderStroke?>
}
/**
* Contains the baseline values used by [AssistChip].
*/
@ExperimentalMaterial3Api
object AssistChipDefaults {
/** Default shape of an assist chip. */
val Shape: Shape @Composable get() = AssistChipTokens.ContainerShape.toShape()
/**
* The height applied for an assist chip.
* Note that you can override it by applying Modifier.height directly on a chip.
*/
val Height = AssistChipTokens.ContainerHeight
/**
* The size of an assist chip icon.
*/
val IconSize = AssistChipTokens.IconSize
/**
* Creates a [ChipColors] that represents the default container , label, and icon colors used in
* a flat [AssistChip].
*
* @param containerColor the container color of this chip when enabled
* @param labelColor the label color of this chip when enabled
* @param leadingIconContentColor the color of this chip's start icon when enabled
* @param trailingIconContentColor the color of this chip's end icon when enabled
* @param disabledContainerColor the container color of this chip when not enabled
* @param disabledLabelColor the label color of this chip when not enabled
* @param disabledLeadingIconContentColor the color of this chip's start icon when not enabled
* @param disabledTrailingIconContentColor the color of this chip's end icon when not enabled
*/
@Composable
fun assistChipColors(
containerColor: Color = Color.Transparent,
labelColor: Color = AssistChipTokens.LabelTextColor.toColor(),
leadingIconContentColor: Color = AssistChipTokens.IconColor.toColor(),
trailingIconContentColor: Color = leadingIconContentColor,
disabledContainerColor: Color = Color.Transparent,
disabledLabelColor: Color = AssistChipTokens.DisabledLabelTextColor.toColor()
.copy(alpha = AssistChipTokens.DisabledLabelTextOpacity),
disabledLeadingIconContentColor: Color =
AssistChipTokens.DisabledIconColor.toColor()
.copy(alpha = AssistChipTokens.DisabledIconOpacity),
disabledTrailingIconContentColor: Color = disabledLeadingIconContentColor,
): ChipColors = DefaultChipColors(
containerColor = containerColor,
labelColor = labelColor,
leadingIconContentColor = leadingIconContentColor,
trailingIconContentColor = trailingIconContentColor,
disabledContainerColor = disabledContainerColor,
disabledLabelColor = disabledLabelColor,
disabledLeadingIconContentColor = disabledLeadingIconContentColor,
disabledTrailingIconContentColor = disabledTrailingIconContentColor
)
/**
* Creates a [ChipElevation] that will animate between the provided values according to the
* Material specification for a flat [AssistChip].
*
* @param defaultElevation the elevation used when the [AssistChip] is has no other
* [Interaction]s
* @param pressedElevation the elevation used when the chip is pressed.
* @param focusedElevation the elevation used when the chip is focused
* @param hoveredElevation the elevation used when the chip is hovered
* @param draggedElevation the elevation used when the chip is dragged
* @param disabledElevation the elevation used when the chip is not enabled
*/
@Composable
fun assistChipElevation(
defaultElevation: Dp = AssistChipTokens.FlatContainerElevation,
pressedElevation: Dp = defaultElevation,
focusedElevation: Dp = defaultElevation,
hoveredElevation: Dp = defaultElevation,
draggedElevation: Dp = AssistChipTokens.DraggedContainerElevation,
disabledElevation: Dp = defaultElevation
): ChipElevation {
return remember(
defaultElevation,
pressedElevation,
focusedElevation,
hoveredElevation,
draggedElevation,
disabledElevation
) {
DefaultChipElevation(
defaultElevation = defaultElevation,
pressedElevation = pressedElevation,
focusedElevation = focusedElevation,
hoveredElevation = hoveredElevation,
draggedElevation = draggedElevation,
disabledElevation = disabledElevation
)
}
}
/**
* Creates a [ChipBorder] that represents the default border used in a flat [AssistChip].
*
* @param borderColor the border color of this chip when enabled
* @param disabledBorderColor the border color of this chip when not enabled
* @param borderWidth the border stroke width of this chip
*/
@Composable
fun assistChipBorder(
borderColor: Color = AssistChipTokens.FlatOutlineColor.toColor(),
disabledBorderColor: Color = AssistChipTokens.FlatDisabledOutlineColor.toColor()
.copy(alpha = AssistChipTokens.FlatDisabledOutlineOpacity),
borderWidth: Dp = AssistChipTokens.FlatOutlineWidth,
): ChipBorder {
return remember(
borderColor,
disabledBorderColor,
borderWidth
) {
DefaultChipBorder(
borderColor = borderColor,
disabledBorderColor = disabledBorderColor,
borderWidth = borderWidth
)
}
}
/**
* Creates a [ChipColors] that represents the default container, label, and icon colors used in
* an elevated [AssistChip].
*
* @param containerColor the container color of this chip when enabled
* @param labelColor the label color of this chip when enabled
* @param leadingIconContentColor the color of this chip's start icon when enabled
* @param trailingIconContentColor the color of this chip's end icon when enabled
* @param disabledContainerColor the container color of this chip when not enabled
* @param disabledLabelColor the label color of this chip when not enabled
* @param disabledLeadingIconContentColor the color of this chip's start icon when not enabled
* @param disabledTrailingIconContentColor the color of this chip's end icon when not enabled
*/
@Composable
fun elevatedAssistChipColors(
containerColor: Color = AssistChipTokens.ElevatedContainerColor.toColor(),
labelColor: Color = AssistChipTokens.LabelTextColor.toColor(),
leadingIconContentColor: Color = AssistChipTokens.IconColor.toColor(),
trailingIconContentColor: Color = leadingIconContentColor,
disabledContainerColor: Color = AssistChipTokens.ElevatedDisabledContainerColor.toColor()
.copy(alpha = AssistChipTokens.ElevatedDisabledContainerOpacity),
disabledLabelColor: Color = AssistChipTokens.DisabledLabelTextColor.toColor()
.copy(alpha = AssistChipTokens.DisabledLabelTextOpacity),
disabledLeadingIconContentColor: Color =
AssistChipTokens.DisabledIconColor.toColor()
.copy(alpha = AssistChipTokens.DisabledIconOpacity),
disabledTrailingIconContentColor: Color = disabledLeadingIconContentColor,
): ChipColors = DefaultChipColors(
containerColor = containerColor,
labelColor = labelColor,
leadingIconContentColor = leadingIconContentColor,
trailingIconContentColor = trailingIconContentColor,
disabledContainerColor = disabledContainerColor,
disabledLabelColor = disabledLabelColor,
disabledLeadingIconContentColor = disabledLeadingIconContentColor,
disabledTrailingIconContentColor = disabledTrailingIconContentColor
)
/**
* Creates a [ChipElevation] that will animate between the provided values according to the
* Material specification for an elevated [AssistChip].
*
* @param defaultElevation the elevation used when the [AssistChip] is has no other
* [Interaction]s
* @param pressedElevation the elevation used when the chip is pressed.
* @param focusedElevation the elevation used when the chip is focused
* @param hoveredElevation the elevation used when the chip is hovered
* @param draggedElevation the elevation used when the chip is dragged
* @param disabledElevation the elevation used when the chip is not enabled
*/
@Composable