Skip to content

Commit

Permalink
Merge branch 'main' into chore/remove_types
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob authored Oct 4, 2024
2 parents a5d39bf + d860282 commit fe15ef4
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions app/javascript/js/controllers/toggle_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export default class extends Controller {
static targets = ['panel']

static values = {
// One may want to have an element that is exempt from triggerring the click outside event
exemptionContainer: String,
// One may want to have elements that are exempt from triggering the click outside event
exemptionContainers: Array,
}

get exemptionContainerTarget() {
return document.querySelector(this.exemptionContainerValue)
get exemptionContainerTargets() {
return this.exemptionContainersValue.map((selector) => document.querySelector(selector)).filter(Boolean)
}

connect() {
Expand All @@ -20,13 +20,9 @@ export default class extends Controller {

clickOutside(e) {
if (this.hasPanelTarget) {
if (this.hasExemptionContainerValue && this.exemptionContainerTarget) {
const inExemptionContainer = this.exemptionContainerTarget.contains(e.target)
const isInExemptionContainer = this.hasExemptionContainersValue && this.exemptionContainerTargets.some((container) => container.contains(e.target))

if (!inExemptionContainer) {
leave(this.panelTarget)
}
} else {
if (!isInExemptionContainer) {
leave(this.panelTarget)
}
}
Expand Down

0 comments on commit fe15ef4

Please sign in to comment.