Skip to content

Commit 46f8925

Browse files
feat: Custom Border Radius Generator (#215)
* feat: Custom Border Radius Generator * changes done Co-authored-by: Dunsin <[email protected]>
1 parent 0b3e8ff commit 46f8925

File tree

5 files changed

+254
-3
lines changed

5 files changed

+254
-3
lines changed

index.html

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ <h1 id="head">Code Magic</h1>
138138
></iconify-icon>
139139
<span>Animator</span>
140140
</li>
141+
<li data-gen="border-radius">
142+
<iconify-icon
143+
inline
144+
icon="ic:twotone-rounded-corner"
145+
style="color: white"
146+
width="20"
147+
height="20"
148+
></iconify-icon>
149+
<span>Border Radius</span>
150+
</li>
141151
<li data-gen="box-shadow">
142152
<iconify-icon
143153
inline
@@ -380,7 +390,6 @@ <h2>
380390
value="3"
381391
/>
382392
</label>
383-
384393
<label id="degree">
385394
<span class="label-title">
386395
<span class="title-display">Angle</span>
@@ -463,8 +472,48 @@ <h2>
463472

464473
<div class="btn" data-button="box-shadow">Get Results</div>
465474
</div>
466-
</div>
475+
476+
477+
<!-- border radius generator -->
478+
<div data-content="border-radius">
479+
<div class="border-radius-preview-box">
480+
<div class="preview"></div>
481+
<div class="border-range-inputs">
482+
<input
483+
type="range"
484+
max="100"
485+
min="0"
486+
id="border-radius-top"
487+
class="border-radius-inputs"
488+
/>
489+
<input
490+
type="range"
491+
max="100"
492+
min="0"
493+
id="border-radius-left"
494+
class="border-radius-inputs"
495+
/>
496+
<input
497+
type="range"
498+
max="100"
499+
min="0"
500+
id="border-radius-right"
501+
class="border-radius-inputs"
502+
/>
503+
<input
504+
type="range"
505+
max="100"
506+
min="0"
507+
id="border-radius-bottom"
508+
class="border-radius-inputs"
509+
/>
510+
</div>
511+
</div>
467512

513+
<div class="btn" data-button="border-radius">Get Results</div>
514+
</div>
515+
</div>
516+
468517
<div class="side-results">
469518
<div class="header">
470519
<iconify-icon
@@ -619,6 +668,22 @@ <h2>Results</h2>
619668
</button>
620669
</div>
621670
</div>
671+
672+
<!-- border radius -->
673+
<div class="download-output" data-result="border-radius">
674+
<div class="output"></div>
675+
<div class="download-btn">
676+
<button class="btn" data-download="border-radius-code">
677+
<iconify-icon
678+
inline
679+
icon="bx:code-alt"
680+
style="color: white; margin-right: 5px"
681+
width="20"
682+
></iconify-icon
683+
>Get Code
684+
</button>
685+
</div>
686+
</div>
622687
</div>
623688
</section>
624689
</main>

src/lib/general.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ function actOnGenerator(attribute: string, outputElement: HTMLElement) {
7878
}
7979
`;
8080
break;
81+
case 'border-radius':
82+
element = outputElement.style;
83+
codeToCopy = `
84+
border-radius: ${element.borderRadius};
85+
`;
86+
break;
8187
case 'box-shadow':
8288
element = outputElement.style;
8389
console.log("element: ", element)
@@ -214,6 +220,18 @@ export const getRadioButtonSet = (attribute: string) =>
214220
`[name = ${attribute}-radio]`
215221
) as NodeListOf<HTMLInputElement>;
216222

223+
export const getBorderTop = (attribute: string) =>
224+
<HTMLInputElement>document.getElementById(`${attribute}-top`);
225+
226+
export const getBorderRight = (attribute: string) =>
227+
<HTMLInputElement>document.getElementById(`${attribute}-right`);
228+
229+
export const getBorderBottom = (attribute: string) =>
230+
<HTMLInputElement>document.getElementById(`${attribute}-bottom`);
231+
232+
export const getBorderLeft = (attribute: string) =>
233+
<HTMLInputElement>document.getElementById(`${attribute}-left`);
234+
217235
export const getStyleSheet = () => {
218236
const stylesheet = Array.from(document.styleSheets).filter(
219237
(styleSheet) =>

src/main.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {gradientTextGenerator} from './pages/gradient-text';
44
import {gradientBorderGenerator} from './pages/gradient-border';
55
import {gradientBackgroundGenerator} from './pages/gradient-background';
66
import {animationGenerator} from './pages/animation';
7+
import {borderRadiusGenerator} from './pages/border-radius';
78
import {boxShadowGenerator} from './pages/box-shadow';
89

910
// Utils
@@ -120,6 +121,26 @@ const titleDisplayElement = <HTMLElement>(
120121
document.querySelector('.title-display')
121122
);
122123

124+
// border radius elements
125+
126+
const borderRadiusInputs = document.querySelectorAll('.border-radius-inputs');
127+
const borderTop = <HTMLInputElement>(
128+
document.querySelector('#border-radius-top')
129+
);
130+
const borderLeft = <HTMLInputElement>(
131+
document.querySelector('#border-radius-left')
132+
);
133+
const borderBottom = <HTMLInputElement>(
134+
document.querySelector('#border-radius-bottom')
135+
);
136+
const borderRight = <HTMLInputElement>(
137+
document.querySelector('#border-radius-right')
138+
);
139+
140+
const borderRadiusPreview = <HTMLElement>(
141+
document.querySelector('.border-radius-preview-box > .preview')
142+
);
143+
123144
menuIcon?.addEventListener('click', () => {
124145
if (navBar?.classList.contains('closed-nav')) {
125146
navBar?.animate(navBarSlideIn, navBarAnimationOptions);
@@ -235,6 +256,9 @@ function generatorsFunction(attribute: string): void {
235256
case 'animation':
236257
animationGenerator();
237258
break;
259+
case 'border-radius':
260+
borderRadiusGenerator();
261+
break;
238262
case 'box-shadow':
239263
boxShadowGenerator();
240264
break;
@@ -369,6 +393,20 @@ for (let i = 0; i < gradientBackgroundInputs.length; i++) {
369393
);
370394
}
371395

396+
// on change event handler for border radius generator range inputs
397+
398+
for (let i = 0; i < borderRadiusInputs.length; i++) {
399+
borderRadiusInputs[i].addEventListener('change', () =>
400+
BorderRadiusGenerator(
401+
borderTop,
402+
borderLeft,
403+
borderBottom,
404+
borderRight,
405+
borderRadiusPreview
406+
)
407+
);
408+
}
409+
372410
//set gradient border preview
373411
for (let i = 0; i < gradientBorderInputs.length; i++) {
374412
gradientBorderInputs[i].addEventListener('change', () =>
@@ -392,7 +430,23 @@ for (let i = 0; i < gradientTextInputs.length; i++) {
392430
);
393431
}
394432

433+
// border radius generator preview
434+
435+
const BorderRadiusGenerator = (
436+
borderTop: HTMLInputElement,
437+
borderLeft: HTMLInputElement,
438+
borderBottom: HTMLInputElement,
439+
borderRight: HTMLInputElement,
440+
borderRadiusPreview: HTMLElement
441+
) => {
442+
borderRadiusPreview.style.borderRadius = `
443+
${borderTop.value}% ${100 - Number(borderTop.value)}%
444+
${borderBottom.value}% ${100 - Number(borderBottom.value)}% /
445+
${borderLeft.value}% ${borderRight.value}%
446+
${100 - Number(borderRight.value)}% ${100 - Number(borderLeft.value)}%`;
447+
};
448+
395449
//Toggle gradient border radius input display
396450
gradientBorderRadius.addEventListener('change', function () {
397451
gradientBorderInput.style.display = this.checked ? 'inline' : 'none';
398-
});
452+
});

src/pages/border-radius.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import * as utils from '../lib/general';
2+
3+
type Values = {
4+
BorderTop: string;
5+
borderLeft: string;
6+
borderRight: string;
7+
borderBottom: string;
8+
};
9+
10+
export function borderRadiusGenerator(): void {
11+
const attribute = 'border-radius';
12+
const getOutputElement = utils.getOutput(attribute);
13+
const resultPage = utils.getResultPage();
14+
15+
resultPage.style.display = 'flex';
16+
if (getOutputElement === null) return;
17+
getOutputElement.style.display = 'grid';
18+
getOutputElement.style.placeItems = 'center';
19+
20+
const borderTop = utils.getBorderTop(attribute);
21+
const borderRight = utils.getBorderRight(attribute);
22+
const borderLeft = utils.getBorderLeft(attribute);
23+
const borderBottom = utils.getBorderBottom(attribute);
24+
25+
const values = {
26+
BorderTop: borderTop.value,
27+
borderLeft: borderLeft.value,
28+
borderRight: borderRight.value,
29+
borderBottom: borderBottom.value,
30+
};
31+
32+
getBorderRadiusResult(attribute, values, getOutputElement);
33+
}
34+
35+
function getBorderRadiusResult(
36+
attribute: string,
37+
values: Values,
38+
outputElement: HTMLElement
39+
): void {
40+
outputElement.style.width = '250px';
41+
outputElement.style.height = '250px';
42+
outputElement.style.borderRadius = `${values.BorderTop}% ${
43+
100 - Number(values.BorderTop)
44+
}%
45+
${values.borderBottom}% ${100 - Number(values.borderBottom)}% /
46+
${values.borderLeft}% ${values.borderRight}%
47+
${100 - Number(values.borderRight)}% ${100 - Number(values.borderLeft)}%`;
48+
49+
const getCodeButtonElement = utils.getCopyCodeButton(attribute);
50+
getCodeButtonElement.addEventListener('click', () => {
51+
utils.copyCodeToClipboard(attribute, outputElement);
52+
utils.showPopup(
53+
'Code Copied',
54+
'Code has been successfully copied to clipboard',
55+
'success'
56+
);
57+
});
58+
}

src/style.css

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,62 @@ input[type='number']::-webkit-outer-spin-button {
441441
margin-bottom: 1rem;
442442
}
443443

444+
[data-content='border-radius'] {
445+
display: flex;
446+
flex-direction: column;
447+
width: 600px;
448+
position: relative;
449+
}
450+
451+
.border-radius-preview-box {
452+
width: 250px;
453+
height: 250px;
454+
border: 1px dashed var(--text-color);
455+
margin-bottom: 1rem;
456+
position: relative;
457+
}
458+
459+
.border-radius-preview-box .preview {
460+
width: 100%;
461+
height: 100%;
462+
background: var(--primary-color);
463+
}
464+
.border-range-inputs input {
465+
width: 100%;
466+
cursor: pointer;
467+
}
468+
469+
#border-radius-code {
470+
padding: 1rem;
471+
background: var(--primary-color);
472+
margin-top: 2rem;
473+
}
474+
475+
.border-range-inputs input:nth-child(1) {
476+
position: absolute;
477+
top: -2rem;
478+
left: 0;
479+
}
480+
481+
.border-range-inputs input:nth-child(2) {
482+
position: absolute;
483+
transform: rotate(90deg);
484+
top: 7rem;
485+
left: -9rem;
486+
}
487+
.border-range-inputs input:nth-child(3) {
488+
position: absolute;
489+
transform: rotate(90deg);
490+
top: 7rem;
491+
left: 9rem;
492+
}
493+
.border-range-inputs input:nth-child(4) {
494+
position: absolute;
495+
bottom: -2rem;
496+
left: 0;
497+
transform: rotate(180deg);
498+
}
499+
444500
/*
445501
* NOTE: The border-radius property is not supported with border-image, therefore,
446502
* the css for the gradient-border below relies on CSS Mask property applied to a pseudo background.

0 commit comments

Comments
 (0)