Skip to content

Commit dd312f3

Browse files
committed
feat(vueltip): allow to control which direction to detect for trunction via modifiers
1 parent 1cbba31 commit dd312f3

5 files changed

Lines changed: 53 additions & 4 deletions

File tree

.changeset/clean-meals-invite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@vingy/vueltip": minor
3+
---
4+
5+
allow to control which direction to detect for truncation

packages/vueltip/src/directive.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import type {
99
Binding,
1010
Content,
11+
Modifier,
1112
TooltipDirective,
1213
} from './types.ts'
1314
import { ensureKey } from './utils'
@@ -22,6 +23,17 @@ const toContent = (value: Binding): Content =>
2223
const extractPlacement = (value: Binding) =>
2324
typeof value === 'string' ? 'top' : value.placement
2425

26+
const truncationDirection = (
27+
modifiers: Partial<Record<Modifier, boolean | undefined>>,
28+
) => {
29+
if (modifiers.none) return 'none'
30+
if (modifiers.both) return 'both'
31+
if (modifiers.x && modifiers.y) return 'both'
32+
if (modifiers.x) return 'x'
33+
if (modifiers.y) return 'y'
34+
return options.defaultTruncateDetection
35+
}
36+
2537
export const vueltipDirective = {
2638
updated: (el, binding) => {
2739
ensureKey(el, (key) =>
@@ -37,6 +49,10 @@ export const vueltipDirective = {
3749
options.placementAttribute,
3850
extractPlacement(binding.value),
3951
)
52+
el.setAttribute(
53+
options.truncateAttribute,
54+
truncationDirection(binding.modifiers ?? {}),
55+
)
4056

4157
el.addEventListener('mouseenter', onMouseover)
4258
el.addEventListener('focus', onMouseover)

packages/vueltip/src/options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import type { Options } from './types'
33
export let options: Options = {
44
placementAttribute: 'vueltip-placement',
55
keyAttribute: 'vueltip-key',
6+
truncateAttribute: 'vueltip-truncate',
67
showDelay: 0,
78
hideDelay: 200,
89
handleDialogModals: false,
10+
defaultTruncateDetection: 'both',
911
}
1012

1113
export const setOptions = (opts?: Partial<Options>) => {

packages/vueltip/src/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,29 @@ export type Binding =
1313
content: string | Content
1414
placement: Placement
1515
}
16+
export type Modifier = 'x' | 'y' | 'none' | 'both'
1617

1718
export type TooltipDirective = Directive<
1819
HTMLElement,
19-
Binding
20+
Binding,
21+
Modifier
2022
>
23+
2124
export type Options = {
2225
/** @default 'vueltip-placement' */
2326
placementAttribute: string
2427
/** @default 'vueltip-key' */
2528
keyAttribute: string
29+
/** @default 'vueltip-truncate' */
30+
truncateAttribute: string
2631
/** @default 0 */
2732
showDelay: number
2833
/** @default 200 */
2934
hideDelay: number
3035
/** @default false */
3136
handleDialogModals: boolean
37+
/** @default 'both' */
38+
defaultTruncateDetection: Modifier
3239
}
3340
export type UseTooltipOptions = {
3441
tooltipElement: Readonly<ShallowRef<HTMLElement | null>>

packages/vueltip/src/utils.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
import { options } from './options'
2+
import { Modifier } from './types'
23

34
export function isTruncated(el: HTMLElement) {
5+
const direction = getTruncationDirection(el)
6+
7+
const x = el.offsetWidth < el.scrollWidth - 1
8+
const y = el.offsetHeight < el.scrollHeight - 1
9+
10+
switch (direction) {
11+
case 'x':
12+
return x
13+
case 'y':
14+
return y
15+
case 'both':
16+
return x || y
17+
case 'none':
18+
return true
19+
}
20+
}
21+
22+
function getTruncationDirection(el: HTMLElement) {
423
return (
5-
// offset is rounded down, scroll is rounded up
6-
el.offsetWidth < el.scrollWidth - 1 ||
7-
el.offsetHeight < el.scrollHeight - 1
24+
(el.getAttribute(
25+
options.truncateAttribute,
26+
) as Modifier) ?? options.defaultTruncateDetection
827
)
928
}
1029

0 commit comments

Comments
 (0)