Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit f2c5922

Browse files
committed
feat: simplify tooltip api
1 parent 534f8b6 commit f2c5922

File tree

9 files changed

+194
-45
lines changed

9 files changed

+194
-45
lines changed

packages/c-button/examples/base-button.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
<template>
22
<div>
3+
<c-button
4+
border="1px solid rgba(47, 43, 67, 0.1)"
5+
border-radius="12px"
6+
mr="3"
7+
variant="outline"
8+
box-shadow="0px 1px 3px rgba(47, 43, 67, 0.1), inset 0px -1px 0px rgba(47, 43, 67, 0.1)"
9+
bg="transparent"
10+
>
11+
Button
12+
</c-button>
313
<c-button mr="3" @click="handleClick" color-scheme="blue">
414
Base button
515
</c-button>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<template>
2+
<div>
3+
<c-button
4+
border="1px solid rgba(47, 43, 67, 0.1)"
5+
border-radius="12px"
6+
mr="3"
7+
variant="outline"
8+
box-shadow="0px 1px 3px rgba(47, 43, 67, 0.1), inset 0px -1px 0px rgba(47, 43, 67, 0.1)"
9+
bg="transparent"
10+
size="lg"
11+
:left-icon="CommandIcon"
12+
>
13+
Button
14+
</c-button>
15+
</div>
16+
</template>
17+
18+
<script lang="ts" setup>
19+
import { createIcon } from "../../vue/src"
20+
21+
const CommandIcon = createIcon({
22+
name: "CommandIcon",
23+
viewBox: "0 0 20 20",
24+
path: `<path d="M8.33333 6.66667H11.6667V5.41667C11.6667 4.83981 11.8377 4.2759 12.1582 3.79626C12.4787 3.31661 12.9342 2.94277 13.4672 2.72202C14.0001 2.50126 14.5866 2.4435 15.1523 2.55604C15.7181 2.66858 16.2378 2.94637 16.6457 3.35427C17.0536 3.76218 17.3314 4.28188 17.444 4.84765C17.5565 5.41343 17.4987 5.99988 17.278 6.53283C17.0572 7.06578 16.6834 7.5213 16.2037 7.84179C15.7241 8.16228 15.1602 8.33333 14.5833 8.33333H13.3333V11.6667H14.5833C15.1602 11.6667 15.7241 11.8377 16.2037 12.1582C16.6834 12.4787 17.0572 12.9342 17.278 13.4672C17.4987 14.0001 17.5565 14.5866 17.444 15.1523C17.3314 15.7181 17.0536 16.2378 16.6457 16.6457C16.2378 17.0536 15.7181 17.3314 15.1523 17.444C14.5866 17.5565 14.0001 17.4987 13.4672 17.278C12.9342 17.0572 12.4787 16.6834 12.1582 16.2037C11.8377 15.7241 11.6667 15.1602 11.6667 14.5833V13.3333H8.33333V14.5833C8.33333 15.1602 8.16228 15.7241 7.84179 16.2037C7.5213 16.6834 7.06578 17.0572 6.53283 17.278C5.99988 17.4987 5.41343 17.5565 4.84765 17.444C4.28188 17.3314 3.76218 17.0536 3.35427 16.6457C2.94637 16.2378 2.66858 15.7181 2.55604 15.1523C2.4435 14.5866 2.50126 14.0001 2.72202 13.4672C2.94277 12.9342 3.31661 12.4787 3.79626 12.1582C4.2759 11.8377 4.83981 11.6667 5.41667 11.6667H6.66667V8.33333H5.41667C4.83981 8.33333 4.2759 8.16228 3.79626 7.84179C3.31661 7.5213 2.94277 7.06578 2.72202 6.53283C2.50126 5.99988 2.4435 5.41343 2.55604 4.84765C2.66858 4.28188 2.94637 3.76218 3.35427 3.35427C3.76218 2.94637 4.28188 2.66858 4.84765 2.55604C5.41343 2.4435 5.99988 2.50126 6.53283 2.72202C7.06578 2.94277 7.5213 3.31661 7.84179 3.79626C8.16228 4.2759 8.33333 4.83981 8.33333 5.41667V6.66667ZM6.66667 6.66667V5.41667C6.66667 5.16944 6.59336 4.92777 6.456 4.72221C6.31865 4.51664 6.12343 4.35643 5.89502 4.26182C5.66661 4.16721 5.41528 4.14245 5.17281 4.19069C4.93033 4.23892 4.7076 4.35797 4.53278 4.53278C4.35797 4.7076 4.23892 4.93033 4.19069 5.17281C4.14245 5.41528 4.16721 5.66661 4.26182 5.89502C4.35643 6.12343 4.51664 6.31865 4.72221 6.456C4.92777 6.59336 5.16944 6.66667 5.41667 6.66667H6.66667ZM6.66667 13.3333H5.41667C5.16944 13.3333 4.92777 13.4066 4.72221 13.544C4.51664 13.6814 4.35643 13.8766 4.26182 14.105C4.16721 14.3334 4.14245 14.5847 4.19069 14.8272C4.23892 15.0697 4.35797 15.2924 4.53278 15.4672C4.7076 15.642 4.93033 15.7611 5.17281 15.8093C5.41528 15.8575 5.66661 15.8328 5.89502 15.7382C6.12343 15.6436 6.31865 15.4834 6.456 15.2778C6.59336 15.0722 6.66667 14.8306 6.66667 14.5833V13.3333ZM13.3333 6.66667H14.5833C14.8306 6.66667 15.0722 6.59336 15.2778 6.456C15.4834 6.31865 15.6436 6.12343 15.7382 5.89502C15.8328 5.66661 15.8575 5.41528 15.8093 5.17281C15.7611 4.93033 15.642 4.7076 15.4672 4.53278C15.2924 4.35797 15.0697 4.23892 14.8272 4.19069C14.5847 4.14245 14.3334 4.16721 14.105 4.26182C13.8766 4.35643 13.6814 4.51664 13.544 4.72221C13.4066 4.92777 13.3333 5.16944 13.3333 5.41667V6.66667ZM13.3333 13.3333V14.5833C13.3333 14.8306 13.4066 15.0722 13.544 15.2778C13.6814 15.4834 13.8766 15.6436 14.105 15.7382C14.3334 15.8328 14.5847 15.8575 14.8272 15.8093C15.0697 15.7611 15.2924 15.642 15.4672 15.4672C15.642 15.2924 15.7611 15.0697 15.8093 14.8272C15.8575 14.5847 15.8328 14.3334 15.7382 14.105C15.6436 13.8766 15.4834 13.6814 15.2778 13.544C15.0722 13.4066 14.8306 13.3333 14.5833 13.3333H13.3333ZM8.33333 8.33333V11.6667H11.6667V8.33333H8.33333Z" fill="currentColor"/>
25+
`,
26+
})
27+
</script>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script setup lang="ts">
2+
import { CSimpleTooltip } from "../src"
3+
import { chakra, CStack, createIcon } from "../../vue/src"
4+
5+
const ChakraIcon = createIcon({
6+
name: "ChakraUIIcon",
7+
viewBox: "0 0 24 24",
8+
path: `
9+
<path fill="currentColor" d="M12 0C5.352 0 0 5.352 0 12s5.352 12 12 12s12-5.352 12-12S18.648 0 12 0zm2.8 4.333c.13-.004.248.136.171.278l-3.044 5.58a.187.187 0 0 0 .164.276h5.26c.17 0 .252.207.128.323l-9.22 8.605c-.165.154-.41-.063-.278-.246l4.364-6.021a.187.187 0 0 0-.151-.296H6.627a.187.187 0 0 1-.131-.32l8.18-8.123a.182.182 0 0 1 .125-.056z"/>
10+
`,
11+
})
12+
</script>
13+
14+
<template>
15+
<c-stack spacing="8">
16+
<c-simple-tooltip
17+
bg="red.500"
18+
label="Chakra UI Vue"
19+
has-arrow
20+
placement="top-start"
21+
>
22+
<chakra-icon box-size="8" />
23+
</c-simple-tooltip>
24+
<c-simple-tooltip label="Chakra UI Vue" has-arrow placement="bottom-end">
25+
Text with arrow
26+
</c-simple-tooltip>
27+
</c-stack>
28+
</template>

packages/c-tooltip/src/c-tooltip-arrow.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ export const CTooltipArrow = defineComponent({
1010

1111
const styles = useStyles()
1212

13-
const arrowBg = computed(
14-
() => attrs.bg ?? attrs.bgColor ?? attrs.backgroundColor
15-
)
13+
const arrowBg = computed(() => styles.value.bg)
1614
const arrowShadow = computed(() => attrs.shadow ?? attrs.boxShadow)
1715

1816
return () => (

packages/c-tooltip/src/c-tooltip-content.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,17 @@ export const CTooltipContent = defineComponent({
4949
).varRef,
5050
}))
5151

52-
TooltipStylesProvider(styles)
52+
const userDefinedBg = computed(
53+
() =>
54+
attrs.background ?? attrs.backgroundColor ?? attrs.bg ?? attrs.bgColor
55+
)
56+
57+
const tooltipStyles = computed(() => ({
58+
...styles.value,
59+
bg: userDefinedBg.value as string | undefined,
60+
}))
61+
62+
TooltipStylesProvider(tooltipStyles)
5363

5464
/** Handles exit transition */
5565
const leaveTransition = (el: Element, done: VoidFunction) => {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import {
2+
PropType,
3+
defineComponent,
4+
h,
5+
Fragment,
6+
mergeProps,
7+
watchEffect,
8+
computed,
9+
} from "vue"
10+
11+
import { getValidChildren, withSingleton } from "@chakra-ui/vue-utils"
12+
import { CTooltipTrigger } from "./c-tooltip-trigger"
13+
import { VueTooltipProps } from "./tooltip.props"
14+
import type * as PP from "@zag-js/popper"
15+
import { CTooltip } from "./c-tooltip"
16+
import { chakra } from "@chakra-ui/vue-system"
17+
import { CTooltipContent } from "./c-tooltip-content"
18+
import { CTooltipArrow } from "./c-tooltip-arrow"
19+
20+
export const CSimpleTooltip = defineComponent({
21+
name: "CSimpleTooltip",
22+
props: {
23+
...VueTooltipProps,
24+
placement: {
25+
type: String as PropType<PP.Placement>,
26+
default: "bottom",
27+
},
28+
label: String as PropType<string>,
29+
hasArrow: Boolean as PropType<boolean>,
30+
},
31+
inheritAttrs: false,
32+
emits: ["open", "close"],
33+
setup(props, { slots, emit, attrs }) {
34+
const mergedProps = mergeProps(props, {
35+
positioning: {
36+
...props.positioning,
37+
placement: props.placement,
38+
},
39+
})
40+
const shouldWrapChildren = computed(
41+
() => typeof getValidChildren(slots)?.[0]?.children === "string"
42+
)
43+
return () => {
44+
if (!props.label) return slots.default?.()
45+
const { label, hasArrow, ...rest } = mergedProps
46+
47+
return (
48+
<CTooltip
49+
{...rest}
50+
onOpen={(...e) => emit("open", ...e)}
51+
onClose={(...e) => emit("close", ...e)}
52+
>
53+
<CTooltipTrigger>
54+
{shouldWrapChildren.value ? (
55+
<chakra.span display={"inline-block"} tabindex={0}>
56+
{slots?.default?.()}
57+
</chakra.span>
58+
) : (
59+
withSingleton(slots, "CTooltip")
60+
)}
61+
</CTooltipTrigger>
62+
<CTooltipContent {...attrs}>
63+
{props.hasArrow && <CTooltipArrow />}
64+
{props.label}
65+
</CTooltipContent>
66+
</CTooltip>
67+
)
68+
}
69+
},
70+
})

packages/c-tooltip/src/c-tooltip.tsx

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,59 +16,22 @@ import {
1616
PropType,
1717
watch,
1818
mergeProps,
19+
watchEffect,
1920
} from "vue"
2021
import { TooltipProvider } from "./tooltip.context"
21-
import { useTooltip, UseTooltipProps } from "./use-tooltip"
22+
import { UseTooltipProps, useTooltip } from "./use-tooltip"
2223
import type * as PP from "@zag-js/popper"
2324
import { filterUndefined } from "@chakra-ui/utils"
2425
import { useId } from "@chakra-ui/vue-composables"
26+
import { VueTooltipProps, CTooltipProps } from "./tooltip.props"
2527

26-
type UseTooltipPropsContext = UseTooltipProps["context"]
27-
28-
export type CTooltipProps = UseTooltipPropsContext
29-
30-
const VueTooltipProps = {
31-
ids: {
32-
type: Object as PropType<CTooltipProps["ids"]>,
33-
},
34-
openDelay: {
35-
type: Number as PropType<CTooltipProps["openDelay"]>,
36-
default: 0,
37-
},
38-
closeDelay: {
39-
type: Number as PropType<CTooltipProps["closeDelay"]>,
40-
default: 0,
41-
},
42-
closeOnPointerDown: {
43-
type: Boolean as PropType<CTooltipProps["closeOnPointerDown"]>,
44-
},
45-
closeOnEsc: {
46-
type: Boolean as PropType<CTooltipProps["closeOnEsc"]>,
47-
default: true,
48-
},
49-
interactive: {
50-
type: Boolean as PropType<CTooltipProps["interactive"]>,
51-
},
52-
"aria-label": {
53-
type: String as PropType<CTooltipProps["aria-label"]>,
54-
},
55-
positioning: {
56-
type: Object as PropType<CTooltipProps["positioning"]>,
57-
},
58-
disabled: {
59-
type: Boolean as PropType<CTooltipProps["disabled"]>,
60-
},
61-
getRootNode: {
62-
type: Function as PropType<CTooltipProps["getRootNode"]>,
63-
},
64-
}
28+
export type { CTooltipProps }
6529

6630
export const CTooltip = defineComponent({
6731
name: "Tooltip",
6832
props: VueTooltipProps,
6933
emits: ["open", "close"],
7034
setup(props, { slots, emit, attrs }) {
71-
console.log(filterUndefined(props))
7235
const tooltipProps = computed<UseTooltipProps>(() => ({
7336
context: filterUndefined(props),
7437
emit,

packages/c-tooltip/src/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export {
1212
} from "./use-tooltip"
1313
export { CTooltipContent, type CTooltipContentProps } from "./c-tooltip-content"
1414
export { CTooltipTrigger } from "./c-tooltip-trigger"
15+
export { CSimpleTooltip } from "./c-tooltip-simple"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { PropType } from "vue"
2+
import { type UseTooltipProps } from "./use-tooltip"
3+
4+
export type UseTooltipPropsContext = UseTooltipProps["context"]
5+
6+
export type CTooltipProps = UseTooltipPropsContext
7+
8+
export const VueTooltipProps = {
9+
ids: {
10+
type: Object as PropType<CTooltipProps["ids"]>,
11+
},
12+
openDelay: {
13+
type: Number as PropType<CTooltipProps["openDelay"]>,
14+
default: 0,
15+
},
16+
closeDelay: {
17+
type: Number as PropType<CTooltipProps["closeDelay"]>,
18+
default: 0,
19+
},
20+
closeOnPointerDown: {
21+
type: Boolean as PropType<CTooltipProps["closeOnPointerDown"]>,
22+
},
23+
closeOnEsc: {
24+
type: Boolean as PropType<CTooltipProps["closeOnEsc"]>,
25+
default: true,
26+
},
27+
interactive: {
28+
type: Boolean as PropType<CTooltipProps["interactive"]>,
29+
},
30+
"aria-label": {
31+
type: String as PropType<CTooltipProps["aria-label"]>,
32+
},
33+
positioning: {
34+
type: Object as PropType<CTooltipProps["positioning"]>,
35+
},
36+
disabled: {
37+
type: Boolean as PropType<CTooltipProps["disabled"]>,
38+
},
39+
getRootNode: {
40+
type: Function as PropType<CTooltipProps["getRootNode"]>,
41+
},
42+
}

0 commit comments

Comments
 (0)