From 262c8f4735d7917e3b53ee1aca87deb4046afe44 Mon Sep 17 00:00:00 2001 From: Hassan Mojab Date: Mon, 1 Jan 2024 17:53:04 +0330 Subject: [PATCH 1/2] Improve arrow positioning --- src/index.ts | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 615cfcc..cf0d0bd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,6 +44,12 @@ type AvailableVariants = { he: number; }; +type AvailableArrowVariants = { + s: number; + m: number; + e: number; +}; + type PositionPairs = [Direction, Direction]; export type PositionMatch = 'ts' | 'tm' | 'te' | 'bs' | 'bm' | 'be' | 'ls' | 'lm' | 'le' | 'rs' | 'rm' | 're'; @@ -190,19 +196,37 @@ export const reposition = ( if (arrow) { // Calculate refBox's center offset from its variant position for arrow positioning const refBoxCenterOffset = vertical ? refBox.width / 2 : refBox.height / 2; - - // When refBox is larger than popBox, have the arrow's variant position be the center of popBox instead. - const arrowVariantVal = refBoxCenterOffset * 2 < variantSize ? - refBox[variantKey] + refBoxCenterOffset : variantVal + variantSize / 2; - - // Arrow position is either on one side of the popBox or the other. - if (positionVal < refBox[positionKey]) { - positionVal += positionSize; - } + const popBoxCenterOffset = variantSize / 2; + + // Check if refBox is larger than popBox + const isRefBoxLarger = refBoxCenterOffset > popBoxCenterOffset; + + /** + * Holds corresponding offset variants (start, middle, end) of arrow from the popper variant values. + * When refBox is larger than popBox, have the arrow's variant position be the center of popBox instead. + */ + const arrowVariantStore: AvailableArrowVariants = { + s: isRefBoxLarger ? popBoxCenterOffset : refBoxCenterOffset, + m: popBoxCenterOffset, + e: isRefBoxLarger ? popBoxCenterOffset : variantSize - refBoxCenterOffset, + }; + + /** + * Holds offsets of top, left, bottom and right alignment of arrow from the popper position values. + */ + const arrowPositionStore: AvailablePositions = { + t: positionSize, + b: 0, + r: 0, + l: positionSize + }; + + const arrowVariantVal = variantVal + arrowVariantStore[v as keyof AvailableArrowVariants]; + const arrowPositionVal = positionVal + arrowPositionStore[p as keyof AvailablePositions]; // Apply styles to arrow arrow.style[variantKey] = `${arrowVariantVal}px`; - arrow.style[positionKey] = `${positionVal}px`; + arrow.style[positionKey] = `${arrowPositionVal}px`; } return (p + v) as PositionMatch; From 97aacb523620ae4cc3c0ab4b43935fa6db755988 Mon Sep 17 00:00:00 2001 From: Hassan Mojab Date: Sun, 7 Jan 2024 16:58:02 +0330 Subject: [PATCH 2/2] Add tests for with arrow in transformed parent --- .../_pages/with-arrow-transformed-parent.html | 55 ++++++++++++++++++ tests/arrow.spec.ts | 1 + ...row-transformed-parent-html-bottom-end.png | Bin 0 -> 5468 bytes ...-transformed-parent-html-bottom-middle.png | Bin 0 -> 5467 bytes ...w-transformed-parent-html-bottom-start.png | Bin 0 -> 5470 bytes ...arrow-transformed-parent-html-left-end.png | Bin 0 -> 5453 bytes ...ow-transformed-parent-html-left-middle.png | Bin 0 -> 5465 bytes ...row-transformed-parent-html-left-start.png | Bin 0 -> 5464 bytes ...rrow-transformed-parent-html-right-end.png | Bin 0 -> 5457 bytes ...w-transformed-parent-html-right-middle.png | Bin 0 -> 5470 bytes ...ow-transformed-parent-html-right-start.png | Bin 0 -> 5466 bytes ...-arrow-transformed-parent-html-top-end.png | Bin 0 -> 5465 bytes ...row-transformed-parent-html-top-middle.png | Bin 0 -> 5463 bytes ...rrow-transformed-parent-html-top-start.png | Bin 0 -> 5466 bytes 14 files changed, 56 insertions(+) create mode 100644 tests/_pages/with-arrow-transformed-parent.html create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-bottom-end.png create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-bottom-middle.png create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-bottom-start.png create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-left-end.png create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-left-middle.png create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-left-start.png create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-right-end.png create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-right-middle.png create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-right-start.png create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-top-end.png create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-top-middle.png create mode 100644 tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-top-start.png diff --git a/tests/_pages/with-arrow-transformed-parent.html b/tests/_pages/with-arrow-transformed-parent.html new file mode 100644 index 0000000..34f9794 --- /dev/null +++ b/tests/_pages/with-arrow-transformed-parent.html @@ -0,0 +1,55 @@ +
+
+
+
+
+
+
+ + + + diff --git a/tests/arrow.spec.ts b/tests/arrow.spec.ts index 1d590d8..a7a4e78 100644 --- a/tests/arrow.spec.ts +++ b/tests/arrow.spec.ts @@ -13,6 +13,7 @@ test.describe('Popper with arrow option', () => { test(`Should position ${pos} with proper arrow placement`, async ({ page }) => { await testPage(page, `with-arrow-custom-position.html#${pos}`); await testPage(page, `with-arrow-large-popper.html#${pos}`); + await testPage(page, `with-arrow-transformed-parent.html#${pos}`); }); } } diff --git a/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-bottom-end.png b/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-bottom-end.png new file mode 100644 index 0000000000000000000000000000000000000000..bfa52494c9ad37ab4104e08bb55fdb9c8e1b480d GIT binary patch literal 5468 zcmeAS@N?(olHy`uVBq!ia0y~yUD`ZOcdmiayWGDasK+Lci$X| z{+_WpYOfE_ECz>5A73zn7zh4wG=OLUH3lXiz+nFr7^V&3|Bvmz-OfJ0fq}tT9oU>U@V$Ke$97dF zCWbS!fz9dz^^fmQ5ubzG( z$iVQs;M?=eFD&RJx`tk)Q1B1aFeg}}( uMvJ4-Kp3rOOn�MFmn#9x9&t;ucLK6TLSU-aR literal 0 HcmV?d00001 diff --git a/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-bottom-middle.png b/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-bottom-middle.png new file mode 100644 index 0000000000000000000000000000000000000000..1e7ea8fb348b0a5c5044173345cf7903e39fc115 GIT binary patch literal 5467 zcmeAS@N?(olHy`uVBq!ia0y~yUTgqiBQ?BV%j;^9( zS}=~*4hq0*Fj_^92Eq_&EZs=@0*tF~kAI#2`GQkE%I2{of_J6sK1T{mE> zIKh6JzxiTy28IvZKN(mU9`ts9t%|yLzkePh!vVP;AQ5}<*I!G%|Jbp6{rPij>lhgx z>;{GDhR;_2?%LV>U}0gHVaX`K(2zOn-m(3*L>5aJm8^WA3~%sKxPR9Ses`njxgN@xNAu+cyU literal 0 HcmV?d00001 diff --git a/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-bottom-start.png b/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-bottom-start.png new file mode 100644 index 0000000000000000000000000000000000000000..2a920d3468e705cf6b334f4973163a17123d9360 GIT binary patch literal 5470 zcmeAS@N?(olHy`uVBq!ia0y~yU>Z-{99mJ&BSmZ@Dn2kC<+sq z6hKr9X9I{5P-9>MQVug1MzZ9#S+>Ad@8Jmhn6vX37!HU(VHRL$5I?v5_QrjC=h`we zG%$Yv2HpXCph#hH{r{5qd2@YZm>CYF{{*&|ADsOBqrA5Kxv&63!b4DV`j~|NoJsbUI8j>sn9`^nn31b$?(5Wg&*q-qvU! rjP|yG`Cv2qYlFSXPWRbc%2RIx(+-2DtDnm{r-UW|kJBhj literal 0 HcmV?d00001 diff --git a/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-left-end.png b/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-left-end.png new file mode 100644 index 0000000000000000000000000000000000000000..56aacfb1f24136667e686859815fb5ab058efe24 GIT binary patch literal 5453 zcmeAS@N?(olHy`uVBq!ia0y~yU^ZXHFAssAo>uC1Bl`fW&p`6_%N^lsRn~l$OkJ987@_yB^r)JFx%bm#4p9apv#(|2O$73q!;IN$d)s*rG+G9T51gufJNI zfx%z`Fo75xIe$G|P=KM~F0irL;OOGO!0=57*xqatWMpKx;S6kYN_eoaFgyqWrlJD@ zN75QfP?$)-ix!je|POXMur2Dpz^s*=syF4MJq_< zgdgk-2UdcVB_6Q{+5P>_N8O}#gtDlBqVy9148 zV3>cHd9Wlt(|99+MNeA OusmJ;T-G@yGywn_h!zw8 literal 0 HcmV?d00001 diff --git a/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-left-middle.png b/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-left-middle.png new file mode 100644 index 0000000000000000000000000000000000000000..2d8c6e7aaaf9a7144503ed950c9b14711236f6b8 GIT binary patch literal 5465 zcmeAS@N?(olHy`uVBq!ia0y~yUHe$2KaRfqnD&*0fr0H41CV5BFkln_`S%cu z1Bl`fW&kyx6nuuFPJxe^pPWuWkvcysyg-K*Pw|GsL>z;MG5WP!#g*aKHV1pkWLQp7Cz{V8%aLMgb7x5Q_td;t*zF1X2n<3@kvZ!C+K!G!RBp!f19F zEfYoy#?jir0hkR&tH{wn7$S|O4QF34G90L?{rkIr}0r1H%vNvgOY&3IkoCDBl1wkb35m@3K*y zgd!FccMV^ymdKI;Vst091G|qW}N^ literal 0 HcmV?d00001 diff --git a/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-right-end.png b/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-right-end.png new file mode 100644 index 0000000000000000000000000000000000000000..efab630bd129e254c94a8997d0fcaaae768bfee0 GIT binary patch literal 5457 zcmeAS@N?(olHy`uVBq!ia0y~yUjfE$*(%6@AOwtURpm{?vPPJF_rb>uMNXWO}OV2;|DR}Mw z{b7;SUVWfZ3<@s~e*w`956V~^Ks1Li10#@9@L^y9QVj--0wB>tqmrY6Fq#rZv%_ea zFj_E<)(#`OisZY@%)pSacB`HJ|5@`6*z4W5_w{FBSa*bZw0;;W^}`zjTVUJn^^cDq zUw>)&|LfcD=U-SD8g@TnwBJo;e-${Gf61_V`R894 zFtZuag$Rv8c+}X@ZpvsiG*qgg(WW%R=s?TpXw&GB8?b#zc_6&upPp&qDR$UZ8)zJZ Mr>mdKI;Vst0Nl$P_W%F@ literal 0 HcmV?d00001 diff --git a/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-right-middle.png b/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-right-middle.png new file mode 100644 index 0000000000000000000000000000000000000000..b55901c4fa1c4371e3837f01a0324502217e6e20 GIT binary patch literal 5470 zcmeAS@N?(olHy`uVBq!ia0y~yU!aoGvJk6M{NGkq60En2kce*eBX#s}Bh z-}Qgq-X6~lG>gIGmyIomX4oOeC;*}lu{eMz4q*mHAf@2LzyhQi3`QkK17S2JjAnJiqaO&1`s8n#=r!m9A+?ZfVhcF3LuJH$;a>fmV+#b z1R1j!$ocx~o0%;$gMv4(ftk?rgPozE79^#7#GawS7SsmaCj6g);hP|^8G6I(4>Q9J zutMpN{0s^9NR82Lm3DI1x8J_~QWEHaE}&BngefU79Eby%b-S~Hf#Ej?kmwO$WMudz z05nTNnTd%Z=6+%o15o|#&mVVOi+WvJ!Oq|?73BHH|6Ck^G44;BM(-N~U{kx{^p5)a zne!MJ4%D9*rU@%y?bdVs=9h&T7$(___98~>31C(m4TRA^7#&#vrh(Bw7!3rZ=F4co e=s=T#v5xs(q{SzVuYEf~#(TQ@xvX5&tEcv7ze&`G=OLUH3lXiXDKqK8-$L`McyDZGWunA<08<6w* z>DSUKHU@`jz&7RqiI4mY4}O8AI_eo1szJ@r9QA*U3~!WyEzu2Af3P!bm=02C_DG)L zKpj$vlk>U6W?laF?U#)i80LP8p7WMr2?3bJj44!s0$RrpkhB|p~)K~{lJ|nm28r3-(e3Vvy qqiJX~4UMKDP$^7dJCewZ^oGA{g2jhscAG^YJ3U?fT-G@yGywo}`atmj literal 0 HcmV?d00001 diff --git a/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-top-end.png b/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-top-end.png new file mode 100644 index 0000000000000000000000000000000000000000..f70ca5cd91e43d22045b92d58b61091f2c5c47d6 GIT binary patch literal 5465 zcmeAS@N?(olHy`uVBq!ia0y~yUpOF(!&>wAjW}r91S2^K#hS3NIA@4-~e$GnG`@&3ugm}8kHOkgwd2R znjI8Cg~4c$K5|9hhr}MIdaG zuw`IK2;UZde6jkU>gTV&mh8Xz=hfWHPm6ZNt}WZb!O*b(8Iu4*LjT=$VV7SP{e1lV zs69i&PeBGohHrQLl^6VAXE5*wW#Id64j=g$685n;Ffc63{AI(`RnNfi8PpE^@TitW zWA53=fze@-r-O2C_PKSXxQjKJ*svFJG+A!0<-(K2VT> zp}~Mr07x+$0vXG|z#$B@mw`dSXGBXz_s9X8+V^)B}ORD%KIsN`rM455^eV^{@jrk#EEs^Uw_+PU}d``t@k0BMdUvN!!vK7;)Get z)+;XnsmllD?DckT4j=g$68`;Rm0)NnzsF_|=7I8$o!~zP1}h+|p=0*q%@>9Dm44hJ zXUoWtp#PH%sB^S-z!nHMlD+^#=HIcSm#e-23!Q(8;tfFlXsdcO5JuZlz%(!#2%~{8 rSR0h1z24FMsQ{{{MhmII90=c-9%Siuc6Nw+fa1#2)z4*}Q$iB}+CxNT literal 0 HcmV?d00001 diff --git a/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-top-start.png b/tests/arrow.spec.ts-snapshots/with-arrow-transformed-parent-html-top-start.png new file mode 100644 index 0000000000000000000000000000000000000000..1fc76c34e888baf3bf6d73b9e420817ade7ded68 GIT binary patch literal 5466 zcmeAS@N?(olHy`uVBq!ia0y~yU}Igr8a;_*|h0-HLc4Q&HhJp^(XLN73HaY}NsO57}JA9Y~T#pHG8JD+Fn z`|&FCSw?l`PaB|d3=K=-H?xBn4ENm`SU>^>i~=C;Ar=P^#UaeV2&5EzMkPlBVKgO- zW{1%-VYFZz{Ix@lX*DpR{{Hy;{Np#Adp7K~nrX|(kkIpxO#vv(u!OS#LQ0WC^?q)K z2Fp9ZR+~kF`Rub6vFrE$+4F;)VS^1Yl^759WbNz{e)RI&H=GO%e;o2gOQ6wW7?fK^>!Q&> v7;SirHarwSg~4bObTkk~J0oQE$Zo_R-J;v6weh1MC?k2g`njxgN@xNA27yEK literal 0 HcmV?d00001