通过 scss 改变主题

1、设置主题

 window.document.documentElement.setAttribute("data-theme", theme); // 修改主题

2、创建放置主题色的_themes.scss文件

$themes: (
 surpass: ( 
          color-theme: #0188FB,
          color-hover: rgba(1,136,251,0.1),
  ),
 landscape: (
          color-theme: #1759F2,
          color-hover: rgba(23,89,242,0.1),
  )
);

3、 创建一个scss混入语法文件


@import './_themes.scss';

/*
  使用demo
  .app-home {
    font-size: 18px;
    @include themeify {
      color: themed('color-hover');
    };
    @include background_color("color-hover")
  }
*/

@mixin themeify {
  @each $theme-name, $theme-map in $themes {
    $theme-map: $theme-map !global;
    [data-theme=#{$theme-name}] & { //
      @content;
    }
  }
}

@function themed($key) {
  @return map-get($theme-map, $key);
}

@mixin background_color($color) {
  @include themeify {
    background-color: themed($color);
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
点击按钮切换scss变量react是指在React项目中,通过点击按钮来切换应用中使用的scss变量。 在React中,我们可以使用state来保存和管理页面中的状态。所以,我们可以创建一个按钮组件,通过点击按钮来改变其中的scss变量。 首先,在项目中引入所需的scss文件,并创建一个变量文件来保存需要切换的scss变量。 然后,创建一个按钮组件,并在其中定义一个状态变量来保存当前的scss变量: ``` import React, { useState } from "react"; import "./styles.scss"; const Button = () => { const [theme, setTheme] = useState("default"); const handleThemeChange = () => { // 根据当前的主题切换到另一个主题 if (theme === "default") { setTheme("alternative"); } else { setTheme("default"); } }; return ( <button onClick={handleThemeChange} className={theme}> 切换主题 </button> ); }; export default Button; ``` 在上面的例子中,我们创建了一个按钮组件,使用useState来定义了一个名为theme的状态变量,默认值为"default"。在handleThemeChange函数中,我们通过判断当前的theme值来切换到另一个主题。然后,在render函数中,我们将这个theme值作为classname应用到按钮上。 最后,在项目的主文件中引入刚刚创建的按钮组件: ``` import React from "react"; import Button from "./Button"; const App = () => { return ( <div> <h1>点击按钮切换scss变量React</h1> <Button /> </div> ); }; export default App; ``` 上述代码中,我们创建了一个App组件,并在其中使用了刚刚创建的按钮组件Button。 这样,当我们点击按钮时,按钮组件的状态会发生改变,从而切换到另一个scss变量,并且页面中使用该变量的样式会跟着改变

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值