forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts_generator.tmpl
172 lines (157 loc) · 6.03 KB
/
ts_generator.tmpl
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
/* Copyright 2021 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. */
{%- if not suppress_sources_comment %}
/* This file is generated from:
{%- for path in in_files %}
* {{path}}
{%- endfor %}
*/
{%- endif %}
{% macro render_variables_as_css(mode) -%}
{%- for model_name, color in colors[mode].items() %}
{{model_name | to_css_var_name}}-rgb: {{color | css_color_rgb}};
{{model_name | to_css_var_name}}: {{css_color_var(model_name, color)}};
{% endfor %}
{%- for name, value in opacities[mode].items() %}
{{name | to_css_var_name}}: {{value | css_opacity}};
{% endfor -%}
{%- endmacro %}
import {css} from 'lit';
{%- if include_style_sheet %}
/* SAFETY_BOILERPLATE */
export interface GetColorsCSSOptions {
/** The html selector the colors should be attached too. */
target?: 'host' | 'html';
/**
* Generate a css dump which sets variables to their dark mode values in light
* mode and vice versa. If true lockTheme is ignored.
*/
invert?: boolean;
/**
* Generate a css dump which sets variables to either their dark mode or light
* mode values and ignores the documents prefers-color-scheme.
*/
lockTheme?: 'light' | 'dark';
}
// Use a ternary expression that can only be evaluated at runtime here to force
// closure to leave these string constants as variables instead of inlining them
// in the below template strings. Not doing this results in a unreasonable file
// size increase. See b/209520919.
const DEFAULT_CSS = window ? `
{{- render_variables_as_css(Modes.DEFAULT) -}}
` : '';
const DARK_MODE_OVERRIDES_CSS = window ? `
{{- render_variables_as_css(Modes.DARK) -}}
` : '';
const UNTYPED_CSS = window ? `
{%- if untyped_css %}
{%- for group_name, vars in untyped_css.items() %}
/* {{group_name}} */
{%- for name, value in vars.items() %}
{{name | to_css_var_name}}: {{value}};
{%- endfor %}
{%- endfor %}
{% endif -%}
` : '';
const TYPOGRAPHY_CSS = window ? `
{%- if typography.font_families or typography.typefaces %}
/* font families */
{%- for name, value in typography.font_families.items() %}
{{name | to_css_var_name}}: {{value}};
{%- endfor %}
/* typefaces */
{%- for name, typeface in typography.typefaces.items() %}
{{name | to_css_var_name}}-font: {{typeface.font_weight}} {{typeface.font_size}}px/{{typeface.line_height}}px {{typeface.font_family | process_simple_ref}};
{{name | to_css_var_name}}-font-family: {{typeface.font_family | process_simple_ref}};
{{name | to_css_var_name}}-font-size: {{typeface.font_size}}px;
{{name | to_css_var_name}}-font-weight: {{typeface.font_weight}};
{{name | to_css_var_name}}-line-height: {{typeface.line_height}}px;
{%- endfor %}
{% endif -%}
` : '';
/**
* Returns a string containing all semantic colors exported in this file as
* css variables. This string an be used to construct a stylesheet which can be
* placed in the dom at runtime, see tools/style_variable_generator/README.md
* for more info. Ensure the css returned by this function is added to the dom
* before the rendering of the first element on the page so you are guaranteed
* that all TS constant references resolve correctly.
*/
export function getColorsCSS(options?: GetColorsCSSOptions) {
let cssString;
if (options?.invert) {
cssString = /* SAFE */ (`
${options?.target === 'host' ? ':host' : 'html:not(body)'} {
${DEFAULT_CSS}
${UNTYPED_CSS}
${TYPOGRAPHY_CSS}
${DARK_MODE_OVERRIDES_CSS}
}
@media (prefers-color-scheme: dark) {
${options?.target === 'host' ? ':host' : 'html:not(body)'} {
${DEFAULT_CSS}
}
}
`);
} else if (options?.lockTheme === 'light') {
// Tag strings which are safe with a special comment so copybara can add
// the right safety wrappers whem moving this code into Google3.
cssString = /* SAFE */ (`
${options?.target === 'host' ? ':host' : 'html:not(body)'} {
${DEFAULT_CSS}
${UNTYPED_CSS}
${TYPOGRAPHY_CSS}
}
`);
} else if (options?.lockTheme === 'dark') {
cssString = /* SAFE */ (`
${options?.target === 'host' ? ':host' : 'html:not(body)'} {
${DEFAULT_CSS}
${UNTYPED_CSS}
${TYPOGRAPHY_CSS}
${DARK_MODE_OVERRIDES_CSS}
}
`);
} else {
cssString = /* SAFE */ (`
${options?.target === 'host' ? ':host' : 'html:not(body)'} {
${DEFAULT_CSS}
${UNTYPED_CSS}
${TYPOGRAPHY_CSS}
}
@media (prefers-color-scheme: dark) {
${options?.target === 'host' ? ':host' : 'html:not(body)'} {
${DARK_MODE_OVERRIDES_CSS}
}
}
`);
}
return cssString;
}
{%- endif %}
{% for model_name, color in colors[Modes.DEFAULT].items() -%}
export const {{model_name | to_ts_var_name}} = css`var({{model_name | to_css_var_name}})`;
{% endfor %}
{%- for model_name, value in opacities[Modes.DEFAULT].items() -%}
export const {{model_name | to_ts_var_name}} = css`var({{model_name | to_css_var_name}})`;
{% endfor -%}
{%- if untyped_css %}
{%- for group_name, vars in untyped_css.items() %}
{%- for name, value in vars.items() %}
export const {{name | to_ts_var_name}} = css`var({{name | to_css_var_name}})`;
{%- endfor %}
{% endfor %}
{%- endif -%}
{%- if typography.font_families or typography.typefaces %}
{%- for name, value in typography.font_families.items() %}
export const {{name | to_ts_var_name}} = css`var({{name | to_css_var_name}}-font)`;
{%- endfor %}
{%- for name, typeface in typography.typefaces.items() %}
export const {{name | to_ts_var_name}}_FONT = css`var({{name | to_css_var_name}}-font)`;
export const {{name | to_ts_var_name}}_FONT_FAMILY = css`var({{name | to_css_var_name}}-font-family)`;
export const {{name | to_ts_var_name}}_FONT_SIZE = css`var({{name | to_css_var_name}}-font-size)`;
export const {{name | to_ts_var_name}}_FONT_WEIGHT = css`var({{name | to_css_var_name}}-font-weight)`;
export const {{name | to_ts_var_name}}_LINE_HEIGHT = css`var({{name | to_css_var_name}}-line-height)`;
{% endfor -%}
{%- endif -%}