Skip to content

Commit 0212dfa

Browse files
authored
Merge pull request #6 from yet3/dev
Lines and checkers pattern, negative offsets
2 parents fd75934 + 7d5c4a8 commit 0212dfa

22 files changed

+434
-27
lines changed

README.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ See a [live demo](https://yet3.github.io/tailwindcss-bg-patterns/)
1616
- [Installation](#installation)
1717
- [Config](#config)
1818
- [Example usage](#grid)
19+
- [Pattern offsets](#pattern-offsets)
1920
- [Patterns](#patterns)
21+
- [Horizontal lines](#horizontal-lines)
22+
- [Vertical lines](#vertical-lines)
2023
- [Grid](#grid)
24+
- [Checkers](#checkers)
2125
- [Hatching](#hatching)
2226
- [Cross-Hatching](#cross-hatching)
2327
- [Polka dot](#polka-dot)
@@ -77,9 +81,40 @@ this code will result in:
7781

7882
<img src="./public/grid.png" alt="Grid pattern" height="200" />
7983

84+
## Pattern offsets
85+
Each pattern can be offset using ``bg-pattern-offset-x`` and ``bg-pattern-offset-y``
86+
87+
Offset also accept arbritary values ``bg-pattern-offset-x-[321px]`` as well as negative values ``-bg-pattern-offset-y-24``
88+
8089
## Patterns
8190

91+
92+
### Horizontal lines
93+
94+
See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#x-lines)
95+
96+
```html
97+
<div
98+
class="bg-blue-500 bg-pattern-x-lines bg-pattern-line-0.5 bg-pattern-spacing-32"
99+
/>
100+
```
101+
102+
<img src="./public/x-lines.png" alt="Horizontal lines" height="200" />
103+
104+
### Vertical lines
105+
106+
See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#y-lines)
107+
108+
```html
109+
<div
110+
class="bg-blue-500 bg-pattern-y-lines bg-pattern-line-0.5 bg-pattern-spacing-32"
111+
/>
112+
```
113+
114+
<img src="./public/y-lines.png" alt="Vertical lines" height="200" />
115+
82116
### Grid
117+
83118
See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#grid)
84119

85120
```html
@@ -90,7 +125,20 @@ See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#grid)
90125

91126
<img src="./public/grid.png" alt="Grid pattern" height="200" />
92127

128+
### Checkers
129+
130+
See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#checkers)
131+
132+
```html
133+
<div
134+
class="bg-blue-500 bg-pattern-checkers bg-pattern-square-white bg-pattern-square-32"
135+
/>
136+
```
137+
138+
<img src="./public/checkers.png" alt="Checkers pattern" height="200" />
139+
93140
### Hatching
141+
94142
See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#hatching)
95143

96144
```html
@@ -102,6 +150,7 @@ See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#hatching)
102150
<img src="./public/hatching.png" alt="Hatching pattern" height="200" />
103151

104152
### Cross-Hatching
153+
105154
See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#cross-hatching)
106155

107156
```html
@@ -113,6 +162,7 @@ See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#cross-hatching
113162
<img src="./public/cross-hatching.png" alt="Cross-Hatching pattern" height="200" />
114163

115164
### Polka dot
165+
116166
See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#polka-dot)
117167

118168
```html
@@ -124,6 +174,7 @@ See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#polka-dot)
124174
<img src="./public/polka-dot.png" alt="Polka dot pattern" height="200" />
125175

126176
### Hexagonal polka dot
177+
127178
See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#hexagonal-polka-dot)
128179

129180
```html
@@ -137,9 +188,6 @@ See it live [here](https://yet3.github.io/tailwindcss-bg-patterns#hexagonal-polk
137188
## Todo
138189

139190
- [ ] Add utility to adjust pattern opacity
140-
- [ ] Add pattern: horizontal-lines (x-lines?)
141-
- [ ] Add pattern: vertical-lines (y-lines?)
142-
- [ ] Add pattern: checkers
143191
- [ ] Add pattern: honeycomb
144192
- [ ] Add pattern: bricks
145193

example/src/routes/+page.svelte

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@
1616
}
1717
];
1818
19+
const OFFSETS_CONFIGS: IPatternConfig[] = [
20+
{
21+
name: 'bg-pattern-offset-x',
22+
options: [
23+
{ name: '-2', description: 'Offset x' },
24+
{ name: '-[321px]', description: 'Custom offset x' }
25+
]
26+
},
27+
{
28+
name: 'bg-pattern-offset-y',
29+
options: [
30+
{ name: '-2', description: 'Offset x' },
31+
{ name: '-[321px]', description: 'Custom offset x' }
32+
]
33+
}
34+
];
35+
1936
const DOT_CONFIGS: IPatternConfig[] = [
2037
{
2138
name: 'bg-pattern-dot',
@@ -28,6 +45,18 @@
2845
}
2946
];
3047
48+
const SQUARE_CONFIGS: IPatternConfig[] = [
49+
{
50+
name: 'bg-pattern-square',
51+
options: [
52+
{ name: '-2', description: 'Squares width' },
53+
{ name: '-[321]', description: 'Custom squares width (in px without unit)' },
54+
{ name: '-red-500', description: 'Squares colors' },
55+
{ name: '-[#a8a8a8]', description: 'Custom squares color' }
56+
]
57+
}
58+
];
59+
3160
const SPACING_CONFIGS: IPatternConfig[] = [
3261
{
3362
name: 'bg-pattern-spacing',
@@ -49,36 +78,54 @@
4978
];
5079
5180
const PATTERNS: IPattern[] = [
81+
{
82+
id: 'x-lines',
83+
name: 'Horizontal lines',
84+
configClasses: [...LINE_CONFIGS, ...SPACING_CONFIGS, ...OFFSETS_CONFIGS],
85+
class: 'bg-blue-500 bg-pattern-x-lines bg-pattern-line-0.5 bg-pattern-spacing-20'
86+
},
87+
{
88+
id: 'y-lines',
89+
name: 'Vertical lines',
90+
configClasses: [...LINE_CONFIGS, ...SPACING_CONFIGS, ...OFFSETS_CONFIGS],
91+
class: 'bg-blue-500 bg-pattern-y-lines bg-pattern-line-0.5 bg-pattern-spacing-32'
92+
},
5293
{
5394
id: 'grid',
5495
name: 'Grid',
55-
configClasses: [...LINE_CONFIGS, ...SPACING_CONFIGS],
96+
configClasses: [...LINE_CONFIGS, ...SPACING_CONFIGS, ...OFFSETS_CONFIGS],
5697
class: 'bg-blue-500 bg-pattern-grid bg-pattern-line-0.5 bg-pattern-spacing-32'
5798
},
99+
{
100+
id: 'checkers',
101+
name: 'Checkers',
102+
configClasses: [...SQUARE_CONFIGS, ...OFFSETS_CONFIGS],
103+
class: 'bg-blue-500 bg-pattern-checkers bg-pattern-square-white bg-pattern-square-32'
104+
},
58105
{
59106
id: 'hatching',
60107
name: 'Hatching',
61-
configClasses: [...LINE_CONFIGS, ...SPACING_CONFIGS, ...HATCHING_DIRECTION_CONFIGS],
108+
configClasses: [...LINE_CONFIGS, ...SPACING_CONFIGS, ...HATCHING_DIRECTION_CONFIGS, ...OFFSETS_CONFIGS],
62109
class:
63110
'bg-blue-500 bg-pattern-hatching bg-pattern-line-0.5 bg-pattern-spacing-16 bg-pattern-hatching-left-to-right'
64111
},
65112
{
66113
id: 'cross-hatching',
67114
name: 'Cross-Hatching',
68-
configClasses: [...LINE_CONFIGS, ...SPACING_CONFIGS, ...HATCHING_DIRECTION_CONFIGS],
115+
configClasses: [...LINE_CONFIGS, ...SPACING_CONFIGS, ...HATCHING_DIRECTION_CONFIGS, ...OFFSETS_CONFIGS],
69116
class: 'bg-blue-500 bg-pattern-cross-hatching bg-pattern-line-0.5 bg-pattern-spacing-16'
70117
},
71118
{
72119
id: 'polka-dot',
73120
name: 'Polka dot',
74-
configClasses: [...DOT_CONFIGS, ...SPACING_CONFIGS],
121+
configClasses: [...DOT_CONFIGS, ...SPACING_CONFIGS, ...OFFSETS_CONFIGS],
75122
class:
76123
'bg-blue-500 bg-pattern-polka-dot bg-pattern-dot-white bg-pattern-dot-8 bg-pattern-spacing-32'
77124
},
78125
{
79126
id: 'hexagonal-polka-dot',
80127
name: 'Hexagonal polka dot',
81-
configClasses: [...DOT_CONFIGS, ...SPACING_CONFIGS],
128+
configClasses: [...DOT_CONFIGS, ...SPACING_CONFIGS, ...OFFSETS_CONFIGS],
82129
class:
83130
'bg-blue-500 bg-pattern-hex-polka-dot bg-pattern-dot-white bg-pattern-dot-8 bg-pattern-spacing-32'
84131
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@yet3/tailwindcss-bg-patterns",
3-
"version": "2.1.0",
3+
"version": "2.2.0",
44
"author": "yet3",
55
"description": "TailwindCSS plugin that css background patterns",
66
"license": "MIT",

public/checkers.png

20.2 KB
Loading

public/x-lines.png

12 KB
Loading

public/y-lines.png

17.6 KB
Loading

src/consts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const LINE_WIDTHS = [
44
0.5, 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16,
55
];
66
export const DOT_SIZES: number[] = [...Array(96)].map((_, i) => i + 1);
7+
export const SQUARE_SIZES: number[] = [...Array(96)].map((_, i) => i + 1);
78
export const SPACING: number[] = [...Array(128 + 1)].map((_, i) => i);
89
export const OFFSETS: number[] = [...Array(128 + 17)].map((_, i) => i);
910

src/index.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import plugin from "tailwindcss/plugin";
2-
import { DOT_SIZES, LINE_WIDTHS, OFFSETS, SPACING } from "./consts";
2+
import { DOT_SIZES, LINE_WIDTHS, OFFSETS, SPACING, SQUARE_SIZES } from "./consts";
33
import { arrToTwConfig } from "./lib/arrToTwConfig";
4+
import {
5+
generateDotColors,
6+
generateDotSize,
7+
matchDotSizeAndColors,
8+
} from "./lib/dot";
49
import {
510
generateLineColors,
611
generateLineWidths,
@@ -9,14 +14,16 @@ import {
914
import { generateOffsets, matchOffsets } from "./lib/offsets";
1015
import { resolveOptions } from "./lib/resolveOptions";
1116
import { generateSpacing, matchSpacing } from "./lib/spacing";
17+
import { generateCheckersClass } from "./patterns/checkers";
1218
import { generateGridClass } from "./patterns/grid";
1319
import {
1420
generateHatchingClass,
1521
genreateHatchingDirection,
1622
} from "./patterns/hatching";
23+
import { generateLinesClass } from "./patterns/lines";
1724
import { generatePolkaDotClass } from "./patterns/polkaDot";
1825
import type { IOptions } from "./types";
19-
import { generateDotColors, generateDotSize, matchDotSizeAndColors } from "./lib/dot";
26+
import { generateSquareColors, generateSuqareSize, matchSquareSizeAndColors } from "./lib/square";
2027

2128
export default plugin.withOptions<IOptions | undefined>(
2229
(options) => (api) => {
@@ -36,11 +43,22 @@ export default plugin.withOptions<IOptions | undefined>(
3643
isHexagonal: true,
3744
}),
3845

46+
generateLinesClass(e(`${opts.prefix}pattern-x-lines`), {
47+
angle: 0,
48+
}),
49+
generateLinesClass(e(`${opts.prefix}pattern-y-lines`), {
50+
angle: 90,
51+
}),
52+
53+
generateCheckersClass(e(`${opts.prefix}pattern-checkers`)),
54+
3955
// Configs
4056
generateLineWidths(api, opts),
4157
generateLineColors(api, opts),
4258
generateDotSize(api, opts),
4359
generateDotColors(api, opts),
60+
generateSuqareSize(api, opts),
61+
generateSquareColors(api, opts),
4462
generateSpacing(api, opts),
4563
genreateHatchingDirection(api, opts),
4664
generateOffsets(api, opts),
@@ -50,13 +68,15 @@ export default plugin.withOptions<IOptions | undefined>(
5068
...matchSpacing(api, opts),
5169
...matchLineWidthsAndColors(api, opts),
5270
...matchDotSizeAndColors(api, opts),
71+
...matchSquareSizeAndColors(api, opts),
5372
...matchOffsets(api, opts),
5473
});
5574
},
5675
() => ({
5776
theme: {
5877
bgPatternLineWidth: arrToTwConfig(LINE_WIDTHS),
5978
bgPatternDotSize: arrToTwConfig(DOT_SIZES),
79+
bgPatternSquareSize: arrToTwConfig(SQUARE_SIZES),
6080
bgPatternSpacing: arrToTwConfig(SPACING),
6181
bgPatternOffsets: arrToTwConfig(OFFSETS, "px"),
6282
},

src/lib/offsets.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export const generateOffsets = (
1616
styles[`.${api.e(`${opts.prefix}pattern-offset-y-${key}`)}`] = {
1717
"--tw-offset-y": offset,
1818
} as unknown as CSSRuleObject;
19+
20+
styles[`.${api.e(`-${opts.prefix}pattern-offset-x-${key}`)}`] = {
21+
"--tw-offset-x": `-${offset}`,
22+
} as unknown as CSSRuleObject;
23+
styles[`.${api.e(`-${opts.prefix}pattern-offset-y-${key}`)}`] = {
24+
"--tw-offset-y": `-${offset}`,
25+
} as unknown as CSSRuleObject;
1926
}
2027
}
2128
return styles as CSSRuleObject;

src/lib/square.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import type { CSSRuleObject, PluginAPI } from "tailwindcss/types/config";
2+
import type { IOptions } from "../types";
3+
4+
export const generateSquareColors = (
5+
api: PluginAPI,
6+
opts: IOptions,
7+
): CSSRuleObject => {
8+
const colors = api.theme("backgroundColor");
9+
const styles: Record<string, unknown> = {};
10+
11+
for (const colorKey in colors) {
12+
const color = colors[colorKey] as string | Record<string, string>;
13+
14+
if (typeof color === "string") {
15+
styles[`.${api.e(`${opts.prefix}pattern-square-${colorKey}`)}`] = {
16+
"--tw-square-color": color,
17+
} as unknown as CSSRuleObject;
18+
} else {
19+
for (const variant in color) {
20+
styles[
21+
`.${api.e(`${opts.prefix}pattern-square-${colorKey}-${variant}`)}`
22+
] = {
23+
"--tw-square-color": color[variant],
24+
} as unknown as CSSRuleObject;
25+
}
26+
}
27+
}
28+
29+
return styles as CSSRuleObject;
30+
};
31+
32+
export const generateSuqareSize = (
33+
api: PluginAPI,
34+
opts: IOptions,
35+
): CSSRuleObject => {
36+
const themeSquareSizes = api.theme("bgPatternSquareSize");
37+
const styles: Record<string, unknown> = {};
38+
if (themeSquareSizes) {
39+
for (const key in themeSquareSizes) {
40+
const width = themeSquareSizes[key] as number;
41+
styles[`.${api.e(`${opts.prefix}pattern-square-${key}`)}`] = {
42+
"--tw-square-size": `${width} /* px */`,
43+
};
44+
}
45+
}
46+
return styles as CSSRuleObject;
47+
};
48+
49+
export const matchSquareSizeAndColors = (api: PluginAPI, opts: IOptions) => ({
50+
[api.e(`${opts.prefix}pattern-square`)]: (value: string) => {
51+
const parsed = Number(value);
52+
if (!Number.isNaN(parsed)) {
53+
return {
54+
"--tw-square-size": `${value} /* px */`,
55+
} as unknown as CSSRuleObject;
56+
}
57+
58+
return {
59+
"--tw-square-color": value,
60+
} as unknown as CSSRuleObject;
61+
},
62+
});

0 commit comments

Comments
 (0)