Skip to content

Commit

Permalink
feature: command+click opens url in a new tab (#3156)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev authored Aug 20, 2024
1 parent b40b6b7 commit 73c393d
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions app/javascript/js/controllers/table_row_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,36 @@ import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
visitRecord(event) {
if (event.type === 'click') {
const isLinkOrButton = event.target.closest('a, button')
const isCheckbox = event.target.closest('input[type="checkbox"]')
if (isLinkOrButton || isCheckbox) {
return // Don't navigate if a link or button is clicked
}

this.#executeTheVisit(event)
if (event.type !== 'click') {
return
}

const isLinkOrButton = event.target.closest('a, button')
const isCheckbox = event.target.closest('input[type="checkbox"]')

if (isLinkOrButton || isCheckbox) {
return // Don't navigate if a link or button is clicked
}
}

#executeTheVisit(event) {
const row = event.target.closest('tr')
if (row && row.dataset.visitPath) {
window.Turbo.visit(row.dataset.visitPath)
const url = row.dataset.visitPath

if (!row || !url) {
return
}

if (event.metaKey || event.ctrlKey) {
this.#visitInNewTab(url)
} else {
this.#visitInSameTab(url)
}
}

#visitInSameTab(url) {
window.Turbo.visit(url)
}

#visitInNewTab(url) {
window.open(url, '_blank').focus()
}
}

0 comments on commit 73c393d

Please sign in to comment.