Skip to content

kwrooijen/clj-stimulus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Clojure Stimulus

Clojars Project

Installation

classtrophobic is a necessary dependency since stimulus uses classes.

package.json

{
    "dependencies": {
        "classtrophobic": "0.1.2",
        "stimulus": "3.1.0",
    }
}

Usage

Read the Stimulus handbook

Clojurescript

(ns app.core
  (:require
   [stimulus.core :as stimulus]))

(defn set-counter! [output counter]
  (set! (.-innerText output) counter))

(defn initialize [state _ctx]
  (reset! state {:counter 0}))

(defn connect [_state {:keys [target]}]
  (set-counter! (:output target) 0))

(defn add-amount [state {:keys [target value]}]
  (swap! state update :counter + (:amount value))
  (set-counter! (:output target) (:counter @state)))

(def controllers
  {:counter/static {:targets [:output]
                    :values {:amount :integer}}
   :counter/initialize initialize
   :counter/connect connect
   :counter/add-amount add-amount})

(stimulus/register-controllers! controllers)

Hiccup

[:div
 {:data-controller :counter
  :data-counter-amount-value 2}
 [:div
  [:span "Counter: "]
  [:span {:data-counter-target :output}]]
 [:button
  {:data-action :click->counter#add-amount}
  "Add Amount"]]

Controllers

Controller actions take two arguments:

  • state which holds an atom during the controllers lifetime.
  • ctx which is a map containing the following keys:
Field Description
:this The controllers object
:value A map of single values
:target A map of the first targets
:targets A map of multiple targets

author / License

Released under the MIT License by Kevin William van Rooijen.

About

Clojure wrapper for Stimulus

Resources

License

Stars

Watchers

Forks