Skip to content

Commit

Permalink
add fastdom
Browse files Browse the repository at this point in the history
  • Loading branch information
halvtomat authored and yankeeinlondon committed Sep 25, 2023
1 parent 5ae36e4 commit 5d55ff7
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 52 deletions.
1 change: 0 additions & 1 deletion src/shims-google-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ declare global {
offsetX?: number;
offsetY?: number;
zIndex?: number | null;
size?: { width: number; height: number };
}

export let CustomMarker: ReturnType<typeof createCustomMarkerClass>;
Expand Down
106 changes: 55 additions & 51 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fastdom from "fastdom";
type ICustomMarkerInterface = google.maps.OverlayView & {
getPosition(): google.maps.LatLng | null;
getVisible(): boolean;
Expand Down Expand Up @@ -62,57 +63,60 @@ export function createCustomMarkerClass(api: typeof google.maps): ICustomMarkerC
const point = overlayProjection?.fromLatLngToDivPixel(this.getPosition());

if (point) {
this.element.style.position = "absolute";
const height = this.opts.size && this.opts.size.height ? this.opts.size.height : this.element.offsetHeight;
const width = this.opts.size && this.opts.size.width ? this.opts.size.width : this.element.offsetWidth;
let x: number, y: number;
switch (this.opts.anchorPoint) {
case "TOP_CENTER":
x = point.x - width / 2;
y = point.y;
break;
case "BOTTOM_CENTER":
x = point.x - width / 2;
y = point.y - height;
break;
case "LEFT_CENTER":
x = point.x;
y = point.y - height / 2;
break;
case "RIGHT_CENTER":
x = point.x - width;
y = point.y - height / 2;
break;
case "TOP_LEFT":
x = point.x;
y = point.y;
break;
case "TOP_RIGHT":
x = point.x - width;
y = point.y;
break;
case "BOTTOM_LEFT":
x = point.x;
y = point.y - height;
break;
case "BOTTOM_RIGHT":
x = point.x - width;
y = point.y - height;
break;
default:
// "center"
x = point.x - width / 2;
y = point.y - height / 2;
}

this.element.style.left = x + "px";
this.element.style.top = y + "px";
// eslint-disable-next-line prettier/prettier
this.element.style.transform = `translateX(${this.opts.offsetX || 0}px) translateY(${this.opts.offsetY || 0}px)`;

if (this.opts.zIndex) {
this.element.style.zIndex = this.opts.zIndex.toString();
}
const element = this.element;
fastdom.measure(() => {
const height = element.offsetHeight;
const width = element.offsetWidth;
let x: number, y: number;
switch (this.opts.anchorPoint) {
case "TOP_CENTER":
x = point.x - width / 2;
y = point.y;
break;
case "BOTTOM_CENTER":
x = point.x - width / 2;
y = point.y - height;
break;
case "LEFT_CENTER":
x = point.x;
y = point.y - height / 2;
break;
case "RIGHT_CENTER":
x = point.x - width;
y = point.y - height / 2;
break;
case "TOP_LEFT":
x = point.x;
y = point.y;
break;
case "TOP_RIGHT":
x = point.x - width;
y = point.y;
break;
case "BOTTOM_LEFT":
x = point.x;
y = point.y - height;
break;
case "BOTTOM_RIGHT":
x = point.x - width;
y = point.y - height;
break;
default:
// "center"
x = point.x - width / 2;
y = point.y - height / 2;
}

fastdom.mutate(() => {
element.style.position = "absolute";
element.style.left = x + "px";
element.style.top = y + "px";

if (this.opts.zIndex) {
element.style.zIndex = this.opts.zIndex.toString();
}
});
});
}
}

Expand Down

0 comments on commit 5d55ff7

Please sign in to comment.