diff --git a/CHANGELOG.md b/CHANGELOG.md index 59e3f20..28ad3f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 3.6.0 + +### Features + +- Add optional `children` prop. If set, the skeleton will render its children with `visibility: hidden`. (#239) + ## 3.5.0 ### Features diff --git a/README.md b/README.md index 50d7628..1446c77 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,9 @@ import Skeleton from 'react-loading-skeleton' import 'react-loading-skeleton/dist/skeleton.css' // Simple, single-line loading skeleton + +
Loading...
+
// Simple, with children // Five-line loading skeleton ``` @@ -166,6 +169,14 @@ return ( + + children?: React.ReactNode | JSX.Element | string + + If you pass children to the Skeleton component, the + children will be rendered with visibility: hidden. + + + diff --git a/package.json b/package.json index 860e1cc..2793c4e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-loading-skeleton", - "version": "3.5.0", + "version": "3.6.0", "description": "Make beautiful, animated loading skeletons that automatically adapt to your app.", "keywords": [ "react", diff --git a/src/Skeleton.tsx b/src/Skeleton.tsx index bf10d70..769769c 100644 --- a/src/Skeleton.tsx +++ b/src/Skeleton.tsx @@ -1,5 +1,10 @@ /* eslint-disable react/no-array-index-key */ -import React, { CSSProperties, PropsWithChildren, ReactElement } from 'react'; +import React, { + CSSProperties, + PropsWithChildren, + ReactElement, + ReactNode, +} from 'react'; import { SkeletonThemeContext } from './SkeletonThemeContext.js'; import { SkeletonStyleProps } from './SkeletonStyleProps.js'; @@ -59,6 +64,8 @@ export interface SkeletonProps extends SkeletonStyleProps { circle?: boolean; style?: CSSProperties; + + children?: ReactNode | JSX.Element | string; } export function Skeleton({ @@ -72,6 +79,8 @@ export function Skeleton({ circle = false, style: styleProp, + + children, ...originalPropsStyleOptions }: SkeletonProps): ReactElement { const contextStyleOptions = React.useContext(SkeletonThemeContext); @@ -104,6 +113,12 @@ export function Skeleton({ const inline = styleOptions.inline ?? false; + const childrenContent = children ? ( +
{children}
+ ) : ( + <>‌ + ); + const elements: ReactElement[] = []; const countCeil = Math.ceil(count); @@ -133,7 +148,7 @@ export function Skeleton({ const skeletonSpan = ( - ‌ + {childrenContent} ); diff --git a/src/__stories__/Skeleton.stories.tsx b/src/__stories__/Skeleton.stories.tsx index 5892fb9..da834e7 100644 --- a/src/__stories__/Skeleton.stories.tsx +++ b/src/__stories__/Skeleton.stories.tsx @@ -30,6 +30,16 @@ export const Inline: React.FC = () => ( ); +export const WithChildren: React.FC = () => ( + +
+ +
Loading...
+
+
+
+); + export const InlineWithText: React.FC = () => (
Some random text Some more random text diff --git a/src/__tests__/Skeleton.test.tsx b/src/__tests__/Skeleton.test.tsx index 062f0bf..bdc21e6 100644 --- a/src/__tests__/Skeleton.test.tsx +++ b/src/__tests__/Skeleton.test.tsx @@ -23,6 +23,21 @@ it('renders a skeleton', () => { expect(skeletonElements[0]).not.toHaveAttribute('style'); }); +it('renders a skeleton with children', () => { + render( + +
Loading...
+
+ ); + + const skeletonElements = getAllSkeletons(); + + expect(skeletonElements).toHaveLength(1); + expect(skeletonElements[0]).toBeVisible(); + + expect(skeletonElements[0]).toHaveTextContent('Loading...'); +}); + it('renders the required number of skeletons', () => { render();