# All files / src/Navigation Navigation.tsx

85.45% Statements 47/55
58.33% Branches 14/24
50% Functions 6/12
85.45% Lines 47/55

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
    1807x
    7x
    7x
    7x

    7x

    7x
    7x

    7x
    7x
    7x
    7x

    7x

    7x

    7x

    7x

    7x
    7x

    7x

    7x

    13x

    13x

    13x

    7x
    7x
    7x

    7x

    7x

    7x

    13x
    13x

    7x

    7x

    7x

    7x

    7x
    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x

    7x
     import * as React from 'react'
    import classNames from 'classnames'
    import { createUseStyles } from '@fluent-ui/styles'
    import { styles } from './Navigation.styled'
    import { Theme } from '../styles'
    import {
      NavigationProps,
      NavigationID,
      NavigationContainer,
      NavigationChild,
      NavigationType,
      NavigationPropTypes,
      NavigationClassProps
    } from './Navigation.type'
    import { useReveal } from '@fluent-ui/hooks' // TODO treeShaking
    import { omit } from '../utils'

    import Header from './components/Header'
    import Footer from './components/Footer'
    import Content from './components/Content'
    import Box from '../Box'

    export const NavigationContext = React.createContext<{
      value: NavigationID
      onChange: (id: NavigationID) => void
      expanded: boolean
      reveal: boolean
      acrylic: boolean
      horizontal: boolean
    }>({
      value: '',
      onChange: (): void => {},
      expanded: true,
      reveal: false,
      acrylic: false,
      horizontal: false
    })

    export const name = 'Navigation'

    const useStyles = createUseStyles<Theme, NavigationClassProps>(styles, { name })

    const Navigation: React.FC<NavigationProps> = React.forwardRef<HTMLDivElement, NavigationProps>(
      (props, ref): React.ReactElement => {
        const {
          as = 'div',
          className: classNameProp,
          horizontal = false,
          expanded = true,
          acrylic = false,
          reveal,
          value,
          onChange,
          children,
          ...rest
        } = props

        const container: NavigationContainer = {
          header: [],
          footer: [],
          content: []
        }
        React.Children.forEach(
          children,
          (child: NavigationChild): void => {
            Iif (child.type && child.type.displayName === 'FNavigationHeader') {
              container.header.push(child)
            } else Iif (child.type && child.type.displayName === 'FNavigationFooter') {
              container.footer.push(child)
            } else {
              container.content.push(child)
            }
          }
        )

        const _reveal = acrylic ? false : reveal
        const [RevealWrapper] = useReveal(66)
        const revealHeader = React.useMemo(
          (): React.ReactElement[] =>
            container.header.map(
              (child, i): React.ReactElement => <RevealWrapper key={i}>{child}</RevealWrapper>
            ),
          [] // eslint-disable-line
        )
        const revealContent = React.useMemo(
          (): React.ReactElement[] =>
            container.content.map(
              (child, i): React.ReactElement => {
                Eif (child.type && child.type.displayName === 'FItem') {
                  return <RevealWrapper key={i}>{child}</RevealWrapper>
                }
                return child
              }
            ),
          [] // eslint-disable-line
        )
        const revealFooter = React.useMemo(
          (): React.ReactElement[] =>
            container.footer.map(
              (child, i): React.ReactElement => <RevealWrapper key={i}>{child}</RevealWrapper>
            ),
          [] // eslint-disable-line
        )

        const contextValue = {
          value: value as NavigationID,
          onChange: onChange as (id: NavigationID) => void,
          expanded: expanded as boolean,
          reveal: _reveal as boolean,
          acrylic: acrylic as boolean,
          horizontal: horizontal as boolean
        }
        const others = omit(rest, ['backgroundColor', 'color'])

        const classes = useStyles(props)
        const className = classNames(
          classes.root,
          {
            [classes.horizontal]: horizontal,
            [classes.expanded]: expanded
          },
          classNameProp
        )
        const headerClassName = classNames(classes.header, {
          [classes.headerHorizontal]: horizontal
        })
        const contentClassName = classNames(classes.content, {
          [classes.contentHorizontal]: horizontal
        })
        const footerClassName = classNames(classes.footer, {
          [classes.footerHorizontal]: horizontal
        })

        return (
          <Box
            className={className}
            as={as}
            ref={ref}
            acrylic={acrylic}
            backgroundColor="standard.light2"
            {...others}
          >
            <NavigationContext.Provider value={contextValue}>
              <Box className={headerClassName}>{_reveal ? revealHeader : container.header}</Box>
              <Box className={contentClassName}>{_reveal ? revealContent : container.content}</Box>
              <Box className={footerClassName}>{_reveal ? revealFooter : container.footer}</Box>
            </NavigationContext.Provider>
          </Box>
        )
      }
    )

    Object.defineProperty(Navigation, 'Header', {
      get(): typeof Header {
        return Header
      }
    })
    Object.defineProperty(Navigation, 'Footer', {
      get(): typeof Footer {
        return Footer
      }
    })
    Object.defineProperty(Navigation, 'Content', {
      get(): typeof Content {
        return Content
      }
    })

    Navigation.displayName = `F${name}`

    Navigation.propTypes = NavigationPropTypes

    Navigation.defaultProps = {
      horizontal: false,
      expanded: true,
      acrylic: false
    }

    export default Navigation as NavigationType