forked from material-components/material-components-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_mixins.scss
239 lines (225 loc) · 5.7 KB
/
_mixins.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
//
// Copyright 2017 Google Inc. All Rights Reserved.
//
// 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
//
// http://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.
//
/**
* Creates a rule that will be applied when an MDC-Web component is within the context of an RTL layout.
*
* Usage Example:
* ```scss
* .mdc-foo {
* position: absolute;
* left: 0;
*
* @include mdc-rtl {
* left: auto;
* right: 0;
* }
*
* &__bar {
* margin-left: 4px;
* @include mdc-rtl(".mdc-foo") {
* margin-left: auto;
* margin-right: 4px;
* }
* }
* }
*
* .mdc-foo--mod {
* padding-left: 4px;
*
* @include mdc-rtl {
* padding-left: auto;
* padding-right: 4px;
* }
* }
* ```
*
* Note that this works by checking for [dir="rtl"] on an ancestor element. While this will work
* in most cases, it will in some cases lead to false negatives, e.g.
*
* ```html
* <html dir="rtl">
* <!-- ... -->
* <div dir="ltr">
* <div class="mdc-foo">Styled incorrectly as RTL!</div>
* </div>
* </html>
* ```
*
* In the future, selectors such as :dir (http://mdn.io/:dir) will help us mitigate this.
*/
@mixin mdc-rtl($root-selector: null) {
@if ($root-selector) {
@at-root {
[dir="rtl"] #{$root-selector} &,
#{$root-selector}[dir="rtl"] & {
@content;
}
}
}
@else {
[dir="rtl"] &,
&[dir="rtl"] {
@content;
}
}
}
/**
* Takes a base box-model property - e.g. margin / border / padding - along with a default
* direction and value, and emits rules which apply the value to the
* "<base-property>-<default-direction>" property by default, but flips the direction
* when within an RTL context.
*
* For example:
*
* ```scss
* .mdc-foo {
* @include mdc-rtl-reflexive-box(margin, left, 8px);
* }
* ```
* is equivalent to:
*
* ```scss
* .mdc-foo {
* margin-left: 8px;
*
* @include mdc-rtl {
* margin-right: 8px;
* margin-left: 0;
* }
* }
* ```
* whereas:
*
* ```scss
* .mdc-foo {
* @include mdc-rtl-reflexive-box(margin, right, 8px);
* }
* ```
* is equivalent to:
*
* ```scss
* .mdc-foo {
* margin-right: 8px;
*
* @include mdc-rtl {
* margin-right: 0;
* margin-left: 8px;
* }
* }
* ```
*
* You can also pass a 4th optional $root-selector argument which will be forwarded to `mdc-rtl`,
* e.g. `@include mdc-rtl-reflexive-box-property(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().
*/
@mixin mdc-rtl-reflexive-box($base-property, $default-direction, $value, $root-selector: null) {
@if (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 mdc-rtl-reflexive-property($base-property, $left-value, $right-value, $root-selector);
}
/**
* 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 mdc-rtl-reflexive-property(margin, auto, 12px);
* }
* ```
* is equivalent to:
*
* ```scss
* .mdc-foo {
* margin-left: auto;
* margin-right: 12px;
*
* @include mdc-rtl {
* margin-left: 12px;
* margin-right: auto;
* }
* }
* ```
*
* A 4th optional $root-selector argument can be given, which will be passed to `mdc-rtl`.
*/
@mixin mdc-rtl-reflexive-property($base-property, $left-value, $right-value, $root-selector: null) {
$prop-left: #{$base-property}-left;
$prop-right: #{$base-property}-right;
@include mdc-rtl-reflexive_($prop-left, $left-value, $prop-right, $right-value, $root-selector);
}
/**
* 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 mdc-rtl-reflexive-position(left, 0);
* position: absolute;
* }
* ```
* is equivalent to:
*
* ```scss
* .mdc-foo {
* position: absolute;
* left: 0;
* right: initial;
*
* @include mdc-rtl {
* right: 0;
* left: initial;
* }
* }
* ```
* An optional third $root-selector argument may also be given, which is passed to `mdc-rtl`.
*/
@mixin mdc-rtl-reflexive-position($pos, $value, $root-selector: null) {
@if (index((right, left), $pos) == null) {
@error "Invalid position #{pos}. Please specifiy either right or left";
}
$left-value: $value;
$right-value: initial;
@if ($pos == right) {
$right-value: $value;
$left-value: initial;
}
@include mdc-rtl-reflexive_(left, $left-value, right, $right-value, $root-selector);
}
@mixin mdc-rtl-reflexive_(
$left-property,
$left-value,
$right-property,
$right-value,
$root-selector: null
) {
#{$left-property}: $left-value;
#{$right-property}: $right-value;
@include mdc-rtl($root-selector) {
#{$left-property}: $right-value;
#{$right-property}: $left-value;
}
}