Skip to content
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

Adds ListSkeleton component [LG-4196] #2295

Merged
merged 10 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-garlics-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@leafygreen-ui/skeleton-loader': minor
---

Adds `ListSkeleton` component
3 changes: 2 additions & 1 deletion packages/skeleton-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"@leafygreen-ui/lib": "^13.3.0",
"@leafygreen-ui/palette": "^4.0.9",
"@leafygreen-ui/tokens": "^2.5.2",
"@leafygreen-ui/typography": "^18.3.0"
"@leafygreen-ui/typography": "^18.3.0",
"lodash": "^4.17.21"
},
"peerDependencies": {
"@leafygreen-ui/leafygreen-provider": "^3.1.12"
Expand Down
17 changes: 17 additions & 0 deletions packages/skeleton-loader/src/ListSkeleton/ListSkeleton.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import { render } from '@testing-library/react';

import { ListSkeleton } from '.';

describe('packages/skeleton-list', () => {
test('renders', () => {
const { queryByTestId } = render(<ListSkeleton />);
expect(queryByTestId('lg-skeleton-list')).toBeInTheDocument();
});

test('renders `count` items', () => {
const { queryAllByTestId } = render(<ListSkeleton count={3} />);
const listItems = queryAllByTestId('lg-skeleton-list_item');
expect(listItems.length).toBe(3);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { StoryMetaType, StoryType } from '@lg-tools/storybook-utils';

import { ListSkeleton, type ListSkeletonProps } from '.';

const meta: StoryMetaType<typeof ListSkeleton> = {
title: 'Components/SkeletonLoader/List',
component: ListSkeleton,
parameters: {
default: null,
},
args: {
count: 5,
bulletsOnly: false,
},
};

export default meta;

export const Basic: StoryType<typeof ListSkeleton> = (
args: ListSkeletonProps,
) => {
return (
<div style={{ width: 256 }}>
<ListSkeleton {...args} />
</div>
);
};

export const BulletsOnly: StoryType<typeof ListSkeleton> = () => {
return (
<div style={{ width: 256 }}>
<ListSkeleton bulletsOnly />
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { css } from '@leafygreen-ui/emotion';
import { spacing } from '@leafygreen-ui/tokens';

export const skeletonListWrapperStyles = css`
width: 100%;
padding: 0;
margin: 0;
`;

export const getSkeletonListItemStyles = (
index = 0,
bulletsOnly?: boolean,
) => css`
list-style: none;

&:not(:first-child),
&:not(:last-child) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this still targets the first and last element, is that what you want?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about

& + & {
 margin-block-start: ${spacing[300]}px;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I don't actually need this anymore since I'm using margin. (was using padding previously)

margin-block: ${spacing[300]}px;
}

width: ${getWidth(index, bulletsOnly)};
`;

const getWidth = (index = 0, bulletsOnly?: boolean) => {
if (bulletsOnly) {
return spacing[400] + 'px';
}

const offset = 25 * (index % 3);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add comments here?

return 100 - offset + '%';
};
43 changes: 43 additions & 0 deletions packages/skeleton-loader/src/ListSkeleton/ListSkeleton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from 'react';
import range from 'lodash/range';

import LeafyGreenProvider from '@leafygreen-ui/leafygreen-provider';

import { Skeleton } from '../Skeleton';

import {
getSkeletonListItemStyles,
skeletonListWrapperStyles,
} from './ListSkeleton.styles';
import { ListSkeletonProps } from './ListSkeleton.types';

export function ListSkeleton({
count = 5,
bulletsOnly,
darkMode,
...rest
}: ListSkeletonProps) {
return (
<LeafyGreenProvider darkMode={darkMode}>
<ul
className={skeletonListWrapperStyles}
data-testid="lg-skeleton-list"
aria-busy="true"
{...rest}
>
{range(count).map(i => (
<li
// Update the key when `count` changes so the item animation stays in sync
key={`${i}/${count}`}
className={getSkeletonListItemStyles(i, bulletsOnly)}
data-testid="lg-skeleton-list_item"
>
<Skeleton size="small" />
</li>
))}
</ul>
</LeafyGreenProvider>
);
}

ListSkeleton.displayName = 'ListSkeleton';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { DarkModeProps, HTMLElementProps } from '@leafygreen-ui/lib';

export interface ListSkeletonProps
extends DarkModeProps,
HTMLElementProps<'ul'> {
count?: number;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add docs?

bulletsOnly?: boolean;
}
2 changes: 2 additions & 0 deletions packages/skeleton-loader/src/ListSkeleton/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { ListSkeleton } from './ListSkeleton';
export { ListSkeletonProps } from './ListSkeleton.types';
58 changes: 20 additions & 38 deletions packages/skeleton-loader/src/SkeletonLoader.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { storybookArgTypes } from '@lg-tools/storybook-utils';
import { StoryFn } from '@storybook/react';

import { css } from '@leafygreen-ui/emotion';
import { DarkModeProps } from '@leafygreen-ui/lib';
import { spacing } from '@leafygreen-ui/tokens';
import { Body, InlineCode } from '@leafygreen-ui/typography';

import {
CardSkeleton,
CodeSkeleton,
FormSkeleton,
ListSkeleton,
ParagraphSkeleton,
Skeleton,
TableSkeleton,
Expand Down Expand Up @@ -46,44 +46,26 @@ const labelStyles = css`
margin-top: ${spacing[5]}px;
`;

export const LiveExample: StoryFn<any> = (props: DarkModeProps) => (
const skeletonComponents = {
Skeleton,
CardSkeleton,
CodeSkeleton,
FormSkeleton,
ListSkeleton,
ParagraphSkeleton,
TableSkeleton,
};

export const LiveExample: StoryFn<any> = () => (
<div className={storyRootStyles}>
<div className={displayOptionContainerStyles}>
<Skeleton />
<Body className={labelStyles} weight="medium">
<InlineCode>Skeleton</InlineCode>
</Body>
</div>
<div className={displayOptionContainerStyles}>
<ParagraphSkeleton withHeader />
<Body className={labelStyles} weight="medium">
<InlineCode>ParagraphSkeleton</InlineCode>
</Body>
</div>
<div className={displayOptionContainerStyles}>
<CardSkeleton />
<Body className={labelStyles} weight="medium">
<InlineCode>CardSkeleton</InlineCode>
</Body>
</div>
<div className={displayOptionContainerStyles}>
<FormSkeleton {...props} />
<Body className={labelStyles} weight="medium">
<InlineCode>FormSkeleton</InlineCode>
</Body>
</div>
<div className={displayOptionContainerStyles}>
<TableSkeleton />
<Body className={labelStyles} weight="medium">
<InlineCode>TableSkeleton</InlineCode>
</Body>
</div>
<div className={displayOptionContainerStyles}>
<CodeSkeleton />
<Body className={labelStyles} weight="medium">
<InlineCode>CodeSkeleton</InlineCode>
</Body>
</div>
{Object.entries(skeletonComponents).map(([name, SkeletonVariant]) => (
<div key={name} className={displayOptionContainerStyles}>
<SkeletonVariant />
<Body className={labelStyles} weight="medium">
<InlineCode>{name}</InlineCode>
</Body>
</div>
))}
</div>
);
LiveExample.parameters = {
Expand Down
1 change: 1 addition & 0 deletions packages/skeleton-loader/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { CardSkeleton, type CardSkeletonProps } from './CardSkeleton';
export { CodeSkeleton, type CodeSkeletonProps } from './CodeSkeleton';
export { FormSkeleton, type FormSkeletonProps } from './FormSkeleton';
export { ListSkeleton, type ListSkeletonProps } from './ListSkeleton';
export {
ParagraphSkeleton,
type ParagraphSkeletonProps,
Expand Down
Loading