Background Pattern

Provides a background image URL based on a predefined pattern. This is a utility Typescript function that can be copy & pasted into your codebase.

Preview

This is how the code looks like in action.

boxes-sm

boxes-md

boxes-lg

dotted

blueprint

hearts

Step-By-Step Guide

1. Copy the Source Code

@/lib/utils.ts

1/**
2 * Provides a background image URL based on a predefined pattern.
3 * @param pattern - The name of the pattern ['blueprint', 'boxes-sm', 'boxes-md', 'boxes-lg', 'dotted', 'hearts'] chosen to convert.
4 * @returns The background image URL for the given pattern.
5 */
6export const backgroundPattern = (pattern: string): string => {
7	let svg: string;
8
9	switch (pattern) {
10		case 'blueprint':
11			svg =
12				'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="8" height="8" fill="#3399cc" stroke="#fff"><path d="M0 0H32V32H0V0Z"/></svg>';
13			break;
14		case 'boxes-sm':
15			svg =
16				'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="8" height="8" fill="none" stroke="#808080"><path d="M0 0H32V32"/></svg>';
17			break;
18		case 'boxes-md':
19			svg =
20				'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="16" height="16" fill="none" stroke="#808080"><path d="M0 0H32V32"/></svg>';
21			break;
22		case 'boxes-lg':
23			svg =
24				'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="32" height="32" fill="none" stroke="#808080"><path d="M0 0H32V32"/></svg>';
25			break;
26
27		case 'dotted':
28			svg =
29				'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="8" height="8" fill="#808080"><circle cx="16" cy="16" r="2" /></svg>';
30			break;
31		case 'hearts':
32			svg =
33				'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 500 500" width="32" height="32" fill="red" stroke="#808080"><path d="M140 20C73 20 20 74 20 140c0 135 136 170 228 303 88-132 229-173 229-303 0-66-54-120-120-120-48 0-90 28-109 69-19-41-60-69-108-69z"/></svg>';
34			break;
35		default:
36			svg =
37				'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="8" height="8" fill="#3399cc" stroke="#fff"><path d="M0 0H32V32H0V0Z"/></svg>';
38	}
39
40	return `url("data:image/svg+xml;utf8,${encodeURIComponent(svg)}")`;
41};

2. Use it in a file

/page.tsx

1import { backgroundPattern } from '@/lib/utils';
2
3export default function SomeComponent(){
4	return (
5		<div className='w-48 h-48' style={{ backgroundImage: backgroundPattern('hearts') }}></div>
6	)
7}

Tech Stack

Only the finest ingredients are used in our products. We made sure that we only use the best technologies available which offer a free tier! Yes, you read that right. Everything can be set up for FREE!

TypeScript

TypeScript

TypeScript ensures that your code is always correct. It is the best way to write JavaScript.

Programming Language

You May Also Like

Didn't find what you were looking for? Check out these other items.

Ui Component Library Template
Internal link to /templates/ui-component-library
Templates

Ui Component Library

Want to create your own Ui Component Library like shadcn/ui? This is your template!

Product Image
Internal link to /functions/capitalize
Functions

Capitalize

Capitalizes the first letter of a string.

Product Image
Internal link to /functions/copy-to-clipboard
Functions

Copy To Clipboard

Copies the given string to the clipboard.

Frequently Asked Questions

We have compiled a list of frequently asked questions. If you have any other questions, please do not hesitate to contact us via email or using the chat function. We are here to help!

Interested in kickstarting your next project?

Get the latest boilerplates, components, and news straight to your inbox.

By signing up, you agree to our Privacy Policy