All files / Text Text.tsx

100% Statements 109/109
96.87% Branches 279/288
100% Functions 52/52
100% Lines 103/103

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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449  35x   35x 35x 35x 35x             35x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x 777x   777x   777x 414x   363x 363x 363x     777x 777x   7x                                     7x                                     7x                                       7x                                       9x                                       7x                                       126x                                       385x                                       28x                                       194x                                           777x                                   35x   35x       35x   7x           7x         7x 7x 7x     35x   7x           7x         7x 7x 7x     35x   7x           7x         7x 7x 7x     35x   7x           7x         7x 7x 7x     35x   9x           9x         9x 9x 9x     35x   7x           7x         7x 7x 7x     35x   126x           126x         126x 126x 126x     35x   28x           28x         28x 28x 28x     35x   194x           194x         194x 194x 194x     35x   385x           385x         385x 385x 385x      
import React from "react";
import styled from "styled-components";
import { TextProps, TextStyleProps } from "./Text.types";
import { Helmet } from "react-helmet";
import { useTheme } from "../ThemeProvider/ThemeProvider";
import { getColorPalette } from "../../helpers/helpers";
import colorTokens from "../../tokens/colors.json";
 
interface StyledProps {
  $disableColor?: boolean;
  $color?: string;
}
 
const Text = ({
  variant = "div",
  strong,
  children,
  align = "center",
  color = colorTokens.default.primary.main,
  preciseColor,
  wrap = false,
  size = "medium",
  disableColor = false,
  fontSize,
  lineHeight,
  style,
  className,
  fontFamily,
  "data-testid": dataTestId,
  ...props
}: TextProps) => {
  const theme = useTheme().theme;
  let _styledProps: StyledProps;
  if (disableColor) {
    _styledProps = { $disableColor: disableColor };
  } else {
    const colorPalette = getColorPalette(theme, color);
    const _color = preciseColor ?? colorPalette![color].accentScale[10];
    _styledProps = { $disableColor: disableColor, $color: _color };
  }
 
  const _Text = () => {
    switch (variant) {
      case "h1":
        return (
          <StyledH1
            $strong={strong}
            $align={align}
            $wrap={wrap}
            $size={size}
            $fontSize={fontSize}
            $lineHeight={lineHeight}
            $fontFamily={fontFamily}
            style={style}
            className={className}
            data-testid={dataTestId}
            {...props}
            {..._styledProps}
          >
            {children}
          </StyledH1>
        );
      case "h2":
        return (
          <StyledH2
            $strong={strong}
            $align={align}
            $wrap={wrap}
            $size={size}
            $fontSize={fontSize}
            $lineHeight={lineHeight}
            $fontFamily={fontFamily}
            style={style}
            className={className}
            data-testid={dataTestId}
            {...props}
            {..._styledProps}
          >
            {children}
          </StyledH2>
        );
      case "h3":
        return (
          <StyledH3
            $variant={variant}
            $strong={strong}
            $align={align}
            $wrap={wrap}
            $size={size}
            $fontSize={fontSize}
            $lineHeight={lineHeight}
            $fontFamily={fontFamily}
            style={style}
            className={className}
            data-testid={dataTestId}
            {...props}
            {..._styledProps}
          >
            {children}
          </StyledH3>
        );
      case "h4":
        return (
          <StyledH4
            $variant={variant}
            $strong={strong}
            $align={align}
            $wrap={wrap}
            $size={size}
            $fontSize={fontSize}
            $lineHeight={lineHeight}
            $fontFamily={fontFamily}
            style={style}
            className={className}
            data-testid={dataTestId}
            {...props}
            {..._styledProps}
          >
            {children}
          </StyledH4>
        );
      case "h5":
        return (
          <StyledH5
            $variant={variant}
            $strong={strong}
            $align={align}
            $wrap={wrap}
            $size={size}
            $fontSize={fontSize}
            $lineHeight={lineHeight}
            $fontFamily={fontFamily}
            style={style}
            className={className}
            data-testid={dataTestId}
            {...props}
            {..._styledProps}
          >
            {children}
          </StyledH5>
        );
      case "h6":
        return (
          <StyledH6
            $variant={variant}
            $strong={strong}
            $align={align}
            $wrap={wrap}
            $size={size}
            $fontSize={fontSize}
            $lineHeight={lineHeight}
            $fontFamily={fontFamily}
            style={style}
            className={className}
            data-testid={dataTestId}
            {...props}
            {..._styledProps}
          >
            {children}
          </StyledH6>
        );
      case "div":
        return (
          <StyledDiv
            $variant={variant}
            $strong={strong}
            $align={align}
            $wrap={wrap}
            $size={size}
            $fontSize={fontSize}
            $lineHeight={lineHeight}
            $fontFamily={fontFamily}
            style={style}
            className={className}
            data-testid={dataTestId}
            {...props}
            {..._styledProps}
          >
            {children}
          </StyledDiv>
        );
      case "paragraph":
        return (
          <StyledP
            $variant={variant}
            $strong={strong}
            $align={align}
            $wrap={wrap}
            $size={size}
            $fontSize={fontSize}
            $lineHeight={lineHeight}
            $fontFamily={fontFamily}
            style={style}
            className={className}
            data-testid={dataTestId}
            {...props}
            {..._styledProps}
          >
            {children}
          </StyledP>
        );
      case "label":
        return (
          <StyledLabel
            $variant={variant}
            $strong={strong}
            $align={align}
            $wrap={wrap}
            $size={size}
            $fontSize={fontSize}
            $lineHeight={lineHeight}
            $fontFamily={fontFamily}
            style={style}
            className={className}
            data-testid={dataTestId}
            {...props}
            {..._styledProps}
          >
            {children}
          </StyledLabel>
        );
      case "span":
        return (
          <StyledSpan
            $variant={variant}
            $strong={strong}
            $align={align}
            $wrap={wrap}
            $size={size}
            $fontSize={fontSize}
            $lineHeight={lineHeight}
            $fontFamily={fontFamily}
            style={style}
            className={className}
            data-testid={dataTestId}
            {...props}
            {..._styledProps}
          >
            {children}
          </StyledSpan>
        );
    }
  };
 
  return (
    <>
      <Helmet>
        <link rel="preconnect" href="https://fonts.googleapis.com" />
        <link
          rel="preconnect"
          href="https://fonts.gstatic.com"
          crossOrigin=""
        />
        <link
          href="https://fonts.googleapis.com/css2?family=Alegreya:wght@400;700&family=Varela+Round&display=swap"
          rel="stylesheet"
        />
      </Helmet>
      {_Text()}
    </>
  );
};
export default Text;
 
