Skip to content

Commit

Permalink
Fix teleport morphing (#3841)
Browse files Browse the repository at this point in the history
* Fix teleport morphing

* Make x-trap more friendly with transitions

* wip

* wip

* wip

* wip
  • Loading branch information
calebporzio authored Nov 1, 2023
1 parent d352364 commit 6344404
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
9 changes: 5 additions & 4 deletions packages/focus/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,13 @@ export default function (Alpine) {

// Start trapping.
if (value && ! oldValue) {
setTimeout(() => {
if (modifiers.includes('inert')) undoInert = setInert(el)
if (modifiers.includes('noscroll')) undoDisableScrolling = disableScrolling()
if (modifiers.includes('noscroll')) undoDisableScrolling = disableScrolling()
if (modifiers.includes('inert')) undoInert = setInert(el)

// Activate the trap after a generous tick. (Needed to play nice with transitions...)
setTimeout(() => {
trap.activate()
});
}, 15)
}

// Stop trapping.
Expand Down
9 changes: 5 additions & 4 deletions packages/morph/src/morph.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export function morph(from, toHtml, options) {
}

function patchChildren(from, to) {
// If we hit a <template x-teleport="body">,
// let's use the teleported nodes for this patch...
if (from._x_teleport) from = from._x_teleport
if (to._x_teleport) to = to._x_teleport

let fromKeys = keyToMap(from.children)
let fromKeyHoldovers = {}

Expand Down Expand Up @@ -444,10 +449,6 @@ function getFirstNode(parent) {
}

function getNextSibling(parent, reference) {
if (reference._x_teleport) {
return reference._x_teleport
}

let next

if (parent instanceof Block) {
Expand Down
12 changes: 9 additions & 3 deletions packages/ui/src/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ function handleRoot(el, Alpine) {
__itemEls: [],
__activeEl: null,
__isOpen: false,
__open() {
__open(activationStrategy) {

this.__isOpen = true

// Safari needs more of a "tick" for focusing after x-show for some reason.
// Probably because Alpine adds an extra tick when x-showing for @click.outside
let nextTick = callback => requestAnimationFrame(() => requestAnimationFrame(callback))

nextTick(() => this.$refs.__items.focus({ preventScroll: true }))
nextTick(() => {
this.$refs.__items.focus({ preventScroll: true })

// Activate the first item every time the menu is open...
activationStrategy && activationStrategy(Alpine, this.$refs.__items, el => el.__activate())
})
},
__close(focusAfter = true) {
this.__isOpen = false
Expand Down Expand Up @@ -67,7 +73,7 @@ function handleButton(el, Alpine) {
'x-init'() { if (this.$el.tagName.toLowerCase() === 'button' && ! this.$el.hasAttribute('type')) this.$el.type = 'button' },
'@click'() { this.$data.__open() },
'@keydown.down.stop.prevent'() { this.$data.__open() },
'@keydown.up.stop.prevent'() { this.$data.__open(dom.Alpine, last) },
'@keydown.up.stop.prevent'() { this.$data.__open(dom.last) },
'@keydown.space.stop.prevent'() { this.$data.__open() },
'@keydown.enter.stop.prevent'() { this.$data.__open() },
})
Expand Down

0 comments on commit 6344404

Please sign in to comment.