LessCss dynamic variables based on ancestor class

前端 未结 1 1110
悲&欢浪女
悲&欢浪女 2020-11-27 23:40

I have a page template which has a branding class on the body element:


    

Africa

相关标签:
1条回答
  • 2020-11-28 00:34

    Well, no, you can't use class name to determine a variable or a return value. So it's usually done in reverse, for example like this:

    @brand-default: #649d84;
    @brand-africa:  #df6f20;
    @brand-nz:      #444444;
    
    h1 {
        .brand-colors();
    }
    
    h2 {
        .brand-colors(background-color);
    }
    
    .brand-colors(@property: color) {
        .color(default);
        .color(africa);
        .color(nz);
    
        .color(@name) {
            .brand-@{name} & {
                @value: 'brand-@{name}';
                @{property}: @@value;
            }
        }
    }
    

    Or like this:

    @brand-default: #649d84;
    @brand-africa:  #df6f20;
    @brand-nz:      #444444;
    
    h1 {
        .brand-colors({
            color: @color;
        });
    }
    
    h2 {
        .brand-colors({
            background-color: @color;
        });
    }
    
    .brand-colors(@style) {
        .brand-color(default);
        .brand-color(africa);
        .brand-color(nz);
    }
    
    .brand-color(@name) {
        .brand-@{name} & {
            @value: ~'brand-@{name}';
            @color: @@value;
            @style();
        }
    }
    

    Or even like this:

    .brand(default) {@{color}: #649d84}
    .brand(africa)  {@{color}: #df6f20}
    .brand(nz)      {@{color}: #444444}
    
    h1 {
        .brand-colors();
    }
    
    h2 {
        .brand-colors(background-color);
    }
    
    .brand-colors(@color: color) {
        .-(default);
        .-(africa);
        .-(nz);
    
        .-(@name) {
            .brand-@{name} & {
                .brand(@name);
            }
        }
    }
    

    Or something in between. Or... oh wait, there's whole family of methods for this stuff (incl. various combinations), see for example:

    • https://stackoverflow.com/a/23660124
    • How to thematize in lesscss
    • https://stackoverflow.com/a/20072967
    • etc.

    Usually list/array/loop based methods are more compact, though personally I prefer something dumb like this:

    .themed({
    
        h1 {
            color: @color;
        }
    
        h2 {
            background-color: @color;
        }
    
    });
    
    .themed(@styles) {
        .-(default, #649d84);
        .-(africa,  #df6f20);
        .-(nz,      #444444);
    
        .-(@name, @color) {
            .brand-@{name} {
                @styles();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题