Skip to content

Commit

Permalink
expose Skeleton publicly
Browse files Browse the repository at this point in the history
  • Loading branch information
mayank99 committed Feb 13, 2024
1 parent d56a91c commit 7b9f11b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-laws-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@itwin/itwinui-react': minor
---

Added a new `Skeleton` component.
14 changes: 14 additions & 0 deletions packages/itwinui-react/src/core/Typography/Skeleton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import { render } from '@testing-library/react';

import { Skeleton } from './Skeleton.js';

it('should work', () => {
const { container } = render(<Skeleton />);
const skeleton = container.querySelector('div') as HTMLElement;
expect(skeleton).toHaveClass('iui-skeleton');
});
55 changes: 55 additions & 0 deletions packages/itwinui-react/src/core/Typography/Skeleton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import * as React from 'react';
import cx from 'classnames';
import { Box } from '../utils/index.js';
import type { PolymorphicForwardRefComponent } from '../utils/index.js';
import { VisuallyHidden } from '../VisuallyHidden/VisuallyHidden.js';

// ----------------------------------------------------------------------------

const SkeletonContext = React.createContext<boolean | undefined>(undefined);

// ----------------------------------------------------------------------------

const SkeletonComponent = React.forwardRef((props, forwardedRef) => {
const isGrouped = React.useContext(SkeletonContext);

return (
<Box
as='div'
ref={forwardedRef}
{...props}
className={cx('iui-skeleton', props?.className)}
>
<div aria-hidden {...{ inert: '' }}>
{props.children}
</div>
{!isGrouped && <VisuallyHidden>Loading</VisuallyHidden>}
</Box>
);
}) as PolymorphicForwardRefComponent<'div'>;

// ----------------------------------------------------------------------------

const SkeletonGroup = ({ children }: { children: React.ReactNode }) => {
return (
<SkeletonContext.Provider value={true}>
{children}
<VisuallyHidden>Loading</VisuallyHidden>
</SkeletonContext.Provider>
);
};
SkeletonGroup.displayName = 'Skeleton.Group';

// ----------------------------------------------------------------------------

/**
* A placeholder displayed while the content is loading.
*/
export const Skeleton = Object.assign(SkeletonComponent, {
Group: SkeletonGroup,
});
Skeleton.displayName = 'Skeleton';
2 changes: 2 additions & 0 deletions packages/itwinui-react/src/core/Typography/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type TextProps = {
/**
* Use it if you are still loading the content.
* @default false
*
* @deprecated Use the `Skeleton` component instead
*/
isSkeleton?: boolean;
};
Expand Down
2 changes: 2 additions & 0 deletions packages/itwinui-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,5 @@ export { Flex } from './core/Flex/Flex.js';
export { Popover } from './core/Popover/Popover.js';

export { Divider } from './core/Divider/Divider.js';

export { Skeleton } from './core/Typography/Skeleton.js';

0 comments on commit 7b9f11b

Please sign in to comment.