Module: DOM::ClassList
- Included in:
- Element
- Defined in:
- opal/fron/dom/modules/classlist.rb
Overview
ClassList management for DOM::Element
Instance Method Summary collapse
-
#add_class(*classes) ⇒ Object
Adds classes to the class list.
-
#has_class(cls) ⇒ Boolean
Returns whether the class list has the given class or not.
-
#remove_class(*classes) ⇒ Object
Removes classes from the class list.
-
#toggle_class(cls, value = nil) ⇒ Object
Toggles the given class based on the second argument, or if omitted then toggles the class.
Instance Method Details
#add_class(*classes) ⇒ Object
Adds classes to the class list
7 8 9 |
# File 'opal/fron/dom/modules/classlist.rb', line 7 def add_class(*classes) classes.each { |cls| `#{@el}.classList.add(#{cls})` } end |
#has_class(cls) ⇒ Boolean
Returns whether the class list has the given class or not
23 24 25 |
# File 'opal/fron/dom/modules/classlist.rb', line 23 def has_class(cls) `#{@el}.classList.contains(#{cls})` end |
#remove_class(*classes) ⇒ Object
Removes classes from the class list
14 15 16 |
# File 'opal/fron/dom/modules/classlist.rb', line 14 def remove_class(*classes) classes.each { |cls| `#{@el}.classList.remove(#{cls})` } end |
#toggle_class(cls, value = nil) ⇒ Object
Toggles the given class based on the second argument, or if omitted then toggles the class.
32 33 34 35 36 37 38 |
# File 'opal/fron/dom/modules/classlist.rb', line 32 def toggle_class(cls, value = nil) if value || (value.nil? && !has_class(cls)) add_class cls else remove_class cls end end |