简体   繁体   English

为什么我的 Scss mixin 在我的 React 项目中不起作用?

[英]Why doesnt my Scss mixin work in my React Project?

So I have a new React Project where im trying SCSS for the styling part.所以我有一个新的 React 项目,我在其中尝试 SCSS 作为样式部分。 Im basically a SCSS noob as I've always used Css.我基本上是一个 SCSS 菜鸟,因为我一直使用 Css。 I went online to find tutorials on how to do it and i got this code in the end:我上网查找有关如何操作的教程,最后得到了以下代码:

// _themes.scss
$themes: (
    light: (
        color-background: #FAFAFA,
        color-card: #FFF
    ),
    dark: (
        color-background: #37474F,
        color-card: #212121
    )
);

// _mixins.scss
@mixin theme-aware($key, $color) {
    @each $theme-name, $theme-color in $themes {
        .theme-#{$theme-name} & {
            #{$key}: map.get(map.get($themes, $theme-name), $color)
        }
    }
}

//App.module.scss

@import "../../assets/themes/mixins";

.container {
    width: 100%;
    height: 100%;
    @include theme-aware('background', 'color-background');
}

Now I've tried about everything:现在我已经尝试了一切:

  • Tried to remove the module from the style sheet turning fixinf the imports试图从样式表中删除模块,从而修复导入
  • Define the mixins in the App.module.scss在 App.module.scss 中定义 mixins
  • Put everying (as in themes and mixins) in the App.module.scss将所有内容(如主题和混合)放入 App.module.scss

Here is what I know and understand so far:以下是我目前所知道和理解的:

  • The .theme-#{$theme-name} will apply to the current theme that is applied to the html parent element .theme-#{$theme-name} 将应用于应用于 html 父元素的当前主题
  • This application is running and it currently switches between and (I have also tried to apply the styling to the React root element with no success)此应用程序正在运行,它当前在和之间切换(我也尝试将样式应用于 React 根元素但没有成功)
  • When i define other mixins, simpler ones, not envolving map-get it works perfectly当我定义其他 mixins,更简单的,不涉及 map-get 时,它可以完美运行
  • Im using Dart Sass seen in npm [here][1]我正在使用 npm [这里][1] 中看到的 Dart Sass

What am I not understanding?我不明白什么? The tutorial i saw online used node-scss and worked, so far is the only difference i can find我在网上看到的教程使用了node-scss并且有效,到目前为止我能找到的唯一区别

EDIT:: I finally found the issue.编辑:: 我终于找到了问题。 It does work IF I dont use React Router.如果我不使用 React Router,它确实有效。 My App.tsx:我的 App.tsx:

export default function App() {

    const dispatch = useDispatch()
    const session = useSelector(sessionSelector)
    const theme = useSelector(themeSelector)

    useEffect(() => {
        document.documentElement.className = '';
        document.documentElement.classList.add(`theme-${theme.name}`)
    }, [theme])

    return (
        <div>
            <Home />
            <Routes>
                <Route path={paths.home} element={<Outlet />}>
                    <Route element={<RequireAuth roles={["SUPER"]} />}>

                    </Route>

                    <Route element={<RequireAuth roles={["SUPER", "ADMIN"]} />}>

                    </Route>

                    <Route element={<RequireAuth roles={["SUPER"]} />}>

                    </Route>
                </Route>
            </Routes>
        </div>
    )
}

As you can see, I apply the scss theme to the document itself.如您所见,我将 scss 主题应用于文档本身。 It does work in my component and all colors change, but the routes do not recognize the class.它在我的组件中确实有效,并且所有颜色都发生了变化,但是路由无法识别该类。 To fix it i applied the style to the body like:为了修复它,我将样式应用于身体,例如:

@import "./assets/themes/mixins";

* {
    @include theme-aware('background', 'color-background');
}

You didn't use .scss extension in react if you don't use JavaScript then make sure add that language extension..如果您不使用 JavaScript,则您没有在反应中使用 .scss 扩展名,那么请确保添加该语言扩展名..

@import 
"../../assets/themes/_mixins.scss"; 
      Or

If your mixin scss file name not contain (_) underscore then use this如果您的 mixin scss 文件名不包含 (_) 下划线,则使用此

@import 
"../../assets/themes/mixins.scss;

The problem is there in _mixins.scss , you need to import of sass:map to use map.get or else you can also use map-get directly without importing sass:map and I have replaced hyphen in .theme-#{$theme-name} in mixins.scss with underscore .theme_#{$theme-name} .问题出在_mixins.scss中,您需要导入sass:map才能使用map.get或者您也可以直接使用map-get而无需导入sass:map并且我已经替换了.theme-#{$theme-name}中的连字符.theme-#{$theme-name}mixins.scss中带有下划线.theme_#{$theme-name} With these two changes in the code you provided the appropriate theme is getting applied.通过您提供的代码中的这两个更改,将应用适当的主题。

// _themes.scss
 $themes: (
light: (
    color-background: #FAFAFA,
    color-card: #FFF
),
dark: (
    color-background: #37474F,
    color-card: #212121
)
);

   // _mixins.scss
   @use "sass:map";
   @import "./themes";

   @mixin theme-aware($key, $color) {
       @each $theme-name, $theme-color in $themes {
          .theme_#{$theme-name} & {
             #{$key}: map.get(map.get($themes, $theme-name), $color);
       }
      }
    }

 //App.module.scss
  @import "./mixins";

 .container {
      width: 100%;
      height: 100%;
       @include theme-aware('background-color','color-background');
   } 


//App.module.scss
import styles from "./app.module.scss";

export default function App() {
     return (
         <div className={styles.theme_dark}>
             <div className={styles.container}>
                <h1>Hello CodeSandbox</h1>
                <h2>Start editing to see some magic happen!</h2>
             </div>
         </div>
      );
    }

工作示例 Working Example you can find here:- https://codesandbox.io/s/customised-theme-6dvt04?file=/src/_mixins.scss您可以在这里找到工作示例:- https://codesandbox.io/s/customised-theme-6dvt04?file=/src/_mixins.scss

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM