代码之家  ›  专栏  ›  技术社区  ›  user1429980

如何在Stylus中生成占位符

  •  1
  • user1429980  · 技术社区  · 10 年前

    我希望生成占位符和变量,这些占位符和变量可以根据配置进行更改 比例 例如如下:

    • $small-margin-top
    • $large-margin-top
    • $small-padding-bottom

    其中每个占位符将相应的生成变量应用于规则:

    $small-margin-top
        margin-top $marginsmall
    
    $large-margin-top
        margin-top $marginLarge
    
    $small-padding-bottom
        margin-bottom $marginSmall
    

    我现在静态定义了变量:

    /* Margin */
    $margin = 1rem
    $marginsmall = $margin / $multiplier
    $marginlarge = $margin * $multiplierLarge
    $marginmini = $marginSmall / $multiplierLarge
    

    但我得到一个错误:

    TypeError:“name”应为字符串,但得到了ident:marginmini

    properties = margin padding
    
    proportions = mini small medium large
    
    directions = top left bottom right
    
    for property in properties
        for proportion in proportions
                for direction in directions
                    ${property}-{direction}-{proportion}
                        {property}-{direction} lookup(property + proportion)
    

    如何为我的 proportions 变量,以便以后可以扩展生成的占位符( @extend $margin-large )?


    编辑: here is the working solution

    1 回复  |  直到 10 年前
        1
  •  2
  •   Panya    10 年前

    这个 lookup bif接受一个字符串,您将传递一个ident(边距、填充等,不带引号)。您可以使用串联将它们转换为字符串。还有,你错过了 $ 标志:

    properties = margin padding
    
    proportions = mini small medium large
    
    directions = top left bottom right
    
    for property in properties
        for proportion in proportions
            for direction in directions
                ${proportion}-{property}-{direction}
                    {property}-{direction} lookup('$' + property + proportion)