Skip to content

Instantly share code, notes, and snippets.

@akoepcke
Forked from stowball/placeholder-image.js
Created March 1, 2023 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akoepcke/41db5c72ad53a1103463e75e1aca6cd7 to your computer and use it in GitHub Desktop.
Save akoepcke/41db5c72ad53a1103463e75e1aca6cd7 to your computer and use it in GitHub Desktop.
Placeholder React Components
function PlaceholderImage({
height,
width,
...consumerProps
}) {
return (
<img
{...consumerProps}
alt=""
src={`https://via.placeholder.com/${width}x${height}`}
/>
);
}
const lipsum =
'Lorem ipsum dolor sit amet consectetur adipisicing elit. Molestias quo voluptas fugit quidem nemo autem eos reprehenderit voluptatum nam. Error odio repellendus quisquam magni rerum hic facere laboriosam, aspernatur fugiat.';
function PlaceholderText({
characters,
...consumerProps
}) {
const string =
characters <= lipsum.length
? lipsum.slice(0, characters)
: lipsum
.repeat(Math.ceil(characters / lipsum.length))
.slice(0, characters);
return (
<span
{...consumerProps}
style={{
backgroundImage:
'repeating-linear-gradient(to bottom, #fff 0px, #fff 2px, #bbb 2px, #bbb 10px, #fff 10px, #fff 18px)',
display: 'inline-block',
fontSize: 12,
lineHeight: 1,
}}
>
<span style={{ visibility: 'hidden' }}>{string}</span>
</span>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment