Skip to content

Commit

Permalink
feat: adds blur placeholders to images
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeherp committed Jun 19, 2021
1 parent 56b6ae1 commit dbe3c72
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/components/atoms/PortfolioItem/PortfolioItem.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { IPortfolioItem } from '@Types';
import { FC } from 'react';
import { StyledPortfolioItem } from './styles';
import Image from 'next/image';
import { IPortfolioItem } from '@Types';
import Link from 'next/link';
import { StyledPortfolioItem } from './styles';
import { shimmer } from 'Utils/shimmer';

export interface PortfolioItemProps {
item: IPortfolioItem;
Expand All @@ -21,6 +22,8 @@ const PortfolioItem: FC<PortfolioItemProps> = ({ item }) => {
height={1080}
objectFit="cover"
alt={title}
placeholder="blur"
blurDataURL={shimmer(1920, 1080)}
/>
</a>
</Link>
Expand Down
3 changes: 1 addition & 2 deletions src/components/atoms/ThemeToggle/ThemeToggle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FC } from 'react';
import { StyledThemeToggle } from './styles';

import { useDispatch, useSelector } from 'react-redux';
Expand All @@ -8,7 +7,7 @@ import Light from 'Public/assets/light.svg';
import Dark from 'Public/assets/dark.svg';
import { setTheme } from 'Redux/actions/theme';

const ThemeToggle: FC = () => {
const ThemeToggle = () => {
const theme = useSelector(getTheme);
const dispatch = useDispatch();

Expand Down
10 changes: 9 additions & 1 deletion src/pages/blog/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ import styled from 'styled-components';
import { Container } from 'Atoms/Container';
import { List } from 'Atoms/List';
import { SeoHead } from 'Atoms/SeoHead';
import { shimmer } from 'Utils/shimmer';

export default function BlogPost({ post, mdxSource }: any) {
const { title, description, date, tags } = post;

const components = {
img: (props: any) => (
<Image {...props} width={1920} height={1080} objectFit="cover" />
<Image
{...props}
width={1920}
height={1080}
objectFit="cover"
placeholder="blur"
blurDataURL={shimmer(1920, 1080)}
/>
),
ul: (props: any) => <List {...props} />,
};
Expand Down
9 changes: 6 additions & 3 deletions src/pages/portfolio/[slug].tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import styled from 'styled-components';
import { getAllDocuments, getBySlug } from 'Utils/api';
import { serialize } from 'next-mdx-remote/serialize';
import { MDXRemote } from 'next-mdx-remote';
import Image from 'next/image';
import { MDXRemote } from 'next-mdx-remote';
import { serialize } from 'next-mdx-remote/serialize';
import { shimmer } from 'Utils/shimmer';
import styled from 'styled-components';

import { Container } from 'Atoms/Container';
import { List } from 'Atoms/List';
Expand Down Expand Up @@ -33,6 +34,8 @@ export default function BlogPost({ item, mdxSource }: any) {
height={1080}
objectFit="cover"
alt={`Screenshot of ${title}`}
placeholder="blur"
blurDataURL={shimmer(1920, 1080)}
/>{' '}
<Headline>{title}</Headline>
<h3>{type}</h3>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/portfolio/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function Blog({ portfolioItems }: any) {
<Container>
<Headline>Portfolio</Headline>
{portfolioItems.map((item: any) => (
<PortfolioItem item={item} />
<PortfolioItem item={item} key={item.slug} />
))}
</Container>
</>
Expand Down
10 changes: 7 additions & 3 deletions src/pages/uses.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import Image from 'next/image';
import styled from 'styled-components';

import { Container } from 'Atoms/Container';
import { List } from 'Atoms/List';
import { SeoHead } from 'Atoms/SeoHead';
import Image from 'next/image';
import styled from 'styled-components';

import desk from 'Public/assets/desk.jpg';

function Home() {
return (
Expand All @@ -21,10 +24,11 @@ function Home() {
2021.
</p>
<Image
src="/assets/desk.jpg"
src={desk}
width={1920}
height={1080}
alt="A photo of my desk"
placeholder="blur"
/>
<h3>Computer & Hardware</h3>
<List>
Expand Down
22 changes: 22 additions & 0 deletions src/utils/shimmer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const shimmerSvg = (width: number, height: number) => `
<svg width="${width}" height="${height}" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="g">
<stop stop-color="rgba(100, 100, 100, 0.2)" offset="20%" />
<stop stop-color="rgba(100, 100, 100, 0.5)" offset="50%" />
<stop stop-color="rgba(100, 100, 100, 0.2)" offset="70%" />
</linearGradient>
</defs>
<rect width="${width}" height="${height}" fill="rgba(100, 100, 100, 0.2)" />
<rect id="r" width="${width}" height="${height}" fill="url(#g)" />
<animate xlink:href="#r" attributeName="x" from="-${width}" to="${width}" dur="1s" repeatCount="indefinite" />
</svg>`;

const toBase64 = (str: string) =>
typeof window === 'undefined'
? Buffer.from(str).toString('base64')
: window.btoa(str);

export const shimmer = (width: number, height: number) => {
return `data:image/svg+xml;base64,${toBase64(shimmerSvg(width, height))}`;
};

0 comments on commit dbe3c72

Please sign in to comment.