Skip to content

Commit 68c1005

Browse files
committed
feat: add tailwind code for gradient background
1 parent 7d1abaa commit 68c1005

File tree

4 files changed

+78
-42
lines changed

4 files changed

+78
-42
lines changed

index.html

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,8 +1401,6 @@ <h2>Results</h2>
14011401
class="css-tailwind"
14021402
data-dropdown="gradient-border-dropdown2"
14031403
>
1404-
<!-- dropdown elements -->
1405-
14061404
<div class="dropdown-item" data-download="gradient-border-code">
14071405
<iconify-icon
14081406
inline
@@ -1460,6 +1458,20 @@ <h2>Results</h2>
14601458
></iconify-icon>
14611459
<span> Get CSS Code </span>
14621460
</div>
1461+
<div class="download-btn">
1462+
<button
1463+
class="btn"
1464+
data-download="gradient-background-tailwind"
1465+
>
1466+
<iconify-icon
1467+
inline
1468+
icon="bx:code-alt"
1469+
style="color: white; margin-right: 5px"
1470+
width="20"
1471+
></iconify-icon>
1472+
Get Tailwind
1473+
</button>
1474+
</div>
14631475
</div>
14641476
</div>
14651477
</div>

src/lib/packages/helpers.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ function convertLinearGradientToTailwind(gradient: string): string {
247247

248248
function generateTailwindClasses(colors: string[]): string {
249249
if (colors.length === 2) {
250-
return `from-[${colors[0]}] [to-[${colors[1]}]`;
250+
return `from-[${colors[0]}] to-[${colors[1]}]`;
251251
} else if (colors.length === 3) {
252252
return `from-[${colors[0]}] via-[${colors[1]}] to-[${colors[2]}]`;
253253
} else if (colors.length === 4) {
@@ -286,6 +286,11 @@ export const actOnTailwindGenerator = (
286286
case 'gradient-border':
287287
codeToCopy = ``;
288288
break;
289+
case 'gradient-background':
290+
codeToCopy = `${convertLinearGradientToTailwind(
291+
element.backgroundImage
292+
)}`;
293+
break;
289294
case 'border-radius':
290295
codeToCopy = `bg-[${element.borderRadius.replace(/ /g, '_')}]`;
291296
break;

src/pages/gradient-background.ts

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
getDegreeSpanElement,
1111
getGradientPreview,
1212
getCssOrTailwindDropdown,
13+
getTailwindButton,
14+
getCssOrTailwindButton,
1315
} from '../lib/getElements';
1416
import {
1517
copyCSSCodeToClipboard,
@@ -21,6 +23,7 @@ import {
2123
createGradientPreview,
2224
getColorsValue,
2325
closeDropdown,
26+
copyTailwindCodeToClipboard,
2427
} from '../lib/packages/utils';
2528

2629
type Values = {
@@ -39,6 +42,40 @@ const resetButton = getResetButton(attribute);
3942
const getCssOrTailwindDropdownElement = getCssOrTailwindDropdown(attribute);
4043
const showCopyClass = 'show-css-tailwind';
4144

45+
export function gradientBackgroundGenerator(
46+
type: 'newResults' | 'oldResults' | null
47+
) {
48+
if (type === null) return;
49+
50+
const getOutputElement = getOutput(attribute);
51+
const resultPage = getResultPage();
52+
53+
resultPage.style.display = 'flex';
54+
if (type === 'oldResults') return;
55+
56+
const values: Values = {
57+
degree: getDegreeElement.value,
58+
};
59+
getGradientBackgroundResult(attribute, values, getOutputElement);
60+
}
61+
62+
export function addGradientBackgroundListener() {
63+
whatColorButtonShouldShow(attribute);
64+
getNewColorButtonElement.addEventListener('click', () => {
65+
addNewColorPicker(attribute);
66+
addEventListenerToTheNewColorPicker();
67+
});
68+
69+
getRemoveColorButtonElement.addEventListener('click', () => {
70+
removeColorPicker(attribute);
71+
addEventListenerToTheNewColorPicker();
72+
});
73+
74+
inputEventListner();
75+
76+
setGradientDegreeValue(getDegreeElement);
77+
}
78+
4279
function copyHandler() {
4380
const outputElement = getOutput(attribute);
4481
copyCSSCodeToClipboard(attribute, outputElement);
@@ -54,9 +91,6 @@ function getCssOrTailwind(e?: MouseEvent): void {
5491
getCssOrTailwindDropdownElement.classList.toggle(showCopyClass);
5592
}
5693

57-
// closes css and tailwind dropdown on outside click
58-
closeDropdown(getCssOrTailwind, getCssOrTailwindDropdownElement, showCopyClass);
59-
6094
/**
6195
* sets the result to the output element
6296
*
@@ -74,41 +108,13 @@ function getGradientBackgroundResult(
74108
}deg, ${getColorsValue(attribute).join(', ')})`;
75109

76110
const getCodeButtonElement = getCopyCodeButton(attribute);
77-
getCodeButtonElement.addEventListener('click', copyHandler);
78-
}
79-
80-
export function gradientBackgroundGenerator(
81-
type: 'newResults' | 'oldResults' | null
82-
) {
83-
if (type === null) return;
84-
85-
const getOutputElement = getOutput(attribute);
86-
const resultPage = getResultPage();
87-
88-
resultPage.style.display = 'flex';
89-
if (type === 'oldResults') return;
90-
91-
const values: Values = {
92-
degree: getDegreeElement.value,
93-
};
94-
getGradientBackgroundResult(attribute, values, getOutputElement);
95-
}
96-
97-
export function addGradientBackgroundListener() {
98-
whatColorButtonShouldShow(attribute);
99-
getNewColorButtonElement.addEventListener('click', () => {
100-
addNewColorPicker(attribute);
101-
addEventListenerToTheNewColorPicker();
102-
});
111+
const getTailwindCodeButtonElement = getTailwindButton(attribute);
112+
const getCssOrTailwindButtonElement = getCssOrTailwindButton(attribute);
103113

104-
getRemoveColorButtonElement.addEventListener('click', () => {
105-
removeColorPicker(attribute);
106-
addEventListenerToTheNewColorPicker();
107-
});
108-
109-
inputEventListner();
114+
getCodeButtonElement.addEventListener('click', copyHandler);
115+
getTailwindCodeButtonElement.addEventListener('click', tailwindHandler);
110116

111-
setGradientDegreeValue(getDegreeElement);
117+
getCssOrTailwindButtonElement.addEventListener('click', getCssOrTailwind);
112118
}
113119

114120
function addEventListenerToTheNewColorPicker() {
@@ -153,7 +159,6 @@ function resetValues() {
153159
}
154160

155161
// get values from all targets to get notified when values change.
156-
157162
function getValues() {
158163
gradientBackgroundInputs.forEach((input) => {
159164
input.addEventListener('input', () => {
@@ -164,5 +169,20 @@ function getValues() {
164169
});
165170
});
166171
}
172+
173+
// Tailwind codecopy handler
174+
function tailwindHandler() {
175+
const outputElement = getOutput(attribute);
176+
copyTailwindCodeToClipboard(attribute, outputElement);
177+
178+
showPopup(
179+
'Tailwind Code Copied',
180+
'Code has been successfully copied to clipboard',
181+
'success'
182+
);
183+
}
184+
185+
closeDropdown(getCssOrTailwind, getCssOrTailwindDropdownElement, showCopyClass);
186+
167187
resetValues();
168188
getValues();

src/style.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,8 +924,7 @@ input[type='number']::-webkit-outer-spin-button {
924924

925925
.btn-dropdown-container,
926926
.image-download {
927-
width: var(--generator-width);
928-
display: inline-block;
927+
display: block;
929928
justify-content: center;
930929
align-items: center;
931930
position: relative;

0 commit comments

Comments
 (0)