How to use focus-lock - 10 common examples

To help you get started, we’ve selected a few focus-lock 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 theKashey / react-focus-lock / src / Lock.js View on Github external
const setObserveNode = useCallback((newObserved) => {
    if (observed.current !== newObserved) {
      observed.current = newObserved;
      setObserved(newObserved);
    }
  }, []);

  if (process.env.NODE_ENV !== 'production') {
    if (typeof allowTextSelection !== 'undefined') {
      // eslint-disable-next-line no-console
      console.warn('React-Focus-Lock: allowTextSelection is deprecated and enabled by default');
    }
  }

  const lockProps = {
    [constants.FOCUS_DISABLED]: disabled && 'disabled',
    [constants.FOCUS_GROUP]: group,
    ...containerProps,
  };

  const hasLeadingGuards = noFocusGuards !== true;
  const hasTailingGuards = hasLeadingGuards && (noFocusGuards !== 'tail');

  return (
    
      {hasLeadingGuards && [
        <div style="{hiddenGuard}" tabindex="{disabled" data-focus-guard="">, // nearest focus guard
        <div style="{hiddenGuard}" tabindex="{disabled" data-focus-guard="">, // first tabbed element guard
      ]}
      </div></div>
github theKashey / react-focus-lock / src / Lock.js View on Github external
if (observed.current !== newObserved) {
      observed.current = newObserved;
      setObserved(newObserved);
    }
  }, []);

  if (process.env.NODE_ENV !== 'production') {
    if (typeof allowTextSelection !== 'undefined') {
      // eslint-disable-next-line no-console
      console.warn('React-Focus-Lock: allowTextSelection is deprecated and enabled by default');
    }
  }

  const lockProps = {
    [constants.FOCUS_DISABLED]: disabled &amp;&amp; 'disabled',
    [constants.FOCUS_GROUP]: group,
    ...containerProps,
  };

  const hasLeadingGuards = noFocusGuards !== true;
  const hasTailingGuards = hasLeadingGuards &amp;&amp; (noFocusGuards !== 'tail');

  return (
    
      {hasLeadingGuards &amp;&amp; [
        <div style="{hiddenGuard}" tabindex="{disabled" data-focus-guard="">, // nearest focus guard
        <div style="{hiddenGuard}" tabindex="{disabled" data-focus-guard="">, // first tabbed element guard
      ]}
      </div></div>
github theKashey / react-focus-lock / src / Trap.js View on Github external
(persistentFocus || focusWasOutsideWindow)
          || !isFreeFocus()
          || (!lastActiveFocus && autoFocus)
        ) {
          if (
            workingNode
            && !(
              focusInside(workingArea)
              || focusIsPortaledPair(activeElement, workingNode)
            )
          ) {
            if (document && !lastActiveFocus && activeElement && !autoFocus) {
              activeElement.blur();
              document.body.focus();
            } else {
              result = moveFocusInside(workingArea, lastActiveFocus);
              lastPortaledElement = {};
            }
          }
          focusWasOutsideWindow = false;
          lastActiveFocus = document && document.activeElement;
        }
      }

      if (document) {
        const newActiveElement = document && document.activeElement;
        const allNodes = getFocusabledIn(workingArea);
        const focusedItem = allNodes.find(({ node }) => node === newActiveElement);
        if (focusedItem) {
          // remove old focus
          allNodes
            .filter(({ guard, node }) => guard && node.dataset.focusAutoGuard)
github theKashey / react-focus-lock / src / Trap.js View on Github external
if (document && !lastActiveFocus && activeElement && !autoFocus) {
              activeElement.blur();
              document.body.focus();
            } else {
              result = moveFocusInside(workingArea, lastActiveFocus);
              lastPortaledElement = {};
            }
          }
          focusWasOutsideWindow = false;
          lastActiveFocus = document && document.activeElement;
        }
      }

      if (document) {
        const newActiveElement = document && document.activeElement;
        const allNodes = getFocusabledIn(workingArea);
        const focusedItem = allNodes.find(({ node }) => node === newActiveElement);
        if (focusedItem) {
          // remove old focus
          allNodes
            .filter(({ guard, node }) => guard && node.dataset.focusAutoGuard)
            .forEach(({ node }) => node.removeAttribute('tabIndex'));

          const focusedIndex = allNodes.indexOf(focusedItem);
          autoGuard(focusedIndex, allNodes.length, +1, allNodes);
          autoGuard(focusedIndex, -1, -1, allNodes);
        }
      }
    }
  }
  return result;
};
github theKashey / dom-focus-lock / src / index.js View on Github external
const activateTrap = () => {
  let result = false;
  if (lastActiveTrap) {
    const observed = lastActiveTrap;
    if(!isFreeFocus()) {
      if (observed && !focusInside(observed)) {
        result = moveFocusInside(observed, lastActiveFocus);
      }
      lastActiveFocus = document.activeElement;
    }
  }
  return result;
};
github theKashey / react-focus-lock / src / Trap.js View on Github external
if (workingNode) {
      const workingArea = [
        workingNode,
        ...shards.map(extractRef).filter(Boolean),
      ];

      if (!activeElement || focusWhitelisted(activeElement)) {
        if (
          (persistentFocus || focusWasOutsideWindow)
          || !isFreeFocus()
          || (!lastActiveFocus && autoFocus)
        ) {
          if (
            workingNode
            && !(
              focusInside(workingArea)
              || focusIsPortaledPair(activeElement, workingNode)
            )
          ) {
            if (document && !lastActiveFocus && activeElement && !autoFocus) {
              activeElement.blur();
              document.body.focus();
            } else {
              result = moveFocusInside(workingArea, lastActiveFocus);
              lastPortaledElement = {};
            }
          }
          focusWasOutsideWindow = false;
          lastActiveFocus = document && document.activeElement;
        }
      }
github theKashey / dom-focus-lock / src / index.js View on Github external
const activateTrap = () => {
  let result = false;
  if (lastActiveTrap) {
    const observed = lastActiveTrap;
    if(!isFreeFocus()) {
      if (observed && !focusInside(observed)) {
        result = moveFocusInside(observed, lastActiveFocus);
      }
      lastActiveFocus = document.activeElement;
    }
  }
  return result;
};
github theKashey / dom-focus-lock / src / index.js View on Github external
const isFreeFocus = () => focusOnBody() || focusIsHidden();
github theKashey / react-focus-lock / src / Trap.js View on Github external
const isFreeFocus = () => focusOnBody() || focusIsHidden();
github theKashey / react-focus-lock / src / FreeFocusInside.js View on Github external
const FreeFocusInside = ({ children, className }) =&gt; (
  <div>
    {children}
  </div>
);