Slide 1

Slide 1 text

Compose beyond Material Sebastiano Poggi @seebrock3r

Slide 2

Slide 2 text

On the menu today • Do you need to ditch Material? • The Compose building blocks • Theming in Compose • Writing a good Compose API • Custom composables • Tips Material YES NO

Slide 3

Slide 3 text

Hold on! Write down this link 👇 https: / / go.sebastiano.dev/beyond-material-2022 Look out for the symbol

Slide 4

Slide 4 text

Hold on! Write down this link 👇 https: / / go.sebastiano.dev/beyond-material-2022 Look out for the symbol

Slide 5

Slide 5 text

Material Design

Slide 6

Slide 6 text

What is Material to you? • The default design system in Jetpack Compose • You use Material composables • For many, Compose == Material • Two Material versions • V2 is Material Theming • V3 is Material You • V3 is still incomplete and alpha Material Theming aka Material 2 Material You Material 3

Slide 7

Slide 7 text

Material is very cool! • Well documented • material.io and m3.material.io

Slide 8

Slide 8 text

Material is very cool! • Well documented • material.io and m3.material.io • Very flexible • Custom colours • Custom shapes • Custom typography Flexibility! Flexibility! Flexibility! Flexibility! Flexibility! Flexibility! Flexibility!

Slide 9

Slide 9 text

Material is very cool! • Well documented • material.io and m3.material.io • Very flexible • Custom colours • Custom shapes • Custom typography Flexibility!

Slide 10

Slide 10 text

Material is very cool! • Well documented • material.io and m3.material.io • Very flexible • Custom colours • Custom shapes • Custom typography Flexibility! Flexibility! Flexibility!

Slide 11

Slide 11 text

Material is very cool! • Enough for most use-cases • Visual tweaks are ok • …but not for all • Behaviour changes • Incompatible semantics Flexibility! Flexibility!

Slide 12

Slide 12 text

Suggested talks Something old, something new: Adding Jetpack Compose 
 to a large open source Android app Maia Grotepass Custom design systems in Compose Ryan Har t er Yesterday, 13 : 20 Today, 15 : 20

Slide 13

Slide 13 text

Other design systems

Slide 14

Slide 14 text

A design system is born • Designers hand over a spec • Components • Typography • Colours • Where do we star t ? • Material: yes or no? yo, new specs! do this The Designer MY SPECS 🤌

Slide 15

Slide 15 text

A design system is born • Designers hand over a spec yo, new specs! do this The Designer MY SPECS 🤌 👍

Slide 16

Slide 16 text

A design system is born • Designers hand over a spec • Components • Typography • Colours • Where do we star t ? • Material: yes or no? yo, new specs! do this The Designer MY SPECS 🤌 👍

Slide 17

Slide 17 text

To Material, or not to Material • Is it compatible with Material? • Existing components • Amount of hacks required • Visual and behaviour • Prefer reusing Material! • Less time and effor t • Battle-tested

Slide 18

Slide 18 text

Step 1: no more Material • You don’t want Material • Good news! • Views: monolithic • Compose: modular • You can get rid of Material Views

Slide 19

Slide 19 text

Step 1: no more Material • You don’t want Material • Good news! • Views: monolithic • Compose: modular • You can get rid of Material Views Compose

Slide 20

Slide 20 text

Step 1: no more Material • You don’t want Material • Good news! • Views: monolithic • Compose: modular • You can get rid of Material Views Compose

Slide 21

Slide 21 text

