-
Notifications
You must be signed in to change notification settings - Fork 4
ECHOES-1335 LoadingSkeleton component #691
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
Open
jeremy-davis-sonarsource
wants to merge
1
commit into
main
Choose a base branch
from
jay/loading-skeleton
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Echoes React | ||
| * Copyright (C) 2023-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3 of the License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License | ||
| * along with this program; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| */ | ||
|
|
||
| import { ReactNode } from 'react'; | ||
| import { | ||
| StyledLoadingSkeletonText, | ||
| StyledLoadingSkeletonTextLastParagraphLine, | ||
| StyledLoadingSkeletonWrapper, | ||
| StyledLoadingSkeletonWrapperDisk, | ||
| StyledParagraphWrapper, | ||
| } from './LoadingSkeletonStyles'; | ||
| import { LoadingSkeletonVariety } from './LoadingSkeletonTypes'; | ||
|
|
||
| export interface LoadingSkeletonProps { | ||
| className?: string; | ||
| /** | ||
| * Wrapped components will be hidden when isLoading is true. If variety is not Text or Paragraph, the LoadingSkeleton will match the size of its child. | ||
| */ | ||
| children?: ReactNode; | ||
| /** | ||
| * Displays the skeleton if `true`. | ||
| * Displays its children if `false`. | ||
| * @default true | ||
| */ | ||
| isLoading?: boolean; | ||
| /** | ||
| * The type of skeleton. The varieties fall in two categories: | ||
| * - text: Text or Paragraph, meant as a placeholder for text (either a label/heading or a paragraph of text). They are auto-sized according to the font size and line-height. | ||
| * - shapes: Rectangle or Disk. These are meant to wrap components and are auto-sized to their child's dimensions. | ||
| */ | ||
| variety: `${LoadingSkeletonVariety}`; | ||
| } | ||
|
|
||
| /** | ||
| * Placeholder to display while we wait for data to be available. | ||
| * This can be used in 3 ways: | ||
| * | ||
| * 1) For text, place the LoadingSkeleton inside the text component (i.e. Text, Heading, ...), but around the contents. | ||
| * It will inherit the size of the font. This works for variety 'text' & 'paragraph'. | ||
| * ``` | ||
| * <Text size='sm'> | ||
| * <LoadingSkeleton isLoading={loading} variety='paragraph'> | ||
| * {stringDataThatNeedsLoading} | ||
| * </LoadingSkeleton> | ||
| * </Text> | ||
| * ``` | ||
| * | ||
| * 2) Wrap components to have the LoadingSkeleton inherit its size (if already known). | ||
| * Works perfectly for fixed-size components whose state depends on async data. | ||
| * | ||
| * ``` | ||
| * <LoadingSkeleton isLoading={loading} variety='rectangle'> | ||
| * <RatingBadge rating={rating} /> | ||
| * </LoadingSkeleton> | ||
| * ``` | ||
| * | ||
| * 3) You can build a loading screen or component with standalone LoadingSkeletons | ||
| * ``` | ||
| * <div> | ||
| * <div> | ||
| * <LoadingSkeleton className='sw-mr-50 sw-w-200 sw-h-200' variety='disk' /> | ||
| * <LoadingSkeleton className='sw-w-300 sw-h-200' variety='rectangle' /> | ||
| * </div> | ||
| * | ||
| * <LoadingSkeleton className='sw-mt-50 sw-w-500 sw-h-300' variety='rectangle' /> | ||
| * </div> | ||
| * ``` | ||
| * | ||
| * ⚠️ Accessibility ⚠️ | ||
| * | ||
| * LoadingSkeletons are `aria-hidden`. A11y must be handled outside of this component, typically by using a LoadingContainer. | ||
| * Check out LoadingContainer's tsdoc for more information. | ||
| */ | ||
| export function LoadingSkeleton(props: Readonly<LoadingSkeletonProps>) { | ||
| const { children, isLoading = true, variety, ...radixProps } = props; | ||
|
|
||
| if (!isLoading) { | ||
| return children; | ||
| } | ||
|
|
||
| const commonProps = { ...radixProps, 'aria-hidden': true }; | ||
|
|
||
| switch (variety) { | ||
| // Text v | ||
| case LoadingSkeletonVariety.Text: | ||
| return <StyledLoadingSkeletonText {...commonProps} />; | ||
| case LoadingSkeletonVariety.Paragraph: | ||
| return ( | ||
|
jeremy-davis-sonarsource marked this conversation as resolved.
|
||
| <StyledParagraphWrapper {...commonProps}> | ||
| <StyledLoadingSkeletonText /> | ||
| <StyledLoadingSkeletonText /> | ||
| <StyledLoadingSkeletonTextLastParagraphLine /> | ||
| </StyledParagraphWrapper> | ||
| ); | ||
|
|
||
| case LoadingSkeletonVariety.Disk: | ||
| return ( | ||
| <StyledLoadingSkeletonWrapperDisk {...commonProps}> | ||
| {children} | ||
| </StyledLoadingSkeletonWrapperDisk> | ||
| ); | ||
|
jeremy-davis-sonarsource marked this conversation as resolved.
|
||
| case LoadingSkeletonVariety.Rectangle: | ||
| default: | ||
| return ( | ||
| <StyledLoadingSkeletonWrapper {...commonProps}>{children}</StyledLoadingSkeletonWrapper> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| LoadingSkeleton.displayName = 'LoadingSkeleton'; | ||
102 changes: 102 additions & 0 deletions
102
src/components/loading-skeleton/LoadingSkeletonStyles.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,102 @@ | ||
| /* | ||
| * Echoes React | ||
| * Copyright (C) 2023-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3 of the License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License | ||
| * along with this program; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| */ | ||
|
|
||
| import { keyframes } from '@emotion/react'; | ||
| import styled from '@emotion/styled'; | ||
| import { cssVar } from '~utils/design-tokens'; | ||
| import { Label } from '../typography'; | ||
| import { StyledHeading } from '../typography/Heading'; | ||
|
|
||
| export const DEFAULT_LOADING_SKELETON_WIDTH_LABEL = '96px'; | ||
| export const DEFAULT_LOADING_SKELETON_WIDTH_HEADING = '176px'; | ||
| export const LAST_PARAGRAPH_LINE_WIDTH = '40%'; | ||
|
|
||
| const shimmer = keyframes` | ||
| 0% { transform: translateX(-100%); } | ||
| 100% { transform: translateX(100%); } | ||
| `; | ||
|
|
||
| const LoadingSkeletonBaseStyle = styled.div` | ||
| background-color: ${cssVar('color-surface-hover')}; | ||
|
|
||
| border-radius: ${cssVar('border-radius-200')}; | ||
|
|
||
| position: relative; // necessary so the ::after pseudoelement doesn't misbehave with the container | ||
| overflow: hidden; | ||
|
|
||
| &::after { | ||
| content: ''; | ||
| position: absolute; | ||
| top: 0; | ||
| left: 0; | ||
| width: 100%; | ||
| height: 100%; | ||
| background: linear-gradient( | ||
| 90deg, | ||
| transparent 0%, | ||
| ${cssVar('color-surface-default')} 50%, | ||
| transparent 100% | ||
| ); | ||
| animation: ${shimmer} 2s infinite; | ||
|
|
||
| @media (prefers-reduced-motion: reduce) { | ||
| animation: none; | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| export const StyledLoadingSkeletonText = styled(LoadingSkeletonBaseStyle)` | ||
| height: 1em; | ||
| margin-block: calc((1lh - 1em) / 2); // set margins to match the line-height | ||
| min-width: ${DEFAULT_LOADING_SKELETON_WIDTH_LABEL}; | ||
| max-width: ${cssVar('sizes-typography-max-width-default')}; | ||
|
|
||
| ${Label} & { | ||
| width: ${DEFAULT_LOADING_SKELETON_WIDTH_LABEL}; | ||
| } | ||
|
|
||
| ${StyledHeading} & { | ||
| width: ${DEFAULT_LOADING_SKELETON_WIDTH_HEADING}; | ||
| } | ||
| `; | ||
|
|
||
| export const StyledLoadingSkeletonTextLastParagraphLine = styled(StyledLoadingSkeletonText)` | ||
| width: ${LAST_PARAGRAPH_LINE_WIDTH}; | ||
| `; | ||
|
|
||
| export const StyledLoadingSkeletonWrapper = styled(LoadingSkeletonBaseStyle)` | ||
| width: fit-content; | ||
| height: fit-content; | ||
|
|
||
| // Hide the child | ||
| & > * { | ||
| visibility: hidden; | ||
| } | ||
| `; | ||
|
|
||
| export const StyledLoadingSkeletonWrapperDisk = styled(StyledLoadingSkeletonWrapper)` | ||
| border-radius: ${cssVar('border-radius-full')}; | ||
| `; | ||
|
|
||
| export const StyledParagraphWrapper = styled.div` | ||
| display: flex; | ||
| flex-direction: column; | ||
| gap: ${cssVar('dimension-space-75')}; | ||
| `; |
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,26 @@ | ||
| /* | ||
| * Echoes React | ||
| * Copyright (C) 2023-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3 of the License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License | ||
| * along with this program; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| */ | ||
|
|
||
| export enum LoadingSkeletonVariety { | ||
| Text = 'text', | ||
| Rectangle = 'rectangle', | ||
| Disk = 'disk', | ||
| Paragraph = 'paragraph', | ||
| } |
52 changes: 52 additions & 0 deletions
52
src/components/loading-skeleton/__tests__/LoadingSkeleton-test.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,52 @@ | ||
| /* | ||
| * Echoes React | ||
| * Copyright (C) 2023-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3 of the License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License | ||
| * along with this program; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| */ | ||
| import { matchers } from '@emotion/jest'; | ||
| import { screen } from '@testing-library/react'; | ||
| import { render } from '~common/helpers/test-utils'; | ||
| import { LoadingSkeleton, LoadingSkeletonVariety } from '..'; | ||
|
|
||
| expect.extend(matchers); | ||
|
|
||
| it.each([ | ||
| [LoadingSkeletonVariety.Disk], | ||
| [LoadingSkeletonVariety.Paragraph], | ||
| [LoadingSkeletonVariety.Rectangle], | ||
| [LoadingSkeletonVariety.Text], | ||
| ])('%s should render correctly', async (variety) => { | ||
| const { container } = render(<LoadingSkeleton variety={variety} />); | ||
|
|
||
| await expect(container).toHaveNoA11yViolations(); | ||
| }); | ||
|
|
||
| it('should hide text content when loading', () => { | ||
| render(<LoadingSkeleton variety="text">content</LoadingSkeleton>); | ||
|
|
||
| expect(screen.queryByText('content')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should show text content when not loading', () => { | ||
| render( | ||
| <LoadingSkeleton isLoading={false} variety="paragraph"> | ||
| content | ||
| </LoadingSkeleton>, | ||
| ); | ||
|
|
||
| expect(screen.getByText('content')).toBeInTheDocument(); | ||
| }); |
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,22 @@ | ||
| /* | ||
| * Echoes React | ||
| * Copyright (C) 2023-2025 SonarSource Sàrl | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the GNU Lesser General Public | ||
| * License as published by the Free Software Foundation; either | ||
| * version 3 of the License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| * Lesser General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Lesser General Public License | ||
| * along with this program; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
| */ | ||
|
|
||
| export { LoadingSkeleton, type LoadingSkeletonProps } from './LoadingSkeleton'; | ||
| export { LoadingSkeletonVariety } from './LoadingSkeletonTypes'; |
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
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.
Uh oh!
There was an error while loading. Please reload this page.