# All files / src/window-splitter useControlledWindowSplitter.ts

99.37% Statements 158/159
50% Branches 2/4
100% Functions 1/1
99.37% Lines 158/159

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
    1601x
    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
    46x
    46x
    46x
    46x
    46x
    46x
    46x
    46x
     "use client";
    import type { Ref, RefCallback } from "react";
    import type {
      ControlledDraggableImplementation,
      ControlledDraggableOptions,
      DraggableKeyboardEventHandlers,
      DraggableMouseEventHandlers,
      DraggableTouchEventHandlers,
    } from "../draggable/useControlledDraggable.js";
    import { useControlledDraggable } from "../draggable/useControlledDraggable.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import { getPercentage } from "../utils/getPercentage.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;
      }
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface ControlledWindowSplitterOptions<
      E extends HTMLElement = HTMLButtonElement,
    > extends Omit<
        ControlledDraggableOptions<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>;

      /**
       * The current value for the window splitter.
       */
      value: number;
    }

    /**
     * @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 ControlledWindowSplitterImplementation<E extends HTMLElement>
      extends ControlledDraggableImplementation<E> {
      splitterProps: WindowSplitterWidgetProps<E>;
    }

    /**
     * You are most likely looking for the `useWindowSplitter` or
     * `useLocalStorageWindowSplitter` hooks instead since this is a low-level hook
     * to implement those two versions.
     *
     * @example
     * Custom Implementation
     * ```tsx
     * import { useControlledWindowSplitter, WindowSplitterImplementation } from "@react-md/core";
     * import { useState } from "react";
     *
     * // this is pretty much the `useWindowSplitter` implementation
     * export function useMyCustomWindowSplitter(): WindowSplitterImplementation {
     *   const [dragging, setDragging] = useState(false);
     *   const [value, setValue] = useState(0);
     *   const splitter = useControlledWindowSplitter({
     *     min: 0,
     *     max: 100,
     *     value,
     *     setValue,
     *     dragging,
     *     setDragging,
     *   });
     *
     *   return {
     *     ...splitter,
     *     value,
     *     setValue,
     *     dragging,
     *   }
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export function useControlledWindowSplitter<
      E extends HTMLElement = HTMLButtonElement,
    >(
      options: ControlledWindowSplitterOptions<E>
    ): ControlledWindowSplitterImplementation<E> {
      const {
        id: propId,
        min,
        max,
        value,
        reversed = false,
        vertical,
        dragging,
        withinOffsetParent,
      } = options;

      const id = useEnsuredId(propId, "splitter");
      const draggableImplementation = useControlledDraggable(options);
      const {
        draggableRef,
        dragPercentage,
        mouseEventHandlers,
        keyboardEventHandlers,
      } = draggableImplementation;

      const percentage =
        dragging && withinOffsetParent
          ? dragPercentage
          : getPercentage({ min, max, value });

      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,
        },
      };
    }