# All files / src/Title Title.tsx

100% Statements 35/35
100% Branches 11/11
100% Functions 8/8
100% Lines 29/29

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
    11818x
    18x
    18x

    18x

    20x

    18x
    4x

    4x

    4x
    4x
    4x
    4x
    4x
    4x
    12x

    4x
    2x

    2x

    18x

    1x

    18x

    1x

    18x

    1x

    18x

    1x

    18x

    1x

    18x

     import React from 'react'
    import { addSubcomponents } from '../_utils/addSubcomponents'
    import { slotify } from '../_utils/slotify'
    import { DivAttributes } from '../_utils/types'
    import {
      StyledAssets,
      StyledSubtext,
      StyledText,
      StyledTitle,
      StyledTitleActions,
      StyledTitleContent,
      StyledTitlePill,
    } from './Title.styles'
    import { TitleProps } from './Title.types'

    function isValidElement(element: unknown) {
      return Boolean(element)
    }

    const Title_ = React.forwardRef<HTMLDivElement, TitleProps>(function Title_(
      { children, ...props },
      ref
    ) {
      const { pills, text, subtext, actions, assets } = slotify(children, {
        pills: TitlePills,
        text: TitleText,
        subtext: TitleSubtext,
        actions: TitleActions,
        assets: TitleAssets,
      })

      const hasActions = isValidElement(actions.slotChildren)
      const hasAssets = isValidElement(assets.slotChildren)
      const hasPills = isValidElement(pills.slotChildren)
      const hasText = isValidElement(text.slotChildren)
      const hasSubtext = isValidElement(subtext.slotChildren)
      const isEmpty = [hasActions, hasAssets, hasPills, hasText, hasSubtext].every(
        (el) => !el
      )

      if (isEmpty) {
        return null
      }

      return (
        <StyledTitle ref={ref} {...props}>
          {hasAssets && (
            <StyledAssets ref={assets.slotRef} {...assets.slotProps}>
              {assets.slotChildren}
            </StyledAssets>
          )}
          <StyledTitleContent $hasActions={hasActions}>
            {hasText && (
              <StyledText ref={text.slotRef} {...text.slotProps}>
                {text.slotChildren}
              </StyledText>
            )}
            {hasPills && (
              <StyledTitlePill ref={pills.slotRef} {...pills.slotProps}>
                {pills.slotChildren}
              </StyledTitlePill>
            )}
            {hasSubtext && (
              <StyledSubtext
                ref={subtext.slotRef}
                {...subtext.slotProps}
                color="gray45"
              >
                {subtext.slotChildren}
              </StyledSubtext>
            )}
          </StyledTitleContent>
          {hasActions && (
            <StyledTitleActions ref={actions.slotRef} {...actions.slotProps}>
              {actions.slotChildren}
            </StyledTitleActions>
          )}
        </StyledTitle>
      )
    })

    const TitlePills = React.forwardRef<HTMLDivElement, DivAttributes>(
      function TitlePills() {
        return <div />
      }
    )
    const TitleText = React.forwardRef<HTMLDivElement, DivAttributes>(
      function TitleText() {
        return <div />
      }
    )
    const TitleSubtext = React.forwardRef<HTMLDivElement, DivAttributes>(
      function TitleSubtext() {
        return <div />
      }
    )
    const TitleActions = React.forwardRef<HTMLDivElement, DivAttributes>(
      function TitleActions() {
        return <div />
      }
    )
    const TitleAssets = React.forwardRef<HTMLDivElement, DivAttributes>(
      function TitleAssets() {
        return <div />
      }
    )

    export const Title = addSubcomponents(
      {
        Pills: TitlePills,
        Text: TitleText,
        Subtext: TitleSubtext,
        Actions: TitleActions,
        Assets: TitleAssets,
      },
      Title_
    )