okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221apps/host: the owner's UI and the Node App Host0b3299a01

main
2.4 KiB34 linesraw
1import { tr } from '../lib/i18n';
2import { useEffect, useRef, type RefObject } from 'react';
3import { DEFAULT_PREVIEW_FRACTION, pointerPreviewFraction, previewWidth } from '../lib/preview-layout';
4export function PreviewResizeHandle({ container, total, fraction, onChange, controls }: {
5    container: RefObject<HTMLDivElement | null>;
6    total: number;
7    fraction: number;
8    onChange: (value: number) => void;
9    controls: string;
10}) {
11    const pointer = useRef<number | null>(null);
12    const width = previewWidth(total, fraction), min = previewWidth(total, 0), max = previewWidth(total, 1);
13    const cleanup = () => { pointer.current = null; document.documentElement.removeAttribute('data-preview-resizing'); };
14    useEffect(() => cleanup, []);
15    return <div className="preview-resize-handle" role="separator" tabIndex={0} aria-label={tr("Resize preview sidebar")} aria-orientation="vertical" aria-controls={controls} aria-valuemin={min} aria-valuemax={max} aria-valuenow={width} aria-valuetext={`Preview width ${width} pixels`} title={tr("Drag to resize · arrow keys adjust · double-click to reset")} style={{ right: width - 6 }} onPointerDown={e => { if (e.button !== 0)
16        return; e.preventDefault(); e.currentTarget.focus({ preventScroll: true }); pointer.current = e.pointerId; e.currentTarget.setPointerCapture(e.pointerId); document.documentElement.setAttribute('data-preview-resizing', 'true'); }} onPointerMove={e => { if (pointer.current !== e.pointerId || !container.current)
17        return; const box = container.current.getBoundingClientRect(); onChange(pointerPreviewFraction(box.width, box.right, e.clientX)); }} onPointerUp={e => { if (pointer.current === e.pointerId) {
18        if (e.currentTarget.hasPointerCapture(e.pointerId))
19            e.currentTarget.releasePointerCapture(e.pointerId);
20        cleanup();
21    } }} onPointerCancel={cleanup} onLostPointerCapture={cleanup} onDoubleClick={() => onChange(DEFAULT_PREVIEW_FRACTION)} onKeyDown={e => { let next: number | undefined; if (e.key === 'ArrowLeft')
22        next = (width + (e.shiftKey ? 64 : 24)) / total;
23    else if (e.key === 'ArrowRight')
24        next = (width - (e.shiftKey ? 64 : 24)) / total;
25    else if (e.key === 'Home')
26        next = 0;
27    else if (e.key === 'End')
28        next = 1;
29    else if (e.key === 'Enter')
30        next = DEFAULT_PREVIEW_FRACTION; if (next !== undefined) {
31        e.preventDefault();
32        onChange(previewWidth(total, next) / total);
33    } }}><span /></div>;
34}