# All files / src/box styles.ts

100% Statements 179/179
87.5% Branches 7/8
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
    1x
    1x
    1x
    1x
    1x
    1x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
    342x
     import { cnb } from "cnbuilder";
    import { bem } from "../utils/bem.js";

    const styles = bem("rmd-box");

    declare module "react" {
      interface CSSProperties {
        "--rmd-box-gap"?: string | number;
        "--rmd-box-padding"?: string | number;
        "--rmd-box-item-min-size"?: string | number;
        "--rmd-box-columns"?: string | number;
      }
    }

    /**
     * @remarks \@since 6.0.0
     */
    export type BoxAlignItems =
      | "start"
      | "flex-start"
      | "center"
      | "end"
      | "flex-end"
      | "stretch";

    /**
     * @remarks \@since 6.0.0
     */
    export type BoxJustifyContent =
      | BoxAlignItems
      | "space-around"
      | "space-between"
      | "space-evenly";

    /**
     * @remarks \@since 6.0.0
     */
    export type BoxFlexDirection = "row" | "column";

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

      /**
       * Set this to `true` to use `display: grid` instead of `display: flex`.
       *
       * @defaultValue `false`
       */
      grid?: boolean;

      /**
       * Set this to `true` to apply `width: 100%`. This can be useful when using
       * nested box layouts.
       *
       * @defaultValue `false`
       */
      fullWidth?: boolean;

      /**
       * Set this to `true` to set `flex-wrap: nowrap`.
       *
       * @defaultValue `false`
       */
      disableWrap?: boolean;

      /**
       * Set this to `true` to disable the default padding.
       *
       * @defaultValue `false`
       */
      disablePadding?: boolean;

      /**
       * This is a grid configuration
       * This should match one of the names in the `$grids` map. So for example:
       *
       * ```scss
       * @use "react-md" with (
       *   $grids: (
       *     small: (
       *       min: 5rem
       *     ),
       *     medium: (
       *       min: 7rem,
       *       gap: 0.5rem,
       *       padding: 2rem,
       *     ),
       *   ),
       * );
       * ```
       *
       * The `gridName` should be `"small" | "medium"`.
       *
       * @defaultValue `""`
       */
      gridName?: string;

      /**
       * @defaultValue `"fit"`
       */
      gridColumns?: "fit" | "fill" | number;

      /**
       * @defaultValue `""`
       */
      align?: BoxAlignItems;

      /**
       * The default value is really `center` or whatever the `$default-align-items` is set to.
       *
       * @defaultValue `""`
       */
      justify?: BoxJustifyContent;

      /**
       * Set this to `true` to set `flex-direction: column` which will stack all
       * items in the box.
       *
       * @defaultValue `false`
       */
      stacked?: boolean;

      /**
       * Set this to `true` to reverse the `flex-direction`. i.e.
       * - `flex-direction: row-reverse`
       * - `flex-direction: column-reverse`
       *
       * @defaultValue `false`
       */
      reversed?: boolean;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export function box(options: BoxOptions = {}): string {
      const {
        className,
        align = "",
        grid = false,
        gridName = "",
        gridColumns = "fit",
        justify = "",
        stacked = false,
        reversed,
        fullWidth = false,
        disableWrap = false,
        disablePadding = false,
      } = options;

      return cnb(
        styles({
          wrap: !disableWrap,
          padded: !disablePadding,
          column: stacked && !reversed,
          reverse: !stacked && reversed,
          "column-reverse": stacked && reversed,
          "full-width": fullWidth,
          grid,
          "grid-fill": gridColumns === "fill",
          "grid-columns": typeof gridColumns === "number",
          [gridName]: grid && gridName,
          "align-start": align === "start" || align === "flex-start",
          "align-center": align === "center",
          "align-end": align === "end" || align === "flex-end",
          "align-stretch": align === "stretch",
          "justify-center": justify === "center",
          "justify-start": justify === "start" || justify === "flex-start",
          "justify-end": justify === "end" || justify === "flex-end",
          "justify-stretch": justify === "stretch",
          "justify-around": justify === "space-around",
          "justify-between": justify === "space-between",
          "justify-evenly": justify === "space-evenly",
        }),
        className
      );
    }