Skip to content

Commit b050934

Browse files
mdevilsXOP
andauthored
feat(Table): add skeleton loading variant (#1229)
* feat(Table): add skeleton loading variant `loading` draws a centered spinner, which gives no sense of the shape of the data that is coming. `loadingVariant="skeleton"` draws placeholder rows instead, with `skeletonRowCount` to choose how many. The default is unchanged, so existing consumers see the spinner they have today. The placeholder bars are decorative and the cell keeps the same "Loading data" text the spinner variant announces, visually hidden, so the state is still read out. The table sets `aria-busy` while loading in either variant. * fix(Table): build skeleton rows from real cells The first version drew the placeholders inside the single spanned cell the spinner uses, which is not a table row: the bars did not line up with the columns and the DOM said one cell where the data will have many. Each placeholder is now a <td> per column. jsx-a11y counts <tr> and <td> as interactive, so aria-hidden on a decorative row trips no-aria-hidden-on-focusable. Neither is focusable without a tabindex, so the two disables carry that reason rather than reshaping the markup around a false positive. * refactor(Table): derive the column count once The header count plus the selection and actions columns was spelled out twice, once for the skeleton rows and once for the spanned loading and no-data row. * fix(Table): address skeleton review feedback Export TableLoadingVariant from the package root, size skeleton bars by table size, and drop needless cn calls. * feat(Skeleton): add Skeleton component and use it in Table * feat(Table): support colSpan on cells and expand skeleton stories Show the skeleton stories at full width with 6 columns of different widths and 10 rows, and add a story with merged skeleton cells. Add visual specs for Skeleton and the new Table stories. * fix(Skeleton): share one shimmer wave across all skeletons Fix the gradient to the viewport so every skeleton shows a slice of the same wave, moving at the same speed whatever its size. Under reduced motion the gradient is dropped instead of freezing mid-wave. * fix(Skeleton): reduce corner radius to 2px * feat(Skeleton): add text and rectangle shapes Text skeletons draw one or more lines, the last of several at 75% width. Rectangles take a height. Width and height accept a space key or any CSS length, and props that do not fit the shape are type errors. The shimmer is now a 1.25s sweep of the fill's opacity with a 0.25s pause. * revert(Table): drop colSpan support on cells Remove colSpan from row cells, the merged-cells skeleton story, its test and its snapshots. * fix(Skeleton): keep later skeletons in phase with the shared sweep CSS animations start when their element mounts, so a skeleton added later swept out of step with the others. Pin each sweep to the document timeline's origin. * fix(Table): give skeleton rows the table rowHeight * fix(Skeleton): export all Skeleton types from the package root * fix(Skeleton): pin each shimmer as it starts Pin the shimmer to the document timeline on animationstart instead of on mount, so animations created later (reduced motion turned off, a hidden parent shown) stay in phase. Leave consumer animations alone, drop the layout effect that warned during server rendering, and stop object properties such as toString passing as space keys. * fix(Skeleton): match only the shimmer's own animation name A prefix check also matched consumer keyframes such as skeleton-shimmer-custom and pinned them. Match the exact name CSS Modules generates. * fix(Skeleton,Table): apply review feedback - Rename the rest props to `props`. - `width` and `height` take plain CSS values; drop the space keys until the new token schema lands. - Size text lines from the font: each bar is 1em tall in a 1lh line, using the `Text` font for its size, so skeleton text is as tall as real text. Table cells use `md`, their font at every table size, and lay the skeleton out like their truncated text, so placeholder rows match data rows. - Animate only under `prefers-reduced-motion: no-preference`; the static fill is the default. - Drop pinning the shimmer to the page clock. Skeletons on screen together still share one sweep through the viewport-fixed gradient. * perf(Skeleton): animate the shimmer with transform Animating background-position repainted every skeleton on every frame: about 5% of the main thread for a 10-row table, 12.5% for 60 rows. Each bar now shows a viewport-wide fixed layer, clipped to the bar, that slides with transform, so the browser animates it without repainting. Skeletons on screen together still share one sweep, without relying on background-attachment: fixed, which iOS Safari ignores. --------- Co-authored-by: Evgeny Khoroshilov <evgeny.khoroshilov@clickhouse.com>
1 parent 5f724cb commit b050934

30 files changed

Lines changed: 615 additions & 6 deletions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@clickhouse/click-ui': minor
3+
---
4+
5+
New `Skeleton` component: a placeholder for content that is still loading, in two shapes. `shape="text"` (the default) draws `lines` of text, each as tall as a line of `Text` at the same `size` (`sm` or `md`); `shape="rectangle"` draws a box of any `height`. `width` and `height` take any CSS value and default to filling the parent. Skeletons on screen together share one shimmer sweep; with reduced motion the fill is static.
6+
7+
`Table` can now show placeholder rows while loading instead of a centered spinner: set `loadingVariant="skeleton"` alongside `loading`, and `skeletonRowCount` to choose how many rows (3 by default). The placeholders follow the table's `rowHeight`.
8+
9+
Visual: unchanged by default. `loadingVariant` defaults to `spinner`, which is what `loading` has always drawn. The table also sets `aria-busy` while loading, in either variant.

‎package.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@
279279
"import": "./dist/esm/components/SidebarNavigationTitle/index.js",
280280
"require": "./dist/cjs/components/SidebarNavigationTitle/index.cjs"
281281
},
282+
"./Skeleton": {
283+
"types": "./dist/types/components/Skeleton/index.d.ts",
284+
"import": "./dist/esm/components/Skeleton/index.js",
285+
"require": "./dist/cjs/components/Skeleton/index.cjs"
286+
},
282287
"./Spacer": {
283288
"types": "./dist/types/components/Spacer/index.d.ts",
284289
"import": "./dist/esm/components/Spacer/index.js",
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
.skeleton {
2+
--skeleton-width: initial;
3+
--skeleton-height: initial;
4+
5+
width: var(--skeleton-width, 100%);
6+
}
7+
8+
/* Each bar is as tall as the font (1em), and the space around it fills the rest of the line
9+
(1lh). So `lines` bars take exactly the height of that many lines of text. */
10+
.skeleton_shape_text {
11+
display: flex;
12+
flex-direction: column;
13+
gap: calc(1lh - 1em);
14+
padding-block: calc((1lh - 1em) / 2);
15+
}
16+
17+
.skeleton_size_sm {
18+
font: var(--typography-styles-product-text-normal-sm);
19+
}
20+
21+
.skeleton_size_md {
22+
font: var(--typography-styles-product-text-normal-md);
23+
}
24+
25+
.skeleton__line,
26+
.skeleton_shape_rectangle {
27+
border-radius: calc(var(--border-radii-1) / 2);
28+
background-color: var(--click-global-color-stroke-default);
29+
}
30+
31+
.skeleton__line {
32+
height: 1em;
33+
}
34+
35+
.skeleton__line:last-child:not(:first-child) {
36+
width: 75%;
37+
}
38+
39+
.skeleton_shape_rectangle {
40+
height: var(--skeleton-height, 100%);
41+
}
42+
43+
/* One highlight sweeps across the viewport, and every bar shows the part of it that passes over
44+
the bar. Each bar has a viewport-wide fixed layer clipped to the bar, so skeletons on screen
45+
together sweep in step whatever their size. The highlight is the page background at 25%, so the
46+
fill dips to about 75% opacity as it passes. The layers move with `transform`, which the browser
47+
animates without repainting. Without this media query match (reduced motion), the fill stays
48+
static. */
49+
@media (prefers-reduced-motion: no-preference) {
50+
.skeleton__line,
51+
.skeleton_shape_rectangle {
52+
/* stylelint-disable-next-line plugin/no-unsupported-browser-features -- the partial support
53+
is for SVG references and some shapes; a basic inset() works in every target browser */
54+
clip-path: inset(0 round calc(var(--border-radii-1) / 2));
55+
}
56+
57+
.skeleton__line::after,
58+
.skeleton_shape_rectangle::after {
59+
content: '';
60+
position: fixed;
61+
top: 0;
62+
left: 0;
63+
width: 200vw;
64+
height: 100vh;
65+
background-image: linear-gradient(
66+
90deg,
67+
transparent 40%,
68+
color-mix(in srgb, var(--click-global-color-background-default) 25%, transparent) 50%,
69+
transparent 60%
70+
);
71+
pointer-events: none;
72+
animation: skeleton-shimmer 1.5s ease-in-out infinite;
73+
}
74+
}
75+
76+
/* The layer is two viewports wide with the highlight in its middle. It starts with the highlight
77+
just left of the viewport and ends just right of it: a 1.25s sweep, then a 0.25s pause. */
78+
@keyframes skeleton-shimmer {
79+
0% {
80+
transform: translateX(-120vw);
81+
}
82+
83+
83.333% {
84+
transform: translateX(20vw);
85+
}
86+
87+
100% {
88+
transform: translateX(20vw);
89+
}
90+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { Meta, StoryObj } from '@storybook/react-vite';
2+
import { Skeleton } from '@/components/Skeleton';
3+
4+
const meta: Meta<typeof Skeleton> = {
5+
component: Skeleton,
6+
title: 'Display/Skeleton',
7+
tags: ['autodocs'],
8+
decorators: [
9+
Story => (
10+
<div
11+
data-testid="skeleton-harness"
12+
style={{ width: '320px', height: '120px', padding: '1rem' }}
13+
>
14+
<Story />
15+
</div>
16+
),
17+
],
18+
};
19+
20+
export default meta;
21+
22+
type Story = StoryObj<typeof Skeleton>;
23+
24+
export const Playground: Story = {
25+
args: {
26+
shape: 'text',
27+
size: 'md',
28+
lines: 1,
29+
},
30+
};
31+
32+
export const Small: Story = {
33+
args: { size: 'sm' },
34+
};
35+
36+
export const Medium: Story = {
37+
args: { size: 'md' },
38+
};
39+
40+
export const TextLines: Story = {
41+
args: { lines: 3 },
42+
};
43+
44+
export const TextWidth: Story = {
45+
args: { width: '60%' },
46+
};
47+
48+
export const Rectangle: Story = {
49+
args: { shape: 'rectangle' },
50+
};
51+
52+
export const RectangleSize: Story = {
53+
args: { shape: 'rectangle', width: '4rem', height: '4rem' },
54+
};
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { Skeleton, SkeletonProps } from '@/components/Skeleton';
2+
import { renderCUI } from '@/utils/test-utils';
3+
4+
describe('Skeleton', () => {
5+
it('is always hidden from assistive technology', () => {
6+
const { getByTestId } = renderCUI(
7+
<Skeleton
8+
data-testid="skeleton"
9+
aria-hidden={false}
10+
/>
11+
);
12+
13+
expect(getByTestId('skeleton')).toHaveAttribute('aria-hidden', 'true');
14+
});
15+
16+
it('draws one text line by default', () => {
17+
const { getByTestId } = renderCUI(<Skeleton data-testid="skeleton" />);
18+
19+
expect(getByTestId('skeleton').children).toHaveLength(1);
20+
});
21+
22+
it('draws the requested number of text lines', () => {
23+
const { getByTestId } = renderCUI(
24+
<Skeleton
25+
data-testid="skeleton"
26+
lines={3}
27+
/>
28+
);
29+
30+
expect(getByTestId('skeleton').children).toHaveLength(3);
31+
});
32+
33+
it('draws a rectangle as a single element', () => {
34+
const { getByTestId } = renderCUI(
35+
<Skeleton
36+
data-testid="skeleton"
37+
shape="rectangle"
38+
/>
39+
);
40+
41+
expect(getByTestId('skeleton').children).toHaveLength(0);
42+
});
43+
44+
it('passes CSS lengths through as custom properties', () => {
45+
const { getByTestId } = renderCUI(
46+
<Skeleton
47+
data-testid="skeleton"
48+
shape="rectangle"
49+
width="120px"
50+
height="60%"
51+
/>
52+
);
53+
54+
const skeleton = getByTestId('skeleton');
55+
expect(skeleton.style.getPropertyValue('--skeleton-width')).toBe('120px');
56+
expect(skeleton.style.getPropertyValue('--skeleton-height')).toBe('60%');
57+
});
58+
59+
it('sets no custom properties by default', () => {
60+
const { getByTestId } = renderCUI(<Skeleton data-testid="skeleton" />);
61+
62+
const skeleton = getByTestId('skeleton');
63+
expect(skeleton.style.getPropertyValue('--skeleton-width')).toBe('');
64+
expect(skeleton.style.getPropertyValue('--skeleton-height')).toBe('');
65+
});
66+
67+
it('keeps the consumer style and className', () => {
68+
const { getByTestId } = renderCUI(
69+
<Skeleton
70+
data-testid="skeleton"
71+
className="custom"
72+
style={{ marginTop: '4px' }}
73+
width="50%"
74+
/>
75+
);
76+
77+
const skeleton = getByTestId('skeleton');
78+
expect(skeleton).toHaveClass('custom');
79+
expect(skeleton.style.marginTop).toBe('4px');
80+
expect(skeleton.style.getPropertyValue('--skeleton-width')).toBe('50%');
81+
});
82+
83+
it('forwards the ref', () => {
84+
const ref = { current: null as HTMLDivElement | null };
85+
renderCUI(<Skeleton ref={ref} />);
86+
87+
expect(ref.current).toBeInstanceOf(HTMLDivElement);
88+
});
89+
90+
it('accepts only the props of its shape', () => {
91+
const invalidProps: SkeletonProps[] = [
92+
// @ts-expect-error `height` is for rectangles only
93+
{ height: '2rem' },
94+
// @ts-expect-error `lines` is for text only
95+
{ shape: 'rectangle', lines: 2 },
96+
// @ts-expect-error `size` is for text only
97+
{ shape: 'rectangle', size: 'sm' },
98+
// @ts-expect-error a skeleton has no children
99+
{ children: 'Loading' },
100+
];
101+
102+
expect(invalidProps).toHaveLength(4);
103+
});
104+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { cn, cva } from '@/lib/cva';
2+
import { CSSProperties, forwardRef, useMemo } from 'react';
3+
import styles from './Skeleton.module.css';
4+
import { SkeletonProps } from './Skeleton.types';
5+
6+
const skeletonVariants = cva(styles.skeleton, {
7+
variants: {
8+
shape: {
9+
text: styles.skeleton_shape_text,
10+
rectangle: styles.skeleton_shape_rectangle,
11+
},
12+
size: {
13+
sm: styles.skeleton_size_sm,
14+
md: styles.skeleton_size_md,
15+
},
16+
},
17+
});
18+
19+
/**
20+
* Decorative placeholder shown while content loads: lines of text, or a rectangle. Always hidden
21+
* from assistive technology; mark the loading area with `aria-busy` instead.
22+
*/
23+
export const Skeleton = forwardRef<HTMLDivElement, SkeletonProps>(
24+
(
25+
{ shape = 'text', size, lines = 1, width, height, className, style, ...props },
26+
ref
27+
) => {
28+
const skeletonStyle = useMemo(
29+
() =>
30+
({
31+
...(width ? { '--skeleton-width': width } : {}),
32+
...(height ? { '--skeleton-height': height } : {}),
33+
...style,
34+
}) as CSSProperties,
35+
[width, height, style]
36+
);
37+
38+
return (
39+
<div
40+
ref={ref}
41+
{...props}
42+
aria-hidden="true"
43+
style={skeletonStyle}
44+
className={cn(
45+
skeletonVariants({
46+
shape,
47+
size: shape === 'text' ? (size ?? 'md') : undefined,
48+
}),
49+
className
50+
)}
51+
>
52+
{shape === 'text' &&
53+
Array.from({ length: Math.max(1, lines) }, (_, index) => (
54+
<div
55+
key={index}
56+
className={styles.skeleton__line}
57+
/>
58+
))}
59+
</div>
60+
);
61+
}
62+
);
63+
Skeleton.displayName = 'Skeleton';
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { HTMLAttributes } from 'react';
2+
3+
export type SkeletonShape = 'text' | 'rectangle';
4+
5+
export type SkeletonSize = 'sm' | 'md';
6+
7+
interface SkeletonBaseProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {
8+
/** Any CSS width (`60%`, `6rem`, `var(--…)`). Defaults to filling the parent. */
9+
width?: string;
10+
}
11+
12+
export interface SkeletonTextProps extends SkeletonBaseProps {
13+
/** Defaults to `text`. */
14+
shape?: 'text';
15+
/** Matches `Text` of the same size: each line is as tall as a line of that text. Defaults to `md`. */
16+
size?: SkeletonSize;
17+
/** Number of lines. The last of several lines is 75% wide. Defaults to 1. */
18+
lines?: number;
19+
height?: never;
20+
}
21+
22+
export interface SkeletonRectangleProps extends SkeletonBaseProps {
23+
shape: 'rectangle';
24+
/** Any CSS height (`4rem`, `var(--…)`). Defaults to filling the parent. */
25+
height?: string;
26+
size?: never;
27+
lines?: never;
28+
}
29+
30+
export type SkeletonProps = SkeletonTextProps | SkeletonRectangleProps;

‎src/components/Skeleton/index.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export { Skeleton } from './Skeleton';
2+
export type {
3+
SkeletonProps,
4+
SkeletonRectangleProps,
5+
SkeletonShape,
6+
SkeletonSize,
7+
SkeletonTextProps,
8+
} from './Skeleton.types';

‎src/components/Table/Table.module.css‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,10 @@
389389
word-break: break-all;
390390
overflow-wrap: break-word;
391391
}
392+
393+
/* Lay the skeleton out like the truncated text it stands in for (an inline block aligned to the
394+
text bottom, see EllipsisContent), so a placeholder row is as tall as a data row. */
395+
.table__data .table__skeleton {
396+
display: inline-flex;
397+
vertical-align: text-bottom;
398+
}

0 commit comments

Comments
 (0)