Skip to content

Commit

Permalink
Style Refactor Skeleton - Addressed Manny's comments
Browse files Browse the repository at this point in the history
Updated Upgrade guide note
  • Loading branch information
thunguyen19 committed Jan 15, 2025
1 parent 54842d8 commit 7490b0b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 73 deletions.
22 changes: 22 additions & 0 deletions modules/docs/mdx/13.0-UPGRADE-GUIDE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ any questions.

- [Codemod](#codemod)
- [Instructions](#instructions)
- [Component Updates](#component-updates)
- [Styling API and CSS Tokens](#styling-api-and-css-tokens)
- [Skeleton](#skeleton)
- [Troubleshooting](#troubleshooting)
- [Glossary](#glossary)
- [Main](#main)
Expand Down Expand Up @@ -86,6 +89,25 @@ yarn remove @workday/canvas-kit-codemod
> your project conventions.

### Styling API and CSS Tokens

Some components have been refactored to use our new
[Canvas Tokens](https://workday.github.io/canvas-tokens/?path=/docs/docs-getting-started--docs) and
[styling API](https://workday.github.io/canvas-kit/?path=/docs/styling-basics--create-modifiers#createstyles-api).
The React interface **has not changed**, but CSS variables are now used for dynamic properties.

> **Note:** These components also support our new `cs` prop for styling. Learn more about styling
> with `cs` in our
> [documentation](https://workday.github.io/canvas-kit/?path=/docs/styling-basics--cs-prop).
### Skeleton

**PR** [#2793](https://github.com/Workday/canvas-kit/pull/3088)

Skeleton has been updated to use our new
[styling utilities](https://workday.github.io/canvas-kit/?path=/docs/styling-basics--create-stencil).


## Troubleshooting

## Glossary
Expand Down
113 changes: 40 additions & 73 deletions modules/react/skeleton/lib/parts/SkeletonText.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import * as React from 'react';

import {createComponent} from '@workday/canvas-kit-react/common';
import {
calc,
createStencil,
CSProps,
cssVar,
handleCsProp,
px2rem,
} from '@workday/canvas-kit-styling';
import {createStencil, cssVar, handleCsProp, px2rem} from '@workday/canvas-kit-styling';
import {system} from '@workday/canvas-tokens-web';

export interface SkeletonTextProps {
Expand All @@ -24,78 +17,52 @@ export interface SkeletonTextProps {
backgroundColor?: string;
}

export interface SkeletonTextLineProps extends CSProps {
/**
* The width of the line in `px` or `%`.
*/
width?: number | string;
/**
* The background color of the skeleton
* @default `system.color.bg.alt.strong`
*/
backgroundColor?: string;
/**
* The borderRadius of the shape in `px` or `%`.
* @default 2px
*/
borderRadius?: number | string;
}

const skeletonTextLineStencil = createStencil({
export const skeletonTextStencil = createStencil({
vars: {
width: '',
backgroundColor: '',
borderRadius: '',
},
base: ({width, borderRadius, backgroundColor}) => ({
backgroundColor: cssVar(backgroundColor, system.color.bg.alt.strong),
borderRadius: cssVar(borderRadius, calc.divide(system.space.x1, 2)), // borderRadius.s // 2px
height: cssVar(calc.subtract(system.space.x6, px2rem(3))),
width: width,
marginBottom: system.space.x3,
base: ({backgroundColor}) => ({
marginBottom: system.space.x6,
'& [data-part="skeleton-text-lines"], & [data-part="skeleton-text-single-or-last-line"]': {
backgroundColor: cssVar(backgroundColor, system.color.bg.alt.strong),
height: px2rem(21),
marginBlockEnd: system.space.x2,
borderRadius: system.shape.half,
},
'& [data-part="skeleton-text-lines"]': {
width: '100%',
},
'& [data-part="skeleton-text-single-or-last-line"]': {
width: '60%',
},
}),
});

const textContainerStencil = createStencil({
base: () => ({
marginBottom: system.space.x4,
}),
});

const Line = createComponent('div')<SkeletonTextLineProps>({
displayName: 'Skeleton.TextLine',
Component: ({width, backgroundColor, ...elemProps}, ref, Element) => (
<Element
ref={ref}
{...handleCsProp(
elemProps,
skeletonTextLineStencil({
width: typeof width === 'number' ? px2rem(width) : width,
backgroundColor,
})
)}
/>
),
});

const createTextLines = (lineCount: number, backgroundColor?: string) => {
const lines = new Array(lineCount).fill(null);

return lines.map((_value, index) => (
<Line
key={index}
backgroundColor={backgroundColor}
width={index < lineCount - 1 || lineCount === 1 ? '100%' : '60%'}
/>
));
};

export const SkeletonText = createComponent('div')({
displayName: 'Skeleton.Text',
Component: ({backgroundColor, lineCount = 2, ...elemProps}: SkeletonTextProps, ref, Element) =>
lineCount <= 0 ? null : (
<Element ref={ref} {...handleCsProp(elemProps, textContainerStencil())}>
{createTextLines(lineCount, backgroundColor)}
Component: ({backgroundColor, lineCount = 2, ...elemProps}: SkeletonTextProps, ref, Element) => {
const lines = new Array(lineCount).fill(null);
return lineCount <= 0 ? null : (
<Element
ref={ref}
{...handleCsProp(
elemProps,
skeletonTextStencil({
backgroundColor,
})
)}
>
{lines.map((_value, index) => (
<div
data-part={
index < lineCount - 1 || lineCount === 1
? 'skeleton-text-lines'
: 'skeleton-text-single-or-last-line'
}
key={index}
></div>
))}
</Element>
),
);
},
});

0 comments on commit 7490b0b

Please sign in to comment.