Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the ability for the popover to auto dismiss #182

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
}
}
}
}