const commonStyles = `
  padding: 0;
  margin: 0;
`;
const StyledH1 = styled.h1<TextStyleProps>`
  font-size: ${(props) =>
    (props.$fontSize ?? (props.$size === "large"
      ? "2.5rem"
      : props.$size === "medium"
        ? "2.25rem"
        : "2rem"))};
  line-height: ${(props) =>
    (props.$lineHeight ?? props.$size === "large")
      ? "4rem"
      : props.$size === "medium"
        ? "3.5rem"
        : "3rem"};
  font-weight: ${(props) => (props.$strong ? "700" : "400")};
  ${(props) => !props.$disableColor && `color: ${props.$color};`}
  font-family: ${(props) => props.$fontFamily ?? '"Alegreya, serif"'};
  ${commonStyles}
`;
const StyledH2 = styled.h2<TextStyleProps>`
  font-size: ${(props) =>
    (props.$fontSize ?? (props.$size === "large"
      ? "2.25rem"
      : props.$size === "medium"
        ? "2rem"
        : "1.75rem"))};
  line-height: ${(props) =>
    (props.$lineHeight ?? props.$size === "large")
      ? "3.5rem"
      : props.$size === "medium"
        ? "3rem"
        : "2.5rem"};
  font-weight: ${(props) => (props.$strong ? "700" : "400")};
  font-family: ${(props) => props.$fontFamily ?? '"Alegreya, serif"'};
  ${(props) => !props.$disableColor && `color: ${props.$color};`}
  ${commonStyles}
`;
const StyledH3 = styled.h3<TextStyleProps>`
  font-size: ${(props) =>
    (props.$fontSize ?? (props.$size === "large"
      ? "2rem"
      : props.$size === "medium"
        ? "1.75rem"
        : "1.5rem"))};
  line-height: ${(props) =>
    (props.$lineHeight ?? props.$size === "large")
      ? "3rem"
      : props.$size === "medium"
        ? "2.5rem"
        : "2rem"};
  font-weight: ${(props) => (props.$strong ? "700" : "400")};
  font-family: ${(props) => props.$fontFamily ?? '"Alegreya, serif"'};
  ${(props) => !props.$disableColor && `color: ${props.$color};`}
  ${commonStyles}
`;
const StyledH4 = styled.h4<TextStyleProps>`
  font-size: ${(props) =>
    (props.$fontSize ?? (props.$size === "large"
      ? "1.75rem"
      : props.$size === "medium"
        ? "1.5rem"
        : "1.25rem"))};
  line-height: ${(props) =>
    (props.$lineHeight ?? props.$size === "large")
      ? "2.5rem"
      : props.$size === "medium"
        ? "2rem"
        : "1.75rem"};
  font-weight: ${(props) => (props.$strong ? "700" : "400")};
  font-family: ${(props) => props.$fontFamily ?? '"Alegreya, serif"'};
  ${(props) => !props.$disableColor && `color: ${props.$color};`}
  ${commonStyles}
`;
const StyledH5 = styled.h5<TextStyleProps>`
  font-size: ${(props) =>
    (props.$fontSize ?? (props.$size === "large"
      ? "1.5rem"
      : props.$size === "medium"
        ? "1.25rem"
        : "1rem"))};
  line-height: ${(props) =>
    (props.$lineHeight ?? props.$size === "large")
      ? "2rem"
      : props.$size === "medium"
        ? "1.75rem"
        : "1.5rem"};
  font-weight: ${(props) => (props.$strong ? "700" : "400")};
  font-family: ${(props) => props.$fontFamily ?? '"Alegreya, serif"'};
  ${(props) => !props.$disableColor && `color: ${props.$color};`}
  ${commonStyles}
`;
const StyledH6 = styled.h6<TextStyleProps>`
  font-size: ${(props) =>
    (props.$fontSize ?? (props.$size === "large"
      ? "1.25rem"
      : props.$size === "medium"
        ? "1rem"
        : "0.875rem"))};
  line-height: ${(props) =>
    (props.$lineHeight ?? props.$size === "large")
      ? "1.75rem"
      : props.$size === "medium"
        ? "1.5rem"
        : "1.25rem"};
  font-weight: ${(props) => (props.$strong ? "700" : "400")};
  font-family: ${(props) => props.$fontFamily ?? '"Alegreya, serif"'};
  ${(props) => !props.$disableColor && `color: ${props.$color};`}
  ${commonStyles}
`;
const StyledDiv = styled.div<TextStyleProps>`
  font-size: ${(props) =>
    (props.$fontSize ?? (props.$size === "large"
      ? "1.25rem"
      : props.$size === "medium"
        ? "1rem"
        : "0.875rem"))};
  line-height: ${(props) =>
    (props.$lineHeight ?? props.$size === "large")
      ? "1.75rem"
      : props.$size === "medium"
        ? "1.5rem"
        : "1.25rem"};
  font-weight: ${(props) => (props.$strong ? "700" : "400")};
  font-family: ${(props) => props.$fontFamily ?? `'Varela Round', sans-serif`};
  ${(props) => !props.$disableColor && `color: ${props.$color};`}
  ${commonStyles}
`;
const StyledLabel = styled.label<TextStyleProps>`
  font-size: ${(props) =>
    (props.$fontSize ?? (props.$size === "large"
      ? "1.25rem"
      : props.$size === "medium"
        ? "1rem"
        : "0.875rem"))};
  line-height: ${(props) =>
    (props.$lineHeight ?? props.$size === "large")
      ? "1.75rem"
      : props.$size === "medium"
        ? "1.5rem"
        : "1.25rem"};
  font-weight: ${(props) => (props.$strong ? "700" : "400")};
  font-family: ${(props) => props.$fontFamily ?? `'Varela Round', sans-serif`};
  ${(props) => !props.$disableColor && `color: ${props.$color};`}
  ${commonStyles}
`;
const StyledSpan = styled.span<TextStyleProps>`
  font-size: ${(props) =>
    (props.$fontSize ?? (props.$size === "large"
      ? "1.25rem"
      : props.$size === "medium"
        ? "1rem"
        : "0.875rem"))};
  line-height: ${(props) =>
    (props.$lineHeight ?? props.$size === "large")
      ? "1.75rem"
      : props.$size === "medium"
        ? "1.5rem"
        : "1.25rem"};
  font-weight: ${(props) => (props.$strong ? "700" : "400")};
  font-family: ${(props) => props.$fontFamily ?? `'Varela Round', sans-serif`};
  ${(props) => !props.$disableColor && `color: ${props.$color};`}
  ${commonStyles}
`;
const StyledP = styled.p<TextStyleProps>`
  font-size: ${(props) =>
    (props.$fontSize ?? (props.$size === "large"
      ? "1.25rem"
      : props.$size === "medium"
        ? "1rem"
        : "0.875rem"))};
  line-height: ${(props) =>
    (props.$lineHeight ?? props.$size === "large")
      ? "1.75rem"
      : props.$size === "medium"
        ? "1.5rem"
        : "1.25rem"};
  font-weight: ${(props) => (props.$strong ? "700" : "400")};
  font-family: ${(props) => props.$fontFamily ?? `'Varela Round', sans-serif`};
  ${(props) => !props.$disableColor && `color: ${props.$color};`}
  ${commonStyles}
`;