-
Notifications
You must be signed in to change notification settings - Fork 97
feat(avatars): add status indicator component #1410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Francois-Esquire
merged 17 commits into
main
from
francois-esquire/avatars-status-indicator
Sep 22, 2022
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c0b438e
feat(avatars): add status indicator component
9352dd7
fix: styles and add pattern example
fd67ecd
fix: transition styling
1e7001c
fix: add type to tests
b892de0
fix: address feedback
ad377d7
fix: correct text overflow alignment
7fdf1b9
fix: consolidate styles between styled status indicators
619f869
chore: add figma link
5872a1a
fix: revert to original status indicator component name
a28087d
fix: is compact sizing alignment
dca8630
fix: box sizing with css bedrock
00ae467
fix: address feedback
40404e5
fix: remove iscompact default value
e569ae6
fix: create base status indicator
d87f2f0
fix: use better types
37cf3bd
fix: address feedback
58347d2
fix: address feedback on margin calculation
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Meta, ArgsTable, Canvas, Story } from '@storybook/addon-docs'; | ||
import { StatusIndicator } from '@zendeskgarden/react-avatars'; | ||
|
||
import { StatusIndicatorStory } from './stories/StatusIndicatorStory'; | ||
|
||
<Meta title="Packages/Avatars/StatusIndicator" component={StatusIndicator} /> | ||
|
||
# API | ||
|
||
<ArgsTable /> | ||
|
||
# Demo | ||
|
||
<Canvas> | ||
<Story | ||
name="StatusIndicator" | ||
argTypes={{ | ||
children: { control: 'text' } | ||
}} | ||
args={{ | ||
'aria-label': 'Label', | ||
children: 'Status' | ||
}} | ||
parameters={{ | ||
design: { | ||
allowFullscreen: true, | ||
type: 'figma', | ||
url: 'https://www.figma.com/file/6g87L4FdKZTA3knt3Rsfdx/Garden?node-id=10927%3A45275' | ||
} | ||
}} | ||
> | ||
{args => <StatusIndicatorStory {...args} />} | ||
</Story> | ||
</Canvas> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Copyright Zendesk, Inc. | ||
* | ||
* Use of this source code is governed under the Apache License, Version 2.0 | ||
* found at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { Story } from '@storybook/react'; | ||
import { StatusIndicator, IStatusIndicatorProps } from '@zendeskgarden/react-avatars'; | ||
|
||
export const StatusIndicatorStory: Story<IStatusIndicatorProps> = ({ ...args }) => { | ||
return <StatusIndicator {...args} />; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/avatars/demo/~patterns/stories/StatusMenuStory.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* Copyright Zendesk, Inc. | ||
* | ||
* Use of this source code is governed under the Apache License, Version 2.0 | ||
* found at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
import React, { useState } from 'react'; | ||
import { Story } from '@storybook/react'; | ||
import { Col, Grid, Row } from '@zendeskgarden/react-grid'; | ||
import { Dropdown, Trigger, Menu, Item } from '@zendeskgarden/react-dropdowns'; | ||
import { Avatar, IStatusIndicatorProps, StatusIndicator } from '@zendeskgarden/react-avatars'; | ||
|
||
export const StatusMenuStory: Story = ({ isCompact }) => { | ||
const [selectedType, setSelectedType] = useState<IStatusIndicatorProps['type']>('available'); | ||
|
||
return ( | ||
<Grid> | ||
<Row style={{ height: 'calc(100vh - 80px)' }}> | ||
<Col textAlign="center" alignSelf="center"> | ||
<Dropdown selectedItem={selectedType} onSelect={value => setSelectedType(value)}> | ||
<Trigger> | ||
<Avatar status={selectedType} size={isCompact ? 'small' : 'medium'}> | ||
<img alt="Example User" src="images/avatars/chrome.png" /> | ||
</Avatar> | ||
</Trigger> | ||
<Menu isCompact={isCompact}> | ||
<Item value="available"> | ||
<StatusIndicator isCompact={isCompact} type="available"> | ||
Online | ||
</StatusIndicator> | ||
</Item> | ||
|
||
<Item value="transfers"> | ||
<StatusIndicator isCompact={isCompact} type="transfers"> | ||
Transfers only | ||
</StatusIndicator> | ||
</Item> | ||
|
||
<Item value="away"> | ||
<StatusIndicator isCompact={isCompact} type="away"> | ||
Away | ||
</StatusIndicator> | ||
</Item> | ||
|
||
<Item value="offline"> | ||
<StatusIndicator isCompact={isCompact} type="offline"> | ||
Offline | ||
</StatusIndicator> | ||
</Item> | ||
</Menu> | ||
</Dropdown> | ||
</Col> | ||
</Row> | ||
</Grid> | ||
); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright Zendesk, Inc. | ||
* | ||
* Use of this source code is governed under the Apache License, Version 2.0 | ||
* found at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { render, renderRtl, cleanup } from 'garden-test-utils'; | ||
|
||
import { StatusIndicator } from './StatusIndicator'; | ||
import { STATUS } from '../types'; | ||
|
||
describe('StatusIndicator', () => { | ||
afterEach(cleanup); | ||
|
||
it('passes ref to underlying DOM element', () => { | ||
const ref = React.createRef<HTMLElement>(); | ||
const { container } = render(<StatusIndicator type="available" ref={ref} />); | ||
|
||
expect(container.firstChild).toBe(ref.current); | ||
}); | ||
|
||
it('has the correct roles', () => { | ||
const { getByRole, container } = render(<StatusIndicator type="available" />); | ||
|
||
expect(getByRole('status')).toBe(container.firstChild); | ||
expect(getByRole('img')).toBe(container.firstChild?.firstChild); | ||
}); | ||
|
||
it('renders with a caption', () => { | ||
const text = 'caption'; | ||
const { getByText } = render(<StatusIndicator type="available">{text}</StatusIndicator>); | ||
|
||
expect(getByText(text).nodeName).toBe('FIGCAPTION'); | ||
}); | ||
|
||
it('renders in compact mode', () => { | ||
const { getByRole } = render(<StatusIndicator type="available" isCompact />); | ||
|
||
expect(getByRole('img')).toHaveStyleRule('height', '8px'); | ||
}); | ||
|
||
it('renders in RTL mode', () => { | ||
const { getByRole } = renderRtl(<StatusIndicator type="transfers">Caption</StatusIndicator>); | ||
|
||
expect(getByRole('img')).toHaveStyleRule('transform', 'scale(-1,1)', { | ||
modifier: "& > svg[data-icon-status='transfers']" | ||
}); | ||
}); | ||
|
||
describe('types', () => { | ||
it.each(STATUS)('renders "$1" status type, and with aria label', type => { | ||
const { getByRole } = render(<StatusIndicator type={type} />); | ||
|
||
expect(getByRole('img')).toHaveAttribute('aria-label', `status: ${type}`); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Copyright Zendesk, Inc. | ||
* | ||
* Use of this source code is governed under the Apache License, Version 2.0 | ||
* found at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
import React, { forwardRef, useMemo } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useText } from '@zendeskgarden/react-theming'; | ||
import ClockIcon12 from '@zendeskgarden/svg-icons/src/12/clock-stroke.svg'; | ||
import ClockIcon16 from '@zendeskgarden/svg-icons/src/16/clock-stroke.svg'; | ||
import ArrowLeftIcon12 from '@zendeskgarden/svg-icons/src/12/arrow-left-sm-stroke.svg'; | ||
import ArrowLeftIcon16 from '@zendeskgarden/svg-icons/src/16/arrow-left-sm-stroke.svg'; | ||
|
||
import { IStatusIndicatorProps, STATUS } from '../types'; | ||
import { | ||
StyledStandaloneStatusIndicator, | ||
StyledStandaloneStatusCaption, | ||
StyledStandaloneStatus | ||
} from '../styled'; | ||
|
||
/** | ||
* @extends HTMLAttributes<HTMLElement> | ||
*/ | ||
const StatusIndicatorComponent = forwardRef<HTMLElement, IStatusIndicatorProps>( | ||
({ children, type, isCompact, 'aria-label': label, ...props }, ref) => { | ||
let ClockIcon = ClockIcon16; | ||
let ArrowLeftIcon = ArrowLeftIcon16; | ||
|
||
if (isCompact) { | ||
ClockIcon = ClockIcon12; | ||
ArrowLeftIcon = ArrowLeftIcon12; | ||
} | ||
|
||
const defaultLabel = useMemo(() => ['status'].concat(type || []).join(': '), [type]); | ||
const ariaLabel = useText( | ||
StatusIndicatorComponent, | ||
{ 'aria-label': label }, | ||
'aria-label', | ||
defaultLabel | ||
); | ||
|
||
return ( | ||
<StyledStandaloneStatus role="status" ref={ref} {...props}> | ||
<StyledStandaloneStatusIndicator | ||
role="img" | ||
type={type} | ||
size={isCompact ? 'small' : 'medium'} | ||
aria-label={ariaLabel} | ||
> | ||
{type === 'away' ? <ClockIcon data-icon-status={type} aria-hidden="true" /> : null} | ||
{type === 'transfers' ? ( | ||
<ArrowLeftIcon data-icon-status={type} aria-hidden="true" /> | ||
) : null} | ||
</StyledStandaloneStatusIndicator> | ||
{children && <StyledStandaloneStatusCaption>{children}</StyledStandaloneStatusCaption>} | ||
jzempel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</StyledStandaloneStatus> | ||
); | ||
} | ||
); | ||
|
||
StatusIndicatorComponent.displayName = 'StatusIndicator'; | ||
|
||
StatusIndicatorComponent.propTypes = { | ||
type: PropTypes.oneOf(STATUS), | ||
isCompact: PropTypes.bool | ||
}; | ||
|
||
StatusIndicatorComponent.defaultProps = { | ||
type: 'offline' | ||
}; | ||
|
||
export const StatusIndicator = StatusIndicatorComponent; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright Zendesk, Inc. | ||
* | ||
* Use of this source code is governed under the Apache License, Version 2.0 | ||
* found at http://www.apache.org/licenses/LICENSE-2.0. | ||
*/ | ||
|
||
import styled, { ThemeProps, DefaultTheme } from 'styled-components'; | ||
import { retrieveComponentStyles, DEFAULT_THEME } from '@zendeskgarden/react-theming'; | ||
|
||
import { TRANSITION_DURATION } from './utility'; | ||
|
||
const COMPONENT_ID = 'avatars.status-indicator.status'; | ||
|
||
export const StyledStandaloneStatus = styled.figure.attrs({ | ||
'data-garden-id': COMPONENT_ID, | ||
'data-garden-version': PACKAGE_VERSION | ||
})<ThemeProps<DefaultTheme>>` | ||
display: inline-flex; | ||
flex-flow: row nowrap; | ||
transition: all ${TRANSITION_DURATION}s ease-in-out; | ||
margin: 0; | ||
box-sizing: content-box; | ||
|
||
${props => retrieveComponentStyles(COMPONENT_ID, props)}; | ||
`; | ||
|
||
StyledStandaloneStatus.defaultProps = { | ||
theme: DEFAULT_THEME | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.