tree: ecd794ba2a60971a4a17c19484092196d2c9886f [path history] [tgz]
  1. .style.yapf
  2. __init__.py
  3. additional_colors_test.json5
  4. base_generator.py
  5. base_generator_test.py
  6. blend_colors_test.json5
  7. blend_colors_test_expected.css
  8. color.py
  9. color_test.py
  10. colors_test.json5
  11. colors_test_custom_dark_toggle_expected.css
  12. colors_test_dark_only_expected.css
  13. colors_test_debug_placeholder_expected.css
  14. colors_test_expected.cc.generated
  15. colors_test_expected.css
  16. colors_test_expected.h.generated
  17. colors_test_expected.proto
  18. colors_test_expected.protojson
  19. colors_test_expected.ts
  20. colors_test_include_style_sheet_expected.ts
  21. colors_test_palette.json5
  22. colors_test_typography_and_untyped_css_expected.ts
  23. colors_test_typography_expected.ts
  24. colors_test_untyped_css_expected.ts
  25. css_generator.py
  26. css_generator.tmpl
  27. DEPS
  28. find_invalid_css_variables.py
  29. find_invalid_css_variables_test.py
  30. inverted_colors_test.json5
  31. inverted_colors_test_expected.css
  32. main.py
  33. opacity.py
  34. OWNERS
  35. path_overrides.py
  36. PRESUBMIT.py
  37. presubmit_support.py
  38. proto_generator.py
  39. proto_generator.tmpl
  40. proto_json_generator.tmpl
  41. README.md
  42. style_variable_generator
  43. style_variable_generator.gni
  44. style_variable_generator_test.py
  45. suppress_sources_comment_test_expected.css
  46. suppress_sources_comment_test_expected.ts
  47. ts_generator.py
  48. ts_generator.tmpl
  49. typography_test.json5
  50. typography_test_expected.css
  51. untyped_css_test.json5
  52. untyped_css_test_expected.css
  53. views_generator.py
  54. views_generator_cc.tmpl
  55. views_generator_h.tmpl
tools/style_variable_generator/README.md

style_variable_generator

This is a python tool that generates cross-platform style variables in order to centralize UI constants.

This script uses third_party/pyjson5 to read input json5 files and then generates various output formats as needed by clients (e.g CSS Variables, preview HTML page).

For input format examples, see the *_test.json5 files which contain up to date illustrations of each feature, as well as expected outputs in the corresponding *_test_expected.* files.

Run ./style_variable_generator -h for usage details.

Typescript

The ts generator mode will output a typescript file which exports all available colors as ts constants. This allows users to directly import colors to use in lit components.

import {html, css, LitElement} from 'lit';
import {customElement, property} from 'lit/decorators.js';
// colors.ts is the output of the script.
import {TEXT_COLOR_PRIMARY} from 'colors.ts';

@customElement('simple-greeting')
export class SimpleGreeting extends LitElement {
  static styles = css`p { color: ${TEXT_COLOR_PRIMARY}} }`;

  @property()
  name = 'Somebody';

  render() {
    return html`<p>Hello, ${this.name}!</p>`;
  }
}

The generated ts file has a single dependency to lit-element which your project will need to be able to resolve when it compiles against it.

NOTE: This file does not export all the colors as rgb strings, but rather each exported constant just points to a css variable. Before you can use these constants you need to include the css variables in your app. The recommended way in chromium is to add a <link> in <head> which points to chrome://resources/chromeos/colors/cros_styles.css.

If you are using semantic colors in a situation where relying on chrome://resources is infeasible you can specify --generator-option 'include_style_sheet=true' and then call getColorsCSS from the generated ts file. This is primarily intended for projects that live outside of chromium and need to ship with an isolated bundle of colors. Once you have the colors you can then add the returned string to your dom via

const allColors = getColorsCSS();
const styleSheet = new CSSStyleSheet();
styleSheet.replaceSync(allColors);
document.adoptedStyleSheets = [styleSheet];

Ensure you run this code before attempting to render the rest of the application so all TS constants correctly resolve.

Note: If you are in Google3 use the installColors helper from //third_party/py/chrome_styles instead which handles non chrome browsers and security considerations.

Generator Options

CSS

Prefix

{
    options: {
        CSS: {
            prefix: 'very'
        }
    },
    colors: {
        important_color: '#ffffff'
    }
}

Puts a prefix before all css variables generated i.e important_color will become --very-important-color in the example above.

NOTE: The typescript generator extends the css generator, as such the css that the typescript file generates will respect the prefix option defined in options.css.prefix.

Dark mode selector

--generator-option 'dark_mode_selector=html[dark]'

Replaces the default media query (@media (prefers-color-scheme: dark)) which triggers colors to switch to dark mode with a custom css selector. The example above would produce

html[dark] {
    ...dark mode colors
}

instead of the default

@media (prefers-color-scheme: dark) {
    html:not(body) {
        ... colors
    }
}

This should only be used if you want to generate a stylesheet for testing where you can control the switch to dark/light mode, in production always prefer to use the default behavior which will respect operating system level dark mode switches.

Debug placeholder

--generator-option 'debug_placeholder=$i18n{CROS_COLORS_DEBUG_OVERRIDES}'

Adds the given token to the CSS output. Chromium will overwrite this token with the debug color CSS when the file is served and debug colors are enabled.

Suppress Sources Comment

If true suppresses adding a comment to the generated output file with a list of all the sources used to generate the file.

--generator-option 'suppress_sources_comment=true'

Defaults to false.

TS

The typescript generator extends the CSS generator so additionally supports all the options from the CSS generator.

Include StyleSheet

--generator-option 'include_style_sheet=true'

If true the generated ts file will also include a function called initializeColors which when called will attach all colors as css variables to the document root. Useful for cases where you don't want to include the colors as a css file. In these cases ensure that this is called before any usage of the ts constants, ideally before the root lit element of an app is rendered.

Defaults to false.

Proto

field_name

{
    options: {
        proto: {
            field_name: 'test_colors'
        }
    },
    colors: {
        important_color: '#ffffff'
    }
}

Name of the field in the output colors message which will contain all exported colors.

field_id

{
    options: {
        proto: {
            field_id: 2
        }
    },
    colors: {
        important_color: '#ffffff'
    }
}

Id of the field in the output colors message which will contain all exported colors.