【问题标题】:How to properly generate Sass Themes如何正确生成 Sass 主题
【发布时间】:2019-02-06 02:56:17
【问题描述】:

我需要根据 sass 变量生成 css。如何转这个:

$themes: (
  light: (
    text: black
  ),

  dark: (
    text: white
  )
);    

.foo {
  display: flex;
  color: @include themify('text');
}

进入这个:

.foo {
  display: flex;
}

.light .foo {
  color: #000;
}

.dark .foo {
  color: #fff;
}

关于如何实现这一点的任何想法?

【问题讨论】:

    标签: css sass


    【解决方案1】:

    您不能使用 mixin 重用颜色 declaration,因此您需要将其传递给 mixin。

    检查一下

    $themes: (
      light: (
        text: black
      ),
    
      dark: (
        text: white
      )
    );
    
    @mixin themify($prop, $key, $themeList: $themes) {
      @each $themeName ,$theme in $themeList {
        $value: map-get($theme, $key);
        .#{$value} {
          #{$prop}: $value;
        } 
      }
    }
    
    .foo {
      display: flex;
      @include themify('color', 'text');
    }
    

    结果将是:https://gist.github.com/felixmosh/c8110a93ef55a0a52bd369b23f88e525

    【讨论】:

      【解决方案2】:

      给你

      /*
       * Theme definitions
       */
      
      $themes: (
        light: (
          backgroundColor: white,
          textColor: #408bbd,
          buttonTextColor: #408bbd,
          buttonTextTransform: none,
          buttonTextHoverColor: #61b0e7,
          buttonColor: #fff,
          buttonBorder: 2px solid #408bbd,
        ),
        dark: (
          backgroundColor: #222,
          textColor: #ddd,
          buttonTextColor: #aaa,
          buttonTextTransform: uppercase,
          buttonTextHoverColor: #ddd,
          buttonColor: #333,
          buttonBorder: 1px solid #333,
        ),
        purple: (
          backgroundColor: purple,
          textColor: #ddd,
          buttonTextColor: #aaa,
          buttonTextTransform: uppercase,
          buttonTextHoverColor: #ddd,
          buttonColor: #333,
          buttonBorder: 1px solid #333,
        ),
      );
      
      /*
       * Implementation of themes
       */
      @mixin themify($themes) {
        @each $theme, $map in $themes {
          .theme-#{$theme} & {
            $theme-map: () !global;
            @each $key, $submap in $map {
              $value: map-get(map-get($themes, $theme), '#{$key}');
              $theme-map: map-merge($theme-map, ($key: $value)) !global;
            }
            @content;
            $theme-map: null !global;
          }
        }
      }
      
      @function themed($key) {
        @return map-get($theme-map, $key);
      }
      
      /*
       * Actual styles for the app
       */
      
      body {
        margin: 0;
      }
      
      html, body {
        height: 100%;
      }
      
      #app-root {
        margin: 0;
        padding: 0;
        height: 100%;
        display: flex;
        flex-direction: column;
        
        > div {
          display: flex;
          flex: 1;
        }
      }
      
      .app-container {
        display: flex;
        flex-direction: column;
        flex: 1;
        align-items: center;
        justify-content: center;
      
        .title {
          font-family: sans-serif;
          font-weight: lighter;
        }
      
        @include themify($themes) {
          color: themed('textColor');  
          background-color: themed('backgroundColor');  
        }
      
        .button {
          max-width: 20em;
          cursor: pointer;
          border-radius: 5px;
          padding: 15px 32px;
          display: inline-block;
          transition: color 0.1s, border-color 0.1s, background-color 0.1s;
      
          @include themify($themes) {
            border: themed('buttonBorder');
            color: themed('buttonTextColor'); 
            border-color: themed('buttonTextColor');
            background-color: themed('buttonColor');
            text-transform: themed('buttonTextTransform');
      
            &:hover {
              color: themed('buttonTextHoverColor'); 
              border-color: themed('buttonTextHoverColor');
              background-color: themed('buttonHoverColor');
            }
          }
        } 
      }
      <main id="app-root">
        <div class="theme-purple">
          <div class="app-container">
            <h1 class="title">Purple theme</h1>
            <button class="button">A button</button>
          </div>
        </div>
        <div class="theme-light">
          <div class="app-container">
            <h1 class="title">Light theme</h1>
            <button class="button">A button</button>
          </div>
        </div>
      
        <div class="theme-dark">
          <div class="app-container">
            <h1 class="title">Dark theme</h1>
            <button class="button">A button</button>
          </div>
        </div>
        
        
      
      </main>

      demo

      【讨论】:

        猜你喜欢
        • 2014-08-25
        • 2014-01-07
        • 2020-01-08
        • 2018-08-20
        • 2014-09-18
        • 2015-07-02
        • 2015-08-06
        • 2016-04-10
        • 1970-01-01
        相关资源
        最近更新 更多