File size: 670 Bytes
7e3630c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import type { DOMElement } from "src/ink";
import { type RefObject } from "react";

/**
 * Walks up the DOMElement tree from the given ref to find the nearest
 * ancestor Box that has a background color set.
 *
 * This provides the same information as Ink's internal `backgroundContext`.
 */
export default function useBackgroundColor(
  ref: RefObject<DOMElement | null>,
): string | undefined {
  let currentNode: DOMElement | undefined = ref.current ?? undefined;
  while (currentNode) {
    if (currentNode.style?.backgroundColor) {
      return currentNode.style.backgroundColor as string;
    }
    currentNode = currentNode.parentNode;
  }

  return undefined;
}