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
GrafanaUI: Add new EmptyState
component
#84891
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0220cbd
add empty search state component
ashharrison90 aa2798c
extract translations
ashharrison90 5460b58
nicer prop spreading
ashharrison90 afc6897
actually expose component in grafana-ui
ashharrison90 d2dddf7
use requestAnimationFrame
ashharrison90 b8d6b26
add rollup-plugin-svg-import
ashharrison90 bbae2f9
move to a single EmptyState component with variants
ashharrison90 5ba8761
update docs
ashharrison90 f4a439d
remove CTA
ashharrison90 8bff79e
add some more prop documentation
ashharrison90 da1f053
just use 1 svg
ashharrison90 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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
43 changes: 43 additions & 0 deletions
43
packages/grafana-ui/src/components/EmptyState/EmptyState.mdx
This file contains 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,43 @@ | ||
import { ArgTypes } from '@storybook/blocks'; | ||
import { EmptyState } from './EmptyState'; | ||
|
||
# EmptyState | ||
|
||
Use an empty state to communicate to the user that there is no data to display, or that a search query returned no results. | ||
|
||
## variant="search" | ||
|
||
### When to use | ||
|
||
Use in place of a results table or list when a search query or filter returns no results. | ||
|
||
There are sensible defaults for the image and message, so in most cases you can use it without any additional props. | ||
|
||
```jsx | ||
import { EmptyState } from '@grafana/ui'; | ||
|
||
<EmptyState variant="search" />; | ||
``` | ||
|
||
### Providing custom overrides | ||
|
||
You can optionally override the message or image, and add additional information or a button (e.g. to clear the search query) | ||
|
||
```jsx | ||
import { Button, EmptyState } from '@grafana/ui'; | ||
|
||
<EmptyState | ||
button={<Button variant="secondary" onClick={clearSearchQuery} />} | ||
image={<AnyReactNode />} | ||
message="No playlists found" | ||
> | ||
Optionally provide some additional information here. Maybe even a link to{' '} | ||
<TextLink href="<externalDocsLink>" external> | ||
documentation. | ||
</TextLink> | ||
</EmptyState>; | ||
``` | ||
|
||
## Props | ||
|
||
<ArgTypes of={EmptyState} /> |
33 changes: 33 additions & 0 deletions
33
packages/grafana-ui/src/components/EmptyState/EmptyState.story.tsx
This file contains 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,33 @@ | ||
import { Meta, StoryFn } from '@storybook/react'; | ||
import React from 'react'; | ||
|
||
import { EmptyState } from './EmptyState'; | ||
import mdx from './EmptyState.mdx'; | ||
|
||
const meta: Meta<typeof EmptyState> = { | ||
title: 'General/EmptyState', | ||
component: EmptyState, | ||
parameters: { | ||
docs: { | ||
page: mdx, | ||
}, | ||
controls: { | ||
exclude: ['button', 'image', 'variant'], | ||
}, | ||
}, | ||
argTypes: { | ||
children: { | ||
type: 'string', | ||
}, | ||
}, | ||
}; | ||
|
||
export const Basic: StoryFn<typeof EmptyState> = (args) => { | ||
return <EmptyState {...args} />; | ||
}; | ||
|
||
Basic.args = { | ||
children: 'Use this space to add any additional information', | ||
}; | ||
|
||
export default meta; |
47 changes: 47 additions & 0 deletions
47
packages/grafana-ui/src/components/EmptyState/EmptyState.tsx
This file contains 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,47 @@ | ||
import React, { ReactNode } from 'react'; | ||
|
||
import { t } from '../../utils/i18n'; | ||
import { Box } from '../Layout/Box/Box'; | ||
import { Stack } from '../Layout/Stack/Stack'; | ||
import { Text } from '../Text/Text'; | ||
|
||
import { GrotNotFound } from './GrotNotFound/GrotNotFound'; | ||
|
||
interface Props { | ||
/** | ||
* Provide a button to render below the message | ||
*/ | ||
button?: ReactNode; | ||
hideImage?: boolean; | ||
/** | ||
* Override the default image for the variant | ||
*/ | ||
image?: ReactNode; | ||
/** | ||
* Message to display to the user | ||
*/ | ||
message?: string; | ||
/** | ||
* Empty state variant. Possible values are 'search'. | ||
*/ | ||
variant: 'search'; | ||
} | ||
|
||
export const EmptyState = ({ | ||
button, | ||
children, | ||
image = <GrotNotFound width={300} />, | ||
message = t('grafana-ui.empty-state.search-message', 'No results found'), | ||
hideImage = false, | ||
}: React.PropsWithChildren<Props>) => { | ||
return ( | ||
<Box paddingY={4} gap={4} display="flex" direction="column" alignItems="center"> | ||
{!hideImage && image} | ||
<Stack direction="column" alignItems="center"> | ||
<Text variant="h4">{message}</Text> | ||
{children && <Text color="secondary">{children}</Text>} | ||
</Stack> | ||
{button} | ||
</Box> | ||
); | ||
}; |
72 changes: 72 additions & 0 deletions
72
packages/grafana-ui/src/components/EmptyState/GrotNotFound/GrotNotFound.tsx
This file contains 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,72 @@ | ||
import { css } from '@emotion/css'; | ||
import React, { SVGProps, useEffect, useRef } from 'react'; | ||
import SVG from 'react-inlinesvg'; | ||
|
||
import { GrafanaTheme2 } from '@grafana/data'; | ||
|
||
import { useStyles2 } from '../../../themes'; | ||
|
||
import notFoundSvg from './grot-not-found.svg'; | ||
|
||
const MIN_ARM_ROTATION = -20; | ||
const MAX_ARM_ROTATION = 5; | ||
const MIN_ARM_TRANSLATION = -5; | ||
const MAX_ARM_TRANSLATION = 5; | ||
|
||
export interface Props { | ||
width?: SVGProps<SVGElement>['width']; | ||
height?: SVGProps<SVGElement>['height']; | ||
} | ||
|
||
export const GrotNotFound = ({ width = 'auto', height }: Props) => { | ||
const svgRef = useRef<SVGElement>(null); | ||
const styles = useStyles2(getStyles); | ||
|
||
useEffect(() => { | ||
const handleMouseMove = (event: MouseEvent) => { | ||
const grotArm = svgRef.current?.querySelector('#grot-not-found-arm'); | ||
const grotMagnifier = svgRef.current?.querySelector('#grot-not-found-magnifier'); | ||
|
||
const { clientX, clientY } = event; | ||
const { innerWidth, innerHeight } = window; | ||
const heightRatio = clientY / innerHeight; | ||
const widthRatio = clientX / innerWidth; | ||
const rotation = getIntermediateValue(heightRatio, MIN_ARM_ROTATION, MAX_ARM_ROTATION); | ||
const translation = getIntermediateValue(widthRatio, MIN_ARM_TRANSLATION, MAX_ARM_TRANSLATION); | ||
|
||
window.requestAnimationFrame(() => { | ||
grotArm?.setAttribute('style', `transform: rotate(${rotation}deg) translateX(${translation}%)`); | ||
grotMagnifier?.setAttribute('style', `transform: rotate(${rotation}deg) translateX(${translation}%)`); | ||
}); | ||
}; | ||
|
||
window.addEventListener('mousemove', handleMouseMove); | ||
|
||
return () => { | ||
window.removeEventListener('mousemove', handleMouseMove); | ||
}; | ||
}, []); | ||
|
||
return <SVG innerRef={svgRef} src={notFoundSvg} className={styles.svg} height={height} width={width} />; | ||
}; | ||
|
||
GrotNotFound.displayName = 'GrotNotFound'; | ||
|
||
const getStyles = (theme: GrafanaTheme2) => { | ||
return { | ||
svg: css({ | ||
'#grot-not-found-arm, #grot-not-found-magnifier': { | ||
transformOrigin: 'center', | ||
}, | ||
}), | ||
}; | ||
}; | ||
|
||
/** | ||
* Given a start value, end value, and a ratio, return the intermediate value | ||
* Works with negative and inverted start/end values | ||
*/ | ||
const getIntermediateValue = (ratio: number, start: number, end: number) => { | ||
const value = ratio * (end - start) + start; | ||
return value; | ||
}; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we document all the props? This will show them in the props table with the description text.