|
1 | 1 | # react-pannellum |
2 | 2 |
|
3 | | -> A library show panorama image for react |
| 3 | +> A React library for embedding [Pannellum](https://pannellum.org/) panorama viewers. |
4 | 4 |
|
5 | | -> This library use source from [https://pannellum.org/](https://pannellum.org/) |
| 5 | +> This library bundles source derived from [pannellum.org](https://pannellum.org/). |
6 | 6 |
|
7 | | -[](https://www.npmjs.com/package/react-pannellum) [](https://standardjs.com) |
| 7 | +[](https://www.npmjs.com/package/react-pannellum)  |
8 | 8 |
|
9 | 9 | ## Install |
10 | 10 |
|
11 | 11 | ```bash |
12 | | -npm install --save react-pannellum |
| 12 | +npm install react-pannellum |
13 | 13 | ``` |
14 | 14 |
|
| 15 | +Peer dependencies **`react`** and **`react-dom`** (versions **^18** or **^19**) must be installed in your application. |
| 16 | + |
| 17 | +## Requirements |
| 18 | + |
| 19 | +- **React 18+** or **React 19** (with matching **React DOM**) |
| 20 | +- An **ESM** toolchain (Vite, webpack, Next.js, etc.). The published package uses [`"type": "module"`](https://nodejs.org/api/packages.html#type); **`require('react-pannellum')` is not supported** — use `import` (or dynamic `import()`). |
| 21 | + |
| 22 | +## Migration (class component → hooks implementation) |
| 23 | + |
| 24 | +Earlier releases exposed a **class** component. The default export is now a **function component** built with React hooks, but **the public API is intentionally stable**: |
| 25 | + |
| 26 | +| Area | Notes | |
| 27 | +|------|--------| |
| 28 | +| **React versions** | Supported as **peer** dependencies: **React 18** and **React 19** (with matching `react-dom`). | |
| 29 | +| **Import** | Same as before: `import ReactPannellum from "react-pannellum"`. | |
| 30 | +| **Static / imperative API** | Methods such as `ReactPannellum.getPitch()`, `loadScene`, `addHotSpot`, etc. remain on the **default export** object (same call pattern as the old class statics). | |
| 31 | +| **Props** | Same props; **`prop-types` was removed** from this package — TypeScript users get exported types (`ReactPannellumProps`, etc.); plain JS apps do not get runtime prop validation from the library. | |
| 32 | +| **Children** | Pass **`children` as normal JSX children** inside `<ReactPannellum>…</ReactPannellum>` (a root `<div>` wraps them). | |
| 33 | +| **TypeScript** | Types ship with the package (`"types"` in `package.json`). | |
| 34 | +| **Build output** | The library is compiled to **ESM** in `dist/` (no CommonJS bundle). | |
| 35 | + |
| 36 | +Internals (hooks, cleanup, listener registration) changed; if you relied on undocumented behavior or private APIs, re-test after upgrading. |
| 37 | + |
15 | 38 | ## Usage |
16 | 39 |
|
17 | 40 | 1. [Config props](#config) |
18 | 41 | 2. [API Events](#apiEvents) |
19 | 42 | 3. [API Event Listeners](#apiEventListeners) |
20 | 43 |
|
21 | | -```jsx |
22 | | -import React from "react"; |
| 44 | +```tsx |
| 45 | +import { useCallback } from "react"; |
23 | 46 | import ReactPannellum, { getConfig } from "react-pannellum"; |
24 | 47 |
|
25 | | -class Example extends React.Component { |
26 | | - click() { |
| 48 | +export function Example() { |
| 49 | + const showConfig = useCallback(() => { |
27 | 50 | console.log(getConfig()); |
28 | | - } |
29 | | - |
30 | | - render() { |
31 | | - const config = { |
32 | | - autoRotate: -2, |
33 | | - }; |
34 | | - return ( |
35 | | - <div> |
36 | | - <ReactPannellum |
37 | | - id="1" |
38 | | - sceneId="firstScene" |
39 | | - imageSource="https://pannellum.org/images/alma.jpg" |
40 | | - config={config} |
41 | | - /> |
42 | | - <div onClick={this.click}>Click me</div> |
43 | | - </div> |
44 | | - ); |
45 | | - } |
| 51 | + }, []); |
| 52 | + |
| 53 | + const config = { |
| 54 | + autoRotate: -2, |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <div> |
| 59 | + <ReactPannellum |
| 60 | + id="1" |
| 61 | + sceneId="firstScene" |
| 62 | + imageSource="https://pannellum.org/images/alma.jpg" |
| 63 | + config={config} |
| 64 | + /> |
| 65 | + <button type="button" onClick={showConfig}> |
| 66 | + Log config |
| 67 | + </button> |
| 68 | + </div> |
| 69 | + ); |
46 | 70 | } |
47 | 71 | ``` |
48 | 72 |
|
@@ -369,7 +393,9 @@ If set to `true`, prevent displaying out-of-range areas of a partial panorama by |
369 | 393 |
|
370 | 394 | ## <a id="apiEvents" ></a> API Events |
371 | 395 |
|
372 | | -### Do not call API event in `componentDidMount` or API event will return undefined. |
| 396 | +The viewer is created **after mount** (inside a `useEffect` in the implementation). **Do not call** the static imperative methods above **in the same synchronous turn as the first render** — the internal viewer may not exist yet, so you can get `undefined` / no-ops. |
| 397 | + |
| 398 | +Use **`onPanoramaLoaded`**, or a **`useEffect`** with an empty dependency array (runs after paint) so the component has mounted and the Pannellum instance exists. |
373 | 399 |
|
374 | 400 | > ### isLoaded |
375 | 401 | > |
@@ -468,7 +494,7 @@ Parameters: |
468 | 494 | - `hfov` [number] Target hfov. |
469 | 495 | - `animated` [(boolean | number)] Animation duration in milliseconds or false for no animation (optional, default 1000). |
470 | 496 | - `callback` [function] Function to call when animation finishes. |
471 | | -- `callbackArg`s [object] Arguments to pass to callback function. |
| 497 | +- `callbackArgs` [object] Arguments to pass to callback function. |
472 | 498 |
|
473 | 499 | > ### getNorthOffset |
474 | 500 | > |
@@ -645,11 +671,19 @@ Returns `true` if active, else `false`. |
645 | 671 | > |
646 | 672 | > Destructor. |
647 | 673 |
|
648 | | -## <a id="apiEventListeners" ></a> Api Event Listeners |
| 674 | +## <a id="apiEventListeners"></a> API event listeners (props) |
649 | 675 |
|
650 | | -> ### onPanoramaLoaded |
651 | | -> |
652 | | -> Will be triggered when panorama is loaded |
| 676 | +### onPanoramaLoaded (optional): `() => void` |
| 677 | + |
| 678 | +Called when the panorama has finished loading (forwarded to Pannellum’s `load` event). |
| 679 | + |
| 680 | +### onPanoramaMouseDown (optional): `(event: unknown) => void` |
| 681 | + |
| 682 | +Called on viewer `mousedown`. |
| 683 | + |
| 684 | +### onPanoramaMouseUp (optional): `(event: unknown) => void` |
| 685 | + |
| 686 | +Called on viewer `mouseup`. |
653 | 687 |
|
654 | 688 | ## License |
655 | 689 |
|
|
0 commit comments