@openmaic/renderer 0.2.3 → 0.2.4
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/dist/effects/SpotlightOverlay.js +2 -12
- package/dist/effects/SpotlightOverlay.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/utils/spotlight-rect.d.ts +7 -0
- package/dist/utils/spotlight-rect.js +51 -0
- package/dist/utils/spotlight-rect.js.map +1 -0
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
3
|
import { useRef, useState, useCallback, useLayoutEffect } from 'react';
|
|
4
|
+
import { measureSpotlightRect } from '../utils/spotlight-rect.js';
|
|
4
5
|
import { AnimatePresence, motion } from 'motion/react';
|
|
5
6
|
|
|
6
7
|
function SpotlightOverlay({ options, elementIdPrefix = 'slide-element-', measurementKey, }) {
|
|
@@ -19,18 +20,7 @@ function SpotlightOverlay({ options, elementIdPrefix = 'slide-element-', measure
|
|
|
19
20
|
}
|
|
20
21
|
const contentEl = domElement.querySelector('.element-content');
|
|
21
22
|
const targetEl = contentEl ?? domElement;
|
|
22
|
-
|
|
23
|
-
const targetRect = targetEl.getBoundingClientRect();
|
|
24
|
-
if (containerRect.width === 0 || containerRect.height === 0) {
|
|
25
|
-
setRect(null);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
setRect({
|
|
29
|
-
x: ((targetRect.left - containerRect.left) / containerRect.width) * 100,
|
|
30
|
-
y: ((targetRect.top - containerRect.top) / containerRect.height) * 100,
|
|
31
|
-
w: (targetRect.width / containerRect.width) * 100,
|
|
32
|
-
h: (targetRect.height / containerRect.height) * 100,
|
|
33
|
-
});
|
|
23
|
+
setRect(measureSpotlightRect(containerRef.current, targetEl));
|
|
34
24
|
}, [spotlightElementId, elementIdPrefix]);
|
|
35
25
|
useLayoutEffect(() => {
|
|
36
26
|
// eslint-disable-next-line react-hooks/set-state-in-effect -- DOM measurement requires effect
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SpotlightOverlay.js","sources":["../../src/effects/SpotlightOverlay.tsx"],"sourcesContent":["'use client';\n\nimport { useRef, useState, useLayoutEffect, useCallback } from 'react';\nimport { motion, AnimatePresence } from 'motion/react';\nimport type { SpotlightEffectOptions } from '../types/effects';\n\ninterface SpotlightRect {\n x: number;\n y: number;\n w: number;\n h: number;\n}\n\nexport interface SpotlightOverlayProps {\n options?: SpotlightEffectOptions;\n /** ID prefix the SlideElement uses on its root div. Default `slide-element-`. */\n elementIdPrefix?: string;\n /** Remeasure when host element data changes without changing the target ID. */\n measurementKey?: unknown;\n}\n\nexport function SpotlightOverlay({\n options,\n elementIdPrefix = 'slide-element-',\n measurementKey,\n}: SpotlightOverlayProps) {\n const containerRef = useRef<HTMLDivElement>(null);\n const [rect, setRect] = useState<SpotlightRect | null>(null);\n\n const spotlightElementId = options?.elementId;\n\n const measure = useCallback(() => {\n if (!spotlightElementId || !containerRef.current) {\n setRect(null);\n return;\n }\n\n const domElement = document.getElementById(`${elementIdPrefix}${spotlightElementId}`);\n if (!domElement) {\n setRect(null);\n return;\n }\n\n const contentEl = domElement.querySelector('.element-content');\n const targetEl = contentEl ?? domElement;\n\n
|
|
1
|
+
{"version":3,"file":"SpotlightOverlay.js","sources":["../../src/effects/SpotlightOverlay.tsx"],"sourcesContent":["'use client';\n\nimport { useRef, useState, useLayoutEffect, useCallback } from 'react';\nimport { measureSpotlightRect } from '../utils/spotlight-rect';\nimport { motion, AnimatePresence } from 'motion/react';\nimport type { SpotlightEffectOptions } from '../types/effects';\n\ninterface SpotlightRect {\n x: number;\n y: number;\n w: number;\n h: number;\n}\n\nexport interface SpotlightOverlayProps {\n options?: SpotlightEffectOptions;\n /** ID prefix the SlideElement uses on its root div. Default `slide-element-`. */\n elementIdPrefix?: string;\n /** Remeasure when host element data changes without changing the target ID. */\n measurementKey?: unknown;\n}\n\nexport function SpotlightOverlay({\n options,\n elementIdPrefix = 'slide-element-',\n measurementKey,\n}: SpotlightOverlayProps) {\n const containerRef = useRef<HTMLDivElement>(null);\n const [rect, setRect] = useState<SpotlightRect | null>(null);\n\n const spotlightElementId = options?.elementId;\n\n const measure = useCallback(() => {\n if (!spotlightElementId || !containerRef.current) {\n setRect(null);\n return;\n }\n\n const domElement = document.getElementById(`${elementIdPrefix}${spotlightElementId}`);\n if (!domElement) {\n setRect(null);\n return;\n }\n\n const contentEl = domElement.querySelector('.element-content');\n const targetEl = contentEl ?? domElement;\n\n setRect(measureSpotlightRect(containerRef.current, targetEl));\n }, [spotlightElementId, elementIdPrefix]);\n\n useLayoutEffect(() => {\n // eslint-disable-next-line react-hooks/set-state-in-effect -- DOM measurement requires effect\n measure();\n\n const container = containerRef.current;\n if (!container || !spotlightElementId) return;\n\n const root = document.getElementById(`${elementIdPrefix}${spotlightElementId}`);\n const target = root?.querySelector('.element-content') ?? root;\n if (!target) return;\n\n const observer = new ResizeObserver(measure);\n observer.observe(container);\n observer.observe(target);\n\n const media = target.querySelectorAll('img, video');\n media.forEach((element) => {\n element.addEventListener('load', measure);\n element.addEventListener('loadedmetadata', measure);\n });\n\n return () => {\n observer.disconnect();\n media.forEach((element) => {\n element.removeEventListener('load', measure);\n element.removeEventListener('loadedmetadata', measure);\n });\n };\n }, [measure, measurementKey, spotlightElementId, elementIdPrefix]);\n\n const active = !!spotlightElementId && !!rect;\n const dimness = options?.dimness ?? 0.7;\n\n return (\n <div\n ref={containerRef}\n style={{\n position: 'absolute',\n inset: 0,\n zIndex: 100,\n pointerEvents: 'none',\n overflow: 'hidden',\n }}\n >\n <AnimatePresence mode=\"wait\">\n {active && rect && (\n <motion.div\n key={`spotlight-${spotlightElementId}`}\n initial={{ opacity: 0 }}\n animate={{ opacity: 1 }}\n exit={{ opacity: 0 }}\n style={{ position: 'absolute', inset: 0 }}\n >\n <svg\n width=\"100%\"\n height=\"100%\"\n viewBox=\"0 0 100 100\"\n preserveAspectRatio=\"none\"\n style={{ position: 'absolute', inset: 0 }}\n >\n <defs>\n <mask id={`mask-${spotlightElementId}`}>\n <rect x=\"0\" y=\"0\" width=\"100\" height=\"100\" fill=\"white\" />\n <motion.rect\n fill=\"black\"\n initial={{\n x: rect.x - 8,\n y: rect.y - 8,\n width: rect.w + 16,\n height: rect.h + 16,\n rx: 4,\n }}\n animate={{\n x: rect.x - 0.4,\n y: rect.y - 0.6,\n width: rect.w + 0.8,\n height: rect.h + 1.2,\n rx: 1,\n }}\n transition={{ duration: 0.6, ease: [0.16, 1, 0.3, 1] }}\n />\n </mask>\n </defs>\n\n <rect\n width=\"100\"\n height=\"100\"\n fill={`rgba(0,0,0,${dimness})`}\n mask={`url(#mask-${spotlightElementId})`}\n />\n\n <motion.rect\n initial={{\n x: rect.x - 4,\n y: rect.y - 4,\n width: rect.w + 8,\n height: rect.h + 8,\n opacity: 0,\n rx: 2,\n }}\n animate={{\n x: rect.x - 0.4,\n y: rect.y - 0.6,\n width: rect.w + 0.8,\n height: rect.h + 1.2,\n opacity: 1,\n rx: 1,\n }}\n fill=\"none\"\n stroke=\"rgba(255,255,255,0.7)\"\n strokeWidth=\"1.2\"\n style={{ vectorEffect: 'non-scaling-stroke' } as React.CSSProperties}\n transition={{ duration: 0.5, delay: 0.05, ease: [0.16, 1, 0.3, 1] }}\n />\n </svg>\n </motion.div>\n )}\n </AnimatePresence>\n </div>\n );\n}\n"],"names":[],"mappings":";;;;;;AAsBM;AAKJ;;AAGA;AAEA;;;;;AAME;;;;;;AAOA;;AAGF;;;AAIE;AAEA;AACA;;AAEA;;AAEA;;AAEA;AACA;AACA;;AAGA;AACE;AACA;AACF;AAEA;;AAEE;AACE;AACA;AACF;AACF;;;AAIF;AAEA;AAIM;AACA;AACA;AACA;AACA;;AAyBc;AACA;AACA;AACA;AACA;AACD;AAEC;AACA;AACA;AACA;AACA;;AAgBJ;AACA;AACA;AACA;AACA;AACA;AACD;AAEC;AACA;AACA;AACA;AACA;AACA;;AAclB;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export { HighlightOverlay, SpotlightOverlay, LaserOverlay, ZoomWrapper, type Hig
|
|
|
5
5
|
export { useSlideBackgroundStyle } from './hooks/useSlideBackgroundStyle';
|
|
6
6
|
export { useViewportSize, type ViewportStyles, type UseViewportSizeOptions, type UseViewportSizeResult, } from './hooks/useViewportSize';
|
|
7
7
|
export { findElementGeometry, findNearestCorner, getElementPercentageGeometry, type PercentageGeometry, } from './utils/geometry';
|
|
8
|
+
export { measureSpotlightRect } from './utils/spotlight-rect';
|
|
8
9
|
export { cn } from './utils/cn';
|
|
9
10
|
export { getElementRange, getLineElementPath, getTableSubThemeColor } from './utils/element';
|
|
10
11
|
export { createTextProseStyles } from './styles';
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,7 @@ export { ZoomWrapper } from './effects/ZoomWrapper.js';
|
|
|
8
8
|
export { useSlideBackgroundStyle } from './hooks/useSlideBackgroundStyle.js';
|
|
9
9
|
export { useViewportSize } from './hooks/useViewportSize.js';
|
|
10
10
|
export { findElementGeometry, findNearestCorner, getElementPercentageGeometry } from './utils/geometry.js';
|
|
11
|
+
export { measureSpotlightRect } from './utils/spotlight-rect.js';
|
|
11
12
|
export { cn } from './utils/cn.js';
|
|
12
13
|
export { getElementRange, getLineElementPath, getTableSubThemeColor } from './utils/element.js';
|
|
13
14
|
export { createTextProseStyles } from './styles.js';
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Translation and transform-origin affect the center, which the browser already
|
|
2
|
+
// measures. Only the linear part is needed for center deltas and box dimensions.
|
|
3
|
+
function linearTransform(element) {
|
|
4
|
+
let matrix;
|
|
5
|
+
for (let node = element; node; node = node.parentElement) {
|
|
6
|
+
const transform = getComputedStyle(node).transform;
|
|
7
|
+
if (transform && transform !== 'none') {
|
|
8
|
+
const parent = new DOMMatrix(transform);
|
|
9
|
+
matrix = matrix ? parent.multiply(matrix) : parent;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
if (matrix)
|
|
13
|
+
return new DOMMatrix([matrix.a, matrix.b, matrix.c, matrix.d, 0, 0]);
|
|
14
|
+
}
|
|
15
|
+
/** Measure the target's rendered border box in the overlay's 0–100 viewBox. */
|
|
16
|
+
function measureSpotlightRect(overlay, target) {
|
|
17
|
+
const frame = overlay.getBoundingClientRect();
|
|
18
|
+
const box = target.getBoundingClientRect();
|
|
19
|
+
if (!frame.width || !frame.height)
|
|
20
|
+
return null;
|
|
21
|
+
const axes = linearTransform(overlay);
|
|
22
|
+
if (!axes || (!axes.b && !axes.c && axes.a > 0 && axes.d > 0)) {
|
|
23
|
+
return {
|
|
24
|
+
x: ((box.left - frame.left) / frame.width) * 100,
|
|
25
|
+
y: ((box.top - frame.top) / frame.height) * 100,
|
|
26
|
+
w: (box.width / frame.width) * 100,
|
|
27
|
+
h: (box.height / frame.height) * 100,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
if (!(target instanceof HTMLElement) || !overlay.clientWidth || !overlay.clientHeight)
|
|
31
|
+
return null;
|
|
32
|
+
// The center survives rotation, whereas the corners of a viewport-aligned
|
|
33
|
+
// bounding rect no longer correspond to the corners of the target.
|
|
34
|
+
const inverse = axes.inverse();
|
|
35
|
+
const center = inverse.transformPoint({
|
|
36
|
+
x: box.left + box.width / 2 - (frame.left + frame.width / 2),
|
|
37
|
+
y: box.top + box.height / 2 - (frame.top + frame.height / 2),
|
|
38
|
+
});
|
|
39
|
+
const local = inverse.multiply(linearTransform(target));
|
|
40
|
+
const width = Math.abs(local.a) * target.offsetWidth + Math.abs(local.c) * target.offsetHeight;
|
|
41
|
+
const height = Math.abs(local.b) * target.offsetWidth + Math.abs(local.d) * target.offsetHeight;
|
|
42
|
+
return {
|
|
43
|
+
x: 50 + ((center.x - width / 2) / overlay.clientWidth) * 100,
|
|
44
|
+
y: 50 + ((center.y - height / 2) / overlay.clientHeight) * 100,
|
|
45
|
+
w: (width / overlay.clientWidth) * 100,
|
|
46
|
+
h: (height / overlay.clientHeight) * 100,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export { measureSpotlightRect };
|
|
51
|
+
//# sourceMappingURL=spotlight-rect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spotlight-rect.js","sources":["../../src/utils/spotlight-rect.ts"],"sourcesContent":["// Translation and transform-origin affect the center, which the browser already\n// measures. Only the linear part is needed for center deltas and box dimensions.\nfunction linearTransform(element: Element) {\n let matrix: DOMMatrix | undefined;\n for (let node: Element | null = element; node; node = node.parentElement) {\n const transform = getComputedStyle(node).transform;\n if (transform && transform !== 'none') {\n const parent = new DOMMatrix(transform);\n matrix = matrix ? parent.multiply(matrix) : parent;\n }\n }\n if (matrix) return new DOMMatrix([matrix.a, matrix.b, matrix.c, matrix.d, 0, 0]);\n}\n\n/** Measure the target's rendered border box in the overlay's 0–100 viewBox. */\nexport function measureSpotlightRect(overlay: HTMLElement, target: Element) {\n const frame = overlay.getBoundingClientRect();\n const box = target.getBoundingClientRect();\n if (!frame.width || !frame.height) return null;\n\n const axes = linearTransform(overlay);\n if (!axes || (!axes.b && !axes.c && axes.a > 0 && axes.d > 0)) {\n return {\n x: ((box.left - frame.left) / frame.width) * 100,\n y: ((box.top - frame.top) / frame.height) * 100,\n w: (box.width / frame.width) * 100,\n h: (box.height / frame.height) * 100,\n };\n }\n\n if (!(target instanceof HTMLElement) || !overlay.clientWidth || !overlay.clientHeight)\n return null;\n // The center survives rotation, whereas the corners of a viewport-aligned\n // bounding rect no longer correspond to the corners of the target.\n const inverse = axes.inverse();\n const center = inverse.transformPoint({\n x: box.left + box.width / 2 - (frame.left + frame.width / 2),\n y: box.top + box.height / 2 - (frame.top + frame.height / 2),\n });\n const local = inverse.multiply(linearTransform(target));\n const width = Math.abs(local.a) * target.offsetWidth + Math.abs(local.c) * target.offsetHeight;\n const height = Math.abs(local.b) * target.offsetWidth + Math.abs(local.d) * target.offsetHeight;\n return {\n x: 50 + ((center.x - width / 2) / overlay.clientWidth) * 100,\n y: 50 + ((center.y - height / 2) / overlay.clientHeight) * 100,\n w: (width / overlay.clientWidth) * 100,\n h: (height / overlay.clientHeight) * 100,\n };\n}\n"],"names":[],"mappings":"AAAA;AACA;AACA,SAAS,eAAe,CAAC,OAAgB,EAAA;AACvC,IAAA,IAAI,MAA6B;AACjC,IAAA,KAAK,IAAI,IAAI,GAAmB,OAAO,EAAE,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC,aAAa,EAAE;QACxE,MAAM,SAAS,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC,SAAS;AAClD,QAAA,IAAI,SAAS,IAAI,SAAS,KAAK,MAAM,EAAE;AACrC,YAAA,MAAM,MAAM,GAAG,IAAI,SAAS,CAAC,SAAS,CAAC;AACvC,YAAA,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,MAAM;QACpD;IACF;AACA,IAAA,IAAI,MAAM;QAAE,OAAO,IAAI,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;AAClF;AAEA;AACM,SAAU,oBAAoB,CAAC,OAAoB,EAAE,MAAe,EAAA;AACxE,IAAA,MAAM,KAAK,GAAG,OAAO,CAAC,qBAAqB,EAAE;AAC7C,IAAA,MAAM,GAAG,GAAG,MAAM,CAAC,qBAAqB,EAAE;IAC1C,IAAI,CAAC,KAAK,CAAC,KAAK,IAAI,C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