Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you test if a mixin exists?

Tags:

sass

mixins

Sass quick question (hopefully) here. Can you test for the existence of a mixin? e.g.

@if thumbnail-mixin {} @else { //define mixin }.

Ideally I'd use @unless, but that only exists on a fork. I'm aware you can overwrite a mixin but I'm thinking more if you can have a default mixin, rather than having to specify N variables in every case.

like image 256
redroot89 Avatar asked Jul 15 '12 18:07

redroot89


People also ask

What is the difference between mixin and variable?

I have been doing some googling and I currently understand the difference being that a variable stores a single line of information whereas, a mixin stores multiple lines of variables.

How do you declare a mixin?

Defining a Mixin A mixin is defined with the @mixin directive. Tip: A tip on hyphens and underscore in Sass: Hyphens and underscores are considered to be the same. This means that @mixin important-text { } and @mixin important_text { } are considered as the same mixin!

Can I use mixin in CSS?

CSS mixins​ Any CSS stylesheet, class or element that is defined in a Stylable CSS file can be used as a mixin source. You can use either a local class or element, or import the mixin from a different stylesheet. In the following example, a locally defined class is used as a mixin in the same stylesheet.


1 Answers

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.

like image 62
KatieK Avatar answered Sep 21 '22 13:09

KatieK