Can you test if a mixin exists?

我是研究僧i 提交于 2019-12-05 00:16:35

Sass does not currently have native functionality to determine if a mixin exists or not.

https://github.com/nex3/sass/issues/561#issuecomment-14430978

You could create a Sass function written in Ruby:

def mixin_exists(mixin_name)
    if(environment.mixin(mixin_name.value))
      Sass::Script::Bool.new(true)
    else
      Sass::Script::Bool.new(false)
    end
end

Or you could use the sass-utilities Compass extension, which has this function included.

The most recent version of Sass (v3.3.0) has a mixin-exists function:

.foo {
  @if mixin-exists(mymixin) {
    exists: true;
  }
  @else {
    exists: false;
  }
}

Sass v3.3 adds other existence tests too:

variable-exists($name)
global-variable-exists($name)
function-exists($name)

More on Sass v3.3.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!