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

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

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

     "use client";
    import { useState } from "react";
    import { getDraggableDefaultValue } from "../draggable/utils.js";
    import type { UseStateInitializer, UseStateSetter } from "../types.js";
    import type {
      ControlledWindowSplitterImplementation,
      ControlledWindowSplitterOptions,
    } from "./useControlledWindowSplitter.js";
    import { useControlledWindowSplitter } from "./useControlledWindowSplitter.js";

    /**
     * @remarks \@since 6.0.0
     */
    export interface WindowSplitterOptions<
      E extends HTMLElement = HTMLButtonElement,
    > extends Omit<
        ControlledWindowSplitterOptions<E>,
        "value" | "setValue" | "dragging" | "setDragging"
      > {
      /**
       * @defaultValue `Math.ceil((max - min) / 2)`
       */
      defaultValue?: UseStateInitializer<number>;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface WindowSplitterImplementation<E extends HTMLElement>
      extends ControlledWindowSplitterImplementation<E> {
      /**
       * The current drag distance in pixels.
       */
      value: number;

      /**
       * This can be used to manually set the {@link value} if that is needed for
       * some custom behavior.
       */
      setValue: UseStateSetter<number>;

      /**
       * This will be `true` when the user is dragging the element through mouse or
       * touch.
       */
      dragging: boolean;
    }

    /**
     * @example
     * Main Window Splitter
     * ```tsx
     * import {
     *   useCSSVariables,
     *   useWindowSplitter,
     *   WindowSplitter,
     * } from "@react-md/core";
     * import type { ReactElement } from "react";
     * import { useMemo } from "react";
     *
     * const POSITION_VAR = "--rmd-window-splitter-position";
     *
     * function Example(): ReactElement {
     *   const { value, splitterProps } = useWindowSplitter({
     *     min: 96,
     *     max: 600,
     *     defaultValue: 256,
     *   });
     *
     *   useCSSVariables(
     *     useMemo(() => [{ name: POSITION_VAR, value: `${value}px` }], [value])
     *   );
     *
     *   return (
     *     <div
     *       style={{
     *         display: "grid",
     *         gridTemplateColumns: `var(POSITION_VAR, 96px) 1fr`,
     *       }}
     *     >
     *       <nav id="main-nav">
     *         // pretend nav content
     *       </nav>
     *       <WindowSplitter
     *         aria-label="Resize Navigation"
     *         aria-controls="main-nav"
     *         {...splitterProps}
     *       />
     *       <main>
     *         // pretend main content
     *       </main>
     *     </>
     *   );
     * }
     * ```
     *
     * Note: This implementation is missing the toggle behavior when the `"Enter"`
     * key is pressed.
     *
     * @remarks \@since 6.0.0
     * @see https://www.w3.org/WAI/ARIA/apg/patterns/windowsplitter/
     */
    export function useWindowSplitter<E extends HTMLElement = HTMLButtonElement>(
      options: WindowSplitterOptions<E>
    ): WindowSplitterImplementation<E> {
      const { min, max, defaultValue, ...remaining } = options;
      const [value, setValue] = useState(() =>
        getDraggableDefaultValue({ min, max, defaultValue })
      );
      const [dragging, setDragging] = useState(false);
      const splitter = useControlledWindowSplitter({
        min,
        max,
        value,
        setValue,
        dragging,
        setDragging,
        ...remaining,
      });

      return {
        ...splitter,
        value,
        setValue,
        dragging,
      };
    }