point-focus 1.0.4 → 2.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,13 +14,25 @@
14
14
  - **Easy Styling:** Modern, modular CSS/SCSS for effortless customization.
15
15
  - **Lightweight** – No external dependencies except React.
16
16
 
17
+ ## Packages
18
+
19
+ Since 2.0 this repository is a monorepo with two packages built on one shared zoom engine. Install the one you need:
20
+
21
+ | Package | Use it when | Size (gzip) |
22
+ |----------------------|----------------------------------------------------------------------------|-----------------------------|
23
+ | `point-focus` | You build with React. Same API as 1.x. | 7.6 KB, engine included |
24
+ | `point-focus-core` | You need no framework: plain HTML, Shopify themes, Vue, Svelte, and so on. | 4.2 KB as a drop-in script |
25
+
26
+ Both are built from the same engine source, so drag, follow and inertia behave identically. Neither depends on the other.
27
+
17
28
  ## 🛠️ Technologies
18
29
 
19
30
  - **React** – Declarative UI
20
31
  - **TypeScript** – Safer development
21
32
  - **SCSS Modules** – Scoped styling
22
33
 
23
- **Requirements:**
34
+ **Requirements:**
35
+
24
36
  - React 17+
25
37
  - Node.js with npm or yarn
26
38
 
@@ -38,64 +50,170 @@ yarn add point-focus
38
50
  import ImageMagnifier from 'point-focus'
39
51
 
40
52
  function Example() {
41
- return (
42
- <ImageMagnifier
43
- src="/images/sample.jpg"
44
- zoomSrc="/images/sample-large.jpg"
45
- moveType="drag"
46
- zoomType="click"
47
- alt='Image alt'
48
- zoomScale={2}
49
- />
50
- )
53
+ return <ImageMagnifier src='/images/sample.jpg' zoomSrc='/images/sample-large.jpg' moveType='drag' zoomType='click' alt='Image alt' zoomScale={2} />
51
54
  }
52
55
  ```
53
56
 
54
57
  ## 🔧 Props Overview
55
58
 
56
- | Prop | Type | Default | Description |
57
- |-------------------------|-------------------------------|-------------|-------------|
58
- | `src` | `string` | — (required) | Main image source |
59
- | `zoomSrc` | `string` | `src` | High-res zoom source |
60
- | `sources` | `array` | — | `<source>` tags for `<picture>` |
61
- | `width` / `height` | `number` | — | Container dimensions |
62
- | `zoomScale` | `number` | `1` | Magnification ratio |
63
- | `zoomPreload` | `boolean` | `false` | Preload zoom image on mount |
64
- | `fadeDuration` | `number` | `150` | Animation duration in ms |
65
- | `moveType` | `'follow' | 'drag' | 'pinch'` | `'follow'` | Pan behavior during zoom |
66
- | `zoomType` | `'click' | 'hover'` | `'click'` | How zoom is activated |
67
- | `clickToZoomOut` | `boolean` | `false` | Allow clicking image to zoom out |
68
- | `hideCloseButton` | `boolean` | `false` | Hide zoom-close button |
69
- | `alt` | `string` | `''` | Alt text for both images |
70
- | `tabIndex` | `number` | `0` | Keyboard navigation index |
71
- | `containerClassName` | `string` | — | Extra class for the wrapper |
72
- | `baseImageClassName` | `string` | — | Extra class for the base image |
73
- | `zoomImageClassName` | `string` | — | Extra class for zoom image |
74
- | `closeButtonClassName` | `string` | — | Custom class for close button |
75
- | `containerAriaLabel` | `string` | — | Accessibility label for container |
76
- | `zoomImageAriaLabel` | `string` | — | ARIA label for zoom image |
77
- | `closeButtonAriaLabel` | `string` | — | ARIA label for close button |
78
- | `closeButtonContent` | `ReactNode` | `X` icon | Replace the default close icon |
79
- | `overlay` | `ReactNode` | — | Optional overlay inside zoom container |
80
- | `loadingPlaceholder` | `ReactNode` | — | Shown while zoom image loads |
81
- | `errorPlaceholder` | `ReactNode` | — | Shown if zoom image fails |
82
- | `externalZoomState` | `boolean` | — | For controlled zoom state |
83
- | `setExternalZoomState` | `(val: boolean) => void` | — | Setter for external zoom state |
84
- | `onMouseEnter` | `() => void` | — | Called on hover enter |
85
- | `onMouseLeave` | `() => void` | — | Called on hover leave |
86
- | `onClickImage` | `() => void` | — | Called when image is clicked |
87
- | `onZoom` | `() => void` | — | Called when zoom starts |
88
- | `onClose` | `() => void` | — | Called when zoom closes |
89
- | `afterZoomImgLoaded` | `() => void` | — | Called when zoom image loads |
90
- | `afterZoomOut` | `() => void` | — | Called after zoom exits |
91
- | `onBaseImageError` | `() => void` | — | Called if base image fails to load |
92
- | `onZoomImageError` | `() => void` | — | Called if zoom image fails to load |
93
- | `onDragStart` | `() => void` | — | Called on drag start |
94
- | `onDragEnd` | `() => void` | — | Called on drag end |
59
+ | Prop | Type | Default | Description |
60
+ | ---------------------- | ------------------------ | ------------ | -------------------------------------- |
61
+ | `src` | `string` | — (required) | Main image source |
62
+ | `zoomSrc` | `string` | `src` | High-res zoom source |
63
+ | `sources` | `array` | — | `<source>` tags for `<picture>` |
64
+ | `width` / `height` | `number` | — | Container dimensions |
65
+ | `zoomScale` | `number` | `1` | Magnification ratio |
66
+ | `zoomPreload` | `boolean` | `false` | Preload zoom image on mount |
67
+ | `fadeDuration` | `number` | `150` | Animation duration in ms |
68
+ | `moveType` | `follow`, `drag`, `pinch`| `follow` | Pan behavior during zoom |
69
+ | `zoomType` | `click`, `hover` | `click` | How zoom is activated |
70
+ | `clickToZoomOut` | `boolean` | `false` | Allow clicking image to zoom out |
71
+ | `hideCloseButton` | `boolean` | `false` | Hide zoom-close button |
72
+ | `alt` | `string` | `''` | Alt text for both images |
73
+ | `tabIndex` | `number` | `0` | Keyboard navigation index |
74
+ | `containerClassName` | `string` | — | Extra class for the wrapper |
75
+ | `baseImageClassName` | `string` | — | Extra class for the base image |
76
+ | `zoomImageClassName` | `string` | — | Extra class for zoom image |
77
+ | `closeButtonClassName` | `string` | — | Custom class for close button |
78
+ | `containerAriaLabel` | `string` | — | Accessibility label for container |
79
+ | `zoomImageAriaLabel` | `string` | — | ARIA label for zoom image |
80
+ | `closeButtonAriaLabel` | `string` | — | ARIA label for close button |
81
+ | `closeButtonContent` | `ReactNode` | `X` icon | Replace the default close icon |
82
+ | `overlay` | `ReactNode` | — | Optional overlay inside zoom container |
83
+ | `loadingPlaceholder` | `ReactNode` | — | Shown while zoom image loads |
84
+ | `disableLoadingFallbacks` | `boolean` | `false` | Prevents/Disables loading fallbacks |
85
+ | `errorPlaceholder` | `ReactNode` | — | Shown if zoom image fails |
86
+ | `disableErrorFallbacks` | `boolean` | `false` | Prevents/Disables error fallbacks |
87
+ | `externalZoomState` | `boolean` | — | For controlled zoom state |
88
+ | `setExternalZoomState` | `(val: boolean) => void` | — | Setter for external zoom state |
89
+ | `onMouseEnter` | `() => void` | — | Called on hover enter |
90
+ | `onMouseLeave` | `() => void` | — | Called on hover leave |
91
+ | `onClickImage` | `() => void` | — | Called when image is clicked |
92
+ | `onZoom` | `() => void` | — | Called when zoom starts |
93
+ | `onClose` | `() => void` | — | Called when zoom closes |
94
+ | `afterZoomImgLoaded` | `() => void` | — | Called when zoom image loads |
95
+ | `afterZoomOut` | `() => void` | — | Called after zoom exits |
96
+ | `onBaseImageError` | `() => void` | — | Called if base image fails to load |
97
+ | `onZoomImageError` | `() => void` | — | Called if zoom image fails to load |
98
+ | `onDragStart` | `() => void` | — | Called on drag start |
99
+ | `onDragEnd` | `() => void` | — | Called on drag end |
100
+
101
+ ## Without React: `point-focus-core`
102
+
103
+ The core package ships a `<point-focus-image>` web component. It renders plain HTML (no shadow DOM), so the BEM classes below can be styled from any stylesheet.
104
+
105
+ ### Install
106
+
107
+ ```bash
108
+ npm install point-focus-core
109
+ ```
110
+
111
+ ```ts
112
+ import { definePointFocus } from 'point-focus-core'
113
+ import 'point-focus-core/point-focus.css'
114
+
115
+ definePointFocus()
116
+ ```
117
+
118
+ Or, with no build step, load the self-registering script and the stylesheet:
119
+
120
+ ```html
121
+ <link rel="stylesheet" href="https://unpkg.com/point-focus-core/dist/point-focus.css" />
122
+ <script src="https://unpkg.com/point-focus-core/dist/point-focus.min.js" defer></script>
123
+ ```
124
+
125
+ ### Usage
126
+
127
+ ```html
128
+ <point-focus-image src="/images/sample.jpg" zoom-src="/images/sample-large.jpg" alt="Image alt" move-type="drag" zoom-scale="2"></point-focus-image>
129
+ ```
130
+
131
+ ### Attributes
132
+
133
+ | Attribute | Values | Default | Description |
134
+ |----------------------|--------------------|------------------|----------------------------------------------|
135
+ | `src` | URL | required | Main image source |
136
+ | `zoom-src` | URL | `src` | High-res zoom source |
137
+ | `alt` | text | empty | Alt text; the zoom image gets "`alt` zoomed" |
138
+ | `zoom-scale` | 1 to 5 | `1` | Magnification ratio |
139
+ | `move-type` | `follow`, `drag` | `follow` | Pan behavior during zoom |
140
+ | `zoom-type` | `click`, `hover` | `click` | How zoom is activated |
141
+ | `fade-duration` | milliseconds | `150` | Fade in and out duration |
142
+ | `container-label` | text | `Zoomable image` | Accessible name of the container |
143
+ | `close-label` | text | `Zoom out` | Accessible name of the close button |
144
+ | `zoom-preload` | flag | off | Load the zoom image on mount |
145
+ | `click-to-zoom-out` | flag | off | Clicking the zoomed image closes it |
146
+ | `hide-close-button` | flag | off | Hide the close button |
147
+ | `disable-drag` | flag | off | Turn off dragging |
148
+ | `disable-inertia` | flag | off | Stop immediately on release, no glide |
149
+
150
+ ### Events and methods
151
+
152
+ | Event | `event.detail` |
153
+ |-----------------|-------------------------------------------------|
154
+ | `pf-zoom` | `{ at: { x, y }, method }` |
155
+ | `pf-close` | `{ triggeredBy }` |
156
+ | `pf-drag-start` | `{ start: { x, y }, source }` |
157
+ | `pf-drag-end` | `{ velocity: { vx, vy }, final: { x, y } }` |
158
+
159
+ Methods: `element.zoomIn(pageX?, pageY?)`, `element.zoomOut()`, and the read-only `element.isZoomed`.
160
+
161
+ ### Custom error content
162
+
163
+ Put a `<template data-error>` inside the element. Its content replaces the default fallback image when the zoom image fails to load.
164
+
165
+ ```html
166
+ <point-focus-image src="/a.jpg" alt="A boat">
167
+ <template data-error><p>Zoom unavailable</p></template>
168
+ </point-focus-image>
169
+ ```
170
+
171
+ ### Using the engine directly
172
+
173
+ `ZoomEngine` holds all the zoom math (follow, drag, inertia, clamping) with no DOM of its own. Use it to build an adapter for another framework: it reports every position through one `onPosition({ left, top })` callback.
174
+
175
+ ## What's new in 2.0
176
+
177
+ ### Fixed
178
+
179
+ - Releasing a drag without momentum no longer snaps the image back to where the drag started.
180
+ - With `clickToZoomOut`, finishing a drag no longer closes the zoom.
181
+ - Releasing over the image no longer starts two overlapping glide animations.
182
+ - Zooming with the keyboard centers correctly on a scrolled page.
183
+
184
+ ### Changed
185
+
186
+ - Drag, follow and inertia run in the shared `point-focus-core` engine. The image position is written straight to the element, so the component no longer re-renders on every animation frame.
187
+ - Class names are now plain BEM (`c-point-focus`, `c-point-focus__zoom-img`, and so on) instead of hashed CSS-module names, so they can be styled directly.
188
+ - `react` and `react-dom` are peer dependencies only, and the package has no other dependencies.
189
+ - The stylesheet adds a visible keyboard focus outline and respects `prefers-reduced-motion`.
190
+
191
+ ### Migrating from 1.x
192
+
193
+ Upgrade with `npm install point-focus@2`. Props, callbacks and markup are unchanged. Only CSS that targeted the old hashed class names needs updating to the new BEM names.
194
+
195
+ ## Development
196
+
197
+ ```
198
+ packages/core point-focus-core: engine, web component, stylesheet
199
+ packages/react point-focus: the React component, with the core engine bundled in
200
+ playground a page rendering both versions side by side
201
+ ```
202
+
203
+ ```bash
204
+ npm install
205
+ npm run dev # playground at http://localhost:5174
206
+ npm test # all tests (Jest, Testing Library, jest-axe)
207
+ npm run typecheck
208
+ npm run build # builds core, then React
209
+ ```
210
+
211
+ The playground runs from source, so edits reload live. A pre-commit hook runs the typecheck and tests, and GitHub Actions runs the typecheck, tests and build on every pull request.
95
212
 
96
213
  ## 🧭 Roadmap
97
214
 
98
215
  ### ✅ Already Implemented
216
+
99
217
  - SSR support (Next.js, Gatsby)
100
218
  - Click or hover zoom trigger
101
219
  - Follow mouse or drag pan
@@ -111,13 +229,14 @@ function Example() {
111
229
  - Programmatic zoom positioning setup via `onLoadCallback`
112
230
 
113
231
  ### 🚧 Planned Features
114
- - [ ] **Pinch-to-zoom** for touch devices
115
- - [ ] **Zoom lens / magnifier glass** effect
116
- - [ ] **Zoom on scroll / mouse wheel**
117
- - [ ] **Double-click or double-tap to zoom**
118
- - [ ] **Animated transitions with easing customization**
119
- - [ ] **Programmatic zoom in/out methods**
120
- - [ ] **Granular keyboard navigation**
232
+
233
+ - [ ] **Pinch-to-zoom** for touch devices
234
+ - [ ] **Zoom lens / magnifier glass** effect
235
+ - [ ] **Zoom on scroll / mouse wheel**
236
+ - [ ] **Double-click or double-tap to zoom**
237
+ - [ ] **Animated transitions with easing customization**
238
+ - [ ] **Programmatic zoom in/out methods**
239
+ - [ ] **Granular keyboard navigation**
121
240
  - [ ] **Fallback UI for broken zoom images**
122
241
  - [ ] **Built-in modal/lightbox integration**
123
242