Skip to content

Commit

Permalink
Add generic controller for closing details on outside click
Browse files Browse the repository at this point in the history
Implemented a new Stimulus controller for `<details>` elements
and applied it to the address form.
This generic controller leverages the `useClickOutside` hook from
`stimulus-use` to automatically close the `<details>` when a
click is detected outside its boundaries.

Co-Authored-By: Elia Schito <[email protected]>
  • Loading branch information
rainerdema and elia committed Nov 15, 2023
1 parent 8b56fef commit 3efd79f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</h2>

<% if @user&.addresses&.any? %>
<details class="text-black text-sm" data-<%= stimulus_id %>-target="addresses">
<details data-controller="details" data-<%= stimulus_id %>-target="addresses" class="text-black text-sm">
<summary
class="text-left flex cursor-pointer select-none"
data-action="keydown.esc-><%= stimulus_id %>#close"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Controller } from '@hotwired/stimulus'
import { useClickOutside } from 'stimulus-use'

export default class extends Controller {
connect() {
useClickOutside(this)
}

clickOutside(event) {
this.element.removeAttribute('open')
}
}

0 comments on commit 3efd79f

Please sign in to comment.