function useDescendant(descendant, context, indexProp) {
  var forceUpdate = useForceUpdate();

  var _React$useContext = Object(react["useContext"])(context),
      registerDescendant = _React$useContext.registerDescendant,
      unregisterDescendant = _React$useContext.unregisterDescendant,
      descendants = _React$useContext.descendants; // This will initially return -1 because we haven't registered the descendant
  // on the first render. After we register, this will then return the correct
  // index on the following render and we will re-register descendants so that
  // everything is up-to-date before the user interacts with a collection.


  var index = indexProp != null ? indexProp : descendants.findIndex(function (item) {
    return item.element === descendant.element;
  }); // Prevent any flashing

  useIsomorphicLayoutEffect(function () {
    if (!descendant.element) forceUpdate();
    registerDescendant(reach_descendants_esm_extends({}, descendant, {
      index: index
    }));
    return function () {
      unregisterDescendant(descendant.element);
    };
  }, [descendant, forceUpdate, index, registerDescendant, unregisterDescendant].concat(Object.values(descendant)));
  return index;
}
This hook registers our descendant by passing it into an array. We can then
search that array by to find its index when registering it in the component.
We use this for focus management, keyboard navigation, and typeahead
functionality for some components.

The hook accepts the element node and (optionally) a key. The key is useful
if multiple descendants have identical text values and we need to
differentiate siblings for some reason.

Our main goals with this are:
  1) maximum composability,
  2) minimal API friction
  3) SSR compatibility*
  4) concurrent safe
  5) index always up-to-date with the tree despite changes
  6) works with memoization of any component in the tree (hopefully)

As for SSR, the good news is that we don't actually need the index on the
server for most use-cases, as we are only using it to determine the order of
composed descendants for keyboard navigation. However, in the few cases where
this is not the case, we can require an explicit index from the app.