# All files / src/table TableHeader.tsx

98.75% Statements 158/160
88.23% Branches 15/17
100% Functions 2/2
98.75% Lines 158/160

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
    1611x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    5x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    2x
    2x

    2x
    2x
    2x
    1x
    1x
    1x
    1x
    1x
    2x
    2x
    7x
    7x
    7x
    2x
    2x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
    7x
     "use client";
    import {
      forwardRef,
      useCallback,
      useMemo,
      useState,
      type HTMLAttributes,
    } from "react";
    import { useEnsuredRef } from "../useEnsuredRef.js";
    import { useIntersectionObserver } from "../useIntersectionObserver.js";
    import {
      TableConfigProvider,
      useTableConfig,
      type TableCellConfig,
      type TableConfigContext,
    } from "./TableConfigurationProvider.js";
    import { useTableContainer } from "./TableContainerProvider.js";
    import { tableHeader } from "./tableHeaderStyles.js";
    import {
      type IsStickyTableSectionActive,
      type TableStickySectionProps,
    } from "./types.js";

    /**
     * @remarks \@since 6.0.0
     */
    export const isTableHeaderStickyActive: IsStickyTableSectionActive = (
      entry
    ) => {
      return (
        entry.intersectionRatio < 1 &&
        entry.boundingClientRect.bottom <= window.innerHeight
      );
    };

    /**
     * @remarks \@since 6.0.0 Added support for "sticky-active" state.
     */
    export interface TableHeaderProps
      extends HTMLAttributes<HTMLTableSectionElement>,
        Pick<TableCellConfig, "lineWrap">,
        TableStickySectionProps {
      /**
       * This is a rename of the `disableHover` of the `TableConfig` since table
       * headers are not hoverable by default. This prop can be enabled to add the
       * row hover color within table headers, but it is not really recommended.
       *
       * @defaultValue `false`
       */
      hoverable?: boolean;

      /** @defaultValue {@link isTableHeaderStickyActive} */
      isStickyActive?: IsStickyTableSectionActive;
    }

    /**
     * **Client Component**
     * TODO: Create separate useStickyTableHeader + StickyTableHeader component
     *
     * Creates a `<thead>` element with some basic styles. This component will also
     * update the table configuration so that all the `TableCell` children will
     * automatically become `<th>` elements instead of the normal `<td>` as well as
     * disabling the hover effect and line wrapping. The hover effect and
     * line-wrapping can be re-enabled if desired through the `hoverable` and
     * `disableNoWrap` props.
     */
    export const TableHeader = forwardRef<
      HTMLTableSectionElement,
      TableHeaderProps
    >(function TableHeader(props, propRef) {
      const {
        className,
        hoverable = false,
        lineWrap: propLineWrap,
        children,
        sticky = false,
        stickyOptions,
        isStickyActive = isTableHeaderStickyActive,
        disableStickyStyles = false,
        stickyActiveClassName,
        ...remaining
      } = props;

      // update the table configuration with the custom overrides for the `<thead>`
      const { dense, hAlign, vAlign, lineWrap, disableHover, disableBorders } =
        useTableConfig({
          lineWrap: propLineWrap,
          disableHover: !hoverable,
        });

      const configuration = useMemo<TableConfigContext>(
        () => ({
          dense,
          header: true,
          hAlign,
          vAlign,
          lineWrap,
          disableBorders,
          disableHover,
        }),
        [dense, hAlign, vAlign, lineWrap, disableBorders, disableHover]
      );

      const [theadRef, theadRefCallback] = useEnsuredRef(propRef);
      const { exists, containerRef } = useTableContainer();
      const [stickyActive, setStickyActive] = useState(false);
      const targetRef = useIntersectionObserver({
        ref: exists ? undefined : theadRefCallback,
        root: containerRef,
        disabled: !sticky || disableStickyStyles,
        threshold: exists ? 0 : 1,
        getRootMargin: useCallback(() => {
          const thead = theadRef.current;
          if (!thead) {
            return;
          }

          let topOffset: number;
          if (exists) {
            topOffset = thead.offsetHeight - 1;
          } else {
            const top = parseFloat(window.getComputedStyle(thead).top);
            topOffset = Number.isNaN(top) ? 1 : top + 1;
          }

          return `-${topOffset}px 0px 0px`;
        }, [exists, theadRef]),
        onUpdate: useCallback(
          ([entry]) => {
            setStickyActive(isStickyActive(entry, exists));
          },
          [exists, isStickyActive]
        ),
        // allow the user defined sticky options to override the default behavior
        ...stickyOptions,
      });

      return (
        <TableConfigProvider value={configuration}>
          <thead
            {...remaining}
            ref={exists ? theadRefCallback : targetRef}
            className={tableHeader({
              className,
              dense,
              sticky,
              stickyActive,
              stickyActiveClassName,
            })}
          >
            {children}
          </thead>
          {exists && sticky && !disableStickyStyles && (
            // rendering a `<tbody>` since it is valid to have 0-many in a table
            // https://html.spec.whatwg.org/multipage/tables.html#the-table-element
            <tbody aria-hidden ref={targetRef} />
          )}
        </TableConfigProvider>
      );
    });