How to use the reakit-utils/tabbable.ensureFocus function in reakit-utils

To help you get started, we’ve selected a few reakit-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github reakit / reakit / packages / reakit / src / Dialog / __utils / useFocusOnShow.ts View on Github external
!shouldFocus ||
      !dialog ||
      nestedDialogs.find(child =>
        Boolean(child.current && !child.current.hidden)
      )
    ) {
      return;
    }

    if (initialFocusRef && initialFocusRef.current) {
      initialFocusRef.current.focus({ preventScroll: true });
    } else {
      const tabbable = getFirstTabbableIn(dialog, true);
      const isActive = () => dialog.contains(document.activeElement);
      if (tabbable) {
        ensureFocus(tabbable, { preventScroll: true, isActive });
      } else {
        ensureFocus(dialog, { preventScroll: true, isActive });
        warning(
          dialog.tabIndex === undefined || dialog.tabIndex < 0,
          "[reakit/Dialog]",
          "It's recommended to have at least one tabbable element inside dialog. The dialog element has been automatically focused.",
          "If this is the intended behavior, pass `tabIndex={0}` to the dialog element to disable this warning.",
          "See https://reakit.io/docs/dialog/#initial-focus"
        );
      }
    }
  }, [dialogRef, nestedDialogs, initialFocusRef, shouldFocus]);
}
github reakit / reakit / packages / reakit / src / Dialog / __utils / useFocusOnShow.ts View on Github external
nestedDialogs.find(child =>
        Boolean(child.current && !child.current.hidden)
      )
    ) {
      return;
    }

    if (initialFocusRef && initialFocusRef.current) {
      initialFocusRef.current.focus({ preventScroll: true });
    } else {
      const tabbable = getFirstTabbableIn(dialog, true);
      const isActive = () => dialog.contains(document.activeElement);
      if (tabbable) {
        ensureFocus(tabbable, { preventScroll: true, isActive });
      } else {
        ensureFocus(dialog, { preventScroll: true, isActive });
        warning(
          dialog.tabIndex === undefined || dialog.tabIndex < 0,
          "[reakit/Dialog]",
          "It's recommended to have at least one tabbable element inside dialog. The dialog element has been automatically focused.",
          "If this is the intended behavior, pass `tabIndex={0}` to the dialog element to disable this warning.",
          "See https://reakit.io/docs/dialog/#initial-focus"
        );
      }
    }
  }, [dialogRef, nestedDialogs, initialFocusRef, shouldFocus]);
}
github reakit / reakit / packages / reakit / src / Dialog / __utils / useFocusOnHide.ts View on Github external
document.activeElement &&
      dialog &&
      !dialog.contains(document.activeElement) &&
      (isTabbable(document.activeElement) ||
        document.activeElement.getAttribute("data-dialog") === "true")
    ) {
      return;
    }

    const finalFocusEl =
      (options.unstable_finalFocusRef &&
        options.unstable_finalFocusRef.current) ||
      (disclosuresRef.current && disclosuresRef.current[0]);

    if (finalFocusEl) {
      ensureFocus(finalFocusEl);
    } else {
      warning(
        true,
        "[reakit/Dialog]",
        "Can't return focus after closing dialog. Either render a disclosure component or provide a `unstable_finalFocusRef` prop.",
        "See https://reakit.io/docs/dialog"
      );
    }
  }, [dialogRef, disclosuresRef, shouldFocus]);
}