Skip to content

Instantly share code, notes, and snippets.

View keyurparalkar's full-sized avatar
💻
Focusing

Keyur Paralkar keyurparalkar

💻
Focusing
View GitHub Profile
@keyurparalkar
keyurparalkar / Slider.tsx
Created February 21, 2024 11:06
Styling the chapters
const StyledContainer = styled.div<StyledContainerProps>`
--slider-pointer: 0%; // when hover happens pointer is updated
--slider-fill: 0%; // when click and drag happens fill is updated
--slider-track-bg-color: ${COLORS.TRACK_BG_COLOR};
--slider-fill-color: ${(props) => props.$fillColor};
position: relative;
height: 30px;
width: ${(props) => props.$total};
display: flex;
@keyurparalkar
keyurparalkar / Seekbar.tsx
Created February 21, 2024 11:02
Seekbar updating chapter fill on playback
const currentChapter = chapters?.filter((chapter) => currentTime && currentTime > chapter.startTime && currentTime < chapter.endTime);
useEffect(() => {
if (sliderRef.current && !isSeeking) {
const newPosPercentage = (currentTime / totalDuration) * 100;
sliderRef.current.updateSliderFill(newPosPercentage);
if (currentChapter.length > 0) {
const { index, endTime, startTime } = currentChapter[0];
const totalChapterDuration = endTime - startTime;
@keyurparalkar
keyurparalkar / Slider.tsx
Last active February 21, 2024 11:01
Exposing function
useImperativeHandle(
ref,
() => {
return {
updateSliderFill(percentageCompleted: number) {
rootRef.current?.style.setProperty("--slider-fill", `${percentageCompleted}%`);
},
updateChapterFill(currentChapterIdx: number, completedPercentage: number) {
chapterRefs.current[currentChapterIdx].style.setProperty("--chapter-fill", `${completedPercentage}%`);
},
@keyurparalkar
keyurparalkar / Slider.tsx
Created February 21, 2024 11:00
Mouse Move event handler
const handleContainerMouseMove = (e: React.MouseEvent<HTMLDivElement>) => {
/**
* NOTE: 'data-dragging' attribute is available only when we are dragging the slider-thumb.
* Thumb is said to be dragged when onMouseDown gets triggered on the .slider-thumb -> [data-dragging] is added to .slider
* -> then handleMouseMove off .slider gets executed.
* The dragging of thumb gets stopped when the data-dragging attribute is removed from .slider
* We remove this attribute on MouseUp of .slider because on mouseup the target element can be different during dragging if the mouseup where on .slider-thumb
* To have better dragging experience the data-dragging is removed on the mouseup of .slider
*/
if (rootRef.current?.getAttribute("data-dragging")) {
@keyurparalkar
keyurparalkar / Slider.tsx
Created February 21, 2024 10:59
Clean up logic
// clean up the later chapters when going from right to left;
if (nextChapterFill > 0) {
for (let i = currentChapterIdx + 1; i < $chapters.length; i++) {
chapterRefs.current[i].style.setProperty("--chapter-fill", "0%");
}
}
@keyurparalkar
keyurparalkar / Slider.tsx
Created February 21, 2024 10:58
Fill logic
// Fill the current chapter;
if (previousChapterFill === 100 && currentChapterFill >= 0) {
const currentChapterWidth = allChapterWidths[currentChapterIdx];
const rect = currentChapterElem.getBoundingClientRect();
const totalChapterWidth = (currentChapterWidth * $total) / 100;
const chapterFillWidth = computeCurrentWidthFromPointerPos(e.pageX, rect.left, totalChapterWidth);
currentChapterElem.style.setProperty("--chapter-fill", `${chapterFillWidth}%`);
}
@keyurparalkar
keyurparalkar / Slider.tsx
Created February 21, 2024 10:57
Chapter Elements
const previousChapterFill = getCSSVariableAbsoluteValue("--chapter-fill", prevChapterElem);
const currentChapterFill = getCSSVariableAbsoluteValue("--chapter-fill", currentChapterElem);
const nextChapterFill = getCSSVariableAbsoluteValue("--chapter-fill", nextChapterElem);
@keyurparalkar
keyurparalkar / Slider.tsx
Created February 21, 2024 10:57
Style container
const StyledSliderFill = styled.div<StyledSliderFillProps>`
height: 5px;
background-color: var(--slider-fill-color);
width: var(${(props) => (props.$hasChapters ? "--chapter-fill" : "--slider-fill")}, 0%);
position: absolute;
pointer-events: none;
`;
@keyurparalkar
keyurparalkar / Slider.tsx
Created February 21, 2024 10:55
Filling prev chapters
// Fill the previous elements:
for (let i = 0; i < currentChapterIdx; i++) {
chapterRefs.current[i].style.setProperty("--chapter-fill", "100%");
}
@keyurparalkar
keyurparalkar / Slider.tsx
Created February 21, 2024 10:55
Chapter Elements
const nextIdx =
currentChapterIdx === $chapters.length - 1 ? $chapters.length - 1 : currentChapterIdx + 1;
const prevChapterIdx = currentChapterIdx === 0 ? 0 : currentChapterIdx - 1;
const prevChapterElem = chapterRefs.current[prevChapterIdx];
const currentChapterElem = chapterRefs.current[currentChapterIdx];
const nextChapterElem = chapterRefs.current[nextIdx];