Skip to content

Instantly share code, notes, and snippets.

@GALTdea
Forked from mrmartineau/stimulus.md
Created July 15, 2020 01:47
Show Gist options
  • Save GALTdea/a9f6bc4da614b69cf8bb22ffd0872cd5 to your computer and use it in GitHub Desktop.
Save GALTdea/a9f6bc4da614b69cf8bb22ffd0872cd5 to your computer and use it in GitHub Desktop.

Revisions

  1. @mrmartineau mrmartineau revised this gist Apr 4, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions stimulus.md
    Original file line number Diff line number Diff line change
    @@ -73,13 +73,15 @@ If your attribute name consists of more than one word, **reference it as camelCa
    ## HTML API
    ### Controller `data-controller`
    e.g. `<div data-controller="ControllerName">`

    e.g. Multiple controllers on the same element: `<div data-controller="ControllerName AnotherControllerName">`

    ### Target `data-target`
    e.g. `<input data-target="ControllerName.TargetName"`>

    ### Action `data-action`
    e.g. `<button data-action="eventName->ControllerName#methodName">Click me</button>`

    e.g. Multiple actions on the same element: `<button data-action="eventName->ControllerName#methodName anotherEventName->ControllerName#anotherMethodName">Click me</button>`


  2. @mrmartineau mrmartineau revised this gist Apr 4, 2018. 1 changed file with 24 additions and 4 deletions.
    28 changes: 24 additions & 4 deletions stimulus.md
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,11 @@ Stimulus defines click as the default event for actions on `<button>` elements.
    - `select` > `change`
    - `textarea` > `change`

    ### Multiple Actions
    If an element has muliple actions you can separate each one with a space `click->hello#greet click->hello#save`.

    The element can even have multiple actions for multiple controllers `click->hello#greet click->history#save`.

    ## Targets
    The data-target value `hello.name` is called a target descriptor. This particular descriptor says:

    @@ -41,26 +46,41 @@ When Stimulus loads your controller class, it looks for target name strings in a
    - `this.sourceTargets` evaluates to an array of all source targets in the controller’s scope.
    - `this.hasSourceTarget` evaluates to true if there is a source target or false if not.

    ### Multiple Targets
    If an element is a target for multiple controllers you can separate each one with a space `hello.name history.text`

    ## Naming Conventions

    | Component | Convention | Rationale |
    | --- | --- | --- |
    | Controller filenames | snake_case.js | Rails works that way |
    | Identifiers | kebab-case | Sometimes used as part of HTML attribute names; analogous to CSS classes, which are conventionally kebab-case |
    | Action names | camelCase | Map directly to JavaScript controller methods |
    | Target names | camelCase | Map directly to JavaScript controller properties |
    | Data attributes | camelCase + kebab-case | Thin wrapper around the HTMLElement.dataSet API; camelCase names in JS, kebab-case attributes in HTML |

    ## Data API
    Each Stimulus controller has a `this.data` object with `has()`, `get()`, and `set()` methods. These methods provide convenient access to data attributes on the controller’s element, scoped by the controller’s identifier.

    For example, in our controller above:

    - `this.data.has("index")` returns `true` if the controller’s element has a `data-slideshow-index` attribute
    - `this.data.get("index")` returns the string value of the element’s data-slideshow-index attribute
    - `this.data.get("index")` returns the string value of the element’s `data-slideshow-index` attribute
    - `this.data.set("index", index)` sets the element’s `data-slideshow-index` attribute to the string value of index

    If your attribute name consists of more than one word, **reference it as camelCase in JavaScript and attribute-case in HTML**. For example, you can read the `data-slideshow-current-class-name` attribute with `this.data.get("currentClassName")`.

    ## HTML API
    ### Controller `data-controller`
    e.g. `<div data-controller="ControllerName">`
    e.g. Multiple controllers on the same element: `<div data-controller="ControllerName AnotherControllerName">`

    ### Target `data-target`
    e.g. `<input data-target="ControllerName.TargetName"`>

    ### Action `data-action`
    e.g. `<button data-action="eventName->ControllerName#methodName">Click me</button>`
    e.g. Multiple actions on the same element: `<button data-action="eventName->ControllerName#methodName anotherEventName->ControllerName#anotherMethodName">Click me</button>`


    ## Code snippets
    @@ -76,11 +96,11 @@ export default class extends Controller {
    static get targets () {
    return []
    }

    initialize () {}

    connect () {}

    disconnect () {}
    }
    ```
    @@ -98,4 +118,4 @@ Example HTML from the [Stimulus](https://stimulusjs.org/) home page
    <span data-target="hello.output">
    </span>
    </div>
    ```
    ```
  3. @mrmartineau mrmartineau revised this gist Apr 4, 2018. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions stimulus.md
    Original file line number Diff line number Diff line change
    @@ -46,11 +46,11 @@ Each Stimulus controller has a `this.data` object with `has()`, `get()`, and `se

    For example, in our controller above:

    - `this.data.has("index")` returns true if the controller’s element has a data-slideshow-index attribute
    - `this.data.has("index")` returns `true` if the controller’s element has a `data-slideshow-index` attribute
    - `this.data.get("index")` returns the string value of the element’s data-slideshow-index attribute
    - `this.data.set("index", index)` sets the element’s data-slideshow-index attribute to the string value of index
    - `this.data.set("index", index)` sets the element’s `data-slideshow-index` attribute to the string value of index

    If your attribute name consists of more than one word, reference it as camelCase in JavaScript and attribute-case in HTML. For example, you can read the data-slideshow-current-class-name attribute with `this.data.get("currentClassName")`.
    If your attribute name consists of more than one word, **reference it as camelCase in JavaScript and attribute-case in HTML**. For example, you can read the `data-slideshow-current-class-name` attribute with `this.data.get("currentClassName")`.

    ## HTML API
    ### Controller `data-controller`
  4. @mrmartineau mrmartineau revised this gist Apr 4, 2018. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions stimulus.md
    Original file line number Diff line number Diff line change
    @@ -53,6 +53,14 @@ For example, in our controller above:
    If your attribute name consists of more than one word, reference it as camelCase in JavaScript and attribute-case in HTML. For example, you can read the data-slideshow-current-class-name attribute with `this.data.get("currentClassName")`.

    ## HTML API
    ### Controller `data-controller`
    e.g. `<div data-controller="ControllerName">`

    ### Target `data-target`
    e.g. `<input data-target="ControllerName.TargetName"`>

    ### Action `data-action`
    e.g. `<button data-action="eventName->ControllerName#methodName">Click me</button>`


    ## Code snippets
  5. @mrmartineau mrmartineau revised this gist Apr 4, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions stimulus.md
    Original file line number Diff line number Diff line change
    @@ -52,6 +52,8 @@ For example, in our controller above:

    If your attribute name consists of more than one word, reference it as camelCase in JavaScript and attribute-case in HTML. For example, you can read the data-slideshow-current-class-name attribute with `this.data.get("currentClassName")`.

    ## HTML API


    ## Code snippets

  6. @mrmartineau mrmartineau revised this gist Apr 3, 2018. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions stimulus.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    # Stimulus cheatsheet
    Website: https://stimulusjs.org/
    GitHub repo: https://github.com/stimulusjs/stimulus
    Handbook: https://stimulusjs.org/handbook/introduction
    Discourse: https://discourse.stimulusjs.org/
    - Website: https://stimulusjs.org/
    - GitHub repo: https://github.com/stimulusjs/stimulus
    - Handbook: https://stimulusjs.org/handbook/introduction
    - Discourse: https://discourse.stimulusjs.org/

    ## Lifecycle callbacks

  7. @mrmartineau mrmartineau revised this gist Apr 3, 2018. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions stimulus.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    # Stimulus cheatsheet
    Website: https://stimulusjs.org/
    GitHub repo: https://github.com/stimulusjs/stimulus
    Handbook: https://stimulusjs.org/handbook/introduction
    Discourse: https://discourse.stimulusjs.org/

    ## Lifecycle callbacks

  8. @mrmartineau mrmartineau revised this gist Apr 3, 2018. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions stimulus.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    # Stimulus docs


    # Stimulus cheatsheet

    ## Lifecycle callbacks

  9. @mrmartineau mrmartineau revised this gist Apr 3, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion stimulus.md
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ The data-target value `hello.name` is called a target descriptor. This particula
    - `hello` is the controller identifier
    - `name` is the target name

    When Stimulus loads your controller class, it looks for target name strings in a static array called targets. For each target name in the array, Stimulus adds three new properties to your controller. Here, our "source" target name becomes the following properties:
    When Stimulus loads your controller class, it looks for target name strings in a static array called targets. For each target name in the array, Stimulus adds three new properties to your controller. Here, our `source` target name becomes the following properties:

    - `this.sourceTarget` evaluates to the first source target in your controller’s scope. If there is no source target, accessing the property throws an error.
    - `this.sourceTargets` evaluates to an array of all source targets in the controller’s scope.
  10. @mrmartineau mrmartineau created this gist Apr 3, 2018.
    89 changes: 89 additions & 0 deletions stimulus.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    # Stimulus docs



    ## Lifecycle callbacks

    - `initialize`: once, when the controller is first instantiated
    - `connect`: anytime the controller is connected to the DOM
    - `disconnect`: anytime the controller is disconnected from the DOM

    ## Action descriptors
    The data-action value `click->hello#greet` is called an action descriptor. This particular descriptor says:

    - `click` is the event name
    - `hello` is the controller identifier
    - `greet` is the name of the method to invoke

    ### Common Events Have a Shorthand Action Notation
    Stimulus defines click as the default event for actions on `<button>` elements. Certain other elements have default events, too. Here’s the full list:

    #### Element > Default event
    - `a` > `click`
    - `button` > `click`
    - `form` > `submit`
    - `input` > `change`
    - `input` > `type=submit click`
    - `select` > `change`
    - `textarea` > `change`

    ## Targets
    The data-target value `hello.name` is called a target descriptor. This particular descriptor says:

    - `hello` is the controller identifier
    - `name` is the target name

    When Stimulus loads your controller class, it looks for target name strings in a static array called targets. For each target name in the array, Stimulus adds three new properties to your controller. Here, our "source" target name becomes the following properties:

    - `this.sourceTarget` evaluates to the first source target in your controller’s scope. If there is no source target, accessing the property throws an error.
    - `this.sourceTargets` evaluates to an array of all source targets in the controller’s scope.
    - `this.hasSourceTarget` evaluates to true if there is a source target or false if not.

    ## Data API
    Each Stimulus controller has a `this.data` object with `has()`, `get()`, and `set()` methods. These methods provide convenient access to data attributes on the controller’s element, scoped by the controller’s identifier.

    For example, in our controller above:

    - `this.data.has("index")` returns true if the controller’s element has a data-slideshow-index attribute
    - `this.data.get("index")` returns the string value of the element’s data-slideshow-index attribute
    - `this.data.set("index", index)` sets the element’s data-slideshow-index attribute to the string value of index

    If your attribute name consists of more than one word, reference it as camelCase in JavaScript and attribute-case in HTML. For example, you can read the data-slideshow-current-class-name attribute with `this.data.get("currentClassName")`.


    ## Code snippets

    ### Empty class
    ```js
    import { Controller } from "stimulus"

    export default class extends Controller {
    static targets = []

    // or
    static get targets () {
    return []
    }

    initialize () {}

    connect () {}

    disconnect () {}
    }
    ```

    ### HTML
    Example HTML from the [Stimulus](https://stimulusjs.org/) home page
    ```html
    <div data-controller="hello">
    <input data-target="hello.name" type="text">

    <button data-action="click->hello#greet">
    Greet
    </button>

    <span data-target="hello.output">
    </span>
    </div>
    ```