Step 1: no more Material build.gradle.kts dependencies { // .. . val composeUiVersion: String by rootProject.extra implementation(“androidx.compose.ui:ui:$composeUiVersion") implementation("androidx.compose.material:material:$composeUiVersion") implementation("androidx.compose.foundation:foundation:$composeUiVersion") implementation("androidx.compose.ui:ui-tooling-preview:$composeUiVersion") debugImplementation("androidx.compose.ui:ui-tooling:$composeUiVersion") }

Slide 22

Slide 22 text

Step 1: no more Material build.gradle.kts dependencies { // .. . val composeUiVersion: String by rootProject.extra implementation(“androidx.compose.ui:ui:$composeUiVersion") implementation("androidx.compose.foundation:foundation:$composeUiVersion") implementation("androidx.compose.ui:ui-tooling-preview:$composeUiVersion") debugImplementation("androidx.compose.ui:ui-tooling:$composeUiVersion") }

Slide 23

Slide 23 text

Pro-tip time! • Sometimes you can’t take it out • 3rd par t y dependencies • Swipe it under the rug • No auto-impor t , no code completion • More info on Intell iJ webhelp

Slide 24

Slide 24 text

Pro-tip time! • Sometimes you can’t take it out • 3rd par t y dependencies • Swipe it under the rug • No auto-impor t , no code completion • More info on Intell iJ webhelp

Slide 25

Slide 25 text

Pro-tip time! • Sometimes you can’t take it out • 3rd par t y dependencies • Swipe it under the rug • No auto-impor t , no code completion • More info on Intell iJ webhelp

Slide 26

Slide 26 text

Done! What now? • Four main components • We took Material out • Most Composables are in Material • Oops. runtime material foundation ui 🫣

Slide 27

Slide 27 text

Text Button Card Checkbox AlertDialog Divider RadioButton CircularProgressIndicator LinearProgressIndicator Snackbar Switch Tab TextField Snackbar Slider BottomDrawer TopAppBar FloatingActionButton Chip Surface Icon MaterialTheme NavigationRail

Slide 28

Slide 28 text

Not just composables • Ripples • Some useful APIs • LocalContentAlpha • LocalContentColor • LocalTextStyle So lonely :(

Slide 29

Slide 29 text

Not just composables • Ripples • Some useful APIs • LocalContentAlpha • LocalContentColor • LocalTextStyle So lonely :( LocalContentAlpha 1.0f 0.5f LocalContentColor #001E31 #006497 LocalTextStyle Regular ExtraBold So lonely :( So lonely :(

Slide 30

Slide 30 text

WE WILL REBUILD UPON FOUNDATION

Slide 31

Slide 31 text

WE WILL REBUILD UPON FOUNDATION

Slide 32

Slide 32 text

@Composable fun Text( ... )

Slide 33

Slide 33 text

@Composable fun Text( ... ) MATERIAL

Slide 34

Slide 34 text

@Composable fun Text( ... ) MATERIAL @Composable fun BasicText( ... ) FOUNDATION

Slide 35

Slide 35 text

@Composable fun Text( ... ) MATERIAL @Composable fun BasicText( ... ) FOUNDATION @Composable fun Layout(…) UI

Slide 36

Slide 36 text

@Composable fun Text( ... ) MATERIAL @Composable fun BasicText( ... ) FOUNDATION @Composable fun Layout(…) UI

Slide 37

Slide 37 text

@Composable fun Text( ... ) MATERIAL @Composable fun BasicText( ... ) FOUNDATION @Composable fun Layout(…) UI @Composable fun ComposeNode(…) RUNTIME * not 100% accurate

Slide 38

Slide 38 text

@Composable fun MyText( ... ) MY THEME @Composable fun BasicText( ... ) FOUNDATION @Composable fun Layout(…) UI @Composable fun ComposeNode(…) RUNTIME * not 100% accurate

Slide 39

Slide 39 text

Compose Foundation • Never star t ing from scratch • Basic building blocks • Layouts • Lazy layouts • “Bare” composables • Foundational concepts LAYOUT Box Row Column Spacer size fillMax* padding LAZY LazyRow LazyColumn LazyLayout Lazy*Grid BASE FOUNDATION Image Canvas [scrolling] [interaction] [visuals]

Slide 40

Slide 40 text

Theming themes composingly

Slide 41

Slide 41 text

Material as a reference • Let’s study Material • Gold reference implementation • Flexibility == complexity • Take what you need • Base concepts apply to other themes • Need to re-implement • But it’s easy!

Slide 42

Slide 42 text

Composition locals • Provide values • Implicit dependency • Useful for common “data”

Slide 43

Slide 43 text

Composition locals fun MyTheme() { CompositionLocalProvider(LocalContentColor provides Color.Black) { BasicText( text = "Hello!", style = TextStyle.Default.copy(color = LocalContentColor.current) ) } } Color.Black

Slide 44

Slide 44 text

Composition locals @Composable fun MyTheme() { CompositionLocalProvider(LocalContentColor provides Color.Black) { BasicText( text = "Hello!", style = TextStyle.Default.copy(color = LocalContentColor.current) ) } } Color.Black

Slide 45

Slide 45 text

Composition locals • Provide values • Implicit dependency • Useful for common “data” • Theming is built on top of it • Scoped to par t of a composition • It’s dangerous — don’t overuse! ⚠ @Composable

Slide 46

Slide 46 text

Indications • Indicates a composable state • React to interactionSource • In Material it’s ripples • Draw behind or in front • Provided via composition local • Star t from DefaultDebugIndication click! IDLE HOVER PRESSED FOCUSED

Slide 47

Slide 47 text

API design principles • Off i cial guidelines • Based on Kotlin conventions • Strongly recommended read • From years of experience • Helps with consistency • Meet users’ expectations

Slide 48

Slide 48 text

API design principles • Contents are composables • Higher order functions • Functions with function parameters • Model variants separately • E.g., Button vs TextButton • Blog post on design choices @Composable fun MyButton( content: @Composable () -> Unit ) { // .. . } @Composable fun MyOutlineButton( content: @Composable () -> Unit ) { // .. . }

Slide 49

Slide 49 text

Slot APIs • Design pattern from Material • Blog post by Chris Banes • Composables do one thing • Have “slots” for sub-components • Provide styling & pre-made components • More flexible and easier to use • Example: Scaffold

Slide 50

Slide 50 text

Slot APIs • Design pattern from Material • Blog post by Chris Banes • Composables do one thing • Have “slots” for sub-components • Provide styling & pre-made components • More flexible and easier to use • Example: Scaffold topBar bottomBar floatingActionButton BottomNavigation TopAppBar FloatingActionButton

Slide 51

Slide 51 text

Scoping with DSLs • Provide scoping if needed • Lambda receivers • Functions, modif i ers, etc • Examples: • Box* → BoxScope • Lazy* → LazyListScope interface LazyListScope { fun item( . .. ) { / / . .. } fun items( ... ) { / / . .. } fun stickyHeader( .. . ) { / / . .. } } interface BoxScope { @Stable fun Modifier.align( ... ): Modifier @Stable fun Modifier.matchParentSize(): Modifier }

Slide 52

Slide 52 text

Great ar t ists steal • Use Material as reference • Same basics • Copy-paste is ok • Simplify things where possible • Copy patterns as well • E.g., use Color.Unspecified androidx.compose.material.Text dev.sebastiano.mytheme.Text Text

Slide 53

Slide 53 text

Great ar t ists steal • Use Material as reference • Same basics • Copy-paste is ok • Simplify things where possible • Copy patterns as well • E.g., use Color.Unspecified androidx.compose.material.Text dev.sebastiano.mytheme.Text Text

Slide 54

Slide 54 text

SO CUSTOM! YAGNI • Won’t need all of Material • Depends on design system • Example: indications • Buttons depress when clicked • No need for indication for that • Consider focus/hover

Slide 55

Slide 55 text

YAGNI • Won’t need all of Material • Depends on design system • Example: indications • Buttons depress when clicked • No need for indication for that • Consider focus/hover SO CUSTOM!

Slide 56

Slide 56 text

Make your Composables

Slide 57

Slide 57 text

Let’s pretend I work at Duolingo! I don’t! I love you Duo please don’t sue kthxbye

Slide 58

Slide 58 text

A button is… what? • Look at the spec • What is a button, really? • Something clickable • What does Material do? • Surface with onClick • Surface is a fancy Box • Add clickable(), done! Normal Pressed Disabled SO CUSTOM! SO CUSTOM! SO CUSTOM! @Composable fun Button( . .. ) { // .. . Surface( onClick = onClick, . .. ) { / / . .. } }

Slide 59

Slide 59 text

@Composable fun Button( . .. ) { //... Box( modifier = modifier.clickable( . .. ), // omissis propagateMinConstraints = true ) { CompositionLocalProvider( . .. ) { ProvideTextStyle( ... ) { Row( modifier = Modifier.drawBehind( . .. ), // omissis horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically, content = content ) } } } }

Slide 60

Slide 60 text

@Composable fun Button( . .. ) { //... Box( modifier = modifier.clickable( . .. ), // omissis propagateMinConstraints = true ) { CompositionLocalProvider( . .. ) { ProvideTextStyle( ... ) { Row( modifier = Modifier.drawBehind( . .. ), // omissis horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically, content = content ) } } } } aka poor man’s Sur f ace handle clicks set content colour set text style button background button content

Slide 61

Slide 61 text

Text is tough, right? • Text is a royal pain to deal with • Layout, shaping, rendering • Emojis and fonts • What if Compose made it easy? • Enter BasicText • It’s Text, minus theming! @Composable fun Text( ... ) { val textColor = // ... val mergedStyle = / / ... BasicText(text, ... ) }

Slide 62

Slide 62 text

@Composable fun Text( ... ) { val textColor = color.takeOrElse { style.color.takeOrElse { LocalPalette.current.foreground.copy(alpha = LocalContentAlpha.current) } } val combinedStyle = style.merge( TextStyle( ... ) ) BasicText( text = text, modifier = modifier, style = combinedStyle, onTextLayout = {}, overflow = overflow, softWrap = softWrap, maxLines = maxLines, inlineContent = emptyMap() ) }

Slide 63

Slide 63 text

Text can’t be that easy! • Editable text is harder • Selection, cursor, clipboard, etc. • Much harder to do in Compose • …or is it? • Enter BasicTextField • It’s TextField, minus theming! • BYO “decoration” and box @Composable fun TextField( ... ) { // .. . val mergedStyle = / / ... BasicTextField( text = text, . .. , decorationBox = { .. . } ) }

Slide 64

Slide 64 text

Text can’t be that easy! • Editable text is harder • Selection, cursor, clipboard, etc. • Much harder to do in Compose • …or is it? • Enter BasicTextField • It’s TextField, minus theming! • BYO “decoration” and box @Composable fun TextField( ... ) { // .. . val mergedStyle = / / ... BasicTextField( text = text, . .. , decorationBox = { .. . } ) } I’m typing here!

Slide 65

Slide 65 text

Text can’t be that easy! • Editable text is harder • Selection, cursor, clipboard, etc. • Much harder to do in Compose • …or is it? • Enter BasicTextField • It’s TextField, minus theming! • BYO “decoration” and box @Composable fun TextField( ... ) { // .. . val mergedStyle = / / ... BasicTextField( text = text, . .. , decorationBox = { .. . } ) } I’m typing here!

Slide 66

Slide 66 text

Finally, some progress (bar) • Foundation won’t help • No built-in composable • How does Material do it? • Time to go low-level • Drawing to the canvas • It’s pretty easy • Don’t forget LTR vs RTL TRACK HIGHLIGHT BAR

Slide 67

Slide 67 text

Finally, some progress (bar) • Foundation won’t help • No built-in composable • How does Material do it? • Time to go low-level • Drawing to the canvas • It’s pretty easy • Don’t forget LTR vs RTL @Composable fun LinearProgressIndicator( ... ) { Canvas( modifier .progressSemantics( . .. ) .size( ... ) ) { drawLinearIndicatorBackground( . .. ) drawLinearIndicator( ... ) } }

Slide 68

Slide 68 text

@Composable fun ProgressBar( .. . ) { //... Canvas( modifier = modifier .progressSemantics(progress) .clip(RoundedCornerShape( .. . )) .defaultMinSize(minHeight = .. . ), ) { / /.. . drawRect(color = colors.track) updateProgressBounds( ... ) drawRoundRect( . .. ) updateHighlightBounds( .. . ) drawRoundRect( . .. ) } }

Slide 69

Slide 69 text

@Composable fun ProgressBar( .. . ) { //... Canvas( modifier = modifier .progressSemantics(progress) .clip(RoundedCornerShape( .. . )) .defaultMinSize(minHeight = .. . ), ) { / /.. . drawRect(color = colors.track) updateProgressBounds( ... ) drawRoundRect( . .. ) updateHighlightBounds( .. . ) drawRoundRect( . .. ) } } draw by hand “I am a progress bar” We cheat...

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

Sucks!

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

clip()

Slide 79

Slide 79 text

clip()

Slide 80

Slide 80 text

clip()

Slide 81

Slide 81 text

@Composable fun ProgressBar( .. . ) { //... Canvas( modifier = modifier .progressSemantics(progress) .clip(RoundedCornerShape( .. . )) .defaultMinSize(minHeight = .. . ), ) { / /.. . drawRect(color = colors.track) updateProgressBounds( ... ) drawRoundRect( . .. ) updateHighlightBounds( .. . ) drawRoundRect( . .. ) } } draw by hand “I am a progress bar” We cheat... draw track secret sauce ✨ draw progress moar secret sauce draw highlight

Slide 82

Slide 82 text

We’re almost there…

Slide 83

Slide 83 text

Cutting corners • Don’t overcomplicate things • YAGNI → drop it • Only as flexible as needed • In our example: • Don’t need shapes • “Hardcoded” params • Remember LayoutDirection @Composable fun Button( ) { ... }

Slide 84

Slide 84 text

Cutting corners • Don’t overcomplicate things • YAGNI → drop it @Composable fun Button( ) { ... }

Slide 85

Slide 85 text

Cutting corners • Don’t overcomplicate things • YAGNI → drop it • Only as flexible as needed • In our example: • Don’t need shapes • “Hardcoded” params • Remember LayoutDirection @Composable fun Button( ) { ... }

Slide 86

Slide 86 text

Accessibility • Never forget a11y! • Not a corner to cut • It’s all about semantics • Apply semantics() modif i er • Specify roles • Test with screen readers • Docs and I/O talks

Slide 87

Slide 87 text

What we learnt today

Slide 88

Slide 88 text

RECAP 1. Study Material

Slide 89

Slide 89 text

1. Study Material 2. Use Foundation RECAP

Slide 90

Slide 90 text

1. Study Material 2. Use Foundation 3. Be eff i cient RECAP

Slide 91

Slide 91 text

RECAP 1. Study Material 2. Use Foundation 3. Be eff i

Slide 92

Slide 92 text

RECAP 1. Study Material 2. Use Foundation 3. Be eff i

Slide 93

Slide 93 text

Questions?

Slide 94

Slide 94 text

Compose beyond Material @seebrock3r https://go.sebastiano.dev/beyond-material-2022 Sebastiano Poggi Thanks Jossi 🙌

Slide 95

Slide 95 text

Compose beyond Material @seebrock3r https://go.sebastiano.dev/beyond-material-2022 Sebastiano Poggi Thanks Jossi 🙌