Skip to content

Commit

Permalink
refactor: update
Browse files Browse the repository at this point in the history
  • Loading branch information
sadeghbarati committed Aug 9, 2024
1 parent 75916b4 commit 06f4674
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 40 deletions.
6 changes: 4 additions & 2 deletions packages/radix-vue/src/DismissableLayer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function usePointerDownOutside(
else {
// We need to remove the event listener in case the outside click has been canceled.
// See: https://github.com/radix-ui/primitives/issues/2171
ownerDocumentClickCleanup()
ownerDocumentClickCleanup && ownerDocumentClickCleanup()
}
isPointerInsideDOMTree.value = false
}
Expand All @@ -130,7 +130,7 @@ export function usePointerDownOutside(

cleanupFn(() => {
window.clearTimeout(timerId)
ownerDocumentPointerdownCleanup()
ownerDocumentPointerdownCleanup && ownerDocumentPointerdownCleanup()
})
})

Expand Down Expand Up @@ -175,6 +175,8 @@ export function useFocusOutside(
}

ownerDocumentFocusinCleanup = useEventListener(ownerDocument, 'focusin', handleFocus)

cleanupFn(() => ownerDocumentFocusinCleanup())
})

return {
Expand Down
14 changes: 8 additions & 6 deletions packages/radix-vue/src/FocusScope/FocusScope.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const focusScope = reactive({
},
})
watchEffect((cleanupFn) => {
watchEffect((onCleanup) => {
if (!isClient)
return
const container = currentElement.value
Expand Down Expand Up @@ -123,19 +123,21 @@ watchEffect((cleanupFn) => {
focus(container)
}
useEventListener(document, 'focusin', handleFocusIn)
useEventListener(document, 'focusout', handleFocusOut)
const documentFocusInCleanup = useEventListener(document, 'focusin', handleFocusIn)
const documentFocusOutCleanup = useEventListener(document, 'focusout', handleFocusOut)
const mutationObserver = new MutationObserver(handleMutations)
if (container)
mutationObserver.observe(container, { childList: true, subtree: true })
cleanupFn(() => {
onCleanup(() => {
documentFocusInCleanup()
documentFocusOutCleanup()
mutationObserver.disconnect()
})
})
watchEffect(async (cleanupFn) => {
watchEffect(async (onCleanup) => {
const container = currentElement.value
await nextTick()
Expand All @@ -160,7 +162,7 @@ watchEffect(async (cleanupFn) => {
}
}
cleanupFn(() => {
onCleanup(() => {
container.removeEventListener(AUTOFOCUS_ON_MOUNT, (ev: Event) =>
emits('mountAutoFocus', ev))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function handlePointerDownOutside(ev: PointerDownOutsideEvent) {
}
}
watchEffect(() => {
watchEffect((onCleanup) => {
const content = currentElement.value
if (menuContext.isRootMenu && content) {
// Bubble dismiss to the root content node and focus its trigger
Expand All @@ -119,7 +119,11 @@ watchEffect(() => {
itemContext.triggerRef.value?.focus()
}
useEventListener(document, EVENT_ROOT_CONTENT_DISMISS, handleClose)
const documentDismissCleanup = useEventListener(document, EVENT_ROOT_CONTENT_DISMISS, handleClose)
onCleanup(() => {
documentDismissCleanup()
})
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ watchEffect((onCleanup) => {
const debounceScrollEnd = useDebounceFn(() => dispatch('SCROLL_END'), 100)
watchEffect(() => {
watchEffect((onCleanup) => {
const viewport = rootContext.viewport.value
const scrollDirection = scrollbarContext.isHorizontal.value
? 'scrollLeft'
Expand All @@ -73,7 +73,11 @@ watchEffect(() => {
prevScrollPos = scrollPos
}
useEventListener(viewport, 'scroll', handleScroll)
const viewportScrollCleanup = useEventListener(viewport, 'scroll', handleScroll)
onCleanup(() => {
viewportScrollCleanup()
})
}
})
</script>
Expand Down
12 changes: 8 additions & 4 deletions packages/radix-vue/src/Select/SelectContentImpl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ watch(isPositioned, () => {
// prevent selecting items on `pointerup` in some cases after opening from `pointerdown`
// and close on `pointerup` outside.
const { onOpenChange, triggerPointerDownPosRef } = rootContext
watchEffect((cleanupFn) => {
let documentPointermoveCleanup: ReturnType<typeof useEventListener>
watchEffect((onCleanup) => {
if (!content.value)
return
let pointerMoveDelta = { x: 0, y: 0 }
Expand Down Expand Up @@ -160,19 +163,20 @@ watchEffect((cleanupFn) => {
if (!content.value?.contains(event.target as HTMLElement))
onOpenChange(false)
}
useEventListener(document, 'pointermove', handlePointerMove)
documentPointermoveCleanup = useEventListener(document, 'pointermove', handlePointerMove)
triggerPointerDownPosRef.value = null
}
if (triggerPointerDownPosRef.value !== null) {
useEventListener(document, 'pointermove', handlePointerMove)
documentPointermoveCleanup = useEventListener(document, 'pointermove', handlePointerMove)
document.addEventListener('pointerup', handlePointerUp, {
capture: true,
once: true,
})
}
cleanupFn(() => {
onCleanup(() => {
documentPointermoveCleanup && documentPointermoveCleanup()
document.removeEventListener('pointerup', handlePointerUp, {
capture: true,
})
Expand Down
8 changes: 6 additions & 2 deletions packages/radix-vue/src/Select/SelectScrollDownButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const { forwardRef, currentElement } = useForwardExpose()
const canScrollDown = ref(false)
watchEffect(() => {
watchEffect((onCleanup) => {
if (contentContext.viewport?.value && contentContext.isPositioned?.value) {
const viewport = contentContext.viewport.value
Expand All @@ -36,7 +36,11 @@ watchEffect(() => {
}
handleScroll()
useEventListener(viewport, 'scroll', handleScroll)
const viewportScrollCleanup = useEventListener(viewport, 'scroll', handleScroll)
onCleanup(() => {
viewportScrollCleanup()
})
}
})
Expand Down
9 changes: 7 additions & 2 deletions packages/radix-vue/src/Select/SelectScrollUpButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ const { forwardRef, currentElement } = useForwardExpose()
const canScrollUp = ref(false)
watchEffect(() => {
watchEffect((onCleanup) => {
if (contentContext.viewport?.value && contentContext.isPositioned?.value) {
const viewport = contentContext.viewport.value
function handleScroll() {
canScrollUp.value = viewport.scrollTop > 0
}
handleScroll()
useEventListener(viewport, 'scroll', handleScroll)
const viewportScrollCleanup = useEventListener(viewport, 'scroll', handleScroll)
onCleanup(() => {
viewportScrollCleanup()
})
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function useWindowSplitterResizeHandlerBehavior({
resizeHandler: Ref<ResizeHandler | null>
panelGroupElement: Ref<ParentNode | null>
}): void {
watchEffect(() => {
watchEffect((onCleanup) => {
const _panelGroupElement = panelGroupElement.value
if (disabled.value || resizeHandler.value === null || _panelGroupElement === null)
return
Expand Down Expand Up @@ -76,6 +76,10 @@ export function useWindowSplitterResizeHandlerBehavior({
}
}

useEventListener(handleElement, 'keydown', onKeyDown)
const handleElementKeydownCleanup = useEventListener(handleElement, 'keydown', onKeyDown)

onCleanup(() => {
handleElementKeydownCleanup()
})
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export function useWindowSplitterPanelGroupBehavior({
const handles = getResizeHandleElementsForGroup(groupId, _panelGroupElement)
assert(handles)

handles.forEach((handle) => {
const cleanupFunctions = handles.map((handle) => {
const handleId = handle.getAttribute('data-panel-resize-handle-id')
assert(handleId)

Expand Down Expand Up @@ -155,7 +155,15 @@ export function useWindowSplitterPanelGroupBehavior({
}
}

useEventListener(handle, 'keydown', onKeyDown)
const handleKeydownCleanup = useEventListener(handle, 'keydown', onKeyDown)

return () => {
handleKeydownCleanup()
}
})

onCleanup(() => {
cleanupFunctions.forEach(cleanupFunction => cleanupFunction())
})
})
}
11 changes: 8 additions & 3 deletions packages/radix-vue/src/Toast/ToastRootImpl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ if (props.type && !['foreground', 'background'].includes(props.type)) {
throw new Error(error)
}
watchEffect(() => {
watchEffect((onCleanup) => {
const viewport = providerContext.viewport.value
if (viewport) {
const handleResume = () => {
Expand All @@ -120,8 +120,13 @@ watchEffect(() => {
emits('pause')
}
useEventListener(viewport, VIEWPORT_PAUSE, handlePause)
useEventListener(viewport, VIEWPORT_RESUME, handleResume)
const viewportPauseCleanup = useEventListener(viewport, VIEWPORT_PAUSE, handlePause)
const viewportResumeCleanup = useEventListener(viewport, VIEWPORT_RESUME, handleResume)
onCleanup(() => {
viewportPauseCleanup()
viewportResumeCleanup()
})
}
})
Expand Down
23 changes: 16 additions & 7 deletions packages/radix-vue/src/Toast/ToastViewport.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ onMounted(() => {
providerContext.onViewportChange(currentElement.value)
})
watchEffect(() => {
watchEffect((onCleanup) => {
const viewport = currentElement.value
if (hasToasts.value && viewport) {
const handlePause = () => {
Expand Down Expand Up @@ -125,12 +125,21 @@ watchEffect(() => {
}
}
useEventListener(viewport, ['focusin', 'pointermove'], handlePause)
useEventListener(viewport, 'focusout', handleFocusOutResume)
useEventListener(viewport, 'pointerleave', handlePointerLeaveResume)
useEventListener(viewport, 'keydown', handleKeyDown)
useEventListener(window, 'blur', handlePause)
useEventListener(window, 'focus', handleResume)
const viewportEventsCleanup = useEventListener(viewport, ['focusin', 'pointermove'], handlePause)
const viewportFocusoutCleanup = useEventListener(viewport, 'focusout', handleFocusOutResume)
const viewportPointerLeaveCleanup = useEventListener(viewport, 'pointerleave', handlePointerLeaveResume)
const viewportKeydownCleanup = useEventListener(viewport, 'keydown', handleKeyDown)
const windowBlurCleanup = useEventListener(window, 'blur', handlePause)
const windowFocusCleanup = useEventListener(window, 'focus', handleResume)
onCleanup(() => {
viewportEventsCleanup()
viewportFocusoutCleanup()
viewportPointerLeaveCleanup()
viewportKeydownCleanup()
windowBlurCleanup()
windowFocusCleanup()
})
}
})
Expand Down
21 changes: 15 additions & 6 deletions packages/radix-vue/src/shared/useGraceArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createEventHook, refAutoReset } from '@vueuse/shared'
import { useEventListener } from '@vueuse/core'

export function useGraceArea(triggerElement: Ref<HTMLElement | undefined>, containerElement: Ref<HTMLElement | undefined>) {
// Reset the inTransit state if idle/scrolled.
// Reset the inTransit state if idle/scrolled.
const isPointerInTransit = refAutoReset(false, 300)

const pointerGraceArea = ref<Polygon | null>(null)
Expand All @@ -26,17 +26,22 @@ export function useGraceArea(triggerElement: Ref<HTMLElement | undefined>, conta
isPointerInTransit.value = true
}

watchEffect(() => {
watchEffect((onCleanup) => {
if (triggerElement.value && containerElement.value) {
const handleTriggerLeave = (event: PointerEvent) => handleCreateGraceArea(event, containerElement.value!)
const handleContentLeave = (event: PointerEvent) => handleCreateGraceArea(event, triggerElement.value!)

useEventListener(triggerElement, 'pointerleave', handleTriggerLeave)
useEventListener(containerElement, 'pointerleave', handleContentLeave)
const triggerElementPointerLeaveCleanup = useEventListener(triggerElement, 'pointerleave', handleTriggerLeave)
const triggerElementPointerLeaveContentCleanup = useEventListener(containerElement, 'pointerleave', handleContentLeave)

onCleanup(() => {
triggerElementPointerLeaveCleanup()
triggerElementPointerLeaveContentCleanup()
})
}
})

watchEffect(() => {
watchEffect((onCleanup) => {
if (pointerGraceArea.value) {
const handleTrackPointerGrace = (event: PointerEvent) => {
if (!pointerGraceArea.value)
Expand All @@ -56,7 +61,11 @@ export function useGraceArea(triggerElement: Ref<HTMLElement | undefined>, conta
}
}

useEventListener(document, 'pointermove', handleTrackPointerGrace)
const documentPointermoveCleanup = useEventListener(document, 'pointermove', handleTrackPointerGrace)

onCleanup(() => {
documentPointermoveCleanup()
})
}
})

Expand Down

0 comments on commit 06f4674

Please sign in to comment.