99 onBeforeMount ,
1010 onUnmounted ,
1111 watchEffect ,
12+ shallowRef ,
1213} from ' vue' ;
1314import { MapProvideKey , MapboxEvents , MapCreationStatus } from ' @libs/enums' ;
1415import {
@@ -146,7 +147,7 @@ const { logError } = useLogger(props.debug);
146147
147148// Reactive state management
148149const innerOptions = ref <Partial <MapOptions >>();
149- const maplibreElRef = ref <HTMLElement >( );
150+ const mapContainerRef = shallowRef <HTMLElement | null >( null );
150151const styleRef = ref (props .options .style as string );
151152
152153const isComponentMounted = ref (false );
@@ -159,12 +160,6 @@ const mapOptions = useOptimizedComputed(
159160 () => {
160161 const baseOptions = { ... props .options };
161162 const mergedOptions = { ... baseOptions , ... innerOptions .value };
162-
163- // Ensure container is properly set
164- if (! mergedOptions .container ) {
165- mergedOptions .container = props .containerId ;
166- }
167-
168163 return mergedOptions ;
169164 },
170165 {
@@ -197,37 +192,6 @@ function setMapOptions(options: Partial<MapOptions>): void {
197192 }
198193}
199194
200- /**
201- * Enhanced container creation with error handling and validation
202- */
203- function createMapContainer(): HTMLElement | null {
204- try {
205- const wrapper = document .getElementById (' maplibre_container' );
206- if (! wrapper ) {
207- logError (' Map container wrapper not found' );
208- return null ;
209- }
210-
211- // Remove existing container if present
212- const existingContainer = document .getElementById (props .containerId );
213- if (existingContainer ) {
214- existingContainer .remove ();
215- }
216-
217- const container = document .createElement (' div' );
218- container .id = props .containerId ;
219- container .className = props .containerClass ;
220- container .style .width = ' 100%' ;
221- container .style .height = ' 100%' ;
222-
223- wrapper .appendChild (container );
224- return container ;
225- } catch (error ) {
226- logError (' Error creating map container:' , error );
227- return null ;
228- }
229- }
230-
231195// Enhanced map creation with comprehensive error handling and performance monitoring
232196const {
233197 mapInstance,
@@ -243,7 +207,7 @@ const {
243207 setMinPitch,
244208 setMinZoom,
245209 setRenderWorldCopies,
246- } = useCreateMapbox (maplibreElRef , styleRef , {
210+ } = useCreateMapbox (mapContainerRef , styleRef , {
247211 ... unref (mapOptions ),
248212 register : (actions : CreateMaplibreActions ) => {
249213 try {
@@ -367,14 +331,12 @@ watchEffect(async () => {
367331 try {
368332 await nextTick ();
369333
370- const container = createMapContainer ();
371- if (container ) {
372- maplibreElRef .value = container ;
334+ if (mapContainerRef .value ) {
373335 isComponentMounted .value = true ;
374336 mapCreationStatus .value = MapCreationStatus .Initializing ;
375337 } else {
376- logError ( ' Failed to create map container' );
377- mapCreationStatus . value = MapCreationStatus . Error ;
338+ // Wait for next tick if container is not ready yet
339+ return ;
378340 }
379341 } catch (error ) {
380342 logError (' Error in container creation watchEffect:' , error );
@@ -389,9 +351,6 @@ function cleanup(): void {
389351 // Stop all watchers
390352 watchers .forEach ((stopWatcher ) => stopWatcher ?.());
391353
392- // Remove container
393- maplibreElRef .value ?.remove ();
394-
395354 // Reset state
396355 isComponentMounted .value = false ;
397356 mapCreationStatus .value = MapCreationStatus .Destroyed ;
@@ -414,7 +373,11 @@ onUnmounted(() => {
414373 </script >
415374
416375<template >
417- <div id =" maplibre_container" >
376+ <div
377+ :id =" containerId"
378+ ref =" mapContainerRef"
379+ :class =" ['maplibre-container', containerClass]"
380+ >
418381 <!-- Loading state -->
419382 <div v-if =" isMapLoading" >
420383 <slot name =" loading" > </slot >
@@ -437,9 +400,4 @@ onUnmounted(() => {
437400 height : 100% ;
438401 overflow : hidden ;
439402}
440-
441- // Legacy support
442- #maplibre_container {
443- @extend .maplibre-container ;
444- }
445403 </style >
0 commit comments