-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from plezanje-net/crags-fix-map-markers
Crags fix map markers
- Loading branch information
Showing
5 changed files
with
86 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Marker, Popup } from "react-leaflet"; | ||
import L from "leaflet"; | ||
import IconMarker from "../ui/icons/marker"; | ||
import { ReactNode } from "react"; | ||
import { useClientRenderToString } from "@/hooks/useClientRenderToString"; | ||
|
||
type TMapMarkerProps = { | ||
marker: TMarker; | ||
index: number; | ||
}; | ||
|
||
type TMarker = { | ||
type: "parking" | "wall"; | ||
position: [number, number]; | ||
popupContent?: ReactNode; | ||
}; | ||
|
||
function MapMarker({ marker, index }: TMapMarkerProps) { | ||
const [icon] = useClientRenderToString(<IconMarker type={marker.type} />); | ||
|
||
return ( | ||
<Marker | ||
key={index} | ||
icon={L.divIcon({ | ||
className: "", | ||
html: icon, | ||
iconSize: [52, 52], | ||
iconAnchor: [26, 52], | ||
popupAnchor: [0, -46], | ||
})} | ||
position={marker.position} | ||
> | ||
<Popup>{marker.popupContent}</Popup> | ||
</Marker> | ||
); | ||
} | ||
|
||
export type { TMarker }; | ||
export default MapMarker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useEffect, useState } from "react"; | ||
import { createRoot } from "react-dom/client"; | ||
|
||
type TUseClientRenderToString = ( | ||
input: React.ReactElement | React.ReactElement[], | ||
deps?: any[] | ||
) => string[]; | ||
|
||
const clientRenderToString = (element: React.ReactElement): Promise<string> => | ||
new Promise((resolve) => { | ||
const container = document.createElement("div"); | ||
const renderCallback = () => { | ||
resolve(container.firstElementChild?.innerHTML || ""); | ||
}; | ||
|
||
createRoot(container).render(<div ref={renderCallback}>{element}</div>); | ||
}); | ||
|
||
export const useClientRenderToString: TUseClientRenderToString = ( | ||
input, | ||
deps = [] | ||
) => { | ||
const [htmlStringList, setHtmlStringList] = useState<string[]>([]); | ||
const elementList = Array.isArray(input) ? input : [input]; | ||
|
||
useEffect(() => { | ||
(async () => { | ||
const markupPromises = elementList.map(clientRenderToString); | ||
const markup: string[] = await Promise.all(markupPromises); | ||
|
||
if (!setHtmlStringList) { | ||
return; | ||
} | ||
|
||
setHtmlStringList(markup); | ||
})(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, deps); | ||
|
||
return htmlStringList; | ||
}; |