8000 fix(BPopover): calculate mouse and element positions in a performant way by xvaara · Pull Request #2252 · bootstrap-vue-next/bootstrap-vue-next · GitHub
[go: up one dir, main page]

Skip to content

fix(BPopover): calculate mouse and element positions in a performant way #2252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions packages/bootstrap-vue-next/src/components/BPopover/BPopover.vue
10000
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ import {
size as sizeMiddleware,
useFloating,
} from '@floating-ui/vue'
import {onClickOutside, useMouseInElement, useToNumber} from '@vueuse/core'
import {onClickOutside, useToNumber} from '@vueuse/core'
import {
computed,
type CSSProperties,
Expand All @@ -91,6 +91,7 @@ import {
watchEffect,
} from 'vue'
import {useDefaults} from '../../composables/useDefaults'
import {useMouse} from '../../composables/useMouse'
import {useId} from '../../composables/useId'
import type {BPopoverProps} from '../../types/ComponentProps'
import {BvTriggerableEvent} from '../../utils'
Expand All @@ -116,6 +117,7 @@ const _props = withDefaults(defineProps<Omit<BPopoverProps, 'modelValue'>>(), {
customClass: '',
delay: () => ({show: 100, hide: 300}),
floatingMiddleware: undefined,
hideMargin: 2,
html: false,
id: undefined,
inline: false,
Expand Down Expand Up @@ -184,7 +186,7 @@ const computedId = useId(() => props.id, 'popover')
const hidden = ref(false)

const element = ref<HTMLElement | null>(null)
const targetTrigger = ref<HTMLElement | null>(null)
const floatingTarget = ref<HTMLElement | null>(null)
const arrow = ref<HTMLElement | null>(null)
const trigger = ref<HTMLElement | null>(null)
const placeholder = ref<HTMLElement | null>(null)
Expand Down Expand Up @@ -276,7 +278,7 @@ const placementRef = computed(() =>
isAutoPlacement.value ? undefined : (props.placement as OriginalPlacement)
)

const {floatingStyles, middlewareData, placement, update} = useFloating(targetTrigger, element, {
const {floatingStyles, middlewareData, placement, update} = useFloating(floatingTarget, element, {
placement: placementRef,
middleware: floatingMiddleware,
strategy: toRef(() => props.strategy),
Expand Down Expand Up @@ -323,9 +325,6 @@ const computedClasses = computed(() => {
]
})

const {isOutside} = useMouseInElement(element)
const {isOutside: triggerIsOutside} = useMouseInElement(trigger)

const toggle = (e?: Event) => {
const event = e ?? new Event('click')
showState.value ? hide(event) : show()
Expand Down Expand Up @@ -368,7 +367,32 @@ const show = () => {
})
}

const hide = (e: Readonly<Event>) => {
const {x, y} = useMouse()

const isElementAndTriggerOutside = () => {
const triggerRect = trigger.value?.getBoundingClientRect()
const elementRect = element.value?.getBoundingClientRect()
const margin = parseInt(props.hideMargin as unknown as string, 10) || 0
const offsetX = window?.scrollX || 0
const offsetY = window?.scrollY || 0
const triggerIsOutside =
!triggerRect ||
x.value < triggerRect.left + offsetX - margin ||
x.value > triggerRect.right + offsetX + margin ||
y.value < triggerRect.top + offsetY - margin ||
y.value > triggerRect.bottom + offsetY + margin

const isOutside =
!elementRect ||
x.value < elementRect.left + offsetX - margin ||
x.value > elementRect.right + offsetX + margin ||
y.value < elementRect.top + offsetY - margin ||
y.value > elementRect.bottom + offsetY + margin

return {triggerIsOutside, isOutside}
}

const hide = (e?: Readonly<Event>) => {
const event = buildTriggerableEvent('hide', {cancelable: true})
emit('hide', event)
if (event.defaultPrevented) {
Expand All @@ -381,17 +405,19 @@ const hide = (e: Readonly<Event>) => {
}
const delay = typeof props.delay === 'number' ? props.delay : props.delay?.hide || 0
setTimeout(() => {
const {triggerIsOutside, isOutside} = isElementAndTriggerOutside()
if (
!e ||
e?.type === 'click' ||
e?.type === 'forceHide' ||
e?.type === 'closeOnHide' ||
(e?.type === 'update:modelValue' && props.manual) ||
(!props.noninteractive &&
isOutside.value &&
triggerIsOutside.value &&
isOutside &&
triggerIsOutside &&
!element.value?.contains(document?.activeElement) &&
!trigger.value?.contains(document?.activeElement)) ||
(props.noninteractive && triggerIsOutside.value)
(props.noninteractive && triggerIsOutside)
) {
showState.value = false
nextTick(() => {
Expand Down Expand Up @@ -437,13 +463,13 @@ const bind = () => {
if (props.reference) {
const elem = getElement(props.reference)
if (elem) {
targetTrigger.value = elem
floatingTarget.value = elem
} else {
// eslint-disable-next-line no-console
console.warn('Reference element not found', props.reference)
}
} else {
targetTrigger.value = trigger.value
floatingTarget.value = trigger.value
}
if (!trigger.value || props.manual) {
return
Expand Down
3 changes: 3 additions & 0 deletions packages/bootstrap-vue-next/src/composables/useMouse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {useMouse as _useMouse, createSharedComposable} from '@vueuse/core'

export const useMouse = createSharedComposable(_useMouse)
1 change: 1 addition & 0 deletions packages/bootstrap-vue-next/src/types/ComponentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,7 @@ export interface BPopoverProps extends TeleporterProps {
hide: number
}>
floatingMiddleware?: Middleware[]
hideMargin?: number
html?: boolean
id?: string
inline?: boolean
Expand Down
Loading
0