|
| 1 | +import * as React from 'react'; |
| 2 | + |
| 3 | +import {createComponent} from '@workday/canvas-kit-react/common'; |
| 4 | +import {createStencil, CSProps, cssVar, handleCsProp, px2rem} from '@workday/canvas-kit-styling'; |
| 5 | +import {system} from '@workday/canvas-tokens-web'; |
| 6 | + |
| 7 | +export interface SkeletonShapeProps extends CSProps { |
| 8 | + /** |
| 9 | + * The width of the shape in `px` or `%`. |
| 10 | + * @default 100% |
| 11 | + */ |
| 12 | + width?: number | string; |
| 13 | + /** |
| 14 | + * The height of the shape in `px` or `%`. |
| 15 | + * @default 100% |
| 16 | + */ |
| 17 | + height?: number | string; |
| 18 | + /** |
| 19 | + * The borderRadius of the shape in `px` or `%`. |
| 20 | + * @default 0 |
| 21 | + */ |
| 22 | + borderRadius?: number | string; |
| 23 | + /** |
| 24 | + * The background color of the skeleton |
| 25 | + * @default `system.color.bg.alt.strong` |
| 26 | + */ |
| 27 | + backgroundColor?: string; |
| 28 | +} |
| 29 | + |
| 30 | +export const skeletonShapeStencil = createStencil({ |
| 31 | + vars: { |
| 32 | + width: '', |
| 33 | + height: '', |
| 34 | + borderRadius: '', |
| 35 | + backgroundColor: '', |
| 36 | + }, |
| 37 | + base: ({width, height, borderRadius, backgroundColor}) => ({ |
| 38 | + backgroundColor: cssVar(backgroundColor, system.color.bg.alt.strong), |
| 39 | + borderRadius: cssVar(borderRadius, system.space.zero), |
| 40 | + height: cssVar(height, '100%'), |
| 41 | + width: width, |
| 42 | + marginBottom: system.space.x4, |
| 43 | + }), |
| 44 | +}); |
| 45 | + |
| 46 | +export const SkeletonShape = createComponent('div')({ |
| 47 | + displayName: 'Skeleton.Shape', |
| 48 | + Component: ( |
| 49 | + {width = '100%', height, backgroundColor, borderRadius, ...elemProps}: SkeletonShapeProps, |
| 50 | + ref, |
| 51 | + Element |
| 52 | + ) => { |
| 53 | + return ( |
| 54 | + <Element |
| 55 | + ref={ref} |
| 56 | + {...handleCsProp( |
| 57 | + elemProps, |
| 58 | + skeletonShapeStencil({ |
| 59 | + width: typeof width === 'number' ? px2rem(width) : width, |
| 60 | + height: typeof height === 'number' ? px2rem(height) : height, |
| 61 | + backgroundColor, |
| 62 | + borderRadius: typeof borderRadius === 'number' ? px2rem(borderRadius) : borderRadius, |
| 63 | + }) |
| 64 | + )} |
| 65 | + /> |
| 66 | + ); |
| 67 | + }, |
| 68 | +}); |
0 commit comments