# All files / window-splitter useWindowSplitter.ts

98.91% Statements 183/185
40% Branches 2/5
100% Functions 1/1
98.91% Lines 183/185

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
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    1861x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    33x

    33x
    33x
    46x
    46x
    46x
    46x
    46x
    46x
     "use client";
    import { useMemo, type CSSProperties, type Ref, type RefCallback } from "react";
    import { useSsr } from "../SsrProvider.js";
    import {
      useDraggable,
      type BaseDraggableOptions,
      type DraggableImplementation,
      type DraggableKeyboardEventHandlers,
      type DraggableMouseEventHandlers,
      type DraggableStateOptions,
      type DraggableTouchEventHandlers,
    } from "../draggable/useDraggable.js";
    import { type ReadonlyCSSVariableList } from "../theme/useCSSVariables.js";
    import { useEnsuredId } from "../useEnsuredId.js";

    declare module "react" {
      interface CSSProperties {
        "--rmd-window-splitter-top"?: number | string;
        "--rmd-window-splitter-right"?: number | string;
        "--rmd-window-splitter-bottom"?: number | string;
        "--rmd-window-splitter-left"?: number | string;
        "--rmd-window-splitter-opacity"?: number | string;
        "--rmd-window-splitter-position"?: number | string;
      }
    }

    const POSITION_VAR = "--rmd-window-splitter-position";

    /**
     * @remarks \@since 6.0.0
     */
    export interface BaseWindowSplitterOptions<
      E extends HTMLElement = HTMLButtonElement,
    > extends Omit<BaseDraggableOptions<E>, keyof DraggableTouchEventHandlers<E>> {
      /**
       * An optional id to use for the window splitter.
       *
       * @defaultValue `"window-splitter-" + useId()`
       */
      id?: string;

      /**
       * An optional ref for the window splitter element. This will be merged into
       * the {@link WindowSplitterWidgetProps.ref}.
       */
      ref?: Ref<E>;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export type WindowSplitterOptions<E extends HTMLElement = HTMLButtonElement> =
      BaseWindowSplitterOptions<E> & DraggableStateOptions;

    /**
     * @remarks \@since 6.0.0
     */
    export interface WindowSplitterWidgetProps<E extends HTMLElement>
      extends Required<DraggableMouseEventHandlers<E>>,
        Required<DraggableKeyboardEventHandlers<E>> {
      "aria-orientation": "vertical" | undefined;
      "aria-valuenow": number;
      "aria-valuemin": number;
      "aria-valuemax": number;
      id: string;
      ref: RefCallback<E>;
      role: "separator";
      reversed: boolean;
      dragging: boolean;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface WindowSplitterImplementation<
      E extends HTMLElement = HTMLButtonElement,
    > extends DraggableImplementation<E> {
      variables: ReadonlyCSSVariableList;
      splitterProps: WindowSplitterWidgetProps<E>;
      positionStyle: CSSProperties;
    }

    /**
     * Used to control the state for the `WindowSplitter` component.
     *
     * @example
     * Viewport WIndow Splitter
     * ```tsx
     * import {
     *   WindowSplitter,
     *   useCSSVariables,
     *   useWindowSize,
     *   useWindowSplitter,
     * } from "@react-md/core";
     * import { useId, type ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   const sheetId = useId();
     *   const { width } = useWindowSize({ disableHeight: true });
     *   const { value, splitterProps } = useWindowSplitter({
     *     min: 80,
     *     max: Math.max(600, width * 0.45),
     *   });
     *   // can set to a global custom property
     *   useCSSVariables([{ name: '--width', value: `${value}px` }]);
     *
     *
     *   return (
     *     <>
     *       <Sheet
     *         {...pretendProps}
     *         id={sheetId}
     *         // use the custom property for the width
     *         style={{ width: "var(--width)" }}
     *         // or just use the width
     *         // style={{ width: value }}
     *       />
     *       <WindowSplitter
     *         {...splitterProps}
     *         aria-controls={sheetId}
     *         aria-label="Resize Navigation"
     *       />
     *       <main
     *         // whatever styles you need...
     *         style={{ marginLeft: "var(--width, 0)" }}
     *         // style={{ marginLeft: value }}
     *       >
     *         Main Content
     *       </main>
     *     </>
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export function useWindowSplitter<E extends HTMLElement = HTMLButtonElement>(
      options: WindowSplitterOptions<E>
    ): WindowSplitterImplementation<E> {
      const { id: propId, reversed = false, vertical, defaultValue } = options;

      const id = useEnsuredId(propId, "splitter");
      const ssr = useSsr();
      const draggableImplementation = useDraggable(options);
      const {
        value,
        dragging,
        percentage,
        draggableRef,
        mouseEventHandlers,
        keyboardEventHandlers,
      } = draggableImplementation;

      let position: string | undefined;
      if (!ssr || typeof defaultValue !== "undefined") {
        position = `${value}px`;
      }

      return {
        ...draggableImplementation,
        splitterProps: {
          "aria-orientation": vertical ? "vertical" : undefined,
          "aria-valuenow": Math.ceil(percentage * 100),
          "aria-valuemin": 0,
          "aria-valuemax": 100,
          id,
          ref: draggableRef,
          role: "separator",
          dragging,
          reversed,
          ...mouseEventHandlers,
          ...keyboardEventHandlers,
        },
        variables: useMemo(() => {
          if (!position) {
            return [];
          }

          return [{ name: POSITION_VAR, value: position }];
        }, [position]),
        positionStyle: {
          [POSITION_VAR]: position,
        },
      };
    }