Browse Source

refactor(nc-gui): extend tooltip toggle conditions

# What's changed?

- toggle tooltip when key is pressed and mouse leaves/reenters element
- toggle tooltip on modifier key change
- use isHovering to detect element hover
pull/4121/head
braks 2 years ago
parent
commit
0f2261c251
  1. 2
      packages/nc-gui/components/dashboard/TreeView.vue
  2. 61
      packages/nc-gui/components/general/Tooltip.vue

2
packages/nc-gui/components/dashboard/TreeView.vue

@ -323,7 +323,7 @@ function openTableCreateDialog() {
:data-id="table.id" :data-id="table.id"
@click="addTableTab(table)" @click="addTableTab(table)"
> >
<GeneralTooltip wrapper-class="pl-5 pr-3 py-2" modifier-key="Alt"> <GeneralTooltip class="pl-5 pr-3 py-2" modifier-key="Alt">
<template #title>{{ table.table_name }}</template> <template #title>{{ table.table_name }}</template>
<div class="flex items-center gap-2 h-full" @contextmenu="setMenuContext('table', table)"> <div class="flex items-center gap-2 h-full" @contextmenu="setMenuContext('table', table)">
<div class="flex w-auto"> <div class="flex w-auto">

61
packages/nc-gui/components/general/Tooltip.vue

@ -1,46 +1,79 @@
<script lang="ts" setup> <script lang="ts" setup>
import { onKeyStroke } from '@vueuse/core' import { onKeyStroke } from '@vueuse/core'
import { ref, watch } from '#imports' import type { CSSProperties } from '@vue/runtime-dom'
import { ref, useElementHover, watch } from '#imports'
interface Props { interface Props {
// Key to be pressed on hover to trigger the tooltip // Key to be pressed on hover to trigger the tooltip
modifierKey?: string modifierKey?: string
wrapperClass?: string tooltipStyle?: CSSProperties
} }
const { modifierKey, wrapperClass } = defineProps<Props>() const { modifierKey, tooltipStyle } = defineProps<Props>()
const el = ref()
const showTooltip = ref(false) const showTooltip = ref(false)
const isMouseOver = ref(false) const isHovering = useElementHover(() => el.value)
const isKeyPressed = ref(false)
if (modifierKey) { onKeyStroke(
onKeyStroke(modifierKey, (e) => { (e) => e.key === modifierKey,
(e) => {
e.preventDefault() e.preventDefault()
if (modifierKey && isMouseOver.value) { if (modifierKey && isHovering.value) {
showTooltip.value = true showTooltip.value = true
} }
})
}
watch(isMouseOver, (val) => { isKeyPressed.value = true
if (!val) { },
{ eventName: 'keydown' },
)
onKeyStroke(
(e) => e.key === modifierKey,
(e) => {
e.preventDefault()
if (modifierKey) {
showTooltip.value = false
}
isKeyPressed.value = false
},
{ eventName: 'keyup' },
)
watch([isHovering, () => modifierKey], ([hovering, key]) => {
if (!hovering) {
showTooltip.value = false showTooltip.value = false
} }
// Show tooltip on mouseover if no modifier key is provided // Show tooltip on mouseover if no modifier key is provided
if (val && !modifierKey) { if (hovering && !key) {
showTooltip.value = true
}
// While hovering if the modifier key was changed and the key is not pressed, hide tooltip
if (hovering && key && !isKeyPressed.value) {
showTooltip.value = false
}
// When mouse leaves the element, then re-enters the element while key stays pressed, show the tooltip
if (!showTooltip.value && hovering && key && isKeyPressed.value) {
showTooltip.value = true showTooltip.value = true
} }
}) })
</script> </script>
<template> <template>
<a-tooltip v-model:visible="showTooltip" :trigger="[]"> <a-tooltip v-model:visible="showTooltip" :overlay-style="tooltipStyle" :trigger="[]">
<template #title> <template #title>
<slot name="title" /> <slot name="title" />
</template> </template>
<div class="w-full" :class="wrapperClass" @mouseenter="isMouseOver = true" @mouseleave="isMouseOver = false">
<div ref="el" class="w-full" :class="$attrs.class" :style="$attrs.style">
<slot /> <slot />
</div> </div>
</a-tooltip> </a-tooltip>

Loading…
Cancel
Save