How to use the @react-md/utils.useToggle function in @react-md/utils

To help you get started, we’ve selected a few @react-md/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 mlaursen / react-md / packages / documentation / src / components / TableOfContents / VisibilityContext.tsx View on Github external
export const TOCVisibilityProvider: FC<{ pathname: string }> = ({
  children,
  pathname,
}) => {
  const { isLargeDesktop } = useAppSize();
  const [visible, show, hide, toggle] = useToggle(isLargeDesktop);

  useEffect(() => {
    if (isLargeDesktop) {
      show();
    } else {
      hide();
    }

    // disabled since I only want to update it on desktop changes
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isLargeDesktop]);

  const rendered =
    pathname !== "/" &&
    pathname !== "/_error" &&
    !pathname.startsWith("/sandbox") &&
github mlaursen / react-md / packages / documentation / components / Layout / useAppSizeContext.ts View on Github external
export default function useAppSizeContext(): AppSize {
  const defaultSize = useContext(DefaultSize);
  const currentSize = useAppSizeContextRMD();
  const [toggled, , , toggle] = useToggle(false);
  const rendered = useRef(false);
  useEffect(() => {
    if (typeof window === "undefined" || rendered.current) {
      return;
    }

    // always want to re-run in dev mode to be safe with hot-reloading
    rendered.current = process.env.NODE_ENV !== "development";

    // if this is the first "render", wait for the initial hydration
    // to finish and then force a re-render so that the correct app size
    // can be used
    const frame = window.requestAnimationFrame(toggle);

    return () => {
      window.cancelAnimationFrame(frame);
github mlaursen / react-md / packages / documentation / src / components / DemoSandbox / SandboxModal.tsx View on Github external
fileName,
  sandbox,
  onFileChange,
  onRequestClose,
}) => {
  const pkgName = toTitle(pkg, " ", true);
  const title = `react-md - ${pkgName} - ${name} Sandbox`;
  const rendered = useRef(false);
  useEffect(() => {
    rendered.current = true;
  }, []);

  const { isPhone, isTablet, isDesktop, isLandscape } = useAppSize();
  const isLandscapeTablet = isLandscape && isTablet;
  const inline = isDesktop || isLandscapeTablet;
  const [isTreeVisible, showTree, hideTree, , setTreeVisible] = useToggle(
    isDesktop
  );

  useEffect(() => {
    if (isTreeVisible !== isDesktop) {
      setTreeVisible(isDesktop);
    }
    // only want to run on media changes
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isDesktop, isTablet, isPhone]);

  useEffect(() => {
    if (isTreeVisible && !isDesktop) {
      setTreeVisible(false);
    }
    // this effect closes the tree on mobile only after a new file is
github mlaursen / react-md / packages / documentation / src / components / Demos / Demo.tsx View on Github external
>
        {children}
      
    );
  }

  let {
    disableFullPageAppBar,
    disableFullPageContent,
  } = props as WithDefaultProps;
  if (emulated && typeof emulated === "object" && !emulated.appBar) {
    disableFullPageAppBar = true;
    disableFullPageContent = true;
  }

  const [toggled, enable, disable] = useToggle(false);
  return (
    
      {index > 0 && }
      <section id="{id}">
         0 ? "bottom" : "initial"}
        &gt;
          {name}
        
        
          {description}
        
        
          </section>
github mlaursen / react-md / packages / documentation / src / components / Customization / ThemeBuilder / Preview.tsx View on Github external
const Preview: FC = () =&gt; {
  const [visible, show, hide] = useToggle(false);
  const container = useRef(null);

  return (
    
      <div>
        
          
            
          
          Theme Preview
        
        </div>
github mlaursen / react-md / packages / documentation / components / Demos / Sheet / PositionExamples.tsx View on Github external
const PositionExamples: FC = () =&gt; {
  const [position, setPosition] = useState("bottom");
  const [toggled, , disable, toggle] = useToggle(false);

  const handleChange = useCallback(
    (event: React.ChangeEvent) =&gt; {
      setPosition(event.currentTarget.value as SheetPosition);
    },
    []
  );

  return (
    
      <button id="sheet-position-example-toggle"></button>
github mlaursen / react-md / packages / documentation / components / Layout / useRTLToggle.tsx View on Github external
export default function useRTLToggle(): ReturnValue {
  const defaultToggled = useMemo(() => {
    if (typeof document === "undefined") {
      return false;
    }

    const html = document.querySelector("html") as HTMLElement;
    return html.getAttribute("dir") === "rtl";
  }, []);
  const [isRTL, , , toggleRTL] = useToggle(defaultToggled);

  useEffect(() => {
    const html = document.querySelector("html") as HTMLElement;
    if (isRTL) {
      html.setAttribute("dir", "rtl");
    } else {
      html.setAttribute("dir", "ltr");
    }
  }, [isRTL]);

  return { isRTL, toggleRTL };
}
github mlaursen / react-md / packages / documentation / components / Demos / Overlay / CustomTheme.tsx View on Github external
const CustomTheme: FC = () =&gt; {
  const [toggled, , disable, toggle] = useToggle(false);
  return (
    
      <button id="custom-theme-button">
        Show Overlay
      </button>
github mlaursen / react-md / packages / documentation / components / Demos / Utils / ResizeObserverExample.tsx View on Github external
const SimpleExample: FC = () =&gt; {
  const [enabled, , , toggle] = useToggle(false);
  const { style, containerRef } = useRandomStyle(enabled);
  const { height, width, onResize } = useSize();

  return (
    
      <button id="start-resizing">
        {enabled ? "Stop" : "Start"}
      </button>
      <div style="{style}">
        </div>
github mlaursen / react-md / packages / documentation / src / components / Phone / Phone.tsx View on Github external
const Phone: FC = props =&gt; {
  const {
    id,
    title,
    children,
    appBar,
    className,
    contentClassName,
    contentStacked: stacked,
    prominent,
    disableAppBar,
    disableContent,
    onPhoneClose,
  } = props as WithDefaultProps;
  const { isPhone } = useAppSize();
  const [visible, enable, disable] = useToggle(false);
  const closePhone = useCallback(() =&gt; {
    disable();
    if (onPhoneClose) {
      onPhoneClose();
    }
  }, [onPhoneClose, disable]);

  if (visible &amp;&amp; !isPhone) {
    closePhone();
  }

  const value = useMemo(
    () =&gt; ({
      id,
      title,
      closePhone,