An open source design system that's fully customizable and agent ready
TypeScript
XDSSkeleton (#22)
Cindy Zhangd5d1508a63fa
diff --git a/apps/storybook/stories/Skeleton.stories.tsx b/apps/storybook/stories/Skeleton.stories.tsxnew file mode 100644index 0000000000..f4102d553c--- /dev/null+++ b/apps/storybook/stories/Skeleton.stories.tsx@@ -0,0 +1,99 @@+import type { Meta, StoryObj } from '@storybook/react';+import { XDSSkeleton } from '@xds/core/Skeleton';+import { XDSHStack, XDSVStack, XDSCard } from '@xds/core/Layout';++const meta: Meta<typeof XDSSkeleton> = {+ title: 'Core/XDSSkeleton',+ component: XDSSkeleton,+ tags: ['autodocs'],+ argTypes: {+ width: {+ control: 'text',+ description: 'Width (number for px, string for any CSS value)',+ },+ height: {+ control: 'text',+ description: 'Height (number for px, string for any CSS value)',+ },+ radius: {+ control: 'select',+ options: ['none', 'inner', 'content', 'element', 'container', 'rounded'],+ description: 'Border radius using design tokens',+ },+ index: {+ control: { type: 'number', min: 0, max: 10, step: 1 },+ description: 'Index for staggered animation timing',+ },+ },+};++export default meta;+type Story = StoryObj<typeof XDSSkeleton>;++export const Default: Story = {+ args: {+ width: 200,+ height: 20,+ radius: 'container',+ index: 0,+ },+};++export const Shapes: Story = {+ render: () => (+ <XDSHStack gap="space4" vAlign="center">+ <XDSSkeleton width={40} height={40} radius="rounded" />+ <XDSSkeleton width={100} height={20} radius="container" />+ <XDSSkeleton width={120} height={32} radius="element" />+ <XDSSkeleton width={80} height={80} radius="none" />+ </XDSHStack>+ ),+};++export const StaggeredList: Story = {+ render: () => (+ <XDSVStack gap="space2">+ <XDSSkeleton width={300} height={16} index={0} />+ <XDSSkeleton width={280} height={16} index={1} />+ <XDSSkeleton width={320} height={16} index={2} />+ <XDSSkeleton width={260} height={16} index={3} />+ <XDSSkeleton width={290} height={16} index={4} />+ </XDSVStack>+ ),+};++export const CardSkeleton: Story = {+ render: () => (+ <XDSCard width={320}>+ <XDSVStack gap="space3">+ {/* Avatar and name row */}+ <XDSHStack gap="space3" vAlign="center">+ <XDSSkeleton width={40} height={40} radius="rounded" index={0} />+ <XDSVStack gap="space1">+ <XDSSkeleton width={120} height={14} index={1} />+ <XDSSkeleton width={80} height={12} index={2} />+ </XDSVStack>+ </XDSHStack>+ {/* Content lines */}+ <XDSSkeleton width="100%" height={14} index={3} />+ <XDSSkeleton width="90%" height={14} index={4} />+ <XDSSkeleton width="75%" height={14} index={5} />+ </XDSVStack>+ </XDSCard>+ ),+};++export const TableRowSkeleton: Story = {+ render: () => (+ <XDSVStack gap="space2">+ {[0, 1, 2, 3].map((rowIndex) => (+ <XDSHStack key={rowIndex} gap="space4" vAlign="center">+ <XDSSkeleton width={50} height={16} index={rowIndex * 4} />+ <XDSSkeleton width={180} height={16} index={rowIndex * 4 + 1} />+ <XDSSkeleton width={100} height={16} index={rowIndex * 4 + 2} />+ <XDSSkeleton width={80} height={16} index={rowIndex * 4 + 3} />+ </XDSHStack>+ ))}+ </XDSVStack>+ ),+};diff --git a/packages/core/package.json b/packages/core/package.jsonindex 1536f5cf70..a0b4577ace 100644--- a/packages/core/package.json+++ b/packages/core/package.json@@ -32,6 +32,11 @@"import": "./dist/Layout/index.mjs","require": "./dist/Layout/index.js"},+ "./Skeleton": {+ "types": "./dist/Skeleton/index.d.ts",+ "import": "./dist/Skeleton/index.mjs",+ "require": "./dist/Skeleton/index.js"+ },"./TextInput": {"types": "./dist/TextInput/index.d.ts","import": "./dist/TextInput/index.mjs",diff --git a/packages/core/src/Skeleton/README.md b/packages/core/src/Skeleton/README.mdnew file mode 100644index 0000000000..07360b232a--- /dev/null+++ b/packages/core/src/Skeleton/README.md@@ -0,0 +1,80 @@+# /packages/core/src/Skeleton++A placeholder loading component that displays an animated pulsing effect while content is loading.++<!-- SYNC: When files in this directory change, update this document. -->++## Features++- **Pulsing Animation**: Smooth opacity animation using stepped timing for a subtle shimmer effect+- **Staggered Animation**: Sequential skeletons can be staggered to create a wave effect+- **High Contrast Support**: Automatically adjusts for users with `prefers-contrast: more`+- **Flexible Sizing**: Width and height props accept pixels or any CSS value+- **Token-aligned Radius**: Border radius options map directly to design tokens++## Usage++```tsx+import { XDSSkeleton } from '@xds/core/Skeleton';++// Basic text placeholder+<XDSSkeleton width={200} height={16} />++// Circular avatar placeholder+<XDSSkeleton width={40} height={40} radius="rounded" />++// Full-width with percentage+<XDSSkeleton width="100%" height={20} />++// Staggered animation for multiple skeletons+<div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>+ <XDSSkeleton width={300} height={16} index={0} />+ <XDSSkeleton width={280} height={16} index={1} />+ <XDSSkeleton width={320} height={16} index={2} />+</div>+```++## Props++| Prop | Type | Default | Description |+|------|------|---------|-------------|+| `width` | `number \| string` | `'100%'` | Width in pixels (number) or CSS value (string) |+| `height` | `number \| string` | `'100%'` | Height in pixels (number) or CSS value (string) |+| `radius` | `'none' \| 'inner' \| 'content' \| 'element' \| 'container' \| 'rounded'` | `'container'` | Border radius using design tokens |+| `index` | `number` | `0` | Index for staggered animation timing |+| `data-testid` | `string` | — | Test ID for testing |++## Radius Options++| Value | Use Case |+|-------|----------|+| `none` | Sharp corners (images, rectangles) |+| `inner` | Inner elements |+| `content` | Content areas |+| `element` | Standard elements |+| `container` | Container-level rounding (default) |+| `rounded` | Fully rounded (avatars, pills, circles) |++## Animation Timing++The skeleton uses three timing constants:++- **DELAY_TIME** (1000ms): Initial delay before animation starts+- **FADE_TIME** (1000ms): Duration of one opacity cycle+- **STAGGER_TIME** (100ms): Delay increment between sequential elements++For element at index `n`, animation starts at: `DELAY_TIME + (STAGGER_TIME × n)`++## Files++| File | Role | Purpose |+|------|------|---------|+| `index.ts` | Entry | Exports component and types |+| `XDSSkeleton.tsx` | Core | Component implementation |++## Implementation Notes++- Uses `steps(10, end)` timing function for a subtle shimmer effect+- Animation alternates between 0.25 and 1.0 opacity+- Background color comes from the `glimmer` token, with `glimmerHighContrast` for accessibility+- Numeric dimensions are converted to pixels; strings are passed through as-isdiff --git a/packages/core/src/Skeleton/XDSSkeleton.tsx b/packages/core/src/Skeleton/XDSSkeleton.tsxnew file mode 100644index 0000000000..d688cdf2d0--- /dev/null+++ b/packages/core/src/Skeleton/XDSSkeleton.tsx@@ -0,0 +1,176 @@+/**+ * @file XDSSkeleton.tsx+ * @input Uses React, StyleX keyframes and tokens+ * @output Exports XDSSkeleton component, XDSSkeletonProps, XDSSkeletonRadius types+ * @position Core implementation of skeleton loading placeholder+ *+ * SYNC: When modified, update these files to stay in sync:+ * - /packages/core/src/Skeleton/README.md+ * - /packages/core/src/Skeleton/index.ts+ * - /apps/storybook/stories/Skeleton.stories.tsx+ */++import { forwardRef, type HTMLAttributes } from 'react';+import * as stylex from '@stylexjs/stylex';+import { color, radius } from '../theme/tokens.stylex';++// =============================================================================+// Animation Timing Constants+// =============================================================================++/**+ * Initial delay before animation starts (ms).+ * Prevents flash of animation for fast-loading content.+ */+const DELAY_TIME = 1000;++/**+ * Duration of one fade cycle (ms).+ * Controls how quickly the skeleton pulses between opacity values.+ */+const FADE_TIME = 1000;++/**+ * Stagger increment between sequential skeleton elements (ms).+ * Creates a wave effect when multiple skeletons are used together.+ */+const STAGGER_TIME = 100;++// =============================================================================+// Animation Keyframes+// =============================================================================++const skeletonFade = stylex.keyframes({+ '0%': { opacity: 0.25 },+ '100%': { opacity: 1 },+});++// =============================================================================+// Styles+// =============================================================================++const styles = stylex.create({+ root: {+ backgroundColor: {+ default: color.glimmer,+ '@media (prefers-contrast: more)': color.glimmerHighContrast,+ },+ opacity: 0.25,+ },+ animate: {+ animationDirection: 'alternate',+ animationDuration: `${FADE_TIME}ms`,+ animationIterationCount: 'infinite',+ animationName: skeletonFade,+ animationTimingFunction: 'steps(10, end)',+ },+});++const radiusStyles = stylex.create({+ none: {+ borderRadius: 0,+ },+ rounded: {+ borderRadius: radius.rounded,+ },+ container: {+ borderRadius: radius.container,+ },+ element: {+ borderRadius: radius.element,+ },+ content: {+ borderRadius: radius.content,+ },+ inner: {+ borderRadius: radius.inner,+ },+});++const dynamicStyles = stylex.create({+ animationDelay: (index: number) => ({+ animationDelay: `${DELAY_TIME + STAGGER_TIME * index}ms`,+ }),+ dimensions: (width: number | string, height: number | string) => ({+ width: typeof width === 'number' ? `${width}px` : width,+ height: typeof height === 'number' ? `${height}px` : height,+ }),+});++// =============================================================================+// Types+// =============================================================================++export type XDSSkeletonRadius = keyof typeof radiusStyles;++// =============================================================================+// Component+// =============================================================================++export interface XDSSkeletonProps extends HTMLAttributes<HTMLDivElement> {+ /**+ * Width of the skeleton.+ * Accepts a number (pixels) or string (any CSS value).+ * @default '100%'+ */+ width?: number | string;+ /**+ * Height of the skeleton.+ * Accepts a number (pixels) or string (any CSS value).+ * @default '100%'+ */+ height?: number | string;+ /**+ * Border radius of the skeleton, using design token names.+ * - 'none': No border radius (sharp corners)+ * - 'inner': Inner radius+ * - 'content': Content radius+ * - 'element': Element radius+ * - 'container': Container radius (default)+ * - 'rounded': Fully rounded (for avatars, pills)+ * @default 'container'+ */+ radius?: XDSSkeletonRadius;+ /**+ * Index for staggered animation timing.+ * Use sequential indices (0, 1, 2, ...) for multiple skeletons+ * to create a wave effect.+ * @default 0+ */+ index?: number;+ /**+ * Test ID for testing purposes.+ */+ 'data-testid'?: string;+}++export const XDSSkeleton = forwardRef<HTMLDivElement, XDSSkeletonProps>(+ (+ {+ width = '100%',+ height = '100%',+ radius: radiusProp = 'container',+ index = 0,+ 'data-testid': testId,+ ...props+ },+ ref+ ) => {+ return (+ <div+ ref={ref}+ data-testid={testId}+ {...stylex.props(+ styles.root,+ styles.animate,+ radiusStyles[radiusProp],+ dynamicStyles.dimensions(width, height),+ dynamicStyles.animationDelay(index)+ )}+ {...props}+ />+ );+ }+);++XDSSkeleton.displayName = 'XDSSkeleton';diff --git a/packages/core/src/Skeleton/index.ts b/packages/core/src/Skeleton/index.tsnew file mode 100644index 0000000000..387556a097--- /dev/null+++ b/packages/core/src/Skeleton/index.ts@@ -0,0 +1,2 @@+export { XDSSkeleton } from './XDSSkeleton';+export type { XDSSkeletonProps, XDSSkeletonRadius } from './XDSSkeleton';diff --git a/packages/core/src/index.ts b/packages/core/src/index.tsindex 7de5aa0eb1..460fee6711 100644--- a/packages/core/src/index.ts+++ b/packages/core/src/index.ts@@ -19,5 +19,8 @@ export * from './Layout';// Layer utilities and components (includes XDSHoverCard)export * from './Layer';+// Skeleton loading placeholder+export * from './Skeleton';+// Themeexport * from './theme';diff --git a/packages/core/tsup.config.ts b/packages/core/tsup.config.tsindex 7380b294d2..1084871bc8 100644--- a/packages/core/tsup.config.ts+++ b/packages/core/tsup.config.ts@@ -11,7 +11,7 @@ import { defineConfig } from 'tsup';import babel from 'esbuild-plugin-babel';export default defineConfig({- entry: ['src/index.ts', 'src/Avatar/index.ts', 'src/Button/index.ts', 'src/Field/index.ts', 'src/Layout/index.ts', 'src/TextInput/index.ts'],+ entry: ['src/index.ts', 'src/Avatar/index.ts', 'src/Button/index.ts', 'src/Field/index.ts', 'src/Layout/index.ts', 'src/Skeleton/index.ts', 'src/TextInput/index.ts'],format: ['cjs', 'esm'],dts: true,splitting: true,