# All files / src/icon IconProvider.tsx

100% Statements 282/282
100% Branches 5/5
100% Functions 2/2
100% Lines 282/282

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
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    2831x
    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
    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
    3020x
    3020x
    3020x
    3020x
    3020x
    49x
    49x
    2971x
    2971x
    2971x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    122x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
    177x
     "use client";
    import {
      createContext,
      useContext,
      useMemo,
      type ReactElement,
      type ReactNode,
    } from "react";
    import { FontIcon } from "./FontIcon.js";

    /**
     * @remarks \@since 5.0.0 The `download` icon has been renamed to `upload`.
     */
    export interface ConfigurableIcons {
      /**
       * The icon for navigating backwards or closing an item to the left.
       *
       * @defaultValue `<FontIcon>keyboard_arrow_left</FontIcon>`
       */
      back?: ReactNode;

      /**
       * @defaultValue `<FontIcon>close</FontIcon>`
       *
       * @remarks \@since 6.0.0
       */
      close?: ReactNode;

      /**
       * The icon to use for unchecked checkboxes.
       *
       * @defaultValue `<FontIcon>check_box_outline_blank</FontIcon>`
       * @remarks \@since 6.0.0 This icon now represents the unchecked state for
       * checkboxes.
       */
      checkbox?: ReactNode;

      /**
       * The icon to use for checked checkboxes.
       *
       * @defaultValue `<FontIcon>check_box</FontIcon>`
       * @remarks \@since 6.0.0
       */
      checkboxChecked?: ReactNode;

      /**
       * The icon to use for indeterminate checkboxes.
       *
       * @defaultValue `<FontIcon>indeterminate_check_box</FontIcon>`
       * @remarks \@since 6.0.0
       */
      checkboxIndeterminate?: ReactNode;

      /**
       * The icon to use for dropdown menus or content that expands vertically in a
       * new material instead of inline like the `expander` icon.
       *
       * @defaultValue `<FontIcon>arrow_drop_down</FontIcon>`
       */
      dropdown?: ReactNode;

      /**
       * The icon to use when there are form errors.
       *
       * @remarks \@since 2.5.0
       * @defaultValue `<FontIcon>error_outline</FontIcon>`
       */
      error?: ReactNode;

      /**
       * The icon to use for expanding content vertically.
       *
       * @defaultValue `<FontIcon>keyboard_arrow_down</FontIcon>`
       */
      expander?: ReactNode;

      /**
       * The icon for navigating forwards or closing an item to the right. This is
       * also used internally for nested dropdown menus.
       *
       * @defaultValue `<FontIcon>keyboard_arrow_right</FontIcon>`
       */
      forward?: ReactNode;

      /**
       * The general to use for displaying a main navigation menu (usually a
       * hamburger menu).
       *
       * @defaultValue `<FontIcon>menu</FontIcon>`
       */
      menu?: ReactNode;

      /**
       * The icon for displaying notifications. This is used internally in the
       * `BadgedButton` in the `@react-md/badge` package.
       *
       * @defaultValue `<FontIcon>notifications</FontIcon>`
       */
      notification?: ReactNode;

      /**
       * The icon for temporarily displaying a password's field value as plain text.
       *
       * @defaultValue `<FontIcon>remove_red_eye</FontIcon>`
       */
      password?: ReactNode;

      /**
       * The icon to use for unchecked radio buttons.
       *
       * @defaultValue `<FontIcon>radio_button_unchecked</FontIcon>`
       * @remarks \@since 6.0.0 This icon now represents the unchecked state for
       * radios.
       */
      radio?: ReactNode;

      /**
       * The icon to use for checked radio buttons.
       *
       * @defaultValue `<FontIcon>radio_button_checked</FontIcon>`
       */
      radioChecked?: ReactNode;

      /**
       * The icon to use for showing that something has been selected that is not a
       * radio or checkbox. This is used internally for the `Chip` in the
       * `@react-md/core` package.
       *
       * @defaultValue `<FontIcon>check</FontIcon>`
       */
      selected?: ReactNode;

      /**
       * The icon to represent sorting content.
       *
       * @defaultValue `<FontIcon>arrow_upward</FontIcon>`
       */
      sort?: ReactNode;

      /**
       * The icon to use for the `FileInput` component (normally file
       * uploads).
       *
       * @remarks \@since 5.0.0
       * @defaultValue `<FontIcon>file_upload</FontIcon>`
       */
      upload?: ReactNode;
    }

    export type ConfiguredIcons = Required<ConfigurableIcons>;

    const DEFAULT_ICONS: ConfiguredIcons = {
      back: <FontIcon>keyboard_arrow_left</FontIcon>,
      close: <FontIcon>close</FontIcon>,
      checkbox: <FontIcon>check_box_outline_blank</FontIcon>,
      checkboxChecked: <FontIcon>check_box</FontIcon>,
      checkboxIndeterminate: <FontIcon>indeterminate_check_box</FontIcon>,
      dropdown: <FontIcon>arrow_drop_down</FontIcon>,
      error: <FontIcon>error_outline</FontIcon>,
      expander: <FontIcon>keyboard_arrow_down</FontIcon>,
      forward: <FontIcon>keyboard_arrow_right</FontIcon>,
      menu: <FontIcon>menu</FontIcon>,
      notification: <FontIcon>notifications</FontIcon>,
      password: <FontIcon>remove_red_eye</FontIcon>,
      radio: <FontIcon>radio_button_unchecked</FontIcon>,
      radioChecked: <FontIcon>radio_button_checked</FontIcon>,
      selected: <FontIcon>check</FontIcon>,
      sort: <FontIcon>arrow_upward</FontIcon>,
      upload: <FontIcon>file_upload</FontIcon>,
    };

    const context = createContext<ConfiguredIcons>(DEFAULT_ICONS);
    context.displayName = "Icon";
    const { Provider } = context;

    /**
     * Gets one of the configured icons from the `IconProvider`. This is probably
     * just for use within `react-md`, but might be helpful outside if you want to
     * reuse the existing icon configuration for other custom components.
     *
     * If te second argument is provided and it is not `undefined`, that value will
     * be used instead of the inherited icon type.
     *
     * @param name - The name of the icon you want to use.
     * @param override - An optional override to use instead of the inherited icon.
     * @returns The overridden icon value or the inherited icon.
     */
    export function useIcon(
      name: keyof ConfigurableIcons,
      override?: ReactNode | undefined
    ): ReactNode {
      const icons = useContext(context);
      if (typeof override !== "undefined") {
        return override;
      }

      return icons[name];
    }

    export interface IconProviderProps extends ConfigurableIcons {
      /**
       * The children that should inherit the icon provider context. This is
       * required since this component is pretty much worthless to use if you don't
       * inherit the overridden icons.
       */
      children: ReactNode;
    }

    /**
     * **Client Component**
     *
     * The `IconProvider` component is used to override all the default icons within
     * `react-md` with a newly defined set of icons. This is super nice since you
     * won't need to create new component wrappers for all the components within
     * `react-md` if you want to switch to SVG icons instead of the default font
     * icons.
     */
    export function IconProvider(props: IconProviderProps): ReactElement {
      const {
        children,
        back = DEFAULT_ICONS.back,
        close = DEFAULT_ICONS.close,
        checkbox = DEFAULT_ICONS.checkbox,
        checkboxChecked = DEFAULT_ICONS.checkboxChecked,
        checkboxIndeterminate = DEFAULT_ICONS.checkboxIndeterminate,
        dropdown = DEFAULT_ICONS.dropdown,
        expander = DEFAULT_ICONS.expander,
        error = DEFAULT_ICONS.error,
        forward = DEFAULT_ICONS.forward,
        menu = DEFAULT_ICONS.menu,
        notification = DEFAULT_ICONS.notification,
        password = DEFAULT_ICONS.password,
        radio = DEFAULT_ICONS.radio,
        radioChecked = DEFAULT_ICONS.radioChecked,
        selected = DEFAULT_ICONS.selected,
        sort = DEFAULT_ICONS.sort,
        upload = DEFAULT_ICONS.upload,
      } = props;

      const value = useMemo<ConfiguredIcons>(
        () => ({
          back,
          close,
          checkbox,
          checkboxChecked,
          checkboxIndeterminate,
          dropdown,
          error,
          expander,
          forward,
          menu,
          notification,
          password,
          radio,
          radioChecked,
          selected,
          sort,
          upload,
        }),
        [
          back,
          close,
          checkbox,
          checkboxChecked,
          checkboxIndeterminate,
          dropdown,
          error,
          expander,
          forward,
          menu,
          notification,
          password,
          radio,
          radioChecked,
          selected,
          sort,
          upload,
        ]
      );

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