# All files / src/link LinkProvider.tsx

100% Statements 51/51
100% Branches 3/3
100% Functions 2/2
100% Lines 51/51

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
    521x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    640x
    640x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    73x
    73x
    73x
    73x
    73x
     "use client";
    import {
      createContext,
      useContext,
      type ForwardRefExoticComponent,
      type ReactElement,
      type ReactNode,
    } from "react";
    import { Link } from "./Link.js";

    /**
     * @remarks \@since 6.0.0
     */
    export type CustomLinkComponent =
      | ForwardRefExoticComponent<{ href: string }>
      | ForwardRefExoticComponent<{ to: string }>
      | "a";

    const context = createContext<CustomLinkComponent>(Link);
    const { Provider } = context;
    context.displayName = "Link";

    /**
     * @remarks \@since 6.0.0
     */
    export function useLink(): CustomLinkComponent {
      return useContext(context);
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface LinkProviderProps {
      /**
       * @defaultValue `useLink() ?? Link`
       */
      value?: CustomLinkComponent;
      children: ReactNode;
    }

    /**
     * **Client Component**
     *
     * @remarks \@since 6.0.0
     */
    export function LinkProvider(props: LinkProviderProps): ReactElement {
      const { value, children } = props;
      const inherited = useLink();

      return <Provider value={value ?? inherited}>{children}</Provider>;
    }