Replies: 2 comments 2 replies
-
@YuinS316 Hello there! I'm here to help you with any issues or questions you have. Let's dive into the problem you're facing! The In the provided code, Here is the relevant part of the code: watch(
() => unrefElement(target),
(el) => {
if (!el)
return
trap = createFocusTrap(el, {
...focusTrapOptions,
onActivate() {
hasFocus.value = true
// Apply if user provided onActivate option
if (options.onActivate)
options.onActivate()
},
onDeactivate() {
hasFocus.value = false
// Apply if user provided onDeactivate option
if (options.onDeactivate)
options.onDeactivate()
},
})
// Focus if immediate is set to true
if (immediate)
activate()
},
{ flush: 'post' },
) Additionally, the import type { WatchSource } from 'vue-demi'
import { isReactive } from 'vue-demi'
import type { MapOldSources, MapSources } from '../utils'
import type { WatchIgnorableReturn } from '../watchIgnorable'
import { watchIgnorable } from '../watchIgnorable'
import type { WatchWithFilterOptions } from '../watchWithFilter'
import { toValue } from '../toValue'
export interface WatchTriggerableReturn<FnReturnT = void> extends WatchIgnorableReturn {
trigger: () => FnReturnT
}
type OnCleanup = (cleanupFn: () => void) => void
export type WatchTriggerableCallback<V = any, OV = any, R = void> = (value: V, oldValue: OV, onCleanup: OnCleanup) => R
export function watchTriggerable<T extends Readonly<WatchSource<unknown>[]>, FnReturnT>(sources: [...T], cb: WatchTriggerableCallback<MapSources<T>, MapOldSources<T, true>, FnReturnT>, options?: WatchWithFilterOptions<boolean>): WatchTriggerableReturn<FnReturnT>
export function watchTriggerable<T, FnReturnT>(source: WatchSource<T>, cb: WatchTriggerableCallback<T, T | undefined, FnReturnT>, options?: WatchWithFilterOptions<boolean>): WatchTriggerableReturn<FnReturnT>
export function watchTriggerable<T extends object, FnReturnT>(source: T, cb: WatchTriggerableCallback<T, T | undefined, FnReturnT>, options?: WatchWithFilterOptions<boolean>): WatchTriggerableReturn<FnReturnT>
export function watchTriggerable<Immediate extends Readonly<boolean> = false>(
source: any,
cb: any,
options: WatchWithFilterOptions<Immediate> = {},
): WatchTriggerableReturn {
let cleanupFn: (() => void) | undefined
function onEffect() {
if (!cleanupFn)
return
const fn = cleanupFn
cleanupFn = undefined
fn()
}
function onCleanup(callback: () => void) {
cleanupFn = callback
}
const _cb = (
value: any,
oldValue: any,
) => {
onEffect()
return cb(value, oldValue, onCleanup)
}
const res = watchIgnorable(source, _cb, options)
const { ignoreUpdates } = res
const trigger = () => {
let res: any
ignoreUpdates(() => {
res = _cb(getWatchSources(source), getOldValue(source))
})
return res
}
return {
...res,
trigger,
}
}
function getWatchSources(sources: any) {
if (isReactive(sources))
return sources
if (Array.isArray(sources))
return sources.map(item => toValue(item))
return toValue(sources)
}
function getOldValue(source: any) {
return Array.isArray(source)
? source.map(() => undefined)
: undefined
} In summary, the |
Beta Was this translation helpful? Give feedback.
-
However, when you need to access an element in Vue, you would use |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I guess the target may change and it wants to cleanup the last event and register again.
But what makes me confusing is that after excuted "unrefElement(target)" and it returns the result is not a reactive value, how can make the "watch" function trigger again?
Can anyone tell me the reason. thanks.
Beta Was this translation helpful? Give feedback.
All reactions