@kolkrabbi/kol-component 0.258.0 → 0.259.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kolkrabbi/kol-component",
3
- "version": "0.258.0",
3
+ "version": "0.259.0",
4
4
  "description": "The core component library",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -442,14 +442,15 @@ function isTypingTarget(el) {
442
442
  * transform layer disable while Space is held so layer mousedowns don't fire
443
443
  * mid-pan. `f` toggles an fps chip beside the zoom readout. From outside: `kol:zoom-at`
444
444
  * { clientX, clientY, factor } · `kol:zoom-to` { scale } (real, 1 = 100%) · `kol:zoom-fit` { to:
445
- * 'frame' | 'width' | { x, y, w, h } } — window events. Zoom is clamped at 2%–3200% real.
445
+ * 'frame' | 'width' | { x, y, w, h } } — window events. Zoom is clamped at 2%–3200% real. Touch:
446
+ * two fingers pinch-zoom + pan; a one-finger tap fires `onSpaceTap`.
446
447
  *
447
448
  * @param {ReactNode} backdrop placed behind the frame (grid / dark bg); the consumer owns its sizing — oversize it so panning never reveals an edge
448
449
  * @param {boolean} showRulers draw the virtual-px rulers (default true)
449
450
  * @param {{h: number[], v: number[]}} guides guide positions in VIRTUAL px
450
451
  * @param {Function} setGuides updater for `guides`; without both, no guides layer renders
451
452
  * @param {boolean} guidesInteractive allow grab + create (default true)
452
- * @param {Function} onSpaceTap fires on a Space press released without panning
453
+ * @param {Function} onSpaceTap fires on a Space press released without panning, or a one-finger touch tap
453
454
  * @param {ReactNode} children the letterboxed frame
454
455
  */
455
456
  export function PanZoomViewport({
@@ -469,6 +470,7 @@ export function PanZoomViewport({
469
470
  const [dragging, setDragging] = useState(false)
470
471
  const [view, setView] = useState({ zoom: 1, x: 0, y: 0 })
471
472
  const dragStart = useRef(null)
473
+ const [pinching, setPinching] = useState(false)
472
474
  const [showFps, setShowFps] = useState(false)
473
475
  const fps = useFps(showFps)
474
476
  /* Ref mirror so the keyup listener calls the latest callback without
@@ -563,6 +565,59 @@ export function PanZoomViewport({
563
565
  return () => node.removeEventListener('wheel', onWheel)
564
566
  }, [])
565
567
 
568
+ /* Touch (ViewportTouchGestures, kol-fxr 2026-10-14): two fingers pinch-zoom at their midpoint and
569
+ * the midpoint's travel pans, in one gesture; the transform layer goes deaf while they are down so
570
+ * a consumer's tool never sees the second finger. One finger stays the consumer's, except a TAP
571
+ * (up within TAP_MS and TAP_PX, no second finger joined) fires `onSpaceTap`. Down is caught in the
572
+ * capture phase on the container; move/up on window, so a finger leaving the frame still ends. */
573
+ useEffect(() => {
574
+ const node = containerRef.current
575
+ if (!node) return
576
+ const TAP_MS = 350, TAP_PX = 12
577
+ const pts = new Map()
578
+ let pinch = null, tap = null
579
+ const pair = () => {
580
+ const [a, b] = [...pts.values()]
581
+ return { mx: (a.x + b.x) / 2, my: (a.y + b.y) / 2, d: Math.hypot(a.x - b.x, a.y - b.y) || 1 }
582
+ }
583
+ const onDown = (e) => {
584
+ if (e.pointerType !== 'touch') return
585
+ pts.set(e.pointerId, { x: e.clientX, y: e.clientY })
586
+ if (pts.size === 1) tap = { id: e.pointerId, t: e.timeStamp, x: e.clientX, y: e.clientY }
587
+ else if (pts.size === 2) { tap = null; pinch = pair(); setPinching(true) }
588
+ }
589
+ const onMove = (e) => {
590
+ if (!pts.has(e.pointerId)) return
591
+ pts.set(e.pointerId, { x: e.clientX, y: e.clientY })
592
+ if (tap && Math.hypot(e.clientX - tap.x, e.clientY - tap.y) > TAP_PX) tap = null
593
+ if (!pinch || pts.size < 2) return
594
+ const prev = pinch, next = pair()
595
+ pinch = next
596
+ const r = node.getBoundingClientRect()
597
+ const fit = fitOf(node)
598
+ setView((v) => {
599
+ const z = zoomAt(v, next.d / prev.d, prev.mx - r.left, prev.my - r.top, fit)
600
+ return { ...z, x: z.x + next.mx - prev.mx, y: z.y + next.my - prev.my }
601
+ })
602
+ }
603
+ const onEnd = (e) => {
604
+ if (!pts.delete(e.pointerId)) return
605
+ if (tap?.id === e.pointerId && e.type === 'pointerup' && e.timeStamp - tap.t < TAP_MS) spaceTapRef.current?.()
606
+ if (tap?.id === e.pointerId) tap = null
607
+ if (pinch && pts.size < 2) { pinch = null; setPinching(false) }
608
+ }
609
+ node.addEventListener('pointerdown', onDown, true)
610
+ window.addEventListener('pointermove', onMove)
611
+ window.addEventListener('pointerup', onEnd)
612
+ window.addEventListener('pointercancel', onEnd)
613
+ return () => {
614
+ node.removeEventListener('pointerdown', onDown, true)
615
+ window.removeEventListener('pointermove', onMove)
616
+ window.removeEventListener('pointerup', onEnd)
617
+ window.removeEventListener('pointercancel', onEnd)
618
+ }
619
+ }, [])
620
+
566
621
  /* A consumer's zoom TOOL announces clicks as `kol:zoom-at` (client coords +
567
622
  * factor) rather than reaching into this state — anchored zoom at the
568
623
  * pointer. Same idiom as `kol:guide-drag-start` below. */
@@ -654,7 +709,7 @@ export function PanZoomViewport({
654
709
  <div
655
710
  ref={containerRef}
656
711
  className="relative w-full h-full overflow-hidden select-none"
657
- style={cursor ? { cursor } : undefined}
712
+ style={{ cursor, touchAction: 'none' }}
658
713
  onMouseDown={onMouseDown}
659
714
  >
660
715
  {/* No transition on the transform: editing chrome (selection
@@ -667,7 +722,7 @@ export function PanZoomViewport({
667
722
  style={{
668
723
  transform: `translate(${view.x}px, ${view.y}px) scale(${view.zoom})`,
669
724
  transformOrigin: '0 0',
670
- pointerEvents: spaceHeld ? 'none' : 'auto',
725
+ pointerEvents: spaceHeld || pinching ? 'none' : 'auto',
671
726
  }}
672
727
  >
673
728
  {backdrop}