Skip to content

Commit

Permalink
Delay cleanup after disconnect (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
hopsoft authored Feb 8, 2023
1 parent 0685520 commit 67234aa
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 67 deletions.
12 changes: 6 additions & 6 deletions app/assets/builds/@turbo-boost/elements.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions app/assets/builds/@turbo-boost/elements.js.map

Large diffs are not rendered by default.

18 changes: 5 additions & 13 deletions app/javascript/elements/toggle_elements/target_element/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import ToggleElement from '../toggle_element'
import './focus'

export default class ToggleTargetElement extends ToggleElement {
connectedCallback () {
Expand Down Expand Up @@ -91,19 +90,8 @@ export default class ToggleTargetElement extends ToggleElement {
)
}

applyFocus () {
if (this.focusElement) this.focusElement.focus()
}

get focusSelector () {
let value = this.getAttribute('focus-selector')
if (this.triggerElement)
value = this.triggerElement.getAttribute('focus-selector') || value
return value
}

get focusElement () {
return this.querySelector(this.focusSelector)
return this.getAttribute('focus-selector')
}

get triggerElement () {
Expand All @@ -114,6 +102,10 @@ export default class ToggleTargetElement extends ToggleElement {
return this.getAttribute('aria-labeledby')
}

set labeledBy (value) {
return this.setAttribute('aria-labeledby', value)
}

get collapseOn () {
const value = this.getAttribute('collapse-on')
if (!value) return []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let focusTimeout

function deactivateTrixAttributes (editor) {
const attributes = [
'bold',
Expand Down Expand Up @@ -43,35 +45,26 @@ function focusTrixEditorElement (element) {
])
}

function shouldEnhanceFocus (element) {
if (!element.tagName.match(/^input|textarea|trix-editor$/i)) return false
const toggleTargetElement = element.closest('turbo-boost-toggle-target') || {}
return !!toggleTargetElement.focusElement
}
function debouncedFocus (element) {
clearTimeout(focusTimeout)

function enhanceFocus (element) {
const trixEditorElement = element.closest('trix-editor')
focusTimeout = setTimeout(() => {
if (!element) return

try {
if (trixEditorElement) {
focusTrixEditorElement(trixEditorElement)
} else {
element.selectionStart = element.selectionEnd = element.value.length
element.focus()
const trixEditorElement = element.closest('trix-editor')

try {
if (trixEditorElement) {
focusTrixEditorElement(trixEditorElement)
} else {
element.selectionStart = element.selectionEnd = element.value.length
}
} catch (_) {
} finally {
element.scrollIntoView({ block: 'center', behavior: 'smooth' })
}
} catch (_) {
} finally {
setTimeout(
() => element.scrollIntoView({ block: 'center', behavior: 'smooth' }),
100
)
}
}, 100)
}

addEventListener(
'focus',
event => {
if (shouldEnhanceFocus(document.activeElement))
enhanceFocus(document.activeElement)
},
true
)
export default element => debouncedFocus(element)
38 changes: 21 additions & 17 deletions app/javascript/elements/toggle_elements/trigger_element/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import ToggleElement, { busyDuration } from '../toggle_element'
import Devtool from './devtool'
import focus from './focus'

let currentFocusSelector

export default class ToggleTriggerElement extends ToggleElement {
connectedCallback () {
super.connectedCallback()

if (this.targetElement)
this.targetElement.setAttribute('aria-labeledby', this.id)

const { start: commandStartEvent } = TurboBoost.Commands.events
this.commandStartHandler = this.onCommandStart.bind(this)
this.addEventListener(commandStartEvent, this.commandStartHandler)
Expand All @@ -25,15 +25,19 @@ export default class ToggleTriggerElement extends ToggleElement {
}

disconnectedCallback () {
const { start: commandStartEvent } = TurboBoost.Commands.events
this.removeEventListener(commandStartEvent, this.commandStartHandler)
// delay cleanup because the trigger may have been morphed out of the DOM,
// but it's needed to apply behavior like focus etc...
setTimeout(() => {
const { start: commandStartEvent } = TurboBoost.Commands.events
this.removeEventListener(commandStartEvent, this.commandStartHandler)

const { before: beforeInvokeEvent } = TurboBoost.Streams.invokeEvents
removeEventListener(beforeInvokeEvent, this.beforeInvokeHandler)
const { before: beforeInvokeEvent } = TurboBoost.Streams.invokeEvents
removeEventListener(beforeInvokeEvent, this.beforeInvokeHandler)

this.devtool.hide({ active: false })
this.devtool.unregisterEventListeners()
delete this.devtool
this.devtool.hide({ active: false })
this.devtool.unregisterEventListeners()
delete this.devtool
}, 1000)
}

initializeDevtool () {
Expand Down Expand Up @@ -61,7 +65,8 @@ export default class ToggleTriggerElement extends ToggleElement {
}

onCommandStart (event) {
this.targetElement.setAttribute('aria-labeledby', this.id)
currentFocusSelector = this.focusSelector
this.targetElement.labeledBy = this.id
this.targetElement.collapseMatches()
this.targetElement.busy = true
this.busy = true
Expand Down Expand Up @@ -92,10 +97,10 @@ export default class ToggleTriggerElement extends ToggleElement {
}, delay - 10)

// runs after the morph is executed
setTimeout(() => {
this.targetElement.setAttribute('aria-labeledby', this.id)
this.targetElement.applyFocus()
}, delay + 100)
setTimeout(
() => focus(this.targetElement.querySelector(currentFocusSelector)),
delay + 100
)
}

// a list of views shared between the trigger and target
Expand Down Expand Up @@ -151,8 +156,7 @@ export default class ToggleTriggerElement extends ToggleElement {

get focusSelector () {
return (
this.getAttribute('focus-selector') ||
this.targetElement.getAttribute('focus-selector')
this.getAttribute('focus-selector') || this.targetElement.focusSelector
)
}

Expand Down

0 comments on commit 67234aa

Please sign in to comment.