Debian Codegraph @

resources/lib/codex/mixins/css-icon.less

raw
1 @import ( reference ) '@wikimedia/codex-icons/codex-icon-paths.less';
2
3 //
4 // To create a CSS-only icon you can do one of the following:
5 // 1. Apply the .cdx-mixin-css-icon() mixin to an empty <span>, passing in at least the icon param.
6 // This method should suffice for any square icon that can exist as a standalone element. This
7 // mixin applies all of the other mixins inside this file. See Message.vue for sample usage.
8 // 2. Apply the individual CSS icon mixins for background, size, alignment, and/or background-image
9 // rules to any element. This can be used to apply an icon within another element, like the
10 // <select> handle. See Select.vue for sample usage.
11 //
12 // These mixins account for icons that vary by reading direction or language.
13 //
14
15 // Get the associated min-size-icon from a size-icon token.
16 .get-calculated-min-size-icon( @param-size-icon ) {
17 // Fallback: when an unrecognized value is passed in, don't crash, but use the same value
18 // for size and min-size (T367098). This code runs for all values, but for recognized values
19 // it's overridden by one of the lines below.
20 @calculated-min-size-icon: @param-size-icon;
21 }
22 .get-calculated-min-size-icon( @param-size-icon ) when ( @param-size-icon = @size-icon-medium ) {
23 @calculated-min-size-icon: @min-size-icon-medium;
24 }
25 .get-calculated-min-size-icon( @param-size-icon ) when ( @param-size-icon = @size-icon-small ) {
26 @calculated-min-size-icon: @min-size-icon-small;
27 }
28 .get-calculated-min-size-icon( @param-size-icon ) when ( @param-size-icon = @size-icon-x-small ) {
29 @calculated-min-size-icon: @min-size-icon-x-small;
30 }
31
32 //
33 // Get background rules for a CSS icon (or mask rules for icons in buttons).
34 //
35 // @param {string} size-icon - The size of the icon, used to set background-size
36 // @param {string} background-position - The background position value
37 // @param {boolean} is-button-icon: Whether this icon is inside of a <button> element.
38 //
39 .cdx-mixin-css-icon-background( @param-size-icon: @size-icon-medium, @param-background-position: @background-position-base, @param-is-button-icon: false ) {
40 .get-calculated-min-size-icon( @param-size-icon );
41
42 // Support Firefox v39-52: fall back to background rules.
43 // Support Chrome: Chrome requires the -webkit prefix so we must use it in all @supports queries.
44 @supports not ( ( -webkit-mask-image: none ) or ( mask-image: none ) ) {
45 background-position: @param-background-position;
46 background-repeat: no-repeat;
47 // Set background size to the relative @param-size-icon or to @calculated-min-size-icon, whichever is larger.
48 // This ensures that the icon will never appear smaller than @calculated-min-size-icon.
49 // Escape the max() call to prevent older Less versions from trying to do the max() calculation at compile time.
50 background-size: calc( ~'max( @{param-size-icon}, @{calculated-min-size-icon} )' );
51 }
52
53 @supports ( -webkit-mask-image: none ) or ( mask-image: none ) {
54 /* stylelint-disable plugin/no-unsupported-browser-features */
55 // Support Chrome
56 -webkit-mask-size: calc( ~'max( @{param-size-icon}, @{calculated-min-size-icon} )' );
57 mask-size: calc( ~'max( @{param-size-icon}, @{calculated-min-size-icon} )' );
58 // Support Chrome
59 -webkit-mask-repeat: no-repeat;
60 mask-repeat: no-repeat;
61 // Support Chrome
62 -webkit-mask-position: @param-background-position;
63 mask-position: @param-background-position;
64 /* stylelint-enable plugin/no-unsupported-browser-features */
65 }
66 }
67
68 //
69 // Get size styles for a CSS icon.
70 //
71 // This sets min-width, min-height, width, and height for a square icon.
72 //
73 // @param {string} size-icon: The size of the icon (base, small, x-small, or indicator)
74 //
75 .cdx-mixin-css-icon-size( @param-size-icon: @size-icon-medium ) {
76 .get-calculated-min-size-icon( @param-size-icon );
77 // Set the default icon size.
78 min-width: @calculated-min-size-icon;
79 min-height: @calculated-min-size-icon;
80 // Scale width/height of the span with font size.
81 width: @param-size-icon;
82 height: @param-size-icon;
83 }
84
85 //
86 // Get alignment styles for a CSS icon.
87 //
88 // @param {string} vertical-align: The vertical-align value
89 //
90 .cdx-mixin-css-icon-alignment( @param-vertical-align: text-bottom ) {
91 display: inline-block;
92 // Vertically align surrounding text in inline, inline-block, and table contexts.
93 vertical-align: @param-vertical-align;
94 }
95
96 // Set the color of a mask-image-based icon
97 //
98 // @param {hex} fill-color - The fill color of the icon
99 // @param {boolean} is-button-icon - Whether this icon is inside of a <button> element
100 .cdx-mixin-css-icon-mask-image-color( @param-fill-color, @param-is-button-icon ) {
101 // For icons outside of buttons, use background-color to set the icon color.
102 & when not ( @param-is-button-icon ) {
103 background-color: @param-fill-color;
104 }
105 // For icons within buttons, set transition rules. The icon color will be changed in the
106 // Button component depending on props and state.
107 & when ( @param-is-button-icon ) {
108 transition-property: @transition-property-icon-css-only;
109 transition-duration: @transition-duration-base;
110 }
111 }
112
113 //
114 // Set the icon image, either via mask-image or background-image.
115 //
116 // For browsers that support mask-image, this mixin will apply the background image as a mask, so
117 // that background-color rules in the Button styles can set the icon color.
118 //
119 // For browsers that don't support mask-image, this mixin sets the icon as a background image with
120 // the color set via opacity.
121 //
122 // @param {string} icon - The icon to show
123 // @param {hex} fill-color - The fill color of the icon (defaults to @color-base)
124 // @param {boolean} is-button-icon - Whether this icon is inside of a <button> element
125 //
126 .cdx-mixin-css-icon-try-mask-image( @param-icon, @param-fill-color, @param-is-button-icon ) {
127 @hex-color-black: #000000;
128 @escaped-color-black: escape( @hex-color-black );
129 @image-url: 'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20" height="20" viewBox="0 0 20 20" fill="@{escaped-color-black}">@{param-icon}</svg>';
130
131 // Support Firefox v39-52: fall back to background-image.
132 // Support Chrome: Chrome requires the -webkit prefix so we must use it in all @supports queries.
133 @supports not ( ( -webkit-mask-image: none ) or ( mask-image: none ) ) {
134 background-image: url( @image-url );
135 // Set icon color to white in night mode.
136 filter: invert( @filter-invert-icon );
137 // Set icon color closer to @color-base.
138 opacity: @opacity-icon-base;
139
140 // Fallback for @color-inverted icons.
141 .cdx-button:not( .cdx-button--weight-quiet ):disabled &,
142 .cdx-button--weight-primary.cdx-button--action-progressive &,
143 .cdx-button--weight-primary.cdx-button--action-destructive & {
144 // Set icon color close to @color-inverted.
145 filter: invert( @filter-invert-primary-button-icon );
146 }
147 }
148
149 // For browsers that support it, use mask-image to set the SVG so we can change the color with
150 // much more granularity.
151 @supports ( -webkit-mask-image: none ) or ( mask-image: none ) {
152 // Support Chrome
153 /* stylelint-disable-next-line plugin/no-unsupported-browser-features */
154 -webkit-mask-image: url( @image-url );
155 /* stylelint-disable-next-line plugin/no-unsupported-browser-features */
156 mask-image: url( @image-url );
157
158 // Set background-color to color the icon
159 .cdx-mixin-css-icon-mask-image-color( @param-fill-color, @param-is-button-icon );
160 }
161 }
162
163 //
164 // Get rules to set the appropriate image for the icon.
165 //
166 // Note that in RTL contexts, this mixin requires `dir="rtl"` either on the icon element itself
167 // or on the <html> element.
168 //
169 // This mixin takes in an icon, which is really a Less variable generated by the codex-icons
170 // package. These variables are lists of icon data that contain:
171 // 1. The default icon path (a string)
172 // 2. Whether the icon should flip in RTL ('true' or 'false')
173 // 3. Exceptions to the flip rule ('false' or a selector string that will rule out languages)
174 // 4. RTL-specific icon path ('false' or the path string)
175 // 5. Whether the icon has language-specific variants ('true' or 'false')
176 // 6+ If there are language-specific variants, they will be included as string pairs after the other
177 // icon data. The first item in the pair is a lang code and the second is the icon path for that
178 // language.
179 //
180 // @param {string} icon - The icon to show (follows the pattern @cdx-icon-icon-name, e.g. @cdx-icon-info-filled )
181 // @param {hex} fill-color - The fill color of the icon (defaults to @color-base)
182 // @param {boolean} is-button-icon - Whether this icon is inside of a <button> element
183 //
184 .cdx-mixin-css-icon-background-image( @param-icon, @param-fill-color: @color-base, @param-is-button-icon: false ) {
185 // Extract icon data from the list.
186 @default-icon: extract( @param-icon, 1 );
187 @should-flip: extract( @param-icon, 2 );
188 @flip-exceptions: extract( @param-icon, 3 );
189 @rtl-icon: extract( @param-icon, 4 );
190 @has-lang-variants: extract( @param-icon, 5 );
191
192 // Add default image.
193 .cdx-mixin-css-icon-try-mask-image( @default-icon, @param-fill-color, @param-is-button-icon );
194
195 // Flip icons with no shouldFlip exceptions.
196 & when ( @should-flip = 'true' ) and ( @flip-exceptions = 'false' ) {
197 &[ dir='rtl' ],
198 html[ dir='rtl' ] &:not( [ dir='ltr' ] ) {
199 transform: scaleX( -1 );
200 }
201 }
202
203 // Flip icons with shouldFlip exceptions.
204 & when ( @should-flip = 'true' ) and not ( @flip-exceptions = 'false' ) {
205 // Create a selector string out of each exception lang code.
206 // Final selector will look like `:not( :lang( he ) ):not( :lang( yi ) )`
207 @exceptions-selector: e( replace( @flip-exceptions, '(^| )([^ ]+)', ':not( :lang( $2 ) )', 'g' ) );
208
209 &[ dir='rtl' ],
210 html[ dir='rtl' ] &:not( [ dir='ltr' ] ) {
211 &@{exceptions-selector} {
212 transform: scaleX( -1 );
213 }
214 }
215 }
216
217 // If an icon has an RTL-specific icon, apply it.
218 & when not ( @rtl-icon = 'false' ) {
219 &[ dir='rtl' ],
220 html[ dir='rtl' ] &:not( [ dir='ltr' ] ) {
221 .cdx-mixin-css-icon-try-mask-image( @rtl-icon, @param-fill-color, @param-is-button-icon );
222 }
223 }
224
225 // Set language-specific icons.
226 & when ( @has-lang-variants = 'true' ) {
227 @icon-list-length: length( @param-icon );
228
229 // Language-specific icons are represented by list items in @param-icon. They consist of a
230 // lang code, e.g. ar, and an icon path.
231 // Since we can't use modern Less features in MediaWiki, we need a recursive mixin.
232 .get-lang-rules( @i: 6 ) when ( @i <= @icon-list-length ) {
233 @lang-data: extract( @param-icon, @i );
234 @lang-code: extract( @lang-data, 1 );
235 @lang-icon: extract( @lang-data, 2 );
236
237 &:lang( @{lang-code} ) {
238 .cdx-mixin-css-icon-try-mask-image( @lang-icon, @param-fill-color, @param-is-button-icon );
239 }
240 .get-lang-rules( @i + 1 );
241 }
242
243 .get-lang-rules();
244 }
245 }
246
247 //
248 // Create a square, standalone CSS icon.
249 //
250 // This mixin only supports icons provided by Codex, which will be embedded as data URLs. To provide
251 // a custom icon (either as a data URL or as a regular URL), set @param-icon to 'none', and set
252 // `mask-image` and `-webkit-mask-image` to the URL of the custom icon.
253 //
254 // @param {string} icon - The icon to show (follows the pattern @cdx-icon-icon-name, e.g. @cdx-icon-info-filled), or 'none'
255 // @param {hex} fill-color - The fill color of the icon
256 // @param {string} size-icon: The size of the icon
257 // @param {boolean} is-button-icon: Whether this icon is inside of a <button> element.
258 // @param {string} background-position - The background position value
259 // @param {string} vertical-align: The vertical-align value
260 //
261 /* stylelint-disable stylistic/indentation */
262 .cdx-mixin-css-icon(
263 @param-icon,
264 @param-fill-color: @color-base,
265 @param-size-icon: @size-icon-medium,
266 @param-is-button-icon: false,
267 @param-background-position: @background-position-base,
268 @param-vertical-align: text-bottom
269 ) {
270 /* stylelint-enable stylistic/indentation */
271 .cdx-mixin-css-icon-background( @param-size-icon, @param-background-position, @param-is-button-icon );
272 .cdx-mixin-css-icon-size( @param-size-icon );
273 .cdx-mixin-css-icon-alignment( @param-vertical-align );
274
275 & when not ( @param-icon = 'none' ) {
276 .cdx-mixin-css-icon-background-image( @param-icon, @param-fill-color, @param-is-button-icon );
277 }
278
279 & when ( @param-icon = 'none' ) {
280 // The caller is going to set mask-image themselves; but we still need to set the color
281 .cdx-mixin-css-icon-mask-image-color( @param-fill-color, @param-is-button-icon );
282 }
283 }