GithubHelp home page GithubHelp logo

isabella232 / material-components-android-compose-theme-adapter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from material-components/material-components-android-compose-theme-adapter

0.0 0.0 0.0 3.39 MB

A library that enables reuse of Material themes defined in XML for theming in Jetpack Compose.

Home Page: https://material-components.github.io/material-components-android-compose-theme-adapter/

License: Apache License 2.0

Kotlin 99.24% Shell 0.76%

material-components-android-compose-theme-adapter's Introduction

MDC-Android Compose Theme Adapter

A library that enables reuse of Material Components for Android XML themes for theming in Jetpack Compose.

Usage

There are two artifacts available:

repositories {
    google()
}

dependencies {
    // Compatible with Compose Material, includes MdcTheme
    implementation "com.google.android.material:compose-theme-adapter:<version>"
    // Compatible with Compose Material 3, includes Mdc3Theme
    implementation "com.google.android.material:compose-theme-adapter-3:<version>"
}

See the releases page for the latest versions.


Compose Material

MDC-Android Compose Theme Adapter header

The basis of Material Design 2 theming in Jetpack Compose is the MaterialTheme composable, where you provide Colors, Shapes and Typography instances containing your styling parameters:

MaterialTheme(
    colors = colors,
    typography = type,
    shapes = shapes
) {
    // M2 Surface, Scaffold, etc.
}

Material Components for Android themes allow for similar theming for views via XML theme attributes, like so:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <!-- Material 2 color attributes -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorSecondary">@color/green_200</item>

    <!-- Material 2 type attributes-->
    <item name="textAppearanceBody1">@style/TextAppearance.MyApp.Body1</item>
    <item name="textAppearanceBody2">@style/TextAppearance.MyApp.Body2</item>

    <!-- Material 2 shape attributes-->
    <item name="shapeAppearanceSmallComponent">@style/ShapeAppearance.MyApp.SmallComponent</item>
</style>

This library attempts to bridge the gap between Material Components for Android M2 XML themes, and themes in Jetpack Compose, allowing your composable MaterialTheme to be based on the Activity's XML theme:

MdcTheme {
    // MaterialTheme.colors, MaterialTheme.shapes, MaterialTheme.typography
    // will now contain copies of the Context's theme
}

This is especially handy when you're migrating an existing app, a Fragment (or other UI container) at a time.

!!! caution If you are using an AppCompat (i.e. non-MDC) theme in your app, you should use the AppCompat Compose Theme Adapter instead, as it attempts to bridge the gap between AppCompat XML themes, and M2 themes in Jetpack Compose.

Customizing the M2 theme

The MdcTheme() function will automatically read the host Context's MDC theme and pass them to MaterialTheme on your behalf, but if you want to customize the generated values, you can do so via the createMdcTheme() function:

val context = LocalContext.current
val layoutDirection = LocalLayoutDirection.current
var (colors, typography, shapes) = createMdcTheme(
    context = context,
    layoutDirection = layoutDirection
)

// Modify colors, typography or shapes as required.
// Then pass them through to MaterialTheme...

MaterialTheme(
    colors = colors,
    typography = type,
    shapes = shapes
) {
    // Rest of M2 layout
}

Limitations

There are some known limitations with the implementation at the moment:

  • This relies on your Activity/Context theme extending one of the Theme.MaterialComponents themes.
  • Text colors are not read from the text appearances by default. You can enable it via the setTextColors function parameter.
  • Variable fonts are not supported in Compose yet, meaning that the value of android:fontVariationSettings are currently ignored.
  • MDC ShapeAppearances allow setting of different corner families (cut, rounded) on each corner, whereas Compose's Shapes allows only a single corner family for the entire shape. Therefore only the app:cornerFamily attribute is read, others (app:cornerFamilyTopLeft, etc) are ignored.
  • You can modify the resulting MaterialTheme in Compose as required, but this only works in Compose. Any changes you make will not be reflected in the Activity theme.

Compose Material 3

MDC-Android Compose Theme Adapter header

The basis of Material Design 3 theming in Jetpack Compose is the MaterialTheme composable, where you provide ColorScheme and Typography instances containing your styling parameters:

MaterialTheme(
    colorScheme = colorScheme,
    typography = typography
) {
    // M3 Surface, Scaffold, etc.
}

Material Components for Android themes allow for similar theming for views via XML theme attributes, like so:

<style name="Theme.MyApp" parent="Theme.Material3.DayNight">
    <!-- Material 3 color attributes -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorSecondary">@color/purple_700</item>
    <item name="colorTertiary">@color/green_200</item>

    <!-- Material 3 type attributes-->
    <item name="textAppearanceBodyLarge">@style/TextAppearance.MyApp.BodyLarge</item>
    <item name="textAppearanceBodyMedium">@style/TextAppearance.MyApp.BodyMedium</item>
</style>

This library attempts to bridge the gap between Material Components for Android M3 XML themes, and themes in Jetpack Compose, allowing your composable MaterialTheme to be based on the Activity's XML theme:

Mdc3Theme {
    // MaterialTheme.colorScheme, MaterialTheme.typography
    // will now contain copies of the Context's theme
}

This is especially handy when you're migrating an existing app, a Fragment (or other UI container) at a time.

Customizing the M3 theme

The Mdc3Theme() function will automatically read the host Context's MDC theme and pass them to MaterialTheme on your behalf, but if you want to customize the generated values, you can do so via the createMdc3Theme() function:

val context = LocalContext.current
var (colorScheme, typography) = createMdc3Theme(
    context = context
)

// Modify colorScheme or typography as required.
// Then pass them through to MaterialTheme...

MaterialTheme(
    colorScheme = colorScheme,
    typography = typography
) {
    // Rest of M3 layout
}

Limitations

There are some known limitations with the implementation at the moment:

  • This relies on your Activity/Context theme extending one of the Theme.Material3 themes.
  • Text colors are not read from the text appearances by default. You can enable it via the setTextColors function parameter.
  • Variable fonts are not supported in Compose yet, meaning that the value of android:fontVariationSettings are currently ignored.
  • You can modify the resulting MaterialTheme in Compose as required, but this only works in Compose. Any changes you make will not be reflected in the Activity theme.

Library Snapshots

Snapshots of the current development version of this library are available, which track the latest commit. See here for more information on how to use them.


Contributions

Please contribute! We will gladly review any pull requests. Make sure to read the Contributing page first though.

License

Copyright 2020 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

    https://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.

material-components-android-compose-theme-adapter's People

Contributors

aasitnikov avatar chrisbanes avatar dsn5ft avatar jossiwolf avatar jreehuis avatar material-admin avatar numeroanddev avatar ricknout avatar yrezgui avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.