How to use the @reach/utils.usePrevious function in @reach/utils

To help you get started, we’ve selected a few @reach/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 reach / reach-ui / packages / alert / src / index.js View on Github external
function useMirrorEffects(type, element, ref) {
  const prevType = usePrevious(type);
  const mirror = useRef(null);
  const mounted = useRef(false);
  useEffect(() => {
    const { ownerDocument } = ref.current || {};
    if (!mounted.current) {
      mounted.current = true;
      mirror.current = createMirror(type, ownerDocument);
      mirror.current.mount(element);
    } else if (!prevType !== type) {
      mirror.current.unmount();
      mirror.current = createMirror(type, ownerDocument);
      mirror.current.mount(element);
    } else {
      mirror.current.update(element, prevType, type);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
github reach / reach-ui / packages / menu-button / src / index.js View on Github external
// Respond to user char key input with typeahead
    const match = findItemFromSearch(menuItems, searchQuery);
    if (searchQuery && match != null) {
      dispatch({
        type: SELECT_ITEM_AT_INDEX,
        payload: { index: match }
      });
    }
    let timeout = window.setTimeout(
      () => searchQuery && dispatch({ type: SEARCH_FOR_ITEM, payload: "" }),
      1000
    );
    return () => window.clearTimeout(timeout);
  }, [dispatch, menuItems, searchQuery]);

  const prevMenuItemsLength = usePrevious(menuItems.length);
  const prevSelected = usePrevious(menuItems[selectionIndex]);
  const prevSelectionIndex = usePrevious(selectionIndex);

  useEffect(() => {
    if (selectionIndex > menuItems.length - 1) {
      /*
       * If for some reason our selection index is larger than our possible
       * index range (let's say the last item is selected and the list
       * dynamically updates), we need to select the last item in the list.
       */
      dispatch({
        type: SELECT_ITEM_AT_INDEX,
        payload: { index: menuItems.length - 1 }
      });
    } else if (
      /*
github reach / reach-ui / packages / menu-button / src / index.js View on Github external
if (searchQuery && match != null) {
      dispatch({
        type: SELECT_ITEM_AT_INDEX,
        payload: { index: match }
      });
    }
    let timeout = window.setTimeout(
      () => searchQuery && dispatch({ type: SEARCH_FOR_ITEM, payload: "" }),
      1000
    );
    return () => window.clearTimeout(timeout);
  }, [dispatch, menuItems, searchQuery]);

  const prevMenuItemsLength = usePrevious(menuItems.length);
  const prevSelected = usePrevious(menuItems[selectionIndex]);
  const prevSelectionIndex = usePrevious(selectionIndex);

  useEffect(() => {
    if (selectionIndex > menuItems.length - 1) {
      /*
       * If for some reason our selection index is larger than our possible
       * index range (let's say the last item is selected and the list
       * dynamically updates), we need to select the last item in the list.
       */
      dispatch({
        type: SELECT_ITEM_AT_INDEX,
        payload: { index: menuItems.length - 1 }
      });
    } else if (
      /*
       * Checks if
       *  - menu length has changed
github reach / reach-ui / packages / alert / examples / basic.example.js View on Github external
function useMessageTimeout(messages, callback, time = 5000) {
  const timeouts = React.useRef([]);
  const lastMessageCount = usePrevious(messages.length);
  React.useEffect(() => {
    if (messages.length && lastMessageCount < messages.length) {
      timeouts.current.push(setTimeout(callback, time));
    }
  }, [messages, lastMessageCount, callback, time]);

  React.useEffect(() => {
    const allTimeouts = timeouts.current;
    return () => {
      allTimeouts.forEach(clearTimeout);
    };
  }, []);
}
github reach / reach-ui / packages / menu-button / src / index.js View on Github external
const match = findItemFromSearch(menuItems, searchQuery);
    if (searchQuery && match != null) {
      dispatch({
        type: SELECT_ITEM_AT_INDEX,
        payload: { index: match }
      });
    }
    let timeout = window.setTimeout(
      () => searchQuery && dispatch({ type: SEARCH_FOR_ITEM, payload: "" }),
      1000
    );
    return () => window.clearTimeout(timeout);
  }, [dispatch, menuItems, searchQuery]);

  const prevMenuItemsLength = usePrevious(menuItems.length);
  const prevSelected = usePrevious(menuItems[selectionIndex]);
  const prevSelectionIndex = usePrevious(selectionIndex);

  useEffect(() => {
    if (selectionIndex > menuItems.length - 1) {
      /*
       * If for some reason our selection index is larger than our possible
       * index range (let's say the last item is selected and the list
       * dynamically updates), we need to select the last item in the list.
       */
      dispatch({
        type: SELECT_ITEM_AT_INDEX,
        payload: { index: menuItems.length - 1 }
      });
    } else if (
      /*
       * Checks if

@reach/utils

Internal, shared utilities for Reach UI.

MIT
Latest version published 2 years ago

Package Health Score

85 / 100
Full package analysis

Similar packages