Skip to content

Commit

Permalink
Fix ghost caret issue by blurring focused inputs upon drag start
Browse files Browse the repository at this point in the history
  • Loading branch information
Temzasse committed May 13, 2024
1 parent d51acb1 commit 9f34270
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/sheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ const Sheet = forwardRef<any, SheetProps>(
y.set(Math.max(y.get() + delta.y, 0));
});

const onDragStart = useEffectEvent(() => {
// Find focused input inside the sheet and blur it when dragging starts
// to prevent a weird ghost caret "bug" on mobile
const focusedElement = document.activeElement as HTMLElement | null;
if (!focusedElement || !sheetRef.current) return;

const isInput =
focusedElement.tagName === 'INPUT' ||
focusedElement.tagName === 'TEXTAREA';

// Only blur the focused element if it's inside the sheet
if (isInput && sheetRef.current.contains(focusedElement)) {
focusedElement.blur();
}
});

const onDragEnd = useEffectEvent((_, { velocity }: PanInfo) => {
if (velocity.y > DRAG_VELOCITY_THRESHOLD) {
// User flicked the sheet down
Expand Down Expand Up @@ -224,6 +240,7 @@ const Sheet = forwardRef<any, SheetProps>(
dragMomentum: false,
dragPropagation: false,
onDrag,
onDragStart,
onDragEnd,
};

Expand Down
1 change: 1 addition & 0 deletions src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface SheetDragProps {
dragMomentum: boolean;
dragPropagation: boolean;
onDrag: DragHandlers['onDrag'];
onDragStart: DragHandlers['onDragStart'];
onDragEnd: DragHandlers['onDragEnd'];
}

Expand Down

0 comments on commit 9f34270

Please sign in to comment.