From aa9293104d4a177837fa95ba506af36a8d4fbd36 Mon Sep 17 00:00:00 2001 From: Anton Tranelis Date: Sun, 12 Oct 2025 09:59:09 +0200 Subject: [PATCH] replaced select position element by toast --- .../Map/Subcomponents/SelectPosition.tsx | 52 ------------- .../Map/Subcomponents/SelectPositionToast.tsx | 76 +++++++++++++++++++ lib/src/Components/Map/UtopiaMapInner.tsx | 12 ++- 3 files changed, 81 insertions(+), 59 deletions(-) delete mode 100644 lib/src/Components/Map/Subcomponents/SelectPosition.tsx create mode 100644 lib/src/Components/Map/Subcomponents/SelectPositionToast.tsx diff --git a/lib/src/Components/Map/Subcomponents/SelectPosition.tsx b/lib/src/Components/Map/Subcomponents/SelectPosition.tsx deleted file mode 100644 index 1aed12c0..00000000 --- a/lib/src/Components/Map/Subcomponents/SelectPosition.tsx +++ /dev/null @@ -1,52 +0,0 @@ -import { DomEvent } from 'leaflet' -import { useEffect, useRef } from 'react' - -import type { Item } from '#types/Item' -import type { LayerProps } from '#types/LayerProps' - -export const SelectPosition = ({ - setSelectNewItemPosition, - selectNewItemPosition, -}: { - setSelectNewItemPosition: React.Dispatch> - selectNewItemPosition?: Item | LayerProps | null -}) => { - const containerRef = useRef(null) - - useEffect(() => { - if (containerRef.current) { - DomEvent.disableClickPropagation(containerRef.current) - DomEvent.disableScrollPropagation(containerRef.current) - } - }, []) - - return ( -
- -
-
- {selectNewItemPosition && 'layer' in selectNewItemPosition && ( - - Select new position of {selectNewItemPosition.name} on the map! - - )} - {selectNewItemPosition && 'markerIcon' in selectNewItemPosition && ( - Select position on the map! - )} -
-
-
- ) -} diff --git a/lib/src/Components/Map/Subcomponents/SelectPositionToast.tsx b/lib/src/Components/Map/Subcomponents/SelectPositionToast.tsx new file mode 100644 index 00000000..e3369aa9 --- /dev/null +++ b/lib/src/Components/Map/Subcomponents/SelectPositionToast.tsx @@ -0,0 +1,76 @@ +import { useEffect, useRef } from 'react' +import { toast } from 'react-toastify' + +import type { Item } from '#types/Item' +import type { LayerProps } from '#types/LayerProps' + +interface SelectPositionToastProps { + selectNewItemPosition: Item | LayerProps | null + setSelectNewItemPosition: React.Dispatch> +} + +export const SelectPositionToast = ({ + selectNewItemPosition, + setSelectNewItemPosition, +}: SelectPositionToastProps) => { + const toastIdRef = useRef(null) + + // Escape-Key Listener + useEffect(() => { + const handleEscape = (event: KeyboardEvent) => { + if (event.key === 'Escape' && selectNewItemPosition) { + toast.dismiss('select-position-toast') + toastIdRef.current = null + setSelectNewItemPosition(null) + } + } + + window.addEventListener('keydown', handleEscape) + return () => window.removeEventListener('keydown', handleEscape) + }, [selectNewItemPosition, setSelectNewItemPosition]) + + useEffect(() => { + if (selectNewItemPosition && !toastIdRef.current) { + let message = '' + if ('layer' in selectNewItemPosition) { + message = `Select the new position of ${selectNewItemPosition.name} on the map!` + } else if ('markerIcon' in selectNewItemPosition) { + message = 'Select the position on the map!' + } + + const CloseButton = () => ( + + ) + + toastIdRef.current = toast( +
+ {message} + +
, + { + toastId: 'select-position-toast', + autoClose: false, + closeButton: false, + closeOnClick: false, + draggable: false, + }, + ) + } + + if (!selectNewItemPosition && toastIdRef.current) { + toast.dismiss(toastIdRef.current) + toastIdRef.current = null + } + }, [selectNewItemPosition, setSelectNewItemPosition]) + + return null +} diff --git a/lib/src/Components/Map/UtopiaMapInner.tsx b/lib/src/Components/Map/UtopiaMapInner.tsx index 9b102a93..fce6159a 100644 --- a/lib/src/Components/Map/UtopiaMapInner.tsx +++ b/lib/src/Components/Map/UtopiaMapInner.tsx @@ -42,7 +42,7 @@ import { LayerControl } from './Subcomponents/Controls/LayerControl' import { SearchControl } from './Subcomponents/Controls/SearchControl' import { TagsControl } from './Subcomponents/Controls/TagsControl' import { TextView } from './Subcomponents/ItemPopupComponents/TextView' -import { SelectPosition } from './Subcomponents/SelectPosition' +import { SelectPositionToast } from './Subcomponents/SelectPositionToast' import type { Feature, Geometry as GeoJSONGeometry, GeoJsonObject } from 'geojson' @@ -317,12 +317,10 @@ export function UtopiaMapInner({ )} - {selectNewItemPosition != null && ( - - )} + ) }