Imagine we have a UserCard component:
function UserCard(props) {
const { avatarUrl, name, loading } = props
return (
<div className='UserCard'>
<Avatar image={avatarUrl} />
<Name name={name} />
</div>
)
}
Now we want to implement a wireframe loading for it.
- We can't design a wireframe copy of
Avatar
andName
components - their authors may and will change their size and shape, we don't want our skeleton to fall out of sync - We can't insert our loader inside
Avatar
andName
components - we don't have control over them or their css - We can control the space between
Avatar
andName
and wrap them from the outside Avatar
andName
may have some transparent portions / border-radius and we want to draw our skeleton animation over their visible part
Ideally the usage of our Loader
should look somewhat like this:
function UserCard(props) {
const { avatarUrl, name, loading } = props
return (
<div className='UserCard'>
<Loader loading={loading}>
<Avatar image={avatarUrl} />
</Loader>
<Loader loading={loading}>
<Name name={loading ? 'some text to control the size of the skeleton' : name} />
</Loader>
</div>
)
}
What are the ways to achieve it?
What if we have similar restrictions but want to add skeleton loading to the UserCard
. From outside we know it's a single div wrapping other divs and we want to show a skeleton instead of each of those child divs.