-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
_rtl.scss
336 lines (315 loc) · 7.97 KB
/
_rtl.scss
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//
// Copyright 2017 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Selector '.mdc-*' should only be used in this project.
// stylelint-disable selector-class-pattern
@use 'sass:list';
@use 'sass:meta';
@use '@material/theme/theme';
$include: true !default;
/// Creates a rule that will be applied when a component is within the context
/// of an RTL layout.
///
/// @example - scss
/// .mdc-foo {
/// padding-left: 4px;
///
/// @include rtl {
/// padding-left: auto;
/// padding-right: 4px;
/// }
/// }
///
/// @example - css
/// .mdc-foo {
/// padding-left: 4px;
/// }
///
/// [dir="rtl"] .mdc-foo,
/// .mdc-foo[dir="rtl"] {
/// padding-left: auto;
/// padding-right: 4px;
/// }
///
/// Note that this mixin works by checking for an ancestor element with
/// `[dir="rtl"]`. As a result, nested `dir` values are not supported:
///
/// @example - html
/// <html dir="rtl">
/// <!-- ... -->
/// <div dir="ltr">
/// <div class="mdc-foo">Styled incorrectly as RTL!</div>
/// </div>
/// </html>
///
/// In the future, selectors such as the `:dir` pseudo-class
/// (http://mdn.io/css/:dir) will help us mitigate this.
///
/// @content Content to be styled in an RTL context.
@mixin rtl() {
@if ($include) {
[dir='rtl'] &,
&[dir='rtl'] {
@content;
}
}
}
// Takes a base box-model property name (`margin`, `border`, `padding`, etc.) along with a
// default direction (`left` or `right`) and value, and emits rules which apply the given value to the
// specified direction by default and the opposite direction in RTL.
//
// For example:
//
// ```scss
// .mdc-foo {
// @include rtl-reflexive-box(margin, left, 8px);
// }
// ```
//
// is equivalent to:
//
// ```scss
// .mdc-foo {
// margin-left: 8px;
// margin-right: 0;
//
// @include rtl {
// margin-left: 0;
// margin-right: 8px;
// }
// }
// ```
//
// whereas:
//
// ```scss
// .mdc-foo {
// @include rtl-reflexive-box(margin, right, 8px);
// }
// ```
//
// is equivalent to:
//
// ```scss
// .mdc-foo {
// margin-left: 0;
// margin-right: 8px;
//
// @include rtl {
// margin-left: 8px;
// margin-right: 0;
// }
// }
// ```
//
// You can also pass an optional 4th `$root-selector` argument which will be forwarded to `mdc-rtl`,
// e.g. `@include rtl-reflexive-box(margin, left, 8px, '.mdc-component')`.
//
// Note that this function will always zero out the original value in an RTL context.
// If you're trying to flip the values, use `mdc-rtl-reflexive-property()` instead.
@mixin reflexive-box(
$base-property,
$default-direction,
$value,
$replace: null
) {
@if (list.index((right, left), $default-direction) == null) {
@error "Invalid default direction: '#{$default-direction}'. Please specifiy either 'right' or 'left'.";
}
$left-value: $value;
$right-value: 0;
@if ($default-direction == right) {
$left-value: 0;
$right-value: $value;
}
@include reflexive-property(
$base-property,
$left-value,
$right-value,
$replace: $replace
);
}
// Takes a base property and emits rules that assign <base-property>-left to <left-value> and
// <base-property>-right to <right-value> in a LTR context, and vice versa in a RTL context.
// For example:
//
// ```scss
// .mdc-foo {
// @include rtl-reflexive-property(margin, auto, 12px);
// }
// ```
//
// is equivalent to:
//
// ```scss
// .mdc-foo {
// margin-left: auto;
// margin-right: 12px;
//
// @include rtl {
// margin-left: 12px;
// margin-right: auto;
// }
// }
// ```
//
// An optional 4th `$root-selector` argument can be given, which will be passed to `mdc-rtl`.
@mixin reflexive-property(
$base-property,
$left-value,
$right-value,
$replace: null
) {
$prop-left: #{$base-property}-left;
$prop-right: #{$base-property}-right;
@include reflexive(
$prop-left,
$left-value,
$prop-right,
$right-value,
$replace: $replace
);
}
// Takes an argument specifying a horizontal position property (either 'left' or 'right') as well
// as a value, and applies that value to the specified position in a LTR context, and flips it in a
// RTL context. For example:
//
// ```scss
// .mdc-foo {
// @include rtl-reflexive-position(left, 0);
// }
// ```
//
// is equivalent to:
//
// ```scss
// .mdc-foo {
// left: 0;
// right: initial;
//
// @include rtl {
// left: initial;
// right: 0;
// }
// }
// ```
//
// An optional third $root-selector argument may also be given, which is passed to `mdc-rtl`.
@mixin reflexive-position($position-property, $value, $replace: null) {
@if (list.index((right, left), $position-property) == null) {
@error "Invalid position #{position-property}. Please specifiy either right or left";
}
// TODO: 'initial' is not supported in IE 11. https://caniuse.com/#feat=css-initial-value
$left-value: $value;
$right-value: initial;
@if ($position-property == right) {
$right-value: $value;
$left-value: initial;
}
@include reflexive(
left,
$left-value,
right,
$right-value,
$replace: $replace
);
}
// Takes pair of properties with values as arguments and flips it in RTL context.
// For example:
//
// ```scss
// .mdc-foo {
// @include rtl-reflexive(left, 2px, right, 5px);
// }
// ```
//
// is equivalent to:
//
// ```scss
// .mdc-foo {
// left: 2px;
// right: 5px;
//
// @include rtl {
// right: 2px;
// left: 5px;
// }
// }
// ```
//
// An optional fifth `$root-selector` argument may also be given, which is passed to `mdc-rtl`.
@mixin reflexive(
$left-property,
$left-value,
$right-property,
$right-value,
$replace: null
) {
$left-replace: null;
$right-replace: null;
@if $replace {
@if meta.type-of($left-value) == 'string' {
$left-replace: $replace;
}
@if meta.type-of($right-value) == 'string' {
$right-replace: $replace;
}
@if $left-replace == null and $right-replace == null {
@error 'mdc-rtl: $replace may only be used with strings but neither left nor right values are strings.';
}
// If any replacements are null, treat the entire value as null (do not
// emit anything).
@each $name, $replacement in $replace {
@if $replacement == null {
$left-value: null;
$right-value: null;
}
}
}
// Do not emit if either value are null
@if $left-value and $right-value {
@include _property($left-property, $left-value, $replace: $left-replace);
@include _property($right-property, $right-value, $replace: $right-replace);
@include rtl {
@include _property(
$left-property,
$right-value,
$replace: $right-replace
);
@include _property($right-property, $left-value, $replace: $left-replace);
}
}
}
///
/// Adds `@noflip` annotation when `$mdc-rtl-include` is true.
///
/// @param {String} $property
/// @param {String} $value
/// @param {Map} $replace
///
@mixin _property($property, $value, $replace: null) {
@include theme.property(
$property,
$value,
$replace: $replace,
$gss: (noflip: $include)
);
}