# All files / src/transition SlideContainer.tsx

100% Statements 188/188
100% Branches 2/2
100% Functions 2/2
100% Lines 188/188

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
    1891x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    11x
    11x
    11x
    11x
    11x
    11x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    11x
    1x
     import { cnb } from "cnbuilder";
    import { forwardRef, type HTMLAttributes } from "react";
    import { bem } from "../utils/bem.js";

    const styles = bem("rmd-slide-container");

    /**
     * @example
     * Direction "left"
     * ```
     *           -------------
     *           |           |
     *           |  Slide 1  |  Slide 2
     *           |           |
     *           -------------
     *           -------------
     *           |           |
     *      Slide 1     Slide 2
     *           |           |
     *           -------------
     *           -------------
     *           |           |
     *  Slide 1  |  Slide 2  |
     *           |           |
     *           -------------
     * ```
     *
     * @example
     * Direction "right"
     * ```
     *           -------------
     *           |           |
     *  Slide 1  |  Slide 2  |
     *           |           |
     *           -------------
     *           -------------
     *           |           |
     *      Slide 1     Slide 2
     *           |           |
     *           -------------
     *           -------------
     *           |           |
     *           |  Slide 1  |  Slide 2
     *           |           |
     *           -------------
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export type SlideDirection = "left" | "right";

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

      /** @see {@link SlideDirection} */
      direction: SlideDirection;
    }

    /**
     * @see {@link useSlideTransition} for an example
     * @remarks \@since 6.0.0
     */
    export function slideContainer(
      options: SlideContainerClassNameOptions
    ): string {
      const { className, direction } = options;

      return cnb(styles({ [direction]: true }), className);
    }

    /** @remarks \@since 6.0.0 */
    export interface SlideContainerProps
      extends HTMLAttributes<HTMLDivElement>,
        SlideContainerClassNameOptions {}

    /**
     * **Server Component**
     * This is a server component, but generally relies on state so probably a client component.
     *
     * @example
     * Simple Example
     * ```tsx
     * import { SlideContainer, Slide } from "@react-md/core";
     * import type { ReactElement, ReactNode } from "react";
     * import { useState } from "react";
     *
     * interface State {
     *   direction: SlideDirection;
     *   activeIndex: number;
     * }
     *
     * function Example(): ReactElement {
     *   const [state, setState] = useState<State>({
     *     direction: "left",
     *     activeIndex: 0,
     *   });
     *   const { direction, activeIndex } = state;
     *
     *   // when changing a slide, `direction` should be set to "left" if the
     *   // previous `activeIndex` is less than the next index
     *   //
     *   // i.e.
     *   // setState((prevState) => ({
     *   //   direction: prevState.activeIndex < index ? "left" : "right",
     *   //   activeIndex: index,
     *   // }))
     *
     *   return (
     *     <SlideContainer direction={direction}>
     *       <Slide active={activeIndex === 0}>
     *         Slide 1
     *       </Slide>
     *       <Slide active={activeIndex === 1}>
     *         Slide 2
     *       </Slide>
     *       <Slide active={activeIndex === 2}>
     *         Slide 3
     *       </Slide>
     *     </SlideContainer>
     *   );
     * }
     * ```
     *
     * @example
     * Persistent Slides
     * ```tsx
     * import type { SlideDirection } from "@react-md/core";
     * import { SlideContainer, Slide } from "@react-md/core";
     * import type { ReactElement, ReactNode } from "react";
     * import { useState } from "react";
     *
     * interface State {
     *   direction: SlideDirection;
     *   activeIndex: number;
     * }
     *
     * function Example(): ReactElement {
     *   const [state, setState] = useState<State>({
     *     direction: "left",
     *     activeIndex: 0,
     *   });
     *   const { direction, activeIndex } = state;
     *
     *   // when changing a slide, `direction` should be set to "left" if the
     *   // previous `activeIndex` is less than the next index
     *   //
     *   // i.e.
     *   // setState((prevState) => ({
     *   //   direction: prevState.activeIndex < index ? "left" : "right",
     *   //   activeIndex: index,
     *   // }))
     *
     *   // enabling `persistent` makes it so that the `<Slide>` never unmounts so
     *   // that state can be maintained while it is not active.
     *   return (
     *     <SlideContainer direction={direction}>
     *       <Slide active={activeIndex === 0} persistent>
     *         Slide 1
     *       </Slide>
     *       <Slide active={activeIndex === 1} persistent>
     *         Slide 2
     *       </Slide>
     *       <Slide active={activeIndex === 2} persistent>
     *         Slide 3
     *       </Slide>
     *     </SlideContainer>
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0
     */
    export const SlideContainer = forwardRef<HTMLDivElement, SlideContainerProps>(
      function SlideContainer(props, ref) {
        const { className, direction, children, ...remaining } = props;

        return (
          <div
            {...remaining}
            ref={ref}
            className={slideContainer({ className, direction })}
          >
            {children}
          </div>
        );
      }
    );