使用 Sass 以可重用的方式设计一组特定的输入类型

2024-03-06

我想要一个 mixin 函数,它返回 HTML5 输入类型的列表。我想在一个地方管理它,当新类型出现时,改变函数,而不是代码中其他地方的所有地方。

问题似乎是 mixins 的设计目的不是返回可以在 CSS 花括号之外使用的字符串。这是我的 mixin 的示例(当前返回错误)以及我如何使用它:

/* 
 * Set all the up-and-coming input text types here for easier reference 
 * Does not include types that are not meant to be displayed full width, such as: 
        type=number, type=range, type=date, type=color
 */
@mixin input_text_types( $focus:false ) {
    @if $focus {
        @return #{input[type=text]:focus, input[type=password]:focus, input[type=search]:focus, input[type=email]:focus, input[type=url]:focus, input[type=tel]:focus};
    } @else {
        @return #{input[type=text], input[type=password], input[type=search], input[type=email], input[type=url], input[type=tel]};
    }
}

In use:

@include input_text_types() {
    width: 80%; 
}

我得到的错误就像error sass/style.scss (Line 134 of sass/_functions.scss: Invalid CSS after "...@return #{input": expected "}", was "[type=text]:foc...")

我尝试过使用或不使用以下格式来格式化输出@return指令,并用我见过的不同方式将字符串值括起来(用引号、单引号、用散列括起来的花括号)。以前有人尝试过做这样的事情吗?


通过使用变量来包含选择器可能会更好地解决您的问题。通过使用 mixin,您将失去将其与相似元素链接的能力。

$form-input-text: 'input[type="text"], input[type="password"], input[type="search"], input[type="email"], input[type="tel"], input[type="url"]';
$form-input-buttons: 'input[type="submit"], input[type="reset"], input[type="button"], button';
$form-input-dates: 'input[type^="date"], input[type="month"], input[type="week"], input[type="time"]';
$form-input-not-radio: 'input:not([type="radio"]):not([type="checkbox"])';

#{$form-input-text}, textarea {
    @include border-radius(.25em);
    border: $form-input-border;
}

#{$form-input-text}, textarea, input[type="file"] {
    width: $form-input-width;
    max-width: 100%;
    -webkit-appearance: textfield
}

#{$form-input-buttons} {
    padding: .25em .5em;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Sass 以可重用的方式设计一组特定的输入类型 的相关文章

随机推荐