# All files / transition SkeletonPlaceholder.tsx

0% Statements 0/170
0% Branches 0/1
0% Functions 0/1
0% Lines 0/170

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

     "use client";
    import {
      forwardRef,
      type HTMLAttributes,
      type ReactElement,
      type ReactNode,
    } from "react";
    import {
      useSkeletonPlaceholder,
      type SkeletonPlaceholderOptions,
    } from "./useSkeletonPlaceholder.js";

    declare module "react" {
      interface CSSProperties {
        "--rmd-skeleton-placeholder-background-color"?: string;
        "--rmd-skeleton-placeholder-height"?: string | number;
        "--rmd-skeleton-placeholder-width"?: string | number;
      }
    }

    /** @remarks \@since 6.0.0 */
    export interface SkeletonPlaceholderProps
      extends HTMLAttributes<HTMLDivElement>,
        SkeletonPlaceholderOptions {
      /**
       * @defaultValue `!!children`
       * @see {@link SkeletonPlaceholderOptions.disabled}
       */
      disabled?: boolean;

      /** @see {@link SkeletonPlaceholderOptions.disabled} for an example */
      children?: ReactNode;
    }

    /**
     * **Client Component**
     *
     * @example
     * Simple Example
     * ```tsx
     * import type { ReactElement } from "@react";
     * import { SkeletonPlaceholder } from "@react-md/core";
     *
     * interface ExampleProps {
     *   loading: boolean;
     *   children: ReactNode;
     * }
     *
     * export default function Example({ loading, children }: ExampleProps): ReactElement {
     *   if (loading) {
     *     return <SkeletonPlaceholder />;
     *   }
     *
     *   return <>{children}</>;
     * }
     * ```
     *
     * @example
     * Pre-rendered Layout
     * ```tsx
     * import type { ReactElement } from "@react";
     * import { SkeletonPlaceholder } from "@react-md/core";
     *
     * interface Data {
     *   id: string;
     *   name: string;
     *   createdBy: string
     *   createdOn: string;
     *   modifiedBy: string;
     *   modifiedOn: string;
     * }
     *
     * function ShowData({
     *   id,
     *   name,
     *   createdBy,
     *   createdOn,
     *   modifiedBy,
     *   modifiedOn,
     * }: Partial<Data>:: ReactElement {
     *   const loading =
     *     !name &&
     *     !createdBy &&
     *     !createdOn &&
     *     !modifiedBy &&
     *     !modifiedOn;
     *
     *   return (
     *     <Box grid gridName="custom-class-name">
     *       <SkeletonPlaceholder disabled={!loading}>
     *         {id}
     *       </SkeletonPlaceholder>
     *       <SkeletonPlaceholder disabled={!loading}>
     *         {name}
     *       </SkeletonPlaceholder>
     *       <SkeletonPlaceholder disabled={!loading}>
     *         {createdOn}
     *       </SkeletonPlaceholder>
     *       <SkeletonPlaceholder disabled={!loading}>
     *         {createdBy}
     *       </SkeletonPlaceholder>
     *       <SkeletonPlaceholder disabled={!loading}>
     *         {modifiedOn}
     *       </SkeletonPlaceholder>
     *       <SkeletonPlaceholder disabled={!loading}>
     *         {modifiedBy}
     *       </SkeletonPlaceholder>
     *     </Box>
     *   );
     * }
     *
     * export function Example(): ReactElement {
     *   const { data } = useLoadSomeDataQuery();
     *
     *   const items = useMemo(() => {
     *     // if the data has been fetched, just return the data
     *     if (data) {
     *       return data;
     *     }
     *
     *     // if the data does not exist, set up a skeleton of your layout by
     *     // rendering a random number of items.
     *     //
     *     // NOTE: This is memoized so you don't create a random length each
     *     // render
     *     const length = randomInt({ min: 3, max: 10 })
     *     return Array.from({ length }, (_, i) => ({ id: `placeholder-${i}` }));
     *   }, [data])
     *
     *   return (
     *     <List>
     *       {items.map((item) => <ShowData {...item} />)}
     *     </List>
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export const SkeletonPlaceholder = forwardRef<
      HTMLDivElement,
      SkeletonPlaceholderProps
    >(function SkeletonPlaceholder(props, ref): ReactElement {
      const {
        style,
        className,
        height,
        width,
        children,
        disabled = !!children,
        minPercentage,
        maxPercentage,
        ...remaining
      } = props;
      const skeleton = useSkeletonPlaceholder({
        disabled,
        style,
        className,
        height,
        width,
        minPercentage,
        maxPercentage,
      });

      return (
        <div {...remaining} ref={ref} {...skeleton}>
          {children}
        </div>
      );
    });