forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to dismiss alt text badge by tapping it in web UI (mastod…
- Loading branch information
Showing
4 changed files
with
81 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useRef, useCallback } from 'react'; | ||
|
||
type Position = [number, number]; | ||
|
||
export const useSelectableClick = ( | ||
onClick: React.MouseEventHandler, | ||
maxDelta = 5, | ||
) => { | ||
const clickPositionRef = useRef<Position | null>(null); | ||
|
||
const handleMouseDown = useCallback((e: React.MouseEvent) => { | ||
clickPositionRef.current = [e.clientX, e.clientY]; | ||
}, []); | ||
|
||
const handleMouseUp = useCallback( | ||
(e: React.MouseEvent) => { | ||
if (!clickPositionRef.current) { | ||
return; | ||
} | ||
|
||
const [startX, startY] = clickPositionRef.current; | ||
const [deltaX, deltaY] = [ | ||
Math.abs(e.clientX - startX), | ||
Math.abs(e.clientY - startY), | ||
]; | ||
|
||
let element: EventTarget | null = e.target; | ||
|
||
while (element && element instanceof HTMLElement) { | ||
if ( | ||
element.localName === 'button' || | ||
element.localName === 'a' || | ||
element.localName === 'label' | ||
) { | ||
return; | ||
} | ||
|
||
element = element.parentNode; | ||
} | ||
|
||
if ( | ||
deltaX + deltaY < maxDelta && | ||
(e.button === 0 || e.button === 1) && | ||
e.detail >= 1 | ||
) { | ||
onClick(e); | ||
} | ||
|
||
clickPositionRef.current = null; | ||
}, | ||
[maxDelta, onClick], | ||
); | ||
|
||
return [handleMouseDown, handleMouseUp] as const; | ||
}; |
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