Skip to content

Commit

Permalink
feat: add preview for scrollbar generator (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bridgetamana authored Aug 21, 2024
1 parent 6f5f8a8 commit 2d7abdf
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
24 changes: 24 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,30 @@ <h1>Input Range</h1>

<!-- Custom Scroll Generator -->
<div data-content="scroll">
<!-- custom scroll generator -->
<h2>Preview</h2>
<br />
<div id="scroll-preview">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
<br />
<div class="input">
<label for="color" class="color">
Track Color
Expand Down
3 changes: 3 additions & 0 deletions src/lib/getElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,6 @@ export const getGridFields = (

export const getGridPreview = (attribute: string): HTMLElement =>
document.getElementById(`${attribute}-preview`) as HTMLElement;

export const getScrollPreview = (): HTMLElement =>
document.getElementById('scroll-preview') as HTMLElement;
56 changes: 56 additions & 0 deletions src/pages/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getCopyCodeButton,
getCssOrTailwindButton,
getCssOrTailwindDropdown,
getScrollPreview,
} from '../lib/getElements';
import {closeDropdown, showPopup} from '../lib/packages/utils';

Expand Down Expand Up @@ -83,6 +84,61 @@ function copyHandler(values: Values) {
}

export function scrollGenerator() {
const previewElement = getScrollPreview();

function applyPreviewStyles(values: Values) {
if (!previewElement) return;

// Apply styles directly to the preview element
previewElement.style.setProperty('--scrollbar-width', `${values.width}px`);
previewElement.style.setProperty(
'--scrollbar-track-color',
values.trackColor
);
previewElement.style.setProperty(
'--scrollbar-thumb-color',
values.thumbColor
);
previewElement.style.setProperty(
'--scrollbar-thumb-hover-color',
values.hoverColor
);

const styleTag = document.createElement('style');
styleTag.id = 'scroll-preview-style';
styleTag.innerHTML = `
#scroll-preview::-webkit-scrollbar {
width: var(--scrollbar-width);
}
#scroll-preview::-webkit-scrollbar-track {
box-shadow: inset 0 0 5px var(--scrollbar-track-color);
}
#scroll-preview::-webkit-scrollbar-thumb {
background: var(--scrollbar-thumb-color);
}
#scroll-preview::-webkit-scrollbar-thumb:hover {
background: var(--scrollbar-thumb-hover-color);
}
`;

document.head.appendChild(styleTag);
}

function updatePreview() {
applyPreviewStyles({
trackColor: trackColor.value,
thumbColor: thumbColor.value,
hoverColor: hoverColor.value,
width: width.value,
});
}

[trackColor, thumbColor, hoverColor, width].forEach((input) => {
input?.addEventListener('input', updatePreview);
});

updatePreview();

getCodeButton?.addEventListener('click', () =>
copyHandler({
trackColor: trackColor.value,
Expand Down
8 changes: 8 additions & 0 deletions src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,14 @@ input[type='range']:focus::-moz-range-thumb {
color: var(--text-color);
}

#scroll-preview {
width: 300px;
height: 200px;
overflow-y: scroll;
border: 1px solid var(--text-color);
padding: 12px;
}

/* FOOTER SECTION */

footer {
Expand Down

0 comments on commit 2d7abdf

Please sign in to comment.