Skip to content

Commit

Permalink
fix(ui): calculate click position
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Jul 10, 2023
1 parent 454c2ba commit 612011a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/ui/src/components/modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const DModal: {
} else if (modalContentRef.current) {
const left = `${(ROOT_DATA.pageSize.width - modalContentRef.current.offsetWidth) / 2}px`;
const top = dTop === 'center' ? `${(ROOT_DATA.pageSize.height - modalContentRef.current.offsetHeight) / 2}px` : topStyle;
dataRef.current.transformOrigin = `calc(${ROOT_DATA.clickEvent.e.clientX}px - ${left}) calc(${ROOT_DATA.clickEvent.e.clientY}px - ${top})`;
dataRef.current.transformOrigin = `calc(${ROOT_DATA.clickEvent.x}px - ${left}) calc(${ROOT_DATA.clickEvent.y}px - ${top})`;
}
}}
afterEnter={() => {
Expand Down
15 changes: 10 additions & 5 deletions packages/ui/src/components/root/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const ROOT = new DConfigContextManager({
export const ROOT_DATA: {
clickEvent?: {
time: number;
e: MouseEvent;
x: number;
y: number;
};
pageSize: { width: number; height: number };
} = {
Expand Down Expand Up @@ -96,10 +97,14 @@ export function DRoot(props: DRootProps): JSX.Element | null {
(e) => {
// Check if click by keydown.
if (!(e.clientX === 0 && e.clientY === 0)) {
ROOT_DATA.clickEvent = {
time: performance.now(),
e,
};
const rect = e.target instanceof Element ? e.target.getBoundingClientRect() : null;
if (rect) {
ROOT_DATA.clickEvent = {
time: performance.now(),
x: e.offsetX + rect.x,
y: e.offsetX + rect.y,
};
}
}
},
{ capture: true }
Expand Down

0 comments on commit 612011a

Please sign in to comment.