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

100% Statements 141/141
50% Branches 1/2
100% Functions 1/1
100% Lines 141/141

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
    1421x
    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
     "use client";
    import { type Ref, type RefCallback } from "react";
    import {
      useDraggable,
      type BaseDraggableOptions,
      type DraggableImplementation,
      type DraggableKeyboardEventHandlers,
      type DraggableMouseEventHandlers,
      type DraggableStateOptions,
      type DraggableTouchEventHandlers,
    } from "../draggable/useDraggable.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;
      }
    }

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

    /**
     * Used to control the state for the `WindowSplitter` component.
     *
     * @example
     * Custom Implementation
     * ```tsx
     * import { useWindowSplitter, 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 = useWindowSplitter({
     *     min: 0,
     *     max: 100,
     *     value,
     *     setValue,
     *     dragging,
     *     setDragging,
     *   });
     *
     *   return {
     *     ...splitter,
     *     value,
     *     setValue,
     *     dragging,
     *   }
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export function useWindowSplitter<E extends HTMLElement = HTMLButtonElement>(
      options: WindowSplitterOptions<E>
    ): WindowSplitterImplementation<E> {
      const { id: propId, reversed = false, vertical } = options;

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

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