hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { Index, Show, createEffect, createSignal, onCleanup, onMount, type Component } from "solid-js"; |
| 2 | import { IconChevronLeft, IconChevronRight, IconPlus, IconX } from "@/components/ui/icons"; |
| 3 | import { cn } from "@/lib/cn"; |
| 4 | import { formatUnreadCount } from "@/lib/notifications"; |
| 5 | import { clamp01, radialOffsets, readFloatingButton, writeFloatingButton } from "@/lib/floating-button"; |
| 6 | import { useT } from "@/stores/preferences-context"; |
| 7 | |
| 8 | export interface FloatingAction { |
| 9 | id: string; |
| 10 | label: string; |
| 11 | Icon: Component<{ class?: string }>; |
| 12 | badge?: number; |
| 13 | onSelect: () => void; |
| 14 | } |
| 15 | |
| 16 | /** Button diameter. */ |
| 17 | const SIZE = 56; |
| 18 | /** Gap the resting button keeps from the layer's edges. */ |
| 19 | const MARGIN = 12; |
| 20 | /** Radial item diameter. */ |
| 21 | const ITEM = 44; |
| 22 | /** Distance from the button centre to each radial item centre. */ |
| 23 | const RADIUS = 84; |
| 24 | /** Movement under this is a tap, not a drag — a thumb is never perfectly still. */ |
| 25 | const TAP_SLOP = 8; |
| 26 | /** How close the button centre has to come to the hide target to snap into it. */ |
| 27 | const HIDE_REACH = 64; |
| 28 | |
| 29 | const badgeText = formatUnreadCount; |
| 30 | const clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value)); |
| 31 | |
| 32 | /** |
| 33 | * The phone shell's floating action button, standing in for the desktop |
| 34 | * header. Tapping it fans its actions out around it; dragging moves it |
| 35 | * anywhere between the status bar and the tab bar; dropping it on the hide |
| 36 | * target that rises while dragging parks it as a small tab on the nearest |
| 37 | * side edge, and one tap on that tab brings it back where it was. |
| 38 | * |
| 39 | * Position and parked state are remembered per device (lib/floating-button). |
| 40 | * Drag follows the pointer-event pattern of MobileNavSheet: pointer capture on |
| 41 | * the button itself, touch-none so the browser does not claim the gesture for |
| 42 | * scrolling. |
| 43 | */ |
| 44 | export function FloatingActionButton(props: { |
| 45 | actions: FloatingAction[]; |
| 46 | /** Unread total shown on the button and, as a dot, on the parked tab. */ |
| 47 | badge: number; |
| 48 | /** An overlay owns the screen; step aside without losing state. */ |
| 49 | hidden: boolean; |
| 50 | }) { |
| 51 | const t = useT(); |
| 52 | const saved = readFloatingButton(); |
| 53 | const [pos, setPos] = createSignal({ x: saved.x, y: saved.y }); |
| 54 | const [parked, setParked] = createSignal(saved.parked); |
| 55 | const [open, setOpen] = createSignal(false); |
| 56 | const [bounds, setBounds] = createSignal({ w: 0, h: 0 }); |
| 57 | // Live top-left while a drag is under way; null at rest. |
| 58 | const [drag, setDrag] = createSignal<{ left: number; top: number } | null>(null); |
| 59 | const [overHide, setOverHide] = createSignal(false); |
| 60 | |
| 61 | let layer: HTMLDivElement | undefined; |
| 62 | let fab: HTMLButtonElement | undefined; |
| 63 | const measure = () => { |
| 64 | if (!layer) return; |
| 65 | const rect = layer.getBoundingClientRect(); |
| 66 | setBounds({ w: rect.width, h: rect.height }); |
| 67 | }; |
| 68 | onMount(() => { |
| 69 | measure(); |
| 70 | window.addEventListener("resize", measure); |
| 71 | onCleanup(() => window.removeEventListener("resize", measure)); |
| 72 | }); |
| 73 | |
| 74 | const persist = () => writeFloatingButton({ ...pos(), parked: parked() }); |
| 75 | |
| 76 | const travelX = () => Math.max(0, bounds().w - SIZE - 2 * MARGIN); |
| 77 | const travelY = () => Math.max(0, bounds().h - SIZE - 2 * MARGIN); |
| 78 | const restLeft = () => MARGIN + pos().x * travelX(); |
| 79 | const restTop = () => MARGIN + pos().y * travelY(); |
| 80 | const hideLeft = () => bounds().w / 2 - SIZE / 2; |
| 81 | const hideTop = () => bounds().h - SIZE - MARGIN; |
| 82 | const left = () => (overHide() ? hideLeft() : (drag()?.left ?? restLeft())); |
| 83 | const top = () => (overHide() ? hideTop() : (drag()?.top ?? restTop())); |
| 84 | |
| 85 | createEffect(() => { |
| 86 | if (props.hidden) setOpen(false); |
| 87 | }); |
| 88 | createEffect(() => { |
| 89 | if (!open()) return; |
| 90 | const onKey = (event: KeyboardEvent) => { |
| 91 | if (event.key !== "Escape") return; |
| 92 | setOpen(false); |
| 93 | fab?.focus(); |
| 94 | }; |
| 95 | window.addEventListener("keydown", onKey); |
| 96 | onCleanup(() => window.removeEventListener("keydown", onKey)); |
| 97 | }); |
| 98 | |
| 99 | let start: { x: number; y: number; left: number; top: number } | null = null; |
| 100 | let moved = false; |
| 101 | // A drag that ends over the button is followed by a click on mouse (not |
| 102 | // always on touch), which must not toggle the menu. Reset on every press so |
| 103 | // a swallowed click cannot leave the next real tap ignored. |
| 104 | let suppressClick = false; |
| 105 | |
| 106 | const startDrag = (event: PointerEvent) => { |
| 107 | if (event.pointerType === "mouse" && event.button !== 0) return; |
| 108 | suppressClick = false; |
| 109 | moved = false; |
| 110 | start = { x: event.clientX, y: event.clientY, left: left(), top: top() }; |
| 111 | (event.currentTarget as HTMLElement).setPointerCapture(event.pointerId); |
| 112 | }; |
| 113 | |
| 114 | const moveDrag = (event: PointerEvent) => { |
| 115 | if (!start) return; |
| 116 | const dx = event.clientX - start.x; |
| 117 | const dy = event.clientY - start.y; |
| 118 | if (!moved && Math.hypot(dx, dy) < TAP_SLOP) return; |
| 119 | if (!moved) { |
| 120 | moved = true; |
| 121 | setOpen(false); |
| 122 | } |
| 123 | const next = { |
| 124 | left: clamp(start.left + dx, 0, bounds().w - SIZE), |
| 125 | top: clamp(start.top + dy, 0, bounds().h - SIZE), |
| 126 | }; |
| 127 | setDrag(next); |
| 128 | const hit = Math.hypot(next.left - hideLeft(), next.top - hideTop()) < HIDE_REACH; |
| 129 | if (hit && !overHide()) navigator.vibrate?.(10); |
| 130 | setOverHide(hit); |
| 131 | }; |
| 132 | |
| 133 | const endDrag = (commit: boolean) => { |
| 134 | if (!start) return; |
| 135 | start = null; |
| 136 | if (!moved) return; |
| 137 | suppressClick = true; |
| 138 | const last = drag(); |
| 139 | if (commit && overHide()) { |
| 140 | // Parked at the spot it was dragged from, so a tap on the tab puts it |
| 141 | // back there rather than on the hide target. |
| 142 | setParked(true); |
| 143 | persist(); |
| 144 | } else if (commit && last) { |
| 145 | setPos({ |
| 146 | x: travelX() ? clamp01((last.left - MARGIN) / travelX()) : pos().x, |
| 147 | y: travelY() ? clamp01((last.top - MARGIN) / travelY()) : pos().y, |
| 148 | }); |
| 149 | persist(); |
| 150 | } |
| 151 | setDrag(null); |
| 152 | setOverHide(false); |
| 153 | }; |
| 154 | |
| 155 | const toggle = () => { |
| 156 | if (suppressClick) { |
| 157 | suppressClick = false; |
| 158 | return; |
| 159 | } |
| 160 | setOpen(!open()); |
| 161 | }; |
| 162 | |
| 163 | const restore = () => { |
| 164 | setParked(false); |
| 165 | persist(); |
| 166 | }; |
| 167 | |
| 168 | const select = (action: FloatingAction) => { |
| 169 | setOpen(false); |
| 170 | action.onSelect(); |
| 171 | }; |
| 172 | |
| 173 | const centreX = () => left() + SIZE / 2; |
| 174 | const centreY = () => top() + SIZE / 2; |
| 175 | const offsets = () => |
| 176 | radialOffsets( |
| 177 | props.actions.length, |
| 178 | bounds().w ? centreX() / bounds().w : 1, |
| 179 | bounds().h ? centreY() / bounds().h : 1, |
| 180 | RADIUS, |
| 181 | ); |
| 182 | // Top-left of item i, kept inside the layer as a last resort for fans the |
| 183 | // heading alone does not keep on screen. |
| 184 | const itemPos = (i: number) => { |
| 185 | const offset = offsets()[i] ?? { dx: 0, dy: 0 }; |
| 186 | return { |
| 187 | left: clamp(centreX() + offset.dx - ITEM / 2, 4, bounds().w - ITEM - 4), |
| 188 | top: clamp(centreY() + offset.dy - ITEM / 2, 4, bounds().h - ITEM - 4), |
| 189 | }; |
| 190 | }; |
| 191 | const parkedRight = () => pos().x >= 0.5; |
| 192 | const dragging = () => drag() !== null; |
| 193 | |
| 194 | return ( |
| 195 | <> |
| 196 | <Show when={open()}> |
| 197 | <button |
| 198 | type="button" |
| 199 | tabIndex={-1} |
| 200 | aria-label={t("nav.close")} |
| 201 | class="fixed inset-0 z-50 cursor-default bg-black/25 lg:hidden" |
| 202 | onClick={() => setOpen(false)} |
| 203 | /> |
| 204 | </Show> |
| 205 | |
| 206 | {/* The layer spans the space between the status bar and the tab bar, so |
| 207 | every position inside it is reachable and none hides under either. |
| 208 | It stays mounted while hidden or parked to keep its measurements. */} |
| 209 | <div |
| 210 | ref={(el) => { |
| 211 | layer = el; |
| 212 | }} |
| 213 | class={cn( |
| 214 | "pointer-events-none fixed inset-x-0 top-[env(safe-area-inset-top)] z-50 transition-opacity duration-200 lg:hidden", |
| 215 | "bottom-[calc(3.5rem+max(env(safe-area-inset-bottom),var(--android-nav-inset,0px)))]", |
| 216 | props.hidden || !bounds().w ? "invisible opacity-0" : "visible opacity-100", |
| 217 | )} |
| 218 | > |
| 219 | <Show when={dragging()}> |
| 220 | <div class="absolute inset-x-0 bottom-0 h-44 bg-gradient-to-t from-black/30 to-transparent" /> |
| 221 | <div |
| 222 | class="absolute flex flex-col items-center" |
| 223 | style={{ left: `${hideLeft()}px`, top: `${hideTop()}px`, width: `${SIZE}px` }} |
| 224 | > |
| 225 | <span class="absolute -top-7 whitespace-nowrap rounded-full bg-background/90 px-2 py-0.5 text-[11px] font-semibold text-foreground"> |
| 226 | {t("fab.hide")} |
| 227 | </span> |
| 228 | <span |
| 229 | class={cn( |
| 230 | "flex items-center justify-center rounded-full border-2 transition-all duration-150", |
| 231 | overHide() |
| 232 | ? "h-[68px] w-[68px] -m-1.5 border-destructive bg-destructive text-destructive-foreground" |
| 233 | : "h-14 w-14 border-white/80 bg-black/40 text-white", |
| 234 | )} |
| 235 | > |
| 236 | <IconX class="h-6 w-6" /> |
| 237 | </span> |
| 238 | </div> |
| 239 | </Show> |
| 240 | |
| 241 | <Show when={!parked()}> |
| 242 | <div id="fab-actions" role="group" aria-label={t("fab.label")}> |
| 243 | <Index each={props.actions}> |
| 244 | {(action, i) => { |
| 245 | const at = () => (open() ? itemPos(i) : { left: centreX() - ITEM / 2, top: centreY() - ITEM / 2 }); |
| 246 | return ( |
| 247 | <button |
| 248 | type="button" |
| 249 | tabIndex={open() ? 0 : -1} |
| 250 | aria-hidden={open() ? undefined : "true"} |
| 251 | aria-label={action().label} |
| 252 | title={action().label} |
| 253 | class={cn( |
| 254 | "absolute left-0 top-0 flex items-center justify-center rounded-full border border-border bg-surface-base text-foreground shadow-lg outline-hidden", |
| 255 | "transition-[transform,opacity] duration-200 ease-out focus-visible:ring-2 focus-visible:ring-ring", |
| 256 | open() ? "pointer-events-auto opacity-100" : "pointer-events-none opacity-0", |
| 257 | )} |
| 258 | style={{ |
| 259 | width: `${ITEM}px`, |
| 260 | height: `${ITEM}px`, |
| 261 | transform: `translate3d(${at().left}px, ${at().top}px, 0) scale(${open() ? 1 : 0.5})`, |
| 262 | "transition-delay": open() ? `${i * 30}ms` : "0ms", |
| 263 | }} |
| 264 | onClick={() => select(action())} |
| 265 | > |
| 266 | {(() => { |
| 267 | const Icon = action().Icon; |
| 268 | return <Icon class="h-5 w-5" />; |
| 269 | })()} |
| 270 | <Show when={(action().badge ?? 0) > 0}> |
| 271 | <span class="absolute -right-1 -top-1 flex h-5 min-w-5 items-center justify-center rounded-full bg-destructive px-1 text-[11px] font-bold text-destructive-foreground ring-2 ring-background"> |
| 272 | {badgeText(action().badge ?? 0)} |
| 273 | </span> |
| 274 | </Show> |
| 275 | </button> |
| 276 | ); |
| 277 | }} |
| 278 | </Index> |
| 279 | </div> |
| 280 | |
| 281 | <button |
| 282 | type="button" |
| 283 | ref={(el) => { |
| 284 | fab = el; |
| 285 | }} |
| 286 | aria-label={t("fab.label")} |
| 287 | aria-expanded={open()} |
| 288 | aria-controls="fab-actions" |
| 289 | class={cn( |
| 290 | "pointer-events-auto absolute left-0 top-0 flex touch-none select-none items-center justify-center rounded-full bg-primary text-primary-foreground shadow-lg shadow-black/20 outline-hidden", |
| 291 | "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background", |
| 292 | dragging() && "shadow-xl", |
| 293 | overHide() && "opacity-80", |
| 294 | )} |
| 295 | // Scale lives in the transform, after the translate: Tailwind's |
| 296 | // scale-* sets the separate `scale` property, which applies after |
| 297 | // `transform` and would scale the position along with the button. |
| 298 | style={{ |
| 299 | width: `${SIZE}px`, |
| 300 | height: `${SIZE}px`, |
| 301 | transform: `translate3d(${left()}px, ${top()}px, 0) scale(${overHide() ? 0.9 : dragging() ? 1.05 : 1})`, |
| 302 | transition: dragging() && !overHide() ? "none" : "transform 200ms ease-out, opacity 150ms", |
| 303 | }} |
| 304 | onPointerDown={startDrag} |
| 305 | onPointerMove={moveDrag} |
| 306 | onPointerUp={() => endDrag(true)} |
| 307 | onPointerCancel={() => endDrag(false)} |
| 308 | onClick={toggle} |
| 309 | > |
| 310 | <IconPlus class={cn("h-6 w-6 transition-transform duration-200", open() && "rotate-45")} /> |
| 311 | <Show when={props.badge > 0 && !open()}> |
| 312 | <span class="absolute -right-0.5 -top-0.5 flex h-5 min-w-5 items-center justify-center rounded-full bg-destructive px-1 text-[11px] font-bold text-destructive-foreground ring-2 ring-background"> |
| 313 | {badgeText(props.badge)} |
| 314 | </span> |
| 315 | </Show> |
| 316 | </button> |
| 317 | </Show> |
| 318 | |
| 319 | <Show when={parked()}> |
| 320 | {/* Parked: a slim tab on the side the button rested nearest. The |
| 321 | visible pill is narrow; the button around it is a full thumb |
| 322 | target. */} |
| 323 | <button |
| 324 | type="button" |
| 325 | aria-label={t("fab.restore")} |
| 326 | title={t("fab.restore")} |
| 327 | class={cn( |
| 328 | "pointer-events-auto absolute flex h-14 w-11 items-center outline-hidden focus-visible:ring-2 focus-visible:ring-ring", |
| 329 | parkedRight() ? "right-0 justify-end" : "left-0 justify-start", |
| 330 | )} |
| 331 | style={{ top: `${restTop()}px` }} |
| 332 | onClick={restore} |
| 333 | > |
| 334 | <span |
| 335 | class={cn( |
| 336 | "relative flex h-12 w-4 items-center justify-center bg-foreground/35 text-background backdrop-blur-sm", |
| 337 | parkedRight() ? "rounded-l-full" : "rounded-r-full", |
| 338 | )} |
| 339 | > |
| 340 | {parkedRight() ? <IconChevronLeft class="h-3.5 w-3.5" /> : <IconChevronRight class="h-3.5 w-3.5" />} |
| 341 | <Show when={props.badge > 0}> |
| 342 | <span |
| 343 | class={cn( |
| 344 | "absolute -top-1 h-2.5 w-2.5 rounded-full bg-destructive ring-2 ring-background", |
| 345 | parkedRight() ? "left-0" : "right-0", |
| 346 | )} |
| 347 | /> |
| 348 | </Show> |
| 349 | </span> |
| 350 | </button> |
| 351 | </Show> |
| 352 | </div> |
| 353 | </> |
| 354 | ); |
| 355 | } |