amp-toggle-class
Advanced tools
Comparing version 1.0.1 to 1.1.0
Toggles the existence of a class on an element. If the class exists it will be removed, if it doesn't it will be added. | ||
If a `condition` argument is supplied, the truthiness of that condition is what determines whether the class should exist on the element or not. This simplifies common use case of an element needing a class if `condition` is true. | ||
If a `condition` argument is supplied, the truthiness of that condition is what determines whether the class should exist on the element or not. This simplifies common use case of an element needing a class if `condition` is true. `condition` can be passed as either a primitive which will eventually coerce to boolean or as a function. If you pass a function, you get current element passed as a first argument. |
{ | ||
"name": "amp-toggle-class", | ||
"description": "toggle-class function part of http://amp.ampersandjs.com.", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
"amp": { | ||
"size": { | ||
"original": "3.66 kB", | ||
"minified": "590 B" | ||
"original": "4.55 kB", | ||
"minified": "716 B" | ||
}, | ||
@@ -17,2 +17,3 @@ "internal": false | ||
"amp-has-class": "^1.0.0", | ||
"amp-is-function": "^1.0.1", | ||
"amp-is-undefined": "^1.0.0", | ||
@@ -19,0 +20,0 @@ "amp-remove-class": "^1.0.0" |
@@ -1,1 +0,1 @@ | ||
toggleClass(element, class, [condition]); | ||
toggleClass(element, class, function || boolean); |
31
test.js
@@ -19,10 +19,33 @@ var test = require('tape'); | ||
el = getEl('oh'); | ||
el = getEl('oh'); | ||
toggleClass(el, 'oh', true); | ||
t.equal(el.className, 'oh', 'should leave it alone if condition is passed and class is already present'); | ||
t.equal(el.className, 'oh', 'should leave it alone if condition is boolean true and class is already present'); | ||
el = getEl('oh'); | ||
toggleClass(el, 'hi', true); | ||
t.equal(el.className, 'oh hi', 'should add class if condition is boolean true'); | ||
el = getEl(''); | ||
toggleClass(el, 'oh', false); | ||
t.equal(el.className, '', 'should not add if not present but condition is false'); | ||
t.equal(el.className, '', 'should not remove class if not present and condition is false'); | ||
el = getEl('oh'); | ||
toggleClass(el, 'oh', false); | ||
t.equal(el.className, '', 'should remove class if condition is boolean false'); | ||
// toggling with condition as a booleans | ||
el = getEl('oh'); | ||
toggleClass(el, 'hi', function (element) { | ||
t.equal(el, element, 'should pass element to a condition function'); | ||
return 1 + 1 === 2; | ||
}); | ||
t.equal(el.className, 'oh hi', 'should add class if condition is a function returning true'); | ||
el = getEl('oh'); | ||
toggleClass(el, 'oh', function (element) { | ||
t.equal(el, element, 'should pass element to a condition function'); | ||
return 1 + 1 === 1; | ||
}); | ||
t.equal(el.className, '', 'should remove class if condition is a function returning false'); | ||
var nonsense = [undefined, null, NaN, 0, '']; | ||
@@ -45,3 +68,3 @@ | ||
t.equal(el, toggleClass(el, 'hi', false), 'should always return element'); | ||
// toggling without conditions | ||
@@ -48,0 +71,0 @@ el = getEl('oh'); |
var isUndefined = require('amp-is-undefined'); | ||
var isFunction = require('amp-is-function'); | ||
var hasClass = require('amp-has-class'); | ||
@@ -15,2 +16,3 @@ var addClass = require('amp-add-class'); | ||
if (!isUndefined(condition)) { | ||
condition = isFunction(condition) ? condition.call(null, el) : condition; | ||
action = condition ? 'add' : 'remove'; | ||
@@ -17,0 +19,0 @@ } else { |
6905
95
5
+ Addedamp-is-function@^1.0.1
+ Addedamp-is-function@1.0.1(transitive)