Skip to content

Commit

Permalink
Grid: Fix resizing in Firefox (#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoorayimhelping authored Jul 29, 2024
1 parent 10d8489 commit 7c852bb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
47 changes: 37 additions & 10 deletions src/components/Grid/ColumnResizer.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { MouseEventHandler, PointerEventHandler, useCallback, useRef } from "react";
import {
MouseEventHandler,
PointerEventHandler,
useCallback,
useRef,
useState,
} from "react";
import styled from "styled-components";
import { ColumnResizeFn, SetResizeCursorPositionFn } from "./types";
import throttle from "lodash/throttle";

const ResizeSpan = styled.div<{ $height: number }>`
const ResizeSpan = styled.div<{ $height: number; $isPressed: boolean }>`
top: 0;
left: calc(100% - 4px);
z-index: 1;
Expand All @@ -17,10 +23,12 @@ const ResizeSpan = styled.div<{ $height: number }>`
&:hover {
background: ${({ theme }) => theme.click.grid.header.cell.color.stroke.selectDirect};
}
&:active {
height: 100%;
position: fixed;
}
${({ $isPressed }) =>
$isPressed &&
`
height: 100%;
position: fixed;
`}
`;
type PointerRefType = {
width: number;
Expand All @@ -42,17 +50,29 @@ const ColumnResizer = ({
}: Props) => {
const resizeRef = useRef<HTMLDivElement>(null);
const pointerRef = useRef<PointerRefType | null>(null);
const [isPressed, setIsPressed] = useState<boolean>(false);
const onColumnResize = throttle(onColumnResizeProp, 1000);

const onMouseDown: MouseEventHandler<HTMLDivElement> = useCallback(
e => {
e.preventDefault();
e.stopPropagation();
setIsPressed(true);

if (e.detail > 1) {
onColumnResize(columnIndex, 0, "auto");
}
},
[columnIndex, onColumnResize]
[columnIndex, onColumnResize, setIsPressed]
);

const onMouseUp: MouseEventHandler<HTMLDivElement> = useCallback(
e => {
e.stopPropagation();

setIsPressed(false);
},
[setIsPressed]
);
const onPointerDown: PointerEventHandler<HTMLDivElement> = useCallback(
e => {
Expand Down Expand Up @@ -87,7 +107,8 @@ const ColumnResizer = ({
if (header) {
resizeRef.current.setPointerCapture(pointerRef.current.pointerId);
const width =
header.scrollWidth + (e.clientX - pointerRef.current.initialClientX);
header.clientWidth + (e.clientX - pointerRef.current.initialClientX);

setResizeCursorPosition(resizeRef.current, e.clientX, width, columnIndex);
pointerRef.current.width = Math.max(width, 50);
}
Expand All @@ -100,11 +121,17 @@ const ColumnResizer = ({
<ResizeSpan
ref={resizeRef}
$height={height}
$isPressed={isPressed}
onPointerDown={onPointerDown}
onPointerUp={e => {
e.preventDefault();
e.stopPropagation();
if (resizeRef.current && pointerRef.current?.pointerId) {

if (
resizeRef.current &&
// 0 is a valid pointerId in Firefox
(pointerRef.current?.pointerId || pointerRef.current?.pointerId === 0)
) {
resizeRef.current.releasePointerCapture(pointerRef.current.pointerId);
const shouldCallResize = e.clientX !== pointerRef.current.initialClientX;
if (shouldCallResize) {
Expand All @@ -118,7 +145,7 @@ const ColumnResizer = ({
onMouseMove={onMouseMove}
onMouseDown={onMouseDown}
onClick={e => e.stopPropagation()}
onMouseUp={e => e.stopPropagation()}
onMouseUp={onMouseUp}
data-resize
/>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Grid/Grid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe("Grid", () => {
expect(lastHeaderCell).toBeNull();
});

it("should render focussed element", () => {
it("should render focused element", () => {
const { queryByTestId, getByText } = renderGrid({});
const rowNumber = getByText("1");
expect(rowNumber).not.toBeNull();
Expand Down
3 changes: 2 additions & 1 deletion src/components/Grid/Grid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const OuterElementType = forwardRef<HTMLDivElement>((props, ref) => (
<div
ref={ref}
data-testid="grid-outer-element"
tabIndex={0}
{...props}
/>
));
Expand Down Expand Up @@ -780,7 +781,7 @@ export const Grid = forwardRef<HTMLDivElement, GridProps>(
width={width}
/>
);
}
};

return (
<ContextMenu
Expand Down

0 comments on commit 7c852bb

Please sign in to comment.