Liking cljdoc? Tell your friends :D

Clojars Project

re-frame Bootstrap Toggle

Bootstrap Toggle is a highly flexible Bootstrap plugin that converts checkboxes into toggles. This is fully reactive rewrite of the JavaScript part for re-frame. It doesn't use checkboxes and of course no jQuery!

Visit http://www.bootstraptoggle.com for demos of the abilities.

Getting Started

Installation

Add this to your dependencies:

[re-frame-bootstrap-toggle "0.1.0"]

Also, you need to include a stylesheet from the css directory.

Usage

(ns your.re-frame.app
  (:require [re-frame.core :refer [subscribe dispatch]]
            [re-frame-bootstrap-toggle.core :refer [toggle]]))

(defn view []
  (let [state (subscribe [:toggle-state])]
    (fn []
      [toggle {:checked @toggle-state
               :on-click #(dispatch [:toggle-the-toggle])}])))

Options

You can pass additional options along with :checked and :on-click:

NameTypeDefaultDescription
:onstring or hiccup"On"Label of the on toggle
:offstring or hiccup"Off"Label of the off toggle
:onstylestring"primary"Style of the on toggle. Possible values are the button classes without the leading btn-, such as "info" or "warning"
:offstylestring"default"Same as :onstyle, but for the off toggle
:widthintegernilSets the width of the toggle. If set to nil, width will be calculated.
:heightintegernilSets the height of the toggle. If set to nil, height will be calculated.

The following options are not yet supported, but patches are welcome!

NameTypeDefaultDescription
:sizestring"normal"Size of the toggle. Possible values are "large", "normal", "small", "mini".
:stylestring Appends the value to the class attribute of the toggle. This can be used to apply custom styles. Refer to Custom Styles for reference.

But I need the checkbox!

While the checkbox introduces some state issues, it's no biggy to just add them yourself in case you want to submit the state in a form. You probably want to add a hidden field.

(defn view []
  (let [state (subscribe [:toggle-state])]
    (fn []
      [:div
        [toggle {:checked @toggle-state
                 :on-click #(dispatch [:toggle-the-toggle])}]
        [:input {:type "hidden"
                 :name "toggle"
                 :value (if @toggle-state "on" "off")}])))

Usage with Bootstrap 4

We use Bootstrap 3.3, but it seems pretty strait forward to use with Bootstrap 4 as well. First you have to set the :offstyle, since the btn-default css class was renamed to btn-secondary. Then you need to include the css rules mentioned in this GitHub issue.

Can you improve this documentation? These fine people already did:
Min Hur, minhur, Johannes Barre & aAXEe
Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close