|
| 1 | +# Skeleton |
| 2 | + |
| 3 | +Shows a placeholder for loading content. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Supports customizable width, height, and shape to fit various UI elements. |
| 8 | + |
| 9 | +- Offers manual control over visibility and rendering duration. |
| 10 | + |
| 11 | +## Import |
| 12 | + |
| 13 | +```tsx |
| 14 | +import { Skeleton } from 'qwik-primitives'; |
| 15 | +``` |
| 16 | + |
| 17 | +## Anatomy |
| 18 | + |
| 19 | +Import all parts and piece them together. |
| 20 | + |
| 21 | +```tsx |
| 22 | +import { component$ } from '@builder.io/qwik'; |
| 23 | +import { Skeleton } from 'qwik-primitives'; |
| 24 | + |
| 25 | +const SkeletonDemo = component$(() => { |
| 26 | + return ( |
| 27 | + <Skeleton.Root> |
| 28 | + <Skeleton.Content /> |
| 29 | + <Skeleton.Fallback /> |
| 30 | + </Skeleton.Root> |
| 31 | + ); |
| 32 | +}); |
| 33 | +``` |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +```tsx |
| 38 | +import { component$, useSignal, useVisibleTask$ } from '@builder.io/qwik'; |
| 39 | +import { Skeleton } from 'qwik-primitives'; |
| 40 | + |
| 41 | +const SkeletonDemo = component$(() => { |
| 42 | + const isLoading = useSignal(true); |
| 43 | + |
| 44 | + // eslint-disable-next-line qwik/no-use-visible-task |
| 45 | + useVisibleTask$(() => { |
| 46 | + setTimeout(() => { |
| 47 | + isLoading.value = false; |
| 48 | + }, 5000); |
| 49 | + }); |
| 50 | + |
| 51 | + return ( |
| 52 | + <Skeleton.Root loading={isLoading.value}> |
| 53 | + <Skeleton.Content> |
| 54 | + {!isLoading.value && ( |
| 55 | + <p style={{ height: '6rem', overflow: 'hidden' }}> |
| 56 | + Lorem ipsum dolor sit amet consectetur adipisicing elit. Laudantium sunt, cumque praesentium qui nobis |
| 57 | + tenetur aliquam repellendus neque dicta ipsam reprehenderit dolores voluptas architecto blanditiis vel, |
| 58 | + exercitationem at. Natus, aliquam! |
| 59 | + </p> |
| 60 | + )} |
| 61 | + </Skeleton.Content> |
| 62 | + |
| 63 | + <Skeleton.Fallback style={{ height: '6rem', backgroundColor: 'purple' }} /> |
| 64 | + </Skeleton.Root> |
| 65 | + ); |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +## API Reference |
| 70 | + |
| 71 | +### Root |
| 72 | + |
| 73 | +Contains all the parts of a skeleton. By default, it will be rendered with the passed children only if the `loading` prop is set to `true` otherwise the component will render the passed children. Use the `forceMount` prop to always render this component with the passed children. This component is based on the `div` element. |
| 74 | + |
| 75 | +| Prop | Type | Default | Description | |
| 76 | +| ------------ | ------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 77 | +| `as` | `FunctionComponent` | `-` | Change the default rendered element for the one passed as, merging their props and behavior. Read our [Composition](https://github.com/ZAHON/qwik-primitives/blob/main/packages/primitives/docs/composition.md) guide for more details. | |
| 78 | +| `loading` | `boolean` | `-` | Toggles whether to hide the content and display the fallback. | |
| 79 | +| `forceMount` | `boolean` | `-` | Used to force mounting when more control is needed. | |
| 80 | +| `style` | `CSSProperties` | `-` | The inline style for the element. | |
| 81 | + |
| 82 | +| Data attribute | Values | |
| 83 | +| -------------------- | ----------------------------------------- | |
| 84 | +| `[data-scope]` | `"skeleton"` | |
| 85 | +| `[data-part]` | `"root"` | |
| 86 | +| `[data-loading]` | Present when the skeleton is loading | |
| 87 | +| `[data-force-mount]` | Present when the component is force mount | |
| 88 | + |
| 89 | +### Content |
| 90 | + |
| 91 | +Contains loading content. By default, it will be rendered with the passed children only if the `loading` prop is set to `true` on `Skeleton.Root` component otherwise the component will render the passed children. Use the `forceMount` prop to always render this component with the passed children. This component is based on the `div` element. |
| 92 | + |
| 93 | +| Prop | Type | Default | Description | |
| 94 | +| ------------ | ------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 95 | +| `as` | `FunctionComponent` | `-` | Change the default rendered element for the one passed as, merging their props and behavior. Read our [Composition](https://github.com/ZAHON/qwik-primitives/blob/main/packages/primitives/docs/composition.md) guide for more details. | |
| 96 | +| `forceMount` | `boolean` | `-` | Used to force mounting when more control is needed. | |
| 97 | +| `style` | `CSSProperties` | `-` | The inline style for the element. | |
| 98 | + |
| 99 | +| Data attribute | Values | |
| 100 | +| -------------------- | --------------------------------------- | |
| 101 | +| `[data-scope]` | `"skeleton"` | |
| 102 | +| `[data-part]` | `"content"` | |
| 103 | +| `[data-loading]` | Present when the skeleton is loading | |
| 104 | +| `[data-force-mount]` | Present when the content is force mount | |
| 105 | + |
| 106 | +### Fallback |
| 107 | + |
| 108 | +Contains alternate content while the actual content has not yet been loaded. By default, it will render only when the `loading` prop is set to `true` on `Skeleton.Root component,` use the `forceMount` prop to always render this component. This component is based on the `div` element. |
| 109 | + |
| 110 | +| Prop | Type | Default | Description | |
| 111 | +| ------------ | ------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 112 | +| `as` | `FunctionComponent` | `-` | Change the default rendered element for the one passed as, merging their props and behavior. Read our [Composition](https://github.com/ZAHON/qwik-primitives/blob/main/packages/primitives/docs/composition.md) guide for more details. | |
| 113 | +| `forceMount` | `boolean` | `-` | Used to force mounting when more control is needed. | |
| 114 | +| `style` | `CSSProperties` | `-` | The inline style for the element. | |
| 115 | + |
| 116 | +| Data attribute | Values | |
| 117 | +| -------------------- | ---------------------------------------- | |
| 118 | +| `[data-scope]` | `"skeleton"` | |
| 119 | +| `[data-part]` | `"fallback"` | |
| 120 | +| `[data-loading]` | Present when the skeleton is loading | |
| 121 | +| `[data-force-mount]` | Present when the fallback is force mount | |
| 122 | + |
| 123 | +## Examples |
| 124 | + |
| 125 | +### With content that is already on page |
| 126 | + |
| 127 | +If you want to indicate the loading state of content that is already on page, wrap it with `Skeleton.Content` and control `Skeleton.Fallback` visibility with `loading` prop. |
| 128 | + |
| 129 | +```tsx |
| 130 | +import { component$, useSignal } from '@builder.io/qwik'; |
| 131 | +import { Skeleton } from 'qwik-primitives'; |
| 132 | + |
| 133 | +const SkeletonDemo = component$(() => { |
| 134 | + const isLoading = useSignal(false); |
| 135 | + |
| 136 | + return ( |
| 137 | + <> |
| 138 | + <Skeleton.Root loading={isLoading.value}> |
| 139 | + <Skeleton.Content> |
| 140 | + <p> |
| 141 | + Lorem ipsum dolor sit amet consectetur adipisicing elit. Laudantium sunt, cumque praesentium qui nobis |
| 142 | + tenetur aliquam repellendus neque dicta ipsam reprehenderit dolores voluptas architecto blanditiis vel, |
| 143 | + exercitationem at. Natus, aliquam! |
| 144 | + </p> |
| 145 | + </Skeleton.Content> |
| 146 | + |
| 147 | + <Skeleton.Fallback style={{ backgroundColor: 'purple' }} /> |
| 148 | + </Skeleton.Root> |
| 149 | + |
| 150 | + <button type="button" onClick$={() => (isLoading.value = !isLoading.value)}> |
| 151 | + Toggle content |
| 152 | + </button> |
| 153 | + </> |
| 154 | + ); |
| 155 | +}); |
| 156 | +``` |
0 commit comments