Skip to content

Commit 0e332b9

Browse files
authored
fix(ktooltip): improve typings for ktooltip (#2703)
1 parent 344d7b8 commit 0e332b9

File tree

7 files changed

+94
-66
lines changed

7 files changed

+94
-66
lines changed

docs/components/tooltip.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Here are the different options:
8585

8686
### maxWidth
8787

88-
You can set the maximum width of the tooltip container with the `maxWidth` property. `maxWidth` defaults to `auto`.
88+
You can set the maximum width of the tooltip container with the `maxWidth` property. `maxWidth` defaults to `none`.
8989

9090
<KTooltip max-width="300" text="A very long tooltip that wraps to the next line. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.">
9191
<InfoIcon />
@@ -121,6 +121,10 @@ A string to be used as `id` attribute on underlying `role="tooltip"` element. Us
121121
<KInput id="full-name-field" />
122122
```
123123

124+
### zIndex
125+
126+
Pass a number to use for the `z-index` property. Default value is `9999`.
127+
124128
### kpopAttributes
125129

126130
Use the `kpopAttributes` prop to configure the underlying [KPop's props](/components/popover).

src/components/KTooltip/KTooltip.vue

Lines changed: 13 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,68 +36,28 @@
3636
</template>
3737

3838
<script setup lang="ts">
39-
import { computed, useId, useSlots } from 'vue'
40-
import type { PropType } from 'vue'
39+
import { computed, useId } from 'vue'
4140
import KPop from '@/components/KPop/KPop.vue'
42-
import type { PopPlacements , PopoverAttributes } from '@/types'
43-
import { PopPlacementsArray } from '@/types'
4441
import { KUI_SPACE_20 } from '@kong/design-tokens'
42+
import type { TooltipProps, TooltipSlots } from '@/types'
4543
4644
defineOptions({
4745
inheritAttrs: false,
4846
})
4947
50-
const props = defineProps({
51-
/**
52-
* Text to show in tooltip
53-
*/
54-
text: {
55-
type: String,
56-
required: false,
57-
default: '',
58-
},
48+
const {
49+
text = '',
50+
placement = 'bottom',
51+
maxWidth = 'none',
52+
label = '',
53+
tooltipId = '',
54+
zIndex = 9999,
55+
kpopAttributes = {},
56+
} = defineProps<TooltipProps>()
5957
60-
/**
61-
* Define which side the tooltip displays
62-
*/
63-
placement: {
64-
type: String as PropType<PopPlacements>,
65-
default: 'bottom',
66-
validator: (value: PopPlacements):boolean => {
67-
return PopPlacementsArray.includes(value)
68-
},
69-
},
70-
/**
71-
* Set the max-width of the ktooltip
72-
*/
73-
maxWidth: {
74-
type: String,
75-
default: 'auto',
76-
},
77-
/**
78-
* @deprecated in favor of text prop
79-
*/
80-
label: {
81-
type: String,
82-
default: '',
83-
},
84-
tooltipId: {
85-
type: String,
86-
default: '',
87-
},
88-
zIndex: {
89-
type: Number,
90-
default: 9999,
91-
},
92-
kpopAttributes: {
93-
type: Object as PropType<PopoverAttributes>,
94-
default: () => {},
95-
},
96-
})
97-
98-
const slots = useSlots()
58+
const slots = defineSlots<TooltipSlots>()
9959
100-
const showTooltip = computed((): boolean => !!props.text || !!props.label || !!slots.content)
60+
const showTooltip = computed((): boolean => !!text || !!label || !!slots.content)
10161
10262
const randomTooltipId = useId()
10363
</script>

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export * from './date-time-picker'
1313
export * from './dropdown'
1414
export * from './empty-state'
1515
export * from './input'
16-
export * from './label'
1716
export * from './method-badge'
1817
export * from './multi-select'
1918
export * from './pagination'
@@ -29,5 +28,6 @@ export * from './table'
2928
export * from './tabs'
3029
export * from './text-area'
3130
export * from './toaster'
31+
export * from './tooltip'
3232
export * from './tree-list'
3333
export * from './utils'

src/types/input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TooltipAttributes } from '@/types/label'
1+
import type { TooltipAttributes } from '@/types/tooltip'
22

33
export interface LabelAttributes {
44
info?: string

src/types/label.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/types/popover.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@ export const PopPlacementsArray = [
2020
'bottom-end',
2121
] as const
2222

23-
export type PopPlacements = AnyElementOf<typeof PopPlacementsArray>
23+
export type PopPlacement = AnyElementOf<typeof PopPlacementsArray>
24+
25+
/**
26+
* @deprecated Use `PopPlacement` instead.
27+
*/
28+
export type Placement = PopPlacement
29+
30+
/**
31+
* @deprecated Use `PopPlacement` instead.
32+
*/
33+
export type PopPlacements = PopPlacement
2434

2535
/**
2636
* @internal
@@ -54,7 +64,7 @@ export interface PopProps {
5464
* One of ['auto', 'top', 'top-start', 'top-end', 'left', 'left-start', 'left-end', 'right', 'right-start', 'right-end', 'bottom', 'bottom-start', 'bottom-end'].
5565
* @default 'auto'
5666
*/
57-
placement?: PopPlacements
67+
placement?: PopPlacement
5868

5969
/**
6070
* Whether popover should be opened on trigger element click or mouseover.

src/types/tooltip.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { PopoverAttributes, PopPlacement } from './popover'
2+
3+
export interface TooltipProps {
4+
/**
5+
* Text to show in tooltip.
6+
* @default ''
7+
*/
8+
text?: string
9+
10+
/**
11+
* Define which side the tooltip displays.
12+
* One of ['auto', 'top', 'top-start', 'top-end', 'left', 'left-start', 'left-end', 'right', 'right-start', 'right-end', 'bottom', 'bottom-start', 'bottom-end'].
13+
* @default 'bottom'
14+
*/
15+
placement?: PopPlacement
16+
17+
/**
18+
* Set the max-width of the ktooltip.
19+
* @default 'none'
20+
*/
21+
maxWidth?: string
22+
23+
/**
24+
* Text to show in tooltip.
25+
* @default ''
26+
* @deprecated Use `text` instead.
27+
*/
28+
label?: string
29+
30+
/**
31+
* A string to be used as id attribute on underlying `role="tooltip"` element.
32+
* Useful for setting accessible attributes (such as `aria-describedby`) on other elements.
33+
* @default ''
34+
*/
35+
tooltipId?: string
36+
37+
/**
38+
* The `z-index` value for the tooltip.
39+
* @default 9999
40+
*/
41+
zIndex?: number
42+
43+
/**
44+
* Use the kpopAttributes prop to configure the underlying KPop's props.
45+
* @default {}
46+
*/
47+
kpopAttributes?: PopoverAttributes
48+
}
49+
50+
export interface TooltipSlots {
51+
/**
52+
* The default slot takes in the element you want the popover to be triggered over.
53+
*/
54+
default?(): any
55+
56+
/**
57+
* The content slot allows you to slot in any html content.
58+
*/
59+
content?(): any
60+
}
61+
62+
export type TooltipAttributes = Pick<TooltipProps, 'label' | 'placement' | 'maxWidth'>

0 commit comments

Comments
 (0)