config.scss

    1. $namespace:'z'; // 修饰命名空间
    2. $state-prefix: 'is-';// 修饰状态
    3. $modifier-separator:'--'; // 修饰类型的
    4. $element-separator: '__'; // 划分空间分隔符

    mixin.scss

    1. @import "../common/var.scss";
    2. // .z-button{}
    3. @mixin b($block) {
    4. $B: $namespace+'-'+$block;
    5. .#{$B}{
    6. @content;
    7. }
    8. }
    9. // .z-button.is-xxx
    10. @mixin when($state) {
    11. @at-root {
    12. &.#{$state-prefix + $state} {
    13. @content;
    14. }
    15. }
    16. }
    17. // &--primary => .z-button--primary
    18. @mixin m($modifier) {
    19. @at-root {
    20. #{&+$modifier-separator+$modifier} {
    21. @content;
    22. }
    23. }
    24. }
    25. // &__header => .z-button__header
    26. @mixin e($element) {
    27. @at-root {
    28. #{&+$element-separator+$element} {
    29. @content;
    30. }
    31. }
    32. }

    实际用法:

    1. @mixin button-variant($color, $background-color, $border-color) {
    2. color: $color;
    3. background: $background-color;
    4. border-color: $border-color;
    5. }
    6. @include b(button) {
    7. // BEM规范
    8. display: inline-block;
    9. cursor: pointer;
    10. @include when(disabled) { // 针对不同类型处理
    11. &,
    12. &:hover,
    13. &:focus {
    14. cursor: not-allowed
    15. }
    16. }
    17. @include when(round) {
    18. border-radius: 20px;
    19. padding: 12px 23px;
    20. }
    21. @include when(loading) {
    22. pointer-events: none;
    23. }
    24. @include m(primary) { //渲染不同类型的button
    25. @include button-variant($--color-white, $--color-primary, $--color-primary)
    26. }
    27. @include m(success) {
    28. @include button-variant($--color-white, $--color-success, $--color-success)
    29. }
    30. @include m(warning) {
    31. @include button-variant($--color-white, $--color-warning, $--color-warning)
    32. }
    33. @include m(danger) {
    34. @include button-variant($--color-white, $--color-danger, $--color-danger)
    35. }
    36. @include m(info) {
    37. @include button-variant($--color-white, $--color-info, $--color-info)
    38. }
    39. }