# All files / src/Navigation Item.tsx

88.24% Statements 30/34
60.61% Branches 20/33
90% Functions 9/10
93.75% Lines 30/32

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
    15710x
    10x
    10x

    10x

    12x

    12x

    12x

    10x

    10x

    10x
    12x
    12x

    12x

    10x

    12x
    12x

    12x

    12x

    22x

    12x

    10x

    12x

    12x
    12x
    6x
    4x
    4x

    12x

    10x

    10x
     import * as React from 'react'
    import { styled, css, variant, th } from '../styles/styled'
    import { NavigationContext } from './Navigation'
    import * as CSS from 'csstype'

    export type ID = string | number

    interface ItemProps extends Omit<React.ComponentPropsWithoutRef<'div'>, 'id'>, StyledProps {
      id?: ID
      title?: string
      onClick?: React.MouseEventHandler<HTMLDivElement>
      children: React.ReactNode
    }

    type Child = React.ReactChild | any

    const StyledItemWrapper = styled.div<{
      reveal: boolean
    }>`
      position: relative;
      cursor: pointer;
      text-decoration: none;
      min-height: 40px;
      height: 40px;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 12px;
      box-sizing: border-box;
      transition: ${th.transition('navigation')};
      color: ${th.color('black.default')};
      background-color: ${({ reveal }): CSS.ColorProperty =>
        reveal ? th.color('standard.light2') : 'transparent'};
      &:hover {
        background-color: ${({ reveal }): CSS.ColorProperty =>
          reveal ? th.color('standard.light1') : th.color('standard.transparent1')};
      }
      &:active {
        color: ${th.color('black.default')};
        background-color: ${({ reveal }): CSS.ColorProperty =>
          reveal ? th.color('standard.dark1') : th.color('standard.transparent2')};
      }
    `
    const StyledItemActiveBar = styled.div<{
      active: boolean
      horizontal: boolean
    }>`
      position: absolute;
      background-color: ${th.color('primary.default')};
      transition: ${th.transition('navigation')};
      ${variant({
        prop: 'horizontal',
        variants: {
          true: css`
            width: 80%;
            height: 4px;
            left: 10%;
            bottom: 0;
          `,
          false: css`
            width: 6px;
            height: 24px;
            left: 0;
            top: 50%;
            margin-top: -12px;
          `
        }
      })}
      ${variant({
        prop: 'active',
        variants: {
          true: css`
            transform: scale3d(1, 1, 1);
          `,
          false: css`
            transform: scale3d(0, 0, 0);
          `
        }
      })}
    `
    const StyledItemIconWrapper = styled.div`
      height: 100%;
      transition: ${th.transition('navigation')};
    `
    const StyledItemTextWrapper = styled.div<{ expanded: boolean }>`
      width: ${({ expanded }): string => (expanded ? 'auto' : '0px')};
      opacity: ${({ expanded }): number => (expanded ? 1 : 0)};
      padding-left: ${({ children, expanded }): string =>
        children ? (expanded ? '12px' : '0px') : '0px'};
      flex: 1;
      align-items: center;
      overflow: hidden;
      white-space: nowrap;
      transition: ${th.transition('navigation')};
    `

    const Item: React.FC<ItemProps> = ({
      id,
      onClick,
      children,
      ...rest
    }: ItemProps): React.ReactElement => {
      const container: {
        icon: any
        content: any
      } = {
        icon: null,
        content: null
      }
      React.Children.forEach(
        children,
        (child: Child): void => {
          if (
            child.type &&
            child.type.displayName &&
            String.prototype.includes.call(child.type.displayName, 'FIcon')
          ) {
            container.icon = React.cloneElement(child, {
              style: { width: 16, height: 16 }
            })
          } else {
            container.content = React.cloneElement(child, {
              style: { fontSize: 14 }
            })
          }
        }
      )

      // handle active item
      const { value: activeID, onChange, expanded, reveal, horizontal } = React.useContext(
        NavigationContext
      )
      function handleItemClick(e: React.MouseEvent<HTMLDivElement>): void {
        onClick && onClick(e)
        onChange && onChange(id as ID)
      }
      const [active, setActive] = React.useState(false)
      React.useEffect((): void => {
        if (id) {
          Iif (activeID === id) setActive(true)
          else Iif (activeID && activeID !== id) setActive(false)
        }
      }, [activeID, id])

      return (
        <StyledItemWrapper onClick={handleItemClick} reveal={reveal} {...rest}>
          {!!id && <StyledItemActiveBar active={active} horizontal={horizontal} />}
          <StyledItemIconWrapper>{container.icon}</StyledItemIconWrapper>
          <StyledItemTextWrapper expanded={expanded}>{container.content}</StyledItemTextWrapper>
        </StyledItemWrapper>
      )
    }

    Item.displayName = 'FNavigationItem'

    export default Item