# All files ExpansionPanel.tsx

100% Statements 283/283
100% Branches 7/7
100% Functions 2/2
100% Lines 283/283

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
    283
    2841x
    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
    85x
    85x
    85x
    85x
    85x
    85x
    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
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
    85x
     import { cnb } from "cnbuilder";
    import type { CSSProperties, ReactNode } from "react";
    import { forwardRef } from "react";
    import type { CardContentProps, CardProps } from "../card";
    import { Card, CardContent } from "../card";
    import { useCollapseTransition } from "../transition";
    import type { PropsWithRef } from "../types";
    import { useEnsuredId } from "../useEnsuredId";
    import { bem } from "../utils";
    import type { ExpansionPanelHeaderProps } from "./ExpansionPanelHeader";
    import { ExpansionPanelHeader } from "./ExpansionPanelHeader";

    const styles = bem("rmd-expansion-panel");

    /** @remarks \@since 6.0.0 */
    export interface ExpansionPanelClassNameOptions {
      className?: string;

      /**
       * @defaultValue `false`
       */
      expanded?: boolean;

      /**
       * Set this to `true` to disable the `margin-top` transition between multiple
       * panels
       *
       * @defaultValue `false`
       */
      disableTransition?: boolean;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export function expansionPanel(
      options: ExpansionPanelClassNameOptions = {}
    ): string {
      const { className, expanded = false, disableTransition = false } = options;

      return cnb(styles({ expanded, animate: !disableTransition }), className);
    }

    /**
     * @remarks \@since 6.0.0 Renamed some card props and updated for the latest
     * API.
     */
    export interface ExpansionPanelProps extends CardProps {
      /**
       * Set this to `true` if the {@link children} should be visible. This should
       * generally be provided by the `useExpansionPanels` hook.
       */
      expanded: boolean;

      /**
       * A function that should toggle the {@link expanded} state. This should
       * generally be provided by the `useExpansionPanels` hook.
       */
      onExpandClick(): void;

      /**
       * Set this to `true` to prevent the panel from toggling the {@link expanded}
       * state. This should generally be provided by the `useExpansionPanels` hook.
       *
       * @defaultValue `false`
       */
      disabled?: boolean;

      /**
       * @defaultValue `true`
       * @see {@link CardProps.fullWidth}
       */
      fullWidth?: boolean;

      /**
       * This can be used to implement a custom header implementation, but it
       * probably shouldn't be needed.
       *
       * @remarks \@since 6.0.0 This was renamed from `customHeader`
       * @see {@link headerProps}
       */
      header?: ReactNode;

      /**
       * This can be used to apply additional props to the header element.
       *
       * Note: You can override the `style` and `className` using
       * {@link headerStyle} and {@link headerClassName} instead for convenience.
       *
       * @example
       * ```tsx
       * <ExpansionPanel
       *   {...props}
       *   headerProps={{
       *     style: {
       *       // custom inline style
       *     },
       *     className: "come-class-name",
       *     icon: <MyCustomIcon />,
       *     iconRotatorProps: {
       *       className: "another-custom-class-name",
       *     },
       *   }}
       * />
       * ```
       */
      headerProps?: Omit<
        ExpansionPanelHeaderProps,
        "id" | "aria-disabled" | "onClick" | "expanded"
      >;

      /**
       * Convenience prop to apply custom style to the `ExpansionPanelHeader`
       * component.
       */
      headerStyle?: CSSProperties;

      /**
       * Convenience prop to apply custom class name to the `ExpansionPanelHeader`
       * component.
       */
      headerClassName?: string;

      /**
       * Content to display within the header of the expansion panel that toggles
       * the expanded state. THis should generally contain some accessible text
       * describing the expansion panel's content.
       *
       * @remarks \@since 6.0.0 THis was renamed from `header`
       */
      headerChildren?: ReactNode;

      /**
       * This can be used to apply additional props to the `CardContent` component
       * if needed.
       *
       * Note: The `disableLastChildPadding` defaults to `true` in this
       * implementation.
       *
       * @example
       * ```tsx
       * contentProps={{
       *   style: {},
       *   className: "some-class-name",
       *   disablePadding: true,
       *   disableSecondaryColor: true,
       *   disableLastChildPadding: false,
       *   // whatever else
       * }}
       * ```
       *
       * @see {@link contentStyle}
       * @see {@link contentClassName}
       * @see {@link disableContentPadding}
       */
      contentProps?: PropsWithRef<Omit<CardContentProps, "role">, HTMLDivElement>;

      /**
       * Convenience prop to apply custom style to the `CardContent` component.
       */
      contentStyle?: CSSProperties;

      /**
       * Convenience prop to apply custom class name to the `CardContent` component.
       */
      contentClassName?: string;

      /**
       * Set to `true` to disable the collapse transition for the card content
       * whenever the {@link expanded} state changes.
       *
       * @defaultValue `false`
       */
      disableTransition?: boolean;

      /**
       * @defaultValue `false`
       * @see {@link CardContentProps.disablePadding}
       */
      disableContentPadding?: boolean;

      /**
       * Set this to `true` to unmount the {@link children} when the
       * {@link expanded} state is `false`.
       *
       * @defaultValue `false`
       * @remarks \@since 6.0.0 Renamed from `persistent` to match the
       * `useCollapseTransition` naming convention.
       */
      temporary?: boolean;
    }

    /**
     * @see `useExpansionPanels` for examples.
     * @remarks
     * \@since 6.0.0 The content will be persistent and invisible using `hidden`
     * instead of unmounting and also animate the `margin-top` style.
     */
    export const ExpansionPanel = forwardRef<
      HTMLHeadingElement,
      ExpansionPanelProps
    >(function ExpansionPanel(props, ref) {
      const {
        id: propId,
        className,
        contentProps,
        contentStyle,
        contentClassName,
        header: propHeader,
        headerProps,
        headerStyle,
        headerClassName,
        headerChildren,
        children,
        expanded,
        onExpandClick,
        disabled = false,
        fullWidth = true,
        temporary = false,
        disableTransition = false,
        disableContentPadding = false,
        ...remaining
      } = props;

      const id = useEnsuredId(propId, "expansion-panel");
      const { elementProps, rendered } = useCollapseTransition({
        nodeRef: contentProps?.ref,
        style: contentStyle,
        className: contentClassName,
        timeout: disableTransition ? 0 : undefined,
        temporary,
        transitionIn: expanded,
        hidden: contentProps?.hidden,
      });

      let header = propHeader;
      if (typeof header === "undefined") {
        header = (
          <ExpansionPanelHeader
            style={headerStyle}
            className={headerClassName}
            disableTransition={disableTransition}
            {...headerProps}
            aria-disabled={disabled || undefined}
            id={id}
            onClick={onExpandClick}
            expanded={expanded}
          >
            {headerChildren}
          </ExpansionPanelHeader>
        );
      }

      return (
        <Card
          {...remaining}
          id={`${id}-container`}
          ref={ref}
          fullWidth={fullWidth}
          className={expansionPanel({
            expanded,
            disableTransition,
            className,
          })}
        >
          {header}
          {rendered && (
            <CardContent
              id={`${id}-content`}
              aria-labelledby={id}
              disablePadding={disableContentPadding}
              disableSecondaryColor
              disableLastChildPadding
              {...contentProps}
              {...elementProps}
              role="region"
            >
              {children}
            </CardContent>
          )}
        </Card>
      );
    });