Toggle

    Layout is pretty simple:

    Inside of Simple List:

    1. <ul>
    2. ...
    3. <li>
    4. <span>Text label</span>
    5. <label class="toggle">
    6. <input type="checkbox">
    7. <span class="toggle-icon"></span>
    8. </label>
    9. </li>
    10. </div>

    Inside of usual List (preferable in item-after):

    1. <div class="list">
    2. <ul>
    3. ...
    4. <li class="item-content">
    5. <div class="item-inner">
    6. <div class="item-title">Text label</div>
    7. <div class="item-after">
    8. <label class="toggle">
    9. <input type="checkbox">
    10. <span class="toggle-icon"></span>
    11. </label>
    12. </div>
    13. </div>
    14. </li>
    15. </ul>
    16. </div>

    Toggle Colors

    Toggle supports all . So to change its color just add color-[color] class to toggle element.

    Let’s look at related App methods to work with Toggle:

    Method returns created Toggle’s instance

    app.toggle.destroy(el)- destroy Toggle instance

    • el - HTMLElement or string (with CSS Selector) or object. Toggle element or Toggle instance to destroy.

    app.toggle.get(el)- get Toggle instance by HTML element

    • el - HTMLElement or string (with CSS Selector). Toggle element.

    Method returns Toggle’s instance

    Toggle Parameters

    Now let’s look at list of available parameters we need to create Toggle:

    1. var toggle = app.toggle.create({ /* parameters */ })

    After that we have its initialized instance (like toggle variable in example above) with useful methods and properties:

    Toggle Events

    Toggle will fire the following DOM events on toggle element and events on app and toggle instance:

    App and Toggle Instance Events

    Toggle instance emits events on both self instance and app instance. App instance events has same names prefixed with toggle.

    If you don’t need to use toggle API and your toggle is inside of page and presented in DOM on moment of page initialization then it can be auto initialized with just adding additional toggle-init class:

    In this case if you need to access created Toggle instance you can use the app.toggle.get app method:

    1. var toggle = app.toggle.get('.toggle');
    2. if (toggle.checked) {
    3. // do something
    4. }

    Examples

    1. <div class="block-title">Super Heroes</div>
    2. <div class="list simple-list">
    3. <ul>
    4. <li>
    5. <span>Batman</span>
    6. <label class="toggle toggle-init color-black">
    7. <input type="checkbox" checked>
    8. <span class="toggle-icon"></span>
    9. </label>
    10. </li>
    11. <li>
    12. <span>Aquaman</span>
    13. <label class="toggle toggle-init color-blue">
    14. <input type="checkbox" checked>
    15. <span class="toggle-icon"></span>
    16. </label>
    17. </li>
    18. <li>
    19. <span>Superman</span>
    20. <label class="toggle toggle-init color-red">
    21. <input type="checkbox" checked>
    22. </label>
    23. </li>
    24. <span>Hulk</span>
    25. <label class="toggle toggle-init color-green">
    26. <input type="checkbox">
    27. <span class="toggle-icon"></span>
    28. </label>
    29. </li>
    30. <li>
    31. <span>Spiderman (Disabled)</span>
    32. <label class="toggle toggle-init disabled">
    33. <input type="checkbox">
    34. <span class="toggle-icon"></span>
    35. </label>
    36. </li>
    37. <li>
    38. <span>Ironman (Disabled)</span>
    39. <label class="toggle toggle-init toggle-init">
    40. <input type="checkbox" checked disabled>
    41. <span class="toggle-icon"></span>
    42. </label>
    43. </li>
    44. <li>
    45. <span>Thor</span>
    46. <label class="toggle toggle-init color-orange">
    47. <input type="checkbox" checked>
    48. <span class="toggle-icon"></span>
    49. </label>
    50. </li>
    51. <li>
    52. <span>Wonder Woman</span>
    53. <label class="toggle toggle-init color-pink">
    54. <input type="checkbox">
    55. <span class="toggle-icon"></span>
    56. </label>
    57. </li>
    58. </div>