Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/clean-meals-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vingy/vueltip": minor
---

allow to control which direction to detect for truncation
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ node_modules

dist

coverage
coverage
__screenshots__
16 changes: 16 additions & 0 deletions packages/vueltip/src/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import type {
Binding,
Content,
Modifier,
TooltipDirective,
} from './types.ts'
import { ensureKey } from './utils'
Expand All @@ -22,6 +23,17 @@ const toContent = (value: Binding): Content =>
const extractPlacement = (value: Binding) =>
typeof value === 'string' ? 'top' : value.placement

const truncationDirection = (
modifiers: Partial<Record<Modifier, boolean | undefined>>,
) => {
if (modifiers.none) return 'none'
if (modifiers.both) return 'both'
if (modifiers.x && modifiers.y) return 'both'
if (modifiers.x) return 'x'
if (modifiers.y) return 'y'
return options.defaultTruncateDetection
}

export const vueltipDirective = {
updated: (el, binding) => {
ensureKey(el, (key) =>
Expand All @@ -37,6 +49,10 @@ export const vueltipDirective = {
options.placementAttribute,
extractPlacement(binding.value),
)
el.setAttribute(
options.truncateAttribute,
truncationDirection(binding.modifiers ?? {}),
)

el.addEventListener('mouseenter', onMouseover)
el.addEventListener('focus', onMouseover)
Expand Down
2 changes: 2 additions & 0 deletions packages/vueltip/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import type { Options } from './types'
export let options: Options = {
placementAttribute: 'vueltip-placement',
keyAttribute: 'vueltip-key',
truncateAttribute: 'vueltip-truncate',
showDelay: 0,
hideDelay: 200,
handleDialogModals: false,
defaultTruncateDetection: 'both',
}

export const setOptions = (opts?: Partial<Options>) => {
Expand Down
13 changes: 10 additions & 3 deletions packages/vueltip/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,29 @@ export type Binding =
content: string | Content
placement: Placement
}
export type Modifier = 'x' | 'y' | 'none' | 'both'

export type TooltipDirective = Directive<
HTMLElement,
Binding
Binding,
Modifier
>

export type Options = {
/** @default 'tooltip-placement' */
/** @default 'vueltip-placement' */
placementAttribute: string
/** @default 'tooltip-key' */
/** @default 'vueltip-key' */
keyAttribute: string
/** @default 'vueltip-truncate' */
truncateAttribute: string
/** @default 0 */
showDelay: number
/** @default 200 */
hideDelay: number
/** @default false */
handleDialogModals: boolean
/** @default 'both' */
defaultTruncateDetection: Modifier
}
export type UseTooltipOptions = {
tooltipElement: Readonly<ShallowRef<HTMLElement | null>>
Expand Down
25 changes: 22 additions & 3 deletions packages/vueltip/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import { options } from './options'
import { Modifier } from './types'

export function isTruncated(el: HTMLElement) {
const direction = getTruncationDirection(el)

const x = el.offsetWidth < el.scrollWidth - 1
const y = el.offsetHeight < el.scrollHeight - 1

switch (direction) {
case 'x':
return x
case 'y':
return y
case 'both':
return x || y
case 'none':
return true
}
}

function getTruncationDirection(el: HTMLElement) {
return (
// offset is rounded down, scroll is rounded up
el.offsetWidth < el.scrollWidth - 1 ||
el.offsetHeight < el.scrollHeight - 1
(el.getAttribute(
options.truncateAttribute,
) as Modifier) ?? options.defaultTruncateDetection
)
}

Expand Down