Skip to content

Commit 2d7abdf

Browse files
authored
feat: add preview for scrollbar generator (#567)
1 parent 6f5f8a8 commit 2d7abdf

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

index.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,30 @@ <h1>Input Range</h1>
13341334

13351335
<!-- Custom Scroll Generator -->
13361336
<div data-content="scroll">
1337+
<!-- custom scroll generator -->
1338+
<h2>Preview</h2>
1339+
<br />
1340+
<div id="scroll-preview">
1341+
<p>
1342+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
1343+
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
1344+
enim ad minim veniam, quis nostrud exercitation ullamco laboris
1345+
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
1346+
in reprehenderit in voluptate velit esse cillum dolore eu fugiat
1347+
nulla pariatur. Excepteur sint occaecat cupidatat non proident,
1348+
sunt in culpa qui officia deserunt mollit anim id est laborum.
1349+
</p>
1350+
<p>
1351+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
1352+
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
1353+
enim ad minim veniam, quis nostrud exercitation ullamco laboris
1354+
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
1355+
in reprehenderit in voluptate velit esse cillum dolore eu fugiat
1356+
nulla pariatur. Excepteur sint occaecat cupidatat non proident,
1357+
sunt in culpa qui officia deserunt mollit anim id est laborum.
1358+
</p>
1359+
</div>
1360+
<br />
13371361
<div class="input">
13381362
<label for="color" class="color">
13391363
Track Color

src/lib/getElements.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,6 @@ export const getGridFields = (
204204

205205
export const getGridPreview = (attribute: string): HTMLElement =>
206206
document.getElementById(`${attribute}-preview`) as HTMLElement;
207+
208+
export const getScrollPreview = (): HTMLElement =>
209+
document.getElementById('scroll-preview') as HTMLElement;

src/pages/scroll.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getCopyCodeButton,
66
getCssOrTailwindButton,
77
getCssOrTailwindDropdown,
8+
getScrollPreview,
89
} from '../lib/getElements';
910
import {closeDropdown, showPopup} from '../lib/packages/utils';
1011

@@ -83,6 +84,61 @@ function copyHandler(values: Values) {
8384
}
8485

8586
export function scrollGenerator() {
87+
const previewElement = getScrollPreview();
88+
89+
function applyPreviewStyles(values: Values) {
90+
if (!previewElement) return;
91+
92+
// Apply styles directly to the preview element
93+
previewElement.style.setProperty('--scrollbar-width', `${values.width}px`);
94+
previewElement.style.setProperty(
95+
'--scrollbar-track-color',
96+
values.trackColor
97+
);
98+
previewElement.style.setProperty(
99+
'--scrollbar-thumb-color',
100+
values.thumbColor
101+
);
102+
previewElement.style.setProperty(
103+
'--scrollbar-thumb-hover-color',
104+
values.hoverColor
105+
);
106+
107+
const styleTag = document.createElement('style');
108+
styleTag.id = 'scroll-preview-style';
109+
styleTag.innerHTML = `
110+
#scroll-preview::-webkit-scrollbar {
111+
width: var(--scrollbar-width);
112+
}
113+
#scroll-preview::-webkit-scrollbar-track {
114+
box-shadow: inset 0 0 5px var(--scrollbar-track-color);
115+
}
116+
#scroll-preview::-webkit-scrollbar-thumb {
117+
background: var(--scrollbar-thumb-color);
118+
}
119+
#scroll-preview::-webkit-scrollbar-thumb:hover {
120+
background: var(--scrollbar-thumb-hover-color);
121+
}
122+
`;
123+
124+
document.head.appendChild(styleTag);
125+
}
126+
127+
function updatePreview() {
128+
applyPreviewStyles({
129+
trackColor: trackColor.value,
130+
thumbColor: thumbColor.value,
131+
hoverColor: hoverColor.value,
132+
width: width.value,
133+
});
134+
}
135+
136+
[trackColor, thumbColor, hoverColor, width].forEach((input) => {
137+
input?.addEventListener('input', updatePreview);
138+
});
139+
140+
updatePreview();
141+
86142
getCodeButton?.addEventListener('click', () =>
87143
copyHandler({
88144
trackColor: trackColor.value,

src/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,14 @@ input[type='range']:focus::-moz-range-thumb {
12411241
color: var(--text-color);
12421242
}
12431243

1244+
#scroll-preview {
1245+
width: 300px;
1246+
height: 200px;
1247+
overflow-y: scroll;
1248+
border: 1px solid var(--text-color);
1249+
padding: 12px;
1250+
}
1251+
12441252
/* FOOTER SECTION */
12451253

12461254
footer {

0 commit comments

Comments
 (0)