classList
implementation for any environment
Abstract
In this project realized two classes:
-
ClassList
– it's full implementation methods of browser classList property, except that this class is abstract and not tied to DOM. You can just add and remove classes in list, then get result string. -
CNClassList
– is the inheritor of previous class. The main goal of this class is synchronize classes list withclassName
property of the same object. Intended for using with NativeScript and TypeScript if you wish.
Installation
npm install cnclasslist
Include
You can use ClassList
and CNClassList
both in browser and in Node.js as CommonJS-module:
const ClassList = ClassList;const CNClassList = CNClassList;
Or in ES2015 format:
;
ClassList
This class implementing full API of standard classList
from browser.
Constructor
You can pass list of classes when creating instance of ClassList.
new...classNames: Array<string>: ClassList;
Example:
let cl = 'first' 'second';
toString
Method Returns string representation of classes list.
: string;
Example:
let cl = 'first' 'second';cl; // → 'first second'
add
Method Adds classes.
: void;
Example:
let cl = ;cl;cl; // → 'first second'
remove
Method Removes classes.
: void;
Example:
let cl = 'first' 'second' 'third';cl;cl; // → 'second'
contains
Method Checks that in the list contains class with given name.
: boolean;
Example:
let cl = 'first' 'second';cl; // → truecl; // → false
toggle
Method Adds class if his was not and removes if him it is.
With parameter force
class may be only added or only removed accordingly his value.
: boolean;
Example:
let cl = 'first';cl;cl; // → 'first second'cl;cl; // → 'first' cl;cl; // → 'first'cl;cl; // → ''
item
Method Returns class name for given index.
: string;
Example:
let cl = 'first' 'second';cl; // → 'second'cl; // → null
length
Property Number of classes.
length: number;
Example:
let cl = 'first' 'second';cllength; // → 2
CNClassList
API of this class exactly as ClassList
, but as addition he do synchronizing list of classes with property className
of the same object.
Constructor
You should pass object when creating instance of CNClassList.
interface NodeInterface className?: string;newnode: NodeInterface: CNClassList;
Example:
let node = className: '' ;let cl = node;
Example of synchronization
let node = className: 'first second' ;let cl = node;cl; // → 'first second'cl;nodeclassName; // → 'first second third'nodeclassName = 'fourth';cl; // → 'fourth'