File size: 2,986 Bytes
a8fb296
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { useEffect, useRef } from 'react'

interface Options {
  /** Move to the next screen. */
  next: () => void
  /** Move to the previous screen. */
  prev: () => void
  first: () => void
  last: () => void
  /** False while a popover or dialog owns the keyboard. */
  enabled?: boolean
}

/** Where the keyboard belongs to the control itself, not to the page. */
const OWN_KEYS = 'input, select, textarea, [contenteditable="true"], table, [data-keys-own], [role="listbox"], [role="menu"]'

/**
 * Keys that move between screens, shared by the challenge and the story: PageDown / Enter / Space / arrow down / arrow right go on,
 * PageUp / Shift+Space / arrow up / arrow left go back, Home and End jump to the ends. Nothing happens while focus is in an
 * input, select, table or a figure that scrolls on its own, and Space and Enter leave a focused button or link to press it.
 * Wheel and touch are untouched: this never traps the reader.
 */
export function useScreenKeys(opts: Options) {
  const o = useRef(opts)
  o.current = opts
  useEffect(() => {
    const onKey = (e: KeyboardEvent) => {
      if (e.ctrlKey || e.metaKey || e.altKey || e.defaultPrevented) return
      const t = e.target as HTMLElement | null
      if (o.current.enabled === false) return
      if (t && t !== document.body && t.closest?.(OWN_KEYS)) return
      const scrollsInside = t?.closest?.('[data-lenis-prevent]') != null
      const onControl = t != null && (t.tagName === 'BUTTON' || t.tagName === 'A' || t.closest?.('summary') != null)
      let act: (() => void) | null = null
      switch (e.key) {
        case 'PageDown':
          act = o.current.next
          break
        case 'Enter':
          // A focused button, link or summary keeps Enter (it presses it); anywhere else Enter goes on, like Next.
          if (onControl || e.shiftKey) return
          act = o.current.next
          break
        case 'PageUp':
          act = o.current.prev
          break
        case ' ':
          // A focused button or link keeps plain Space (it presses it); Shift+Space still goes back.
          if (onControl && !e.shiftKey) return
          act = e.shiftKey ? o.current.prev : o.current.next
          break
        case 'ArrowDown':
          if (scrollsInside) return
          act = o.current.next
          break
        case 'ArrowUp':
          if (scrollsInside) return
          act = o.current.prev
          break
        case 'ArrowRight':
          act = o.current.next
          break
        case 'ArrowLeft':
          act = o.current.prev
          break
        case 'Home':
          if (scrollsInside) return
          act = o.current.first
          break
        case 'End':
          if (scrollsInside) return
          act = o.current.last
          break
        default:
          return
      }
      e.preventDefault()
      act()
    }
    window.addEventListener('keydown', onKey)
    return () => window.removeEventListener('keydown', onKey)
  }, [])
}