Skip to content

Commit

Permalink
Adding the ability for the popover to auto dismiss (#182)
Browse files Browse the repository at this point in the history
* Adding the ability for the popover to auto dismiss

* update readme and check
  • Loading branch information
Dmitry Sychev authored May 15, 2023
1 parent 11f7338 commit 7d6bf9e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ application.register('popover', Popover)

`data-popover-translate-y="-128%"` defines the css transform-translate Y value used in positioning the popover. It can be anything from a percentage to rem units to pixels.

`data-alert-dismiss-after-value` can be provided to make the popover dimiss after x miliseconds. Default is `undefined`.

### Autosave (Rails-only)

Autosaving forms are really helpful for saving drafts of records. This
Expand Down
11 changes: 10 additions & 1 deletion src/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
static values = {
dismissAfter: Number
}
static targets = ['content']

// Sets the popover offset using Stimulus data map objects.
Expand All @@ -43,8 +46,14 @@ export default class extends Controller {
toggle() {
if (this.contentTarget.classList.contains('hidden')) {
this.contentTarget.classList.remove('hidden')

if (this.hasDismissAfterValue) {
setTimeout(() => {
this.contentTarget.classList.add('hidden')
}, this.dismissAfterValue)
}
} else {
this.contentTarget.classList.add('hidden')
}
}
}
}

0 comments on commit 7d6bf9e

Please sign in to comment.