https://kotlinlang.org logo
#compose
Title
# compose
t

Tolriq

01/13/2022, 6:03 PM
What's the recommended way to deal with apha on text in material 3 that will be easier to migrate later if there's an official way ?
c

Chris Sinco [G]

01/13/2022, 8:13 PM
Do you mean deal with alpha in the text color?
t

Tolriq

01/13/2022, 8:26 PM
Yes, providing the Contentalpha does not work. and it's not documented if it will be added In the same way or something else or not at all
c

Chris Sinco [G]

01/13/2022, 8:30 PM
Yes, providing the Contentalpha does not work
Is this in a specific Material3 component or just
Text
?
t

Tolriq

01/13/2022, 8:34 PM
I mostly only use it for Text with the normal
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium)
From component source it seems wanted.
c

Chris Sinco [G]

01/13/2022, 8:35 PM
Can you share a code snippet please?
t

Tolriq

01/13/2022, 8:39 PM
A simple
Copy code
CompositionLocalProvider(LocalContentColor provides TextContentAlpha.medium) {
            Text(subtitle)
        }
But it's normal I guess. Material Text have:
Copy code
val textColor = color.takeOrElse {
        style.color.takeOrElse {
            LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
        }
    }
but Material 3 text have
Copy code
val textColor = color.takeOrElse {
        style.color.takeOrElse {
            LocalContentColor.current
        }
    }
So I guess there's not LocalContentAlpha support at all in material 3. Just wondering if it will be added in the same way or in a new way. If same way I can provide a localcolor and apply the alpha easy to search and replace. If not maybe I need to directly work with typography and TextStyles to supply colors with alpha directly.
Oups wrong code pasted for the snipet it's my actual workaround.
Copy code
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium) {
            Text(subtitle)
        }
Is what was working in m2 and no more in m3
c

Chris Sinco [G]

01/14/2022, 12:20 AM
cc @Nick Rout @nickbutcher
Hmm I’m not sure that is intentional on the Material3 part - I can follow up with the team on this. Seems like a bug to me since taking away the LocalContentAlpha seems like a step backwards WRT developer experience.
The current workaround for now would be to set the color directly + change the alpha, or use an alpha modifier. Not ideal for multiple text elements (which is why I’m thinking this is a bug), but it should still work.
a

Albert Chang

01/14/2022, 12:37 AM
In M3 I'm using
MatetialTheme.colorScheme.onSurfaceVariant
(which is actually opaque) for medium alpha text. But indeed there doesn't seem to be a good way to set disabled alpha.
t

Tolriq

01/14/2022, 6:32 AM
@Chris Sinco [G] Yes that what I mostly currently do I use
Copy code
object ContentColorAlpha {
    val high: Color
        @Composable
        get() = LocalContentColor.current.copy(alpha = ContentAlpha.high)
    val medium: Color
        @Composable
        get() = LocalContentColor.current.copy(alpha = ContentAlpha.medium)
    val disabled: Color
        @Composable
        get() = LocalContentColor.current.copy(alpha = ContentAlpha.disabled)
}
With
Copy code
CompositionLocalProvider(LocalContentColor provides ContentColorAlpha.medium) {
The advantage is that it works also on other components so I can apply that on a row and it applies to all inside it and it works whatever the current color is. The inconvenient is that it does not work for components with forced color and I must add it there too. I hope you can have some feedback on this, maybe I need to open an issue? @Albert Chang The problem of onSurfaceVariant is that it's only one color for one case, and some non dynamictonal theme may use something that does not work as this use case :(
c

Chris Sinco [G]

01/19/2022, 2:09 AM
So I talked to the teams internally and it seems with Material3 the design team decided that using opacity in color for Text is NOT something that will be carried forward. That is why LocalContentAlpha is no longer there. I realize Material3 documentation for both designers and developers haven’t made this clear, so I let the team know that we should note this in our docs going forward of this change.
The usage of ContentAlpha in general was to support the emphasis subsystem in Material, which is going away now in favor of using fully opaque colors for Text in a Material3 theme, e.g. onSurfaceVariant for medium emphasis. It still doesn’t answer the question of how to think about disabled emphasis with Material3, so that is something the team will hopefully answer with the beta library release and guidelines.
For now though in Material3 Text, you will only able to pass alpha through the Color value itself, or use an alpha modifier on the Text element directly. But CompositionLocal for alpha on content elements (Text, Icon) won’t be available going forward. If you’d like to see it added back to the Material library, please open a request so we can track things more easily with the respective teams.
So practically speaking, in your snippet above, you would have to replace calls to ContentAlpha with your own constants.
t

Tolriq

01/19/2022, 6:56 AM
Thanks for the feedback. Alpha is not only used for lower emphasis but also for the disabled indicator. If alpha is not supposed to be used at all it would be nice to have some docs about colors and custom themes (not tonal) for lower emphasis and disabled state. Including how to derivate those colors from a source color. I'll open an issue to discuss this part.
The other question is about applying an alpha modifier on a box for example and performance. Is there any impact and so should applying alpha to the subcomponent colors only be preferred?
a

Albert Chang

01/19/2022, 7:07 AM
To be clear, alpha is still used for disabled state and the value is defined on a per-component basis. See here.
Generally setting alpha directly (e.g. by setting a non-opaque color for
Text
or by specifying the
alpha
parameter of
Image
) is more efficient than using an alpha modifier as it doesn’t require the content to be rendered into an offscreen buffer.
t

Tolriq

01/19/2022, 7:36 AM
Ok thanks for confirming. I'm still a little lost with M3 color system. Like tonal elevation using the primary color but the calculation is not really exposed for custom components. Like if primaryvariant and alpha are used for different emphasis there are color variations between them no? Or should we apply the alpha over the low emphasis for consistency? For example a form with some low emphasis text that have everything disabled when submit that can take some time. What color to apply ?
a

Albert Chang

01/19/2022, 7:44 AM
There is no "low emphasis" even in M2. In M3 only disabled state uses alpha.
t

Tolriq

01/19/2022, 7:52 AM
In M2 it's
CompositionLocalProvider(LocalContentAlpha provides ContentAlpha.medium)
no ?
a

Albert Chang

01/19/2022, 7:54 AM
Yeah that's medium emphasis.
t

Tolriq

01/19/2022, 7:54 AM
Ok so yes sorry I mixed the term talking about medium emphasis then.
So previously I would use medium then disabled for the medium emphasis that are disabled during form post. Now medium is onSurfaceVariant that is a little tinted. When disabling the form should I use just the disabled alpha on the high emphasis color or apply it to the medium emphasis
a

Albert Chang

01/19/2022, 8:01 AM
In M2 disabled state doesn't differentiate between emphasises. I'm not sure if there's an official guideline for this in M3.
t

Tolriq

01/19/2022, 8:07 AM
Hum 😞 What would you do? Implementation is different when switching colors and applying alpha depending on state.
a

Albert Chang

01/19/2022, 8:18 AM
I would say using a consistent disabled color is simpler and provides higher contrast (so better a11y).
t

Tolriq

01/19/2022, 8:28 AM
Ok thanks again. Easier implementation and maintenance so I'll go this road.
27 Views