11import React from "react" ;
2+ import { ViewStyle } from "react-native" ;
23import Svg , { Path } from "react-native-svg" ;
34
5+ import { useTheme } from "../../theme" ;
46import { IconProps } from "../../utils" ;
57import { Icon } from "../icon" ;
68
79import { TooltipPlacement } from "./Tooltip" ;
810
9- const UpArrow : React . FC < IconProps > = ( { fill = "#27272A " } ) => {
11+ const UpArrow : React . FC < IconProps > = ( { fill = "#171717 " } ) => {
1012 return (
1113 < Svg width = "100%" height = "100%" viewBox = "0 0 12 5" fill = "none" >
1214 < Path
@@ -17,7 +19,7 @@ const UpArrow: React.FC<IconProps> = ({ fill = "#27272A" }) => {
1719 ) ;
1820} ;
1921
20- const LeftArrow : React . FC < IconProps > = ( { fill = "#27272A " } ) => {
22+ const LeftArrow : React . FC < IconProps > = ( { fill = "#171717 " } ) => {
2123 return (
2224 < Svg width = "100%" height = "100%" viewBox = "0 0 5 12" fill = "none" >
2325 < Path
@@ -28,7 +30,7 @@ const LeftArrow: React.FC<IconProps> = ({ fill = "#27272A" }) => {
2830 ) ;
2931} ;
3032
31- const RightArrow : React . FC < IconProps > = ( { fill = "#27272A " } ) => {
33+ const RightArrow : React . FC < IconProps > = ( { fill = "#171717 " } ) => {
3234 return (
3335 < Svg width = "100%" height = "100%" viewBox = "0 0 5 12" fill = "none" >
3436 < Path
@@ -39,7 +41,7 @@ const RightArrow: React.FC<IconProps> = ({ fill = "#27272A" }) => {
3941 ) ;
4042} ;
4143
42- const DownArrow : React . FC < IconProps > = ( { fill = "#27272A " } ) => {
44+ const DownArrow : React . FC < IconProps > = ( { fill = "#171717 " } ) => {
4345 return (
4446 < Svg width = "12" height = "5" viewBox = "0 0 12 5" fill = "none" >
4547 < Path
@@ -50,22 +52,126 @@ const DownArrow: React.FC<IconProps> = ({ fill = "#27272A" }) => {
5052 ) ;
5153} ;
5254
55+ interface ArrowStyle {
56+ style : Pick < ViewStyle , "left" | "top" > ;
57+ }
5358interface TooltipArrowProps {
54- placement : TooltipPlacement ;
59+ actualPlacement ?: TooltipPlacement ;
60+ arrowProps ?: ArrowStyle ;
5561}
5662
57- const TooltipArrow : React . FC < TooltipArrowProps > = ( { placement } ) => {
58- if ( placement . split ( " " ) [ 0 ] === "top" ) {
59- return < Icon icon = { < DownArrow /> } size = { 12 } /> ;
60- } else if ( placement . split ( " " ) [ 0 ] === "left" ) {
61- return < Icon icon = { < RightArrow /> } size = { 12 } /> ;
62- } else if ( placement . split ( " " ) [ 0 ] === "right" ) {
63- return < Icon icon = { < LeftArrow /> } size = { 12 } /> ;
64- } else if ( placement . split ( " " ) [ 0 ] === "bottom" ) {
65- return < Icon icon = { < UpArrow /> } size = { 12 } /> ;
63+ const defaultArrowHeight = 12 ;
64+
65+ const getArrowStyles = ( props : {
66+ placement : TooltipPlacement ;
67+ height : number ;
68+ width : number ;
69+ } ) => {
70+ let additionalStyles : any = {
71+ transform : [ ] ,
72+ } ;
73+
74+ const diagonalLength = getDiagonalLength (
75+ defaultArrowHeight ,
76+ defaultArrowHeight ,
77+ ) ;
78+ if ( props . placement === "top" && props . width ) {
79+ additionalStyles . transform . push ( { translateX : - props . width / 2 } ) ;
80+ additionalStyles . transform . push ( { translateY : 4.5 } ) ;
81+
82+ additionalStyles . bottom = Math . ceil (
83+ ( diagonalLength - defaultArrowHeight ) / 2 ,
84+ ) ;
85+ }
86+
87+ if ( props . placement === "bottom" && props . width ) {
88+ additionalStyles . transform . push ( { translateX : - props . width / 2 } ) ;
89+ additionalStyles . transform . push ( { translateY : 2.3 } ) ;
90+ additionalStyles . top = Math . ceil ( ( diagonalLength - defaultArrowHeight ) / 2 ) ;
91+ }
92+
93+ if ( props . placement === "left" && props . height ) {
94+ additionalStyles . transform . push ( { translateY : - props . height / 2 } ) ;
95+ additionalStyles . transform . push ( { translateX : 0.7 } ) ;
96+ additionalStyles . right = Math . ceil (
97+ ( diagonalLength - defaultArrowHeight ) / 2 ,
98+ ) ;
99+ }
100+
101+ if ( props . placement === "right" && props . height ) {
102+ additionalStyles . transform . push ( { translateY : - props . height / 2 } ) ;
103+ additionalStyles . transform . push ( { translateX : 2.3 } ) ;
104+ additionalStyles . left = Math . ceil (
105+ ( diagonalLength - defaultArrowHeight ) / 2 ,
106+ ) ;
107+ }
108+
109+ return additionalStyles ;
110+ } ;
111+
112+ const getDiagonalLength = ( height : number , width : number ) => {
113+ return Math . pow ( height * height + width * width , 0.5 ) ;
114+ } ;
115+
116+ const TooltipArrow : React . FC < TooltipArrowProps > = ( {
117+ actualPlacement = "bottom" ,
118+ arrowProps,
119+ } ) => {
120+ const tailwind = useTheme ( ) ;
121+ const additonalArrowStyles = getArrowStyles ( {
122+ placement : actualPlacement ,
123+ height : 12 ,
124+ width : 12 ,
125+ } ) ;
126+ const arrowStyles : ViewStyle = {
127+ position : "absolute" ,
128+ height : 12 ,
129+ width : 12 ,
130+ ...additonalArrowStyles ,
131+ ...arrowProps ?. style ,
132+ } ;
133+ console . log ( arrowStyles ) ;
134+ if ( actualPlacement ?. split ( " " ) [ 0 ] === "top" ) {
135+ return (
136+ < Icon
137+ color = { tailwind . getColor ( "text-gray-900" ) }
138+ style = { arrowStyles }
139+ icon = { < DownArrow /> }
140+ size = { 12 }
141+ />
142+ ) ;
143+ } else if ( actualPlacement ?. split ( " " ) [ 0 ] === "left" ) {
144+ return (
145+ < Icon
146+ color = { tailwind . getColor ( "text-gray-900" ) }
147+ style = { arrowStyles }
148+ icon = { < RightArrow /> }
149+ size = { 12 }
150+ />
151+ ) ;
152+ } else if ( actualPlacement ?. split ( " " ) [ 0 ] === "right" ) {
153+ return (
154+ < Icon
155+ color = { tailwind . getColor ( "text-gray-900" ) }
156+ style = { arrowStyles }
157+ icon = { < LeftArrow /> }
158+ size = { 12 }
159+ />
160+ ) ;
161+ } else if ( actualPlacement ?. split ( " " ) [ 0 ] === "bottom" ) {
162+ return (
163+ < Icon
164+ color = { tailwind . getColor ( "text-gray-900" ) }
165+ style = { arrowStyles }
166+ icon = { < UpArrow /> }
167+ size = { 12 }
168+ />
169+ ) ;
66170 } else {
67171 return null ;
68172 }
69173} ;
70174
175+ TooltipArrow . displayName = "PopperArrow" ;
176+
71177export default TooltipArrow ;
0 commit comments