File size: 2,113 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { createContext, memo, useContext, useSyncExternalStore, type ReactNode } from 'react'

/**
 * The screen the reader is on (#1411). Moving to the next screen used to re-render every panel of the story (all of them are
 * mounted at once), because `active` was a prop of each. The index now lives in a small store: a panel subscribes to the one
 * question it asks ("am I the active screen?"), so a Next re-renders the two frames whose answer changed, and the rest of each
 * panel, held by `MemoPanel`, is left alone.
 */
export interface ActiveScreenStore {
  get: () => number | null
  set: (i: number) => void
  subscribe: (fn: () => void) => () => void
}

export function createActiveScreenStore(): ActiveScreenStore {
  let at: number | null = null
  const subs = new Set<() => void>()
  return {
    get: () => at,
    set: (i) => {
      if (i === at) return
      at = i
      subs.forEach((fn) => fn())
    },
    subscribe: (fn) => {
      subs.add(fn)
      return () => subs.delete(fn)
    },
  }
}

export const ActiveScreenContext = createContext<ActiveScreenStore | null>(null)

/** Whether screen `index` is the one in view. Without a provider (a story that still passes `active`), `fallback` decides. */
export function useScreenActive(index: number, fallback = false): boolean {
  const store = useContext(ActiveScreenContext)
  return useSyncExternalStore(
    store ? store.subscribe : noSubscribe,
    () => (store ? store.get() === index : fallback),
    () => (store ? store.get() === index : fallback),
  )
}
const noSubscribe = () => () => {}

interface MemoPanelProps {
  /** Everything `render` reads other than the active screen; the panel is drawn again only when one of them changes. */
  deps: readonly unknown[]
  render: () => ReactNode
}

/** One screen's content, drawn again only when its `deps` change (never because the reader moved to another screen). */
export const MemoPanel = memo(
  function MemoPanel({ render }: MemoPanelProps) {
    return <>{render()}</>
  },
  (a, b) => a.deps.length === b.deps.length && a.deps.every((d, i) => Object.is(d, b.deps[i])),
)