Skip to content
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

Fix(T34716): add an update method in the tooltip directory #542

Merged
merged 8 commits into from
Sep 22, 2023
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ Since v0.0.10, this Changelog is formatted according to the [Common Changelog][c

## UNRELEASED

### Added
- ([#542](https://github.com/demos-europe/demosplan-ui/pull/542)) Add update hook to tooltip directive for dynamic value updates ([@sakutademos](https://github.com/sakutademos))

## v0.1.15 - 2023-09-18

### Added
- ([#523](https://github.com/demos-europe/demosplan-ui/pull/523)) Added a method that checks the z-index of the parent element and appends zIndex + 1 to the style of the tooltip ([@sakutademos](https://github.com/sakutademos))
- ([#523](https://github.com/demos-europe/demosplan-ui/pull/523)) Add a method that checks the z-index of the parent element and appends zIndex + 1 to the style of the tooltip ([@sakutademos](https://github.com/sakutademos))

## v0.1.14 - 2023-09-07

Expand Down
36 changes: 28 additions & 8 deletions src/components/DpTooltip/utils/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { v4 as uuid } from 'uuid'
let handleShowTooltip = null
let handleHideTooltip = null
let handleTimeoutForDestroy = null
let tooltips = {}

const deleteTooltip = (tooltipEl) => {
if (tooltipEl) {
Expand Down Expand Up @@ -32,13 +33,16 @@ const getZIndex = (element) => {
}

const hideTooltip = (tooltipEl) => {
tooltipEl.classList.add('z-below-zero')
tooltipEl.classList.add('opacity-0')
if (tooltipEl) {
tooltipEl.classList.add('z-below-zero')
tooltipEl.classList.add('opacity-0')
}

handleTimeoutForDestroy = setTimeout(() => deleteTooltip(tooltipEl), 300)
}

const createTooltip = (id, el, value, container, classes) => {
const createTooltip = (id, container, classes) => {
const value = tooltips[id]
// this has to be in sync with the Template in DpTooltip
const tooltipHtml =
`<div class="tooltip absolute ${classes} z-below-zero" role="tooltip" id="${id}">` +
Expand All @@ -47,7 +51,6 @@ const createTooltip = (id, el, value, container, classes) => {
`</div>`

const range = document.createRange()
el.setAttribute('aria-describedby', id)

const content = range.createContextualFragment(tooltipHtml)

Expand All @@ -59,11 +62,13 @@ const initTooltip = (el, value, options) => {

const id = `tooltip-${uuid()}`
const zIndex = getZIndex(el)
tooltips[id] = value

el.setAttribute('aria-describedby', id)

handleShowTooltip = () => showTooltip(
id,
el,
value,
options,
zIndex
)
Expand All @@ -75,9 +80,9 @@ const initTooltip = (el, value, options) => {
el.addEventListener('blur', handleHideTooltip)
}

const showTooltip = async (id, wrapperEl, value, { place = 'top', container = 'body', classes = '' }, zIndex) => {
const showTooltip = async (id, wrapperEl, { place = 'top', container = 'body', classes = '' }, zIndex) => {
if (!document.getElementById(wrapperEl.getAttribute('aria-describedby'))) {
createTooltip(id, wrapperEl, value, container, classes)
createTooltip(id, container, classes)
} else {
clearTimeout(handleTimeoutForDestroy)
}
Expand Down Expand Up @@ -129,4 +134,19 @@ const showTooltip = async (id, wrapperEl, value, { place = 'top', container = 'b
tooltipEl.classList.remove('opacity-0')
}

export { destroyTooltip, initTooltip }
const updateTooltip = (wrapper, value, options) => {
if (!value) return

const wrapperId = wrapper.getAttribute('aria-describedby')
tooltips[wrapperId] = value

const zIndex = getZIndex(wrapper)
const tooltipEl = document.getElementById(wrapperId)

if (tooltipEl) {
deleteTooltip(tooltipEl)
showTooltip(wrapperId, wrapper, options, zIndex)
}
}

export { destroyTooltip, initTooltip, updateTooltip }
22 changes: 21 additions & 1 deletion src/directives/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { VPopover as Popover } from 'v-tooltip'
import { destroyTooltip, initTooltip } from '../../components/DpTooltip/utils/tooltip'
import { destroyTooltip, initTooltip, updateTooltip } from '../../components/DpTooltip/utils/tooltip'

/**
* @deprecated Use DpTooltip instead
Expand Down Expand Up @@ -60,6 +60,26 @@ const Tooltip = {

initTooltip(element, content, options)
},

update: function (element, binding) {
let content = binding.value
let options = { place: 'top' }

if (binding.value && typeof binding.value === 'object') {
content = binding.value.content

if (binding.value.container) {
options = { ...options, container: binding.value.container }
}

if (binding.value.classes) {
options = { ...options, classes: binding.value.classes }
}
}

updateTooltip(element, content, options)
},

unbind: function (element) {
destroyTooltip(element)
}
Expand Down