# All files / src/Command Command.tsx

84.62% Statements 44/52
73.91% Branches 17/23
55.56% Functions 5/9
91.67% Lines 44/48

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

    7x
    7x

    7x

    7x

    7x

    7x

    6x
    6x
    6x
    6x

    6x

    6x

    20x
    5x
    15x
    3x

    12x

    6x
    6x

    6x

    6x

    6x

    6x
    6x

    3x

    7x

    3x

    7x

    5x

    7x

    7x

    7x
     import * as React from 'react'
    import { More as MoreIcon } from '@fluent-ui/icons' // TODO treeShaking
    import { useClickOutside, useReveal, usePopper } from '@fluent-ui/hooks' // TODO treeShaking
    import { omit } from '../utils'
    import Box from '../Box'
    import { styles } from './Command.styled'
    import Secondary from './components/Secondary'
    import Content from './components/Content'
    import Portal from '../Portal'
    import Item from '../Item'
    import Transition from '../Transition'
    import {
      CommandProps,
      CommandContainer,
      CommandChild,
      CommandType,
      CommandPropTypes,
      CommandClassProps
    } from './Command.type'
    import { createUseStyles } from 'react-jss'
    import classNames from 'classnames'

    export const name = 'Command'

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

    export const CommandContext = React.createContext(false)

    const Command: React.FC<CommandProps> = React.forwardRef<HTMLDivElement, CommandProps>(
      (props, ref): React.ReactElement => {
        const {
          as = 'div',
          className: classNameProp,
          acrylic = false,
          reveal = false,
          children,
          ...rest
        } = props
        const classes = useStyles(props)
        const className = classNames(classes.root, classNameProp)

        const container: CommandContainer = {
          content: [],
          standard: [],
          secondary: []
        }
        React.Children.forEach(
          children,
          (child: CommandChild): void => {
            if (child.type.displayName! === 'FCommandContent') {
              container.content.push(child)
            } else if (child.type.displayName === 'FCommandSecondary') {
              container.secondary = child.props.children
            } else {
              container.standard.push(child)
            }
          }
        )
        // Reveal does not take effect when using acrylic
        const _reveal = acrylic ? false : reveal
        const [RevealWrapper] = useReveal()
        // Secondary Popup related
        const [secondaryVisible, setSecondaryVisible] = React.useState(false)
        function handleSecondaryVisible(): void {
          if (secondaryVisible) return
          setSecondaryVisible((visible: boolean): boolean => !visible)
        }
        // Click on the area outside the More menu to close the More menu.
        const [referenceRef, popperRef] = usePopper<HTMLDivElement, HTMLDivElement>({
          placement: 'bottom'
        })
        useClickOutside(
          popperRef,
          (event: MouseEvent | TouchEvent): void => {
            // @ts-ignore
            if (!referenceRef.current || referenceRef.current.contains(event.target)) return
            setSecondaryVisible((visible: boolean): boolean => !visible)
          }
        )
        const otherProps = omit(rest, ['display', 'backgroundColor', 'color'])
        return (
          <Box
            ref={ref}
            as={as}
            acrylic={acrylic}
            className={className}
            backgroundColor="standard.light2"
            {...otherProps}
          >
            <CommandContext.Provider value={_reveal as boolean}>
              {!!container.content.length && <div className={classes.content}>{container.content}</div>}
              <div className={classes.primary}>
                {_reveal
                  ? container.standard.map(
                      (child, i): React.ReactElement => <RevealWrapper key={i}>{child}</RevealWrapper>
                    )
                  : container.standard}
              </div>
              {!!container.secondary.length &&
                (_reveal ? (
                  <RevealWrapper>
                    <Item
                      ref={referenceRef}
                      style={{ height: '100%' }}
                      onClick={handleSecondaryVisible}
                      prefix={<MoreIcon />}
                    />
                  </RevealWrapper>
                ) : (
                  <Item ref={referenceRef} onClick={handleSecondaryVisible} prefix={<MoreIcon />} />
                ))}

              <Portal>
                <Transition visible={secondaryVisible} wrapper={true} mountOnEnter unmountOnExit>
                  <Box
                    ref={popperRef}
                    acrylic={acrylic}
                    backgroundColor="standard.light2"
                    className={classes.secondaryRoot}
                  >
                    {container.secondary}
                  </Box>
                </Transition>
              </Portal>
            </CommandContext.Provider>
          </Box>
        )
      }
    )

    Object.defineProperty(Command, 'Secondary', {
      get(): typeof Secondary {
        return Secondary
      }
    })
    Object.defineProperty(Command, 'Content', {
      get(): typeof Content {
        return Content
      }
    })

    Command.displayName = `F${name}`

    Command.propTypes = CommandPropTypes

    export default Command as CommandType