Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react-chart): fix event coordinates calculation in a scrolled con…
…tainer (#1950)
- Loading branch information
1 parent
785f8a1
commit 498969f
Showing
6 changed files
with
57 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { getOffset } from './root-offset'; | ||
|
||
describe('Root Offset', () => { | ||
describe('#getOffset', () => { | ||
it('should return element offset', () => { | ||
const mock = { | ||
getBoundingClientRect: () => ({ left: 400, top: 300 }), | ||
ownerDocument: { | ||
defaultView: { pageXOffset: 40, pageYOffset: 30 }, | ||
}, | ||
}; | ||
|
||
expect(getOffset(mock as any)).toEqual([440, 330]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import { NumberArray } from '../types'; | ||
// This function is to be called from context where DOM is available - | ||
// so *window* can be accessed safely. | ||
/** @internal */ | ||
export const getRootOffset = (root: Element) => { | ||
const { left, top } = root.getBoundingClientRect(); | ||
const { pageXOffset, pageYOffset } = window; | ||
export const getOffset = (element: Element): NumberArray => { | ||
const { left, top } = element.getBoundingClientRect(); | ||
// Safer way to get window. | ||
const { defaultView } = element.ownerDocument!; | ||
const { pageXOffset, pageYOffset } = defaultView!; | ||
return [left + pageXOffset, top + pageYOffset]; | ||
}; |