# All files / draggable useLocalStorageDraggableControls.ts

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

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

     "use client";
    import { useEffect, useState } from "react";
    import type { NonNullRef, UseStateInitializer, UseStateSetter } from "../types";
    import type {
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      LocalStorageHookOptions,
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      LocalStorageHookReturnValue,
    } from "../useLocalStorage";
    import { useLocalStorage } from "../useLocalStorage";
    import type { BaseDraggableOptions } from "./useControlledDraggable";
    import { getDraggableDefaultValue } from "./utils";

    /**
     * @remarks \@since 6.0.0
     */
    export interface LocalStorageDraggableControlsOptions
      extends Pick<BaseDraggableOptions<HTMLElement>, "min" | "max"> {
      /**
       * @defaultValue `Math.ceil((max - min) / 2)`
       */
      defaultValue?: UseStateInitializer<number>;

      /**
       * @defaultValue `""`
       * @see {@link LocalStorageHookOptions.key}
       */
      localStorageKey: string;

      /**
       * This will always be considered `true` while the user is dragging the
       * element to improve performance. The `value` will be persisted to local
       * storage once the user has stopped dragging.
       *
       * @defaultValue `false`
       * @see {@link LocalStorageHookOptions.manual}
       */
      localStorageManual?: boolean;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface LocalStorageDraggableControls {
      /**
       * 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;

      /** @internal */
      setDragging: UseStateSetter<boolean>;

      /**
       * @see {@link LocalStorageHookReturnValue.persist}
       */
      persistToLocalStorage(): void;

      /**
       * @see {@link LocalStorageHookReturnValue.remove}
       */
      removeFromLocalStorage(): void;
    }

    /**
     * @remarks \@since 6.0.0
     * @internal
     */
    export function useLocalStorageDraggableControls(
      options: LocalStorageDraggableControlsOptions
    ): LocalStorageDraggableControls {
      const { min, max, defaultValue, localStorageKey, localStorageManual } =
        options;
      const [dragging, setDragging] = useState(false);
      const { value, setValue, persist, remove } = useLocalStorage({
        key: localStorageKey,
        manual: localStorageManual || dragging,
        defaultValue: () => getDraggableDefaultValue({ min, max, defaultValue }),
        deserializer(item) {
          const value = parseFloat(item);
          if (Number.isNaN(item)) {
            return min;
          }

          return Math.max(min, Math.min(max, value));
        },
      });

      return {
        value,
        setValue,
        dragging,
        setDragging,
        persistToLocalStorage: persist,
        removeFromLocalStorage: remove,
      };
    }

    /**
     * @remarks \@since 6.0.0
     * @internal
     */
    export interface LocalStorageDraggablePersistanceOptions {
      dragging: boolean;
      persistToLocalStorage(): void;
      draggedOnce: NonNullRef<boolean>;
    }

    /**
     * @remarks \@since 6.0.0
     * @internal
     */
    export function useLocalStorageDraggablePersistance(
      options: LocalStorageDraggablePersistanceOptions
    ): void {
      const { dragging, draggedOnce, persistToLocalStorage } = options;

      useEffect(() => {
        if (!dragging && draggedOnce.current) {
          persistToLocalStorage();
        }
      }, [draggedOnce, dragging, persistToLocalStorage]);
    }