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

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

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

     import { cnb } from "cnbuilder";
    import type { ButtonHTMLAttributes } from "react";
    import { forwardRef, useCallback, useMemo, useState } from "react";
    import type { DefinedCSSVariableName } from "../theme";
    import { useCSSVariables } from "../theme";
    import type { UseStateInitializer } from "../types";
    import { useResizeListener } from "../useResizeListener";
    import { useWindowSplitter, WindowSplitter } from "../window-splitter";
    import { useMainLayout } from "./useMainLayout";

    // declare module "react" {
    //   interface CSSProperties {
    //     "--rmd-layout-size"?: number | string;
    //     "--rmd-layout-mini-size"?: number | string;
    //     "--rmd-layout-static-size"?: number | string;
    //     "--rmd-layout-header-offset"?: 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 { mainId, currentLayout } = useMainLayout();
      const isMini = currentLayout === "resizable-mini";

      const [viewportMax, setViewportMax] = useState(getViewportMax);
      const max = typeof propMax === "number" ? propMax : viewportMax;
      const min =
        typeof propMin === "number" ? propMin : isMini ? DEFAULT_MINI_NAV_WIDTH : 0;

      useResizeListener({
        disabled: disabled || typeof propMax === "number",
        onUpdate: useCallback(() => {
          setViewportMax(getViewportMax());
        }, []),
      });

      const { value, dragging, splitterProps } = useWindowSplitter({
        id,
        ref,
        min,
        max,
        step,
        disabled,
        onKeyDown,
        onMouseUp,
        onMouseDown,
        onMouseMove,
        defaultValue,
        localStorageKey,
      });
      useCSSVariables<DefinedCSSVariableName>(
        useMemo(() => {
          if (!value || disabled) {
            return [];
          }

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

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