# react-ol > React component library for OpenLayers maps. Provides composable React components wrapping the OpenLayers (`ol`) API — render maps, tile/vector layers, geometry features, and custom overlays declaratively. **Package:** `@mixelburg/react-ol` **npm:** https://www.npmjs.com/package/@mixelburg/react-ol **Docs:** https://mixelburg.github.io/react-ol **Source:** https://github.com/mixelburg/react-ol **Peer deps:** `ol ^10.7`, `react ^19`, `react-dom ^19` --- ## Installation ```bash bun add @mixelburg/react-ol ol # or npm install @mixelburg/react-ol ol ``` Import the OpenLayers stylesheet once in your app entry: ```ts import "ol/ol.css"; ``` --- ## Core Concepts - **``** — root container. Creates and provides the OL map instance via React context. - **``** — controls the viewport (center, zoom). Can be uncontrolled (defaultCenter/defaultZoom) or fully controlled. - **Layers** — ``, ``, ``, `` — add/remove layers declaratively as children of ``. - **Features** — ``, ``, ``, `` — must be children of a ``. - **``** — render arbitrary React JSX at a geographic coordinate (tooltips, popups, markers). All coordinates use `{ lat: number, long: number }` (WGS-84 / EPSG:4326). Internal conversion to/from EPSG:3857 is handled automatically. --- ## API Reference ### `` ```tsx import { OpenLayersMap } from "@mixelburg/react-ol"; console.log(coords)} // { lat, long } wrapperProps={{ style: { width: "100%", height: "400px" } }} ref={mapRef} // MapRef > {/* MapView, layers, overlays */} ``` **`MapRef` methods:** - `getMap(): OLMap | null` — raw OL map instance - `fitToFeatures(features, opts?)` — fit view to array of OL Features - `fitToLayers(layers, opts?)` — fit view to array of OL layers - `fitToCoordinates(coords[], opts?)` — fit view to array of `{ lat, long }` --- ### `` ```tsx import { MapView } from "@mixelburg/react-ol"; // Uncontrolled // Controlled ``` Extends OpenLayers `ViewOptions` minus `center`/`zoom` (those are replaced by the lat/long-based props above). --- ### `` ```tsx import { MapTileLayer } from "@mixelburg/react-ol"; import OSM from "ol/source/OSM"; import XYZ from "ol/source/XYZ"; ``` --- ### `` ```tsx import { MapVectorLayer } from "@mixelburg/react-ol"; ``` --- ### `` ```tsx import { PointFeature } from "@mixelburg/react-ol"; import { Style, Circle, Fill } from "ol/style"; console.log(feature.get("id"))} onMouseEnter={(feature) => setHovered(feature)} onMouseExit={() => setHovered(null)} /> ``` --- ### `` ```tsx {}} /> ``` --- ### `` ```tsx ``` --- ### `` ```tsx ``` --- ### `` Render any React element pinned to a geographic coordinate (popups, custom markers, labels): ```tsx import { ReactMapOverlay } from "@mixelburg/react-ol";
Hello, map!
``` --- ### `useMapRef` hook Access the OL map instance imperatively inside a child of ``: ```tsx import { useMapRef } from "@mixelburg/react-ol"; const MyControl = () => { const map = useMapRef(); // OLMap | null const handleFit = () => map?.getView().fit(extent); return ; }; ``` --- ### `reactIconToSrc` utility Convert a React SVG element into a data URI for use as an OL icon source: ```tsx import { reactIconToSrc } from "@mixelburg/react-ol"; import { Icon, Style } from "ol/style"; const style = new Style({ image: new Icon({ src: reactIconToSrc() }), }); ``` --- ## Full Example ```tsx import { OpenLayersMap, MapView, MapTileLayer, MapVectorLayer, PointFeature, ReactMapOverlay } from "@mixelburg/react-ol"; import OSM from "ol/source/OSM"; import "ol/ol.css"; export default function App() { return ( console.log("clicked", coords)} >
London HQ
); } ``` --- ## Types ```ts type Coordinates = { lat: number; long: number }; type BaseFeatureProps = { style?: StyleLike; hoverStyle?: StyleLike; properties?: Record; visible?: boolean; onClick?: (feature: Feature, event: any) => void; onMouseEnter?: (feature: Feature, event: any) => void; onMouseExit?: (feature: Feature, event: any) => void; }; interface MapRef { getMap(): OLMap | null; fitToFeatures(features: Feature[], options?: FitOptions): void; fitToLayers(layers: BaseLayer[], options?: FitOptions): void; fitToCoordinates(coordinates: Coordinates[], options?: FitOptions): void; } ```