|
@@ -1,10 +1,12 @@
|
|
|
'use client'
|
|
'use client'
|
|
|
|
|
|
|
|
-import { useCallback, useEffect, useRef, useState } from 'react'
|
|
|
|
|
|
|
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
|
|
import {
|
|
import {
|
|
|
- readSupportPosition,
|
|
|
|
|
- writeSupportPosition,
|
|
|
|
|
|
|
+ readLegacySupportPosition,
|
|
|
|
|
+ readSupportAnchor,
|
|
|
|
|
+ type SupportAnchor,
|
|
|
type SupportPosition,
|
|
type SupportPosition,
|
|
|
|
|
+ writeSupportAnchor,
|
|
|
} from './supportVisibility'
|
|
} from './supportVisibility'
|
|
|
|
|
|
|
|
/** Distance to the viewport edges the button is never allowed to cross. */
|
|
/** Distance to the viewport edges the button is never allowed to cross. */
|
|
@@ -16,24 +18,64 @@ const EDGE_MARGIN = 8
|
|
|
*/
|
|
*/
|
|
|
const MOBILE_NAV_HEIGHT = 56
|
|
const MOBILE_NAV_HEIGHT = 56
|
|
|
|
|
|
|
|
|
|
+interface Viewport {
|
|
|
|
|
+ width: number
|
|
|
|
|
+ height: number
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function readViewport(): Viewport {
|
|
|
|
|
+ return { width: window.innerWidth, height: window.innerHeight }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
function clamp(value: number, min: number, max: number): number {
|
|
function clamp(value: number, min: number, max: number): number {
|
|
|
// max can fall below min on a very small viewport; min wins so the button
|
|
// max can fall below min on a very small viewport; min wins so the button
|
|
|
// stays reachable rather than being pushed off the top.
|
|
// stays reachable rather than being pushed off the top.
|
|
|
return Math.max(min, Math.min(value, max))
|
|
return Math.max(min, Math.min(value, max))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function defaultPosition(size: number): SupportPosition {
|
|
|
|
|
- const isMobile = window.innerWidth < 768
|
|
|
|
|
|
|
+/** The bottom right corner, clear of the bottom navigation in a narrow window. */
|
|
|
|
|
+function defaultAnchor(viewport: Viewport): SupportAnchor {
|
|
|
|
|
+ const isMobile = viewport.width < 768
|
|
|
return {
|
|
return {
|
|
|
- x: window.innerWidth - size - (isMobile ? 16 : 24),
|
|
|
|
|
- y: window.innerHeight - size - (isMobile ? MOBILE_NAV_HEIGHT + 16 : 24),
|
|
|
|
|
|
|
+ horizontal: 'right',
|
|
|
|
|
+ x: isMobile ? 16 : 24,
|
|
|
|
|
+ vertical: 'bottom',
|
|
|
|
|
+ y: isMobile ? MOBILE_NAV_HEIGHT + 16 : 24,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function clampToViewport(position: SupportPosition, size: number): SupportPosition {
|
|
|
|
|
|
|
+function clampToViewport(
|
|
|
|
|
+ position: SupportPosition,
|
|
|
|
|
+ size: number,
|
|
|
|
|
+ viewport: Viewport
|
|
|
|
|
+): SupportPosition {
|
|
|
return {
|
|
return {
|
|
|
- x: clamp(position.x, EDGE_MARGIN, window.innerWidth - size - EDGE_MARGIN),
|
|
|
|
|
- y: clamp(position.y, EDGE_MARGIN, window.innerHeight - size - EDGE_MARGIN),
|
|
|
|
|
|
|
+ x: clamp(position.x, EDGE_MARGIN, viewport.width - size - EDGE_MARGIN),
|
|
|
|
|
+ y: clamp(position.y, EDGE_MARGIN, viewport.height - size - EDGE_MARGIN),
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** Where an anchored button is drawn in a window of this size, kept on screen. */
|
|
|
|
|
+function resolveAnchor(anchor: SupportAnchor, size: number, viewport: Viewport): SupportPosition {
|
|
|
|
|
+ return clampToViewport(
|
|
|
|
|
+ {
|
|
|
|
|
+ x: anchor.horizontal === 'left' ? anchor.x : viewport.width - size - anchor.x,
|
|
|
|
|
+ y: anchor.vertical === 'top' ? anchor.y : viewport.height - size - anchor.y,
|
|
|
|
|
+ },
|
|
|
|
|
+ size,
|
|
|
|
|
+ viewport
|
|
|
|
|
+ )
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** Ties a place on screen to the two window edges it is nearest to. */
|
|
|
|
|
+function anchorFor(position: SupportPosition, size: number, viewport: Viewport): SupportAnchor {
|
|
|
|
|
+ const left = position.x + size / 2 < viewport.width / 2
|
|
|
|
|
+ const top = position.y + size / 2 < viewport.height / 2
|
|
|
|
|
+ return {
|
|
|
|
|
+ horizontal: left ? 'left' : 'right',
|
|
|
|
|
+ x: left ? position.x : viewport.width - size - position.x,
|
|
|
|
|
+ vertical: top ? 'top' : 'bottom',
|
|
|
|
|
+ y: top ? position.y : viewport.height - size - position.y,
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -45,11 +87,24 @@ function clampToViewport(position: SupportPosition, size: number): SupportPositi
|
|
|
* every page, this lets the user move it and keeps that choice per device,
|
|
* every page, this lets the user move it and keeps that choice per device,
|
|
|
* alongside the dismissal preference.
|
|
* alongside the dismissal preference.
|
|
|
*
|
|
*
|
|
|
|
|
+ * The place is kept as a distance to the nearest window edges and only turned
|
|
|
|
|
+ * into pixels for the window as it is now. A small window can push the button
|
|
|
|
|
+ * inward to keep it on screen, but that never replaces the place it belongs:
|
|
|
|
|
+ * making the window large again puts it back.
|
|
|
|
|
+ *
|
|
|
* Pointer events rather than mouse events, so a touch drag works without a
|
|
* Pointer events rather than mouse events, so a touch drag works without a
|
|
|
* second code path.
|
|
* second code path.
|
|
|
*/
|
|
*/
|
|
|
export function useDraggablePosition(size: number) {
|
|
export function useDraggablePosition(size: number) {
|
|
|
- const [position, setPosition] = useState<SupportPosition | null>(null)
|
|
|
|
|
|
|
+ // Null until dragged: the default corner, which depends on the window width.
|
|
|
|
|
+ const [anchor, setAnchor] = useState<SupportAnchor | null>(null)
|
|
|
|
|
+ // A place saved as pixels from the top-left by an earlier build, kept as it
|
|
|
|
|
+ // is until a window comes along that it fits in. The window it was measured
|
|
|
|
|
+ // in is not known, only that it was at least that large, so anchoring it to
|
|
|
|
|
+ // a smaller one would tie it to the wrong distance from the edges.
|
|
|
|
|
+ const [legacy, setLegacy] = useState<SupportPosition | null>(null)
|
|
|
|
|
+ // Null until the window has been measured, which only the browser can do.
|
|
|
|
|
+ const [viewport, setViewport] = useState<Viewport | null>(null)
|
|
|
const [dragging, setDragging] = useState(false)
|
|
const [dragging, setDragging] = useState(false)
|
|
|
// Set while a drag is in progress and cleared on the next click, so releasing
|
|
// Set while a drag is in progress and cleared on the next click, so releasing
|
|
|
// after a drag does not also open the panel.
|
|
// after a drag does not also open the panel.
|
|
@@ -57,25 +112,46 @@ export function useDraggablePosition(size: number) {
|
|
|
const originRef = useRef<{ pointerX: number; pointerY: number; x: number; y: number } | null>(
|
|
const originRef = useRef<{ pointerX: number; pointerY: number; x: number; y: number } | null>(
|
|
|
null
|
|
null
|
|
|
)
|
|
)
|
|
|
|
|
+ // What the drag in progress will save when the pointer is released.
|
|
|
|
|
+ const draggedAnchorRef = useRef<SupportAnchor | null>(null)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
- const stored = readSupportPosition()
|
|
|
|
|
- setPosition(clampToViewport(stored ?? defaultPosition(size), size))
|
|
|
|
|
- }, [size])
|
|
|
|
|
|
|
+ const stored = readSupportAnchor()
|
|
|
|
|
+ setAnchor(stored)
|
|
|
|
|
+ setLegacy(stored ? null : readLegacySupportPosition())
|
|
|
|
|
+ setViewport(readViewport())
|
|
|
|
|
+ }, [])
|
|
|
|
|
|
|
|
- // A window that shrinks, or a phone that rotates, can leave a stored position
|
|
|
|
|
- // outside the viewport with no way to reach the button.
|
|
|
|
|
|
|
+ // The first window the old place fits in stands in for the one it was
|
|
|
|
|
+ // measured in: the button is drawn exactly where the earlier build drew it,
|
|
|
|
|
+ // and from then on it is saved relative to that window's edges, so a reload
|
|
|
|
|
+ // at another size no longer reads the old pixels again.
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
- const onResize = () => {
|
|
|
|
|
- setPosition((current) => (current ? clampToViewport(current, size) : current))
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if (!legacy || !viewport) return
|
|
|
|
|
+ const shown = clampToViewport(legacy, size, viewport)
|
|
|
|
|
+ if (shown.x !== legacy.x || shown.y !== legacy.y) return
|
|
|
|
|
+ const converted = anchorFor(legacy, size, viewport)
|
|
|
|
|
+ writeSupportAnchor(converted)
|
|
|
|
|
+ setAnchor(converted)
|
|
|
|
|
+ setLegacy(null)
|
|
|
|
|
+ }, [legacy, viewport, size])
|
|
|
|
|
+
|
|
|
|
|
+ useEffect(() => {
|
|
|
|
|
+ const onResize = () => setViewport(readViewport())
|
|
|
window.addEventListener('resize', onResize)
|
|
window.addEventListener('resize', onResize)
|
|
|
window.addEventListener('orientationchange', onResize)
|
|
window.addEventListener('orientationchange', onResize)
|
|
|
return () => {
|
|
return () => {
|
|
|
window.removeEventListener('resize', onResize)
|
|
window.removeEventListener('resize', onResize)
|
|
|
window.removeEventListener('orientationchange', onResize)
|
|
window.removeEventListener('orientationchange', onResize)
|
|
|
}
|
|
}
|
|
|
- }, [size])
|
|
|
|
|
|
|
+ }, [])
|
|
|
|
|
+
|
|
|
|
|
+ const position = useMemo(() => {
|
|
|
|
|
+ if (!viewport) return null
|
|
|
|
|
+ if (anchor) return resolveAnchor(anchor, size, viewport)
|
|
|
|
|
+ if (legacy) return clampToViewport(legacy, size, viewport)
|
|
|
|
|
+ return resolveAnchor(defaultAnchor(viewport), size, viewport)
|
|
|
|
|
+ }, [anchor, legacy, viewport, size])
|
|
|
|
|
|
|
|
const onPointerDown = useCallback(
|
|
const onPointerDown = useCallback(
|
|
|
(event: React.PointerEvent) => {
|
|
(event: React.PointerEvent) => {
|
|
@@ -112,7 +188,16 @@ export function useDraggablePosition(size: number) {
|
|
|
movedRef.current = true
|
|
movedRef.current = true
|
|
|
setDragging(true)
|
|
setDragging(true)
|
|
|
}
|
|
}
|
|
|
- setPosition(clampToViewport({ x: origin.x + dx, y: origin.y + dy }, size))
|
|
|
|
|
|
|
+ const current = readViewport()
|
|
|
|
|
+ const next = anchorFor(
|
|
|
|
|
+ clampToViewport({ x: origin.x + dx, y: origin.y + dy }, size, current),
|
|
|
|
|
+ size,
|
|
|
|
|
+ current
|
|
|
|
|
+ )
|
|
|
|
|
+ draggedAnchorRef.current = next
|
|
|
|
|
+ setAnchor(next)
|
|
|
|
|
+ // Moved by hand: wherever an earlier build had it no longer matters.
|
|
|
|
|
+ setLegacy(null)
|
|
|
},
|
|
},
|
|
|
[size]
|
|
[size]
|
|
|
)
|
|
)
|
|
@@ -122,10 +207,8 @@ export function useDraggablePosition(size: number) {
|
|
|
originRef.current = null
|
|
originRef.current = null
|
|
|
if (movedRef.current) {
|
|
if (movedRef.current) {
|
|
|
setDragging(false)
|
|
setDragging(false)
|
|
|
- setPosition((current) => {
|
|
|
|
|
- if (current) writeSupportPosition(current)
|
|
|
|
|
- return current
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ if (draggedAnchorRef.current) writeSupportAnchor(draggedAnchorRef.current)
|
|
|
|
|
+ draggedAnchorRef.current = null
|
|
|
}
|
|
}
|
|
|
}, [])
|
|
}, [])
|
|
|
|
|
|