Skip to content

Commit 28ba828

Browse files
committed
feat: Added new mixin for optional css-modules
1 parent 1eb544b commit 28ba828

1 file changed

Lines changed: 33 additions & 30 deletions

File tree

packages/utils/src/_mixins.scss

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,27 @@
314314
}
315315
}
316316

317+
/// This mixin should mostally be used internally within react-md so that
318+
/// react-md mixins that use a `rmd-` prefixed class work correctly if included
319+
/// from a css module file. This basically prefixes the react-md class with
320+
/// `:global` and suffixes with `:local &`.
321+
///
322+
/// @param {String} class-name - The class name that should be optionally
323+
/// prefixed with `:global` when the `$css-modules` parameter is `true`.
324+
/// @param {Boolean} css-modules [false] - Boolean if the class name should be
325+
/// updated to be used with css modules.
326+
/// @param {Boolean} parent-selector [true] - Boolean if the selector should end
327+
/// with the parent selector ` & ` so that the `$class-name` is a parent of the
328+
/// current class.
329+
@mixin rmd-utils-optional-css-modules($class-name, $css-modules: false, $parent-selector: true) {
330+
$selector: if($css-modules, ':global #{$class-name} :local', $class-name);
331+
$selector: if($parent-selector, '#{$selector} &', $selector);
332+
333+
#{$selector} {
334+
@content;
335+
}
336+
}
337+
317338
/// This mixin allows you to add styles to an element only when the user is
318339
/// interacting with your app on a touch device.
319340
///
@@ -335,18 +356,12 @@
335356
/// }
336357
/// }
337358
///
338-
/// @param {Boolean} css-modules [false] - Boolean if this is being used within CSS
339-
/// Modules which will update the selector to work correctly by wrapping
359+
/// @param {Boolean} css-modules [false] - Boolean if this is being used within
360+
/// CSS Modules which will update the selector to work correctly by wrapping
340361
/// different parts with `:global` and `:local`.
341362
@mixin rmd-utils-touch-only($css-modules: false) {
342-
@if $css-modules {
343-
:global .rmd-utils--touch :local & {
344-
@content;
345-
}
346-
} @else {
347-
.rmd-utils--touch & {
348-
@content;
349-
}
363+
@include rmd-utils-optional-css-modules('.rmd-utils--touch', $css-modules) {
364+
@content;
350365
}
351366
}
352367

@@ -371,18 +386,12 @@
371386
/// }
372387
/// }
373388
///
374-
/// @param {Boolean} css-modules [false] - Boolean if this is being used within CSS
375-
/// Modules which will update the selector to work correctly by wrapping
389+
/// @param {Boolean} css-modules [false] - Boolean if this is being used within
390+
/// CSS Modules which will update the selector to work correctly by wrapping
376391
/// different parts with `:global` and `:local`.
377392
@mixin rmd-utils-keyboard-only($css-modules: false) {
378-
@if $css-modules {
379-
:global .rmd-utils--keyboard :local & {
380-
@content;
381-
}
382-
} @else {
383-
.rmd-utils--keyboard & {
384-
@content;
385-
}
393+
@include rmd-utils-optional-css-modules('.rmd-utils--keyboard', $css-modules) {
394+
@content;
386395
}
387396
}
388397

@@ -407,18 +416,12 @@
407416
/// }
408417
/// }
409418
///
410-
/// @param {Boolean} css-modules [false] - Boolean if this is being used within CSS
411-
/// Modules which will update the selector to work correctly by wrapping
419+
/// @param {Boolean} css-modules [false] - Boolean if this is being used within
420+
/// CSS Modules which will update the selector to work correctly by wrapping
412421
/// different parts with `:global` and `:local`.
413422
@mixin rmd-utils-mouse-only($css-modules: false) {
414-
@if $css-modules {
415-
:global .rmd-utils--mouse :local & {
416-
@content;
417-
}
418-
} @else {
419-
.rmd-utils--mouse & {
420-
@content;
421-
}
423+
@include rmd-utils-optional-css-modules('.rmd-utils--mouse', $css-modules) {
424+
@content;
422425
}
423426
}
424427

0 commit comments

Comments
 (0)