How adamscybot/react-leaflet-component-marker works
Project Type and Purpose
• This is a small, focused React component library (an open-source tool) named react-leaflet-component-marker.
• Purpose: allow users to drop *fully interactive* React components into a Leaflet map as markers, leveraging React Portals instead of static HTML rendering.
File-Structure Analysis
• Top-level build & config:
• .github/workflows/ (CI for build and release)
• package.json, tsconfig.json/tsconfig.build.json, ESLint/Prettier configs
• Source code:
• src/index.ts – library entry point exporting the custom Marker.
• src/Marker.tsx – core React component wrapper around react-leaflet’s <Marker>.
• src/lib/ – utilities: logging.ts (warnings, debug) and useCoordsFromPointExpression.ts (coordinate parsing).
• Tests:
• cypress/ for component-level tests.
• Types:
• @types/react-leaflet-core.d.ts – extended typings.
Key Architecture Insights from README
• Uses React’s createPortal under the hood to mount marker children.
• Exposes an API identical to React-Leaflet’s <Marker> plus extended props (componentIconOpts).
• Supports two layout modes: fit-content and fit-parent.
Main Components
a. Consumer Application (downstream)
• A React app that imports and uses @adamscybot/react-leaflet-component-marker.
b. Library Module (@adamscybot/react-leaflet-component-marker)
• Entry: src/index.ts.
• Core: src/Marker.tsx
• Wraps react-leaflet’s <Marker>.
• Calculates CSS position via useCoordsFromPointExpression.
• Emits warnings via logging.ts.
• Utilities:
• useCoordsFromPointExpression – converts Leaflet points to pixel offsets.
• logging – console warnings.
c. External Dependencies
• React & React-DOM (for Portals)
• react-leaflet (for Map and Marker)
• leaflet (for map engine & DivIcon)
• Cypress (for component tests)
d. Build & Release
• TypeScript compiler – tsconfig.build.json.
• CI workflows: build, test, publish to npm.
Relationships and Data-Flow
1. Consumer App calls → Library Entry
2. Library delegates → react-leaflet <Marker>
3. When rendering, Library
• Creates a DOM
wrapper (Leaflet DivIcon)
• Uses React-DOM Portal to mount user’s JSX into that wrapper
• Listens to map events via react-leaflet context
4. Leaflet Engine controls actual map panning/zoom; fit-parent mode delegates size back to Leaflet.
5. Tests simulate user interactions via Cypress against the component in isolation.
Architectural Patterns & Principles
• Adapter/Wrapper Pattern: wraps existing <Marker> API, preserving original contract.
• Portal Pattern: detaches React component subtree and remounts it into an external DOM node.
• Utility Extraction: coordinate parsing & logging are factored into small hooks/modules.
• Strong Typing: full TypeScript coverage for API safety.
Detail Level
• Configuration (tsconfig, eslint, Prettier) grouped under “Dev Config.”