From 3a690aaffaa18be36321928090974a680f86a24b Mon Sep 17 00:00:00 2001 From: Anton Tranelis Date: Fri, 10 Oct 2025 12:30:42 +0200 Subject: [PATCH] optimized layout --- app/src/pages/MapContainer.tsx | 2 +- .../HeaderView/ItemTitle.tsx | 69 ++++++++++++++++++- .../ItemPopupComponents/StartEndView.tsx | 6 +- lib/src/Components/Profile/ProfileView.tsx | 2 +- .../Subcomponents/ProfileStartEndView.tsx | 2 +- lib/src/types/ItemType.d.ts | 2 +- 6 files changed, 74 insertions(+), 9 deletions(-) diff --git a/app/src/pages/MapContainer.tsx b/app/src/pages/MapContainer.tsx index b30c1cb7..e622bfbd 100644 --- a/app/src/pages/MapContainer.tsx +++ b/app/src/pages/MapContainer.tsx @@ -120,7 +120,7 @@ function MapContainer({ layers, map }: { layers: LayerProps[]; map: any }) { parameterField={ layer.itemType.custom_profile_url ? 'extended.external_profile_id' : 'id' } - text={layer.itemType.botton_label ?? 'Profile'} + text={layer.itemType.button_label ?? 'Profile'} target={layer.itemType.custom_profile_url ? '_blank' : '_self'} /> )} diff --git a/lib/src/Components/Map/Subcomponents/ItemPopupComponents/HeaderView/ItemTitle.tsx b/lib/src/Components/Map/Subcomponents/ItemPopupComponents/HeaderView/ItemTitle.tsx index 3bb795c7..890af834 100644 --- a/lib/src/Components/Map/Subcomponents/ItemPopupComponents/HeaderView/ItemTitle.tsx +++ b/lib/src/Components/Map/Subcomponents/ItemPopupComponents/HeaderView/ItemTitle.tsx @@ -1,3 +1,4 @@ +import { useEffect, useRef, useState } from 'react' import { MapPinIcon } from '@heroicons/react/24/solid' import { useGeoDistance } from '#components/Map/hooks/useGeoDistance' @@ -24,6 +25,9 @@ export function ItemTitle({ }: ItemTitleProps) { const { distance } = useGeoDistance(item.position ?? undefined) const { formatDistance } = useFormatDistance() + const titleRef = useRef(null) + const containerRef = useRef(null) + const [fontSize, setFontSize] = useState('tw:text-xl') const { address } = useReverseGeocode( item.position?.coordinates as [number, number] | undefined, @@ -34,10 +38,71 @@ export function ItemTitle({ const title = item.name ?? item.layer?.item_default_name const subtitle = item.subname + useEffect(() => { + if (!containerRef.current || !title) { + return + } + + const calculateFontSize = () => { + const container = containerRef.current + if (!container) return + + const containerWidth = container.offsetWidth + + // Create temporary element to measure text width + const measureElement = document.createElement('span') + measureElement.style.position = 'absolute' + measureElement.style.visibility = 'hidden' + measureElement.style.whiteSpace = 'nowrap' + measureElement.style.fontWeight = '700' // font-bold + measureElement.textContent = title + document.body.appendChild(measureElement) + + // Measure at different font sizes - include larger sizes only if big is true + const fontSizes = big + ? [ + { class: 'tw:text-2xl', pixels: 24 }, + { class: 'tw:text-xl', pixels: 20 }, + { class: 'tw:text-lg', pixels: 18 } + ] + : [ + { class: 'tw:text-xl', pixels: 20 }, + { class: 'tw:text-lg', pixels: 18 } + ] + + let selectedSize = 'tw:text-lg' + + for (const size of fontSizes) { + measureElement.style.fontSize = `${size.pixels}px` + const textWidth = measureElement.offsetWidth + + if (textWidth <= containerWidth) { + selectedSize = size.class + break + } + } + + document.body.removeChild(measureElement) + setFontSize(selectedSize) + } + + // Initial calculation + calculateFontSize() + + // Watch for container size changes + const resizeObserver = new ResizeObserver(calculateFontSize) + resizeObserver.observe(containerRef.current) + + return () => { + resizeObserver.disconnect() + } + }, [title, big]) + return ( -
+
diff --git a/lib/src/Components/Map/Subcomponents/ItemPopupComponents/StartEndView.tsx b/lib/src/Components/Map/Subcomponents/ItemPopupComponents/StartEndView.tsx index 01119772..74da45bc 100644 --- a/lib/src/Components/Map/Subcomponents/ItemPopupComponents/StartEndView.tsx +++ b/lib/src/Components/Map/Subcomponents/ItemPopupComponents/StartEndView.tsx @@ -8,8 +8,8 @@ import type { Item } from '#types/Item' */ export const StartEndView = ({ item }: { item?: Item }) => { return ( -
-
+
+