Skip to content

Commit 65a04d0

Browse files
author
danhnguyen
committed
📦refactor(marker): reorganize imports and enhance marker attribute handling
1 parent a9015bc commit 65a04d0

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

libs/components/Marker.vue

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<script lang="ts" setup>
2-
import { computed, inject, ref, shallowRef, useSlots, watch } from 'vue';
2+
import {
3+
computed,
4+
inject,
5+
onUpdated,
6+
ref,
7+
shallowRef,
8+
useAttrs,
9+
useSlots,
10+
watch,
11+
} from 'vue';
312
import { MapProvideKey } from '@libs/enums';
413
import { useCreateMarker } from '@libs/composables';
514
import type { Anchor } from '@libs/types';
@@ -59,6 +68,10 @@ interface Emits {
5968
(e: 'dragend', ev: Event): void;
6069
}
6170
71+
defineOptions({
72+
inheritAttrs: false,
73+
});
74+
6275
// Component props with sensible defaults
6376
const props = withDefaults(defineProps<Partial<MarkerProps>>(), {
6477
options: () => ({}),
@@ -69,13 +82,16 @@ const emits = defineEmits<Emits>();
6982
7083
// Slots for custom marker content
7184
const slots = useSlots();
85+
const attrs = useAttrs();
7286
7387
// Injected dependencies
7488
const mapInstance = inject(MapProvideKey, shallowRef(null));
7589
const markerElRef = ref<HTMLElement>();
90+
const markerAttrNames = new Set<string>();
7691
7792
if (typeof document !== 'undefined') {
7893
markerElRef.value = document.createElement('div');
94+
syncMarkerAttrs();
7995
}
8096
8197
// Computed properties for better performance
@@ -116,6 +132,43 @@ const eventHandlers = {
116132
},
117133
};
118134
135+
function normalizeMarkerClass(value: unknown): string {
136+
if (typeof value === 'string') return value;
137+
if (Array.isArray(value)) return value.map(normalizeMarkerClass).filter(Boolean).join(' ');
138+
if (value && typeof value === 'object') {
139+
return Object.entries(value)
140+
.filter(([, enabled]) => enabled)
141+
.map(([name]) => name)
142+
.join(' ');
143+
}
144+
return '';
145+
}
146+
147+
function syncMarkerAttrs() {
148+
const el = markerElRef.value;
149+
if (!el) return;
150+
151+
markerAttrNames.forEach((name) => el.removeAttribute(name));
152+
markerAttrNames.clear();
153+
154+
Object.entries(attrs).forEach(([name, value]) => {
155+
if (value == null || name.startsWith('on')) return;
156+
157+
if (name === 'class') {
158+
el.className = normalizeMarkerClass(value);
159+
return;
160+
}
161+
162+
if (name === 'style') {
163+
if (typeof value === 'string') el.setAttribute('style', value);
164+
return;
165+
}
166+
167+
el.setAttribute(name, String(value));
168+
markerAttrNames.add(name);
169+
});
170+
}
171+
119172
// Create marker with optimized configuration
120173
const { setDraggable, setLngLat } = useCreateMarker({
121174
map: mapInstance,
@@ -145,6 +198,8 @@ watch(
145198
}
146199
},
147200
);
201+
202+
onUpdated(syncMarkerAttrs);
148203
</script>
149204
<template>
150205
<Teleport v-if="markerElRef && hasCustomElement" :to="markerElRef">

0 commit comments

Comments
 (0)