# All files / src/main-layout-current MainNavWindowSplitter.tsx

0% Statements 0/145
0% Branches 0/1
0% Functions 0/1
0% Lines 0/145

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146

     import { cnb } from "cnbuilder";
    import type { ButtonHTMLAttributes } from "react";
    import { forwardRef, useEffect, useMemo, useRef, useState } from "react";
    import { useAppSize } from "../AppSizeProvider";
    import { getDraggableDefaultValue } from "../draggable/utils";
    import { useSsr } from "../SsrProvider";
    import type { CSSVariable } from "../theme";
    import { useCSSVariables } from "../theme";
    import type { UseStateInitializer } from "../types";
    import { useWindowSplitter, WindowSplitter } from "../window-splitter";
    import { useMainLayoutContext } from "./MainLayoutProvider";

    declare module "react" {
      interface CSSProperties {
        "--rmd-layout-size"?: number | string;
        "--rmd-layout-mini-size"?: number | string;
        "--rmd-layout-static-size"?: number | string;
      }
    }

    const DEFAULT_MINI_NAV_WIDTH = 56;
    const DEFAULT_MAX_WIDTH = 600;
    const getViewportMax = (): number =>
      typeof window === "undefined"
        ? DEFAULT_MAX_WIDTH
        : Math.min(DEFAULT_MAX_WIDTH, window.innerWidth - 56);

    export interface MainNavWindowSplitterProps
      extends Omit<ButtonHTMLAttributes<HTMLButtonElement>, "defaultValue"> {
      min?: number;
      max?: number;
      step?: number;
      defaultValue?: UseStateInitializer<number>;
      localStorageKey?: string;
    }

    export const MainNavWindowSplitter = forwardRef<
      HTMLButtonElement,
      MainNavWindowSplitterProps
    >(function MainNavWindowSplitter(props, ref) {
      const {
        "aria-labelledby": ariaLabelledBy,
        "aria-label": ariaLabel = ariaLabelledBy ? undefined : "Resize Navigation",
        "aria-controls": ariaControls,
        id,
        className,
        min: propMin,
        max: propMax,
        step,
        disabled,
        onKeyDown,
        onMouseUp,
        onMouseDown,
        onMouseMove,
        defaultValue,
        localStorageKey,
        ...remaining
      } = props;

      const ssr = useSsr();
      const { mini, mainId, layout, expanded } = useMainLayoutContext();
      const [viewportMax, setViewportMax] = useState(() =>
        ssr ? DEFAULT_MAX_WIDTH : getViewportMax()
      );
      useEffect(() => {
        if (typeof propMax === "number" || disabled) {
          return;
        }

        if (ssr) {
          setViewportMax(getViewportMax());
        }

        const callback = (): void => {
          setViewportMax(getViewportMax());
        };

        window.addEventListener("resize", callback);

        return () => {
          window.removeEventListener("resize", callback);
        };
      }, [disabled, propMax, ssr]);
      const max = typeof propMax === "number" ? propMax : viewportMax;
      const min =
        typeof propMin === "number" ? propMin : mini ? DEFAULT_MINI_NAV_WIDTH : 0;

      const { value, dragging, splitterProps, setValue } = useWindowSplitter({
        id,
        ref,
        min,
        max,
        step,
        disabled,
        onKeyDown,
        onMouseUp,
        onMouseDown,
        onMouseMove,
        defaultValue,
        localStorageKey,
      });

      // const prevVisible = useRef(expanded);
      // useEffect(() => {
      //   if (disabled || expanded === prevVisible.current) {
      //     return;
      //   }

      //   prevVisible.current = expanded;

      //   if (mini) {
      //     setValue(
      //       expanded ? getDraggableDefaultValue({ min, max, defaultValue }) : min
      //     );
      //     return;
      //   }
      // }, [defaultValue, disabled, expanded, max, min, mini, setValue]);

      const { isPhone } = useAppSize();
      const cssVariables = useMemo<CSSVariable[]>(() => {
        if (!value || disabled || isPhone) {
          return [];
        }

        return [
          {
            name: "--rmd-layout-size",
            value: `${value}px`,
          },
        ];
      }, [disabled, value]);
      useCSSVariables(cssVariables);

      return (
        <WindowSplitter
          aria-label={ariaLabel as string}
          aria-labelledby={ariaLabelledBy}
          aria-controls={ariaControls || mainId}
          {...remaining}
          {...splitterProps}
          dragging={dragging}
          className={cnb("rmd-layout-splitter", className)}
        />
      );
    });