Skip to content

Commit 020fb28

Browse files
Children prop with hidden visibility
1 parent f8b040d commit 020fb28

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

src/Skeleton.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/* eslint-disable react/no-array-index-key */
2-
import React, { CSSProperties, PropsWithChildren, ReactElement } from 'react';
2+
import React, {
3+
CSSProperties,
4+
PropsWithChildren,
5+
ReactElement,
6+
ReactNode,
7+
} from 'react';
38
import { SkeletonThemeContext } from './SkeletonThemeContext.js';
49
import { SkeletonStyleProps } from './SkeletonStyleProps.js';
510

@@ -59,6 +64,8 @@ export interface SkeletonProps extends SkeletonStyleProps {
5964

6065
circle?: boolean;
6166
style?: CSSProperties;
67+
68+
children?: ReactNode | JSX.Element | string;
6269
}
6370

6471
export function Skeleton({
@@ -72,6 +79,8 @@ export function Skeleton({
7279
circle = false,
7380

7481
style: styleProp,
82+
83+
children,
7584
...originalPropsStyleOptions
7685
}: SkeletonProps): ReactElement {
7786
const contextStyleOptions = React.useContext(SkeletonThemeContext);
@@ -104,6 +113,12 @@ export function Skeleton({
104113

105114
const inline = styleOptions.inline ?? false;
106115

116+
const childrenContent = children ? (
117+
<div style={{ visibility: 'hidden' }}>{children}</div>
118+
) : (
119+
<>&zwnj;</>
120+
);
121+
107122
const elements: ReactElement[] = [];
108123

109124
const countCeil = Math.ceil(count);
@@ -133,7 +148,7 @@ export function Skeleton({
133148

134149
const skeletonSpan = (
135150
<span className={className} style={thisStyle} key={i}>
136-
&zwnj;
151+
{childrenContent}
137152
</span>
138153
);
139154

src/__stories__/Skeleton.stories.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ export const Inline: React.FC = () => (
3030
</SideBySide>
3131
);
3232

33+
export const WithChildren: React.FC = () => (
34+
<SideBySide>
35+
<div>
36+
<Skeleton width={100} inline style={{ marginRight: '0.5rem' }}>
37+
<div>Loading...</div>
38+
</Skeleton>
39+
</div>
40+
</SideBySide>
41+
);
42+
3343
export const InlineWithText: React.FC = () => (
3444
<div>
3545
Some random text <Skeleton width={150} inline /> Some more random text

src/__tests__/Skeleton.test.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ it('renders a skeleton', () => {
2323
expect(skeletonElements[0]).not.toHaveAttribute('style');
2424
});
2525

26+
it('renders a skeleton with children', () => {
27+
render(
28+
<Skeleton>
29+
<div>Loading...</div>
30+
</Skeleton>
31+
);
32+
33+
const skeletonElements = getAllSkeletons();
34+
35+
expect(skeletonElements).toHaveLength(1);
36+
expect(skeletonElements[0]).toBeVisible();
37+
38+
expect(skeletonElements[0]).toHaveTextContent('Loading...');
39+
});
40+
2641
it('renders the required number of skeletons', () => {
2742
render(<Skeleton count={4} />);
2843

0 commit comments

Comments
 (0)