# All files ExpansionPanelHeader.tsx

100% Statements 179/179
50% Branches 1/2
100% Functions 1/1
100% Lines 179/179

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
    1801x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x
     import { cnb } from "cnbuilder";
    import type { ButtonHTMLAttributes, MouseEventHandler, ReactNode } from "react";
    import { forwardRef } from "react";
    import { ButtonUnstyled } from "../button";
    import type { IconRotatorProps } from "../icon";
    import { IconRotator, useIcon } from "../icon";
    import type { PropsWithRef } from "../types";
    import type { TypographyProps, TypographyType } from "../typography";
    import { Typography } from "../typography";

    /**
     * @remarks \@since 6.0.0 Updated to include additional heading/Typography
     * props.
     */
    export interface ExpansionPanelHeaderProps
      extends ButtonHTMLAttributes<HTMLButtonElement> {
      id: string;

      /**
       * This should be a function that toggles the expansion state for the parent
       * `ExpansionPanel`.
       */
      onClick: MouseEventHandler<HTMLButtonElement>;
      expanded: boolean;

      /**
       * @defaultValue `"h3"`
       */
      as?: "h1" | "h2" | "h3" | "h4" | "h5" | "h6";

      /**
       * @defaultValue `useIcon("expander")`
       */
      icon?: ReactNode;

      /**
       * @defaultValue `"subtitle-1"`
       */
      headingType?: TypographyType;

      /**
       * Any additional props to provide to the heading element that wraps the
       * expansion panel button.
       */
      headingProps?: PropsWithRef<TypographyProps, HTMLHeadingElement>;
      iconRotatorProps?: Omit<IconRotatorProps, "rotated" | "disableTransition">;

      /**
       * Any children to display **before** the button in the heading element. This
       * should really only be used if you need to add additional clickable elements
       * within the header.
       */
      beforeChildren?: ReactNode;

      /**
       * Any children to display **after** the button in the heading element.This
       * should really only be used if you need to add additional clickable elements
       * within the header.
       *
       * @example
       * ```tsx
       * afterChildren={
       *   <DropdownMenu buttonType="icon" ButtonChildren={<MoreVertSVGIcon />}>
       *     <MenuItem>Item 1</MenuItem>
       *     <MenuItem>Item 2</MenuItem>
       *     <MenuItem>Item 3</MenuItem>
       *   </DropdownMenu>
       * }
       * ```
       */
      afterChildren?: ReactNode;

      /**
       * @defaultValue `false`
       */
      disableTransition?: boolean;
    }

    /**
     * This is mostly an internal component, but can also be used to implement a
     * custom header implementation if needed.
     *
     * @example
     * Custom Header
     * ```tsx
     * import type { ExpansionPanelProps } from "@react-md/core";
     * import {
     *   ExpansionPanel,
     *   ExpansionPanelHeader,
     * } from "@react-md/core";
     * import type { ReactElement } from "react";
     * const { useId } from "react";
     *
     * export type CustomExpansionPanelProps = ExpansionPanelProps & { id: string; };
     *
     * export function CustomExpansionPanel(props: CustomExpansionPanelProps): ReactElement {
     *   const {
     *     id,
     *     disabled,
     *     onExpandClick,
     *     expanded,
     *     disableTransition,
     *     headerChildren,
     *   } = props;
     *
     *   return (
     *     <ExpansionPanel
     *       {...props}
     *       header={(
     *         <ExpansionPanelHeader
     *           aria-disabled={disabled || undefined}
     *           id={id}
     *           onClick={onExpandClick}
     *           expanded={expanded}
     *           disableTransition={disableTransition}
     *           // whatever props and any custom implementation
     *         >
     *           {headerChildren}
     *         </ExpansionPanelHeader>
     *       )}
     *     />
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0 Updated to be wrapped by the {@link Typography}
     * component and rendered as an `<h6>`.
     */
    export const ExpansionPanelHeader = forwardRef<
      HTMLHeadingElement,
      ExpansionPanelHeaderProps
    >(function ExpansionPanelHeader(props, ref) {
      const {
        id,
        headingType = "subtitle-1",
        headingProps,
        iconRotatorProps,
        icon: propIcon,
        expanded,
        className,
        children,
        beforeChildren,
        afterChildren,
        disableTransition = false,
        ...remaining
      } = props;

      const icon = useIcon("expander", propIcon);

      return (
        <Typography
          ref={ref}
          type={headingType}
          margin="none"
          {...headingProps}
          className={cnb("rmd-expansion-panel__heading", headingProps?.className)}
        >
          {beforeChildren}
          <ButtonUnstyled
            {...remaining}
            aria-expanded={expanded}
            id={id}
            className={cnb("rmd-expansion-panel__button", className)}
          >
            {children}
            {icon && (
              <IconRotator
                {...iconRotatorProps}
                rotated={expanded}
                disableTransition={disableTransition}
              >
                {icon}
              </IconRotator>
            )}
          </ButtonUnstyled>
          {afterChildren}
        </Typography>
      );
    });