Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: incorrect localPoint result in Firefox #1861

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion packages/visx-event/src/localPointGeneric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,37 @@ import { EventType } from './types';
import { isSVGElement, isSVGGraphicsElement, isSVGSVGElement } from './typeGuards';
import getXAndYFromEvent from './getXAndYFromEvent';

const CHILD_ID = '__visx-child-ctm-workaround';

/**
* Gets the screen CTM applied to children.
*
* Normally this is equivalent to `node.getScreenCTM()` but
* when `node` is a nested `SVGSVGElement` Firefox returns
* the screen CTM of the parent `SVGSVGElement`. See:
* - https://bugzilla.mozilla.org/show_bug.cgi?id=1344537
* - https://bugzilla.mozilla.org/show_bug.cgi?id=1446011
*/
function getChildScreenCTM(node: SVGGraphicsElement): DOMMatrix | null {
if (!(node instanceof SVGSVGElement)) return node.getScreenCTM();
let child = node.children.namedItem(CHILD_ID) as SVGGraphicsElement;
if (child === null) {
child = document.createElementNS('http://www.w3.org/2000/svg', 'g');
child.id = CHILD_ID;
node.appendChild(child);
}
const screenCTM = child.getScreenCTM();
return screenCTM;
}

export default function localPoint(node: Element, event: EventType) {
if (!node || !event) return null;

const coords = getXAndYFromEvent(event);

// find top-most SVG
const svg = isSVGElement(node) ? node.ownerSVGElement : node;
const screenCTM = isSVGGraphicsElement(svg) ? svg.getScreenCTM() : null;
const screenCTM = isSVGGraphicsElement(svg) ? getChildScreenCTM(svg) : null;

if (isSVGSVGElement(svg) && screenCTM) {
let point = svg.createSVGPoint();
Expand Down
6 changes: 3 additions & 3 deletions packages/visx-vendor/scripts/buildVendor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ async function build() {

const { stdout, stderr } = await exec(
`babel \
--config-file ${BABEL_CONFIG_FILE} \
--config-file "${BABEL_CONFIG_FILE}" \
--only ${transpileGlob} \
--out-dir ${VENDOR_CJS_PATH} \
${NODE_MODULES_PATH}`,
--out-dir "${VENDOR_CJS_PATH}" \
"${NODE_MODULES_PATH}"`,
);

if (stdout) {
Expand Down