|
| 1 | +/** |
| 2 | + * Copyright Zendesk, Inc. |
| 3 | + * |
| 4 | + * Use of this source code is governed under the Apache License, Version 2.0 |
| 5 | + * found at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import React, { forwardRef, useMemo } from 'react'; |
| 9 | +import PropTypes from 'prop-types'; |
| 10 | +import { useText } from '@zendeskgarden/react-theming'; |
| 11 | +import ClockIcon12 from '@zendeskgarden/svg-icons/src/12/clock-stroke.svg'; |
| 12 | +import ClockIcon16 from '@zendeskgarden/svg-icons/src/16/clock-stroke.svg'; |
| 13 | +import ArrowLeftIcon12 from '@zendeskgarden/svg-icons/src/12/arrow-left-sm-stroke.svg'; |
| 14 | +import ArrowLeftIcon16 from '@zendeskgarden/svg-icons/src/16/arrow-left-sm-stroke.svg'; |
| 15 | + |
| 16 | +import { IStatusIndicatorProps, STATUS } from '../types'; |
| 17 | +import { |
| 18 | + StyledStandaloneStatusIndicator, |
| 19 | + StyledStandaloneStatusCaption, |
| 20 | + StyledStandaloneStatus |
| 21 | +} from '../styled'; |
| 22 | + |
| 23 | +/** |
| 24 | + * @extends HTMLAttributes<HTMLElement> |
| 25 | + */ |
| 26 | +const StatusIndicatorComponent = forwardRef<HTMLElement, IStatusIndicatorProps>( |
| 27 | + ({ children, type, isCompact, 'aria-label': label, ...props }, ref) => { |
| 28 | + let ClockIcon = ClockIcon16; |
| 29 | + let ArrowLeftIcon = ArrowLeftIcon16; |
| 30 | + |
| 31 | + if (isCompact) { |
| 32 | + ClockIcon = ClockIcon12; |
| 33 | + ArrowLeftIcon = ArrowLeftIcon12; |
| 34 | + } |
| 35 | + |
| 36 | + const defaultLabel = useMemo(() => ['status'].concat(type || []).join(': '), [type]); |
| 37 | + const ariaLabel = useText( |
| 38 | + StatusIndicatorComponent, |
| 39 | + { 'aria-label': label }, |
| 40 | + 'aria-label', |
| 41 | + defaultLabel |
| 42 | + ); |
| 43 | + |
| 44 | + return ( |
| 45 | + <StyledStandaloneStatus role="status" ref={ref} {...props}> |
| 46 | + <StyledStandaloneStatusIndicator |
| 47 | + role="img" |
| 48 | + type={type} |
| 49 | + size={isCompact ? 'small' : 'medium'} |
| 50 | + aria-label={ariaLabel} |
| 51 | + > |
| 52 | + {type === 'away' ? <ClockIcon data-icon-status={type} aria-hidden="true" /> : null} |
| 53 | + {type === 'transfers' ? ( |
| 54 | + <ArrowLeftIcon data-icon-status={type} aria-hidden="true" /> |
| 55 | + ) : null} |
| 56 | + </StyledStandaloneStatusIndicator> |
| 57 | + {children && <StyledStandaloneStatusCaption>{children}</StyledStandaloneStatusCaption>} |
| 58 | + </StyledStandaloneStatus> |
| 59 | + ); |
| 60 | + } |
| 61 | +); |
| 62 | + |
| 63 | +StatusIndicatorComponent.displayName = 'StatusIndicator'; |
| 64 | + |
| 65 | +StatusIndicatorComponent.propTypes = { |
| 66 | + type: PropTypes.oneOf(STATUS), |
| 67 | + isCompact: PropTypes.bool |
| 68 | +}; |
| 69 | + |
| 70 | +StatusIndicatorComponent.defaultProps = { |
| 71 | + type: 'offline' |
| 72 | +}; |
| 73 | + |
| 74 | +export const StatusIndicator = StatusIndicatorComponent; |
0 commit comments