Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Slider - Draggable functionality #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/slider/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@

<br>

<!-- Slider with Draggable Feature -->
<tp-slider flexible-height="yes" infinite="yes" draggable="yes">
<tp-slider-arrow direction="previous"><button>&laquo; Previous</button></tp-slider-arrow>
<tp-slider-arrow direction="next"><button>Next &raquo;</button></tp-slider-arrow>
<tp-slider-track>
<tp-slider-slides>
<tp-slider-slide><img src="https://picsum.photos/id/65/600/300" width="600" height="300" alt=""></tp-slider-slide>
<tp-slider-slide><img src="https://picsum.photos/id/76/600/300" width="600" height="300" alt=""></tp-slider-slide>
<tp-slider-slide><img src="https://picsum.photos/id/237/600/300" width="600" height="300" alt=""></tp-slider-slide>
<tp-slider-slide>
<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>
<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>
<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>
</tp-slider-slide>
</tp-slider-slides>
</tp-slider-track>
<tp-slider-nav>
<tp-slider-nav-item><button>1</button></tp-slider-nav-item>
<tp-slider-nav-item><button>2</button></tp-slider-nav-item>
<tp-slider-nav-item><button>3</button></tp-slider-nav-item>
<tp-slider-nav-item><button>4</button></tp-slider-nav-item>
</tp-slider-nav>
<tp-slider-count current="1" total="4" format="$current / $total">1 / 4</tp-slider-count>
</tp-slider>

<br>

<!--Slider with 'fade in' effect and not flexible height-->
<tp-slider behaviour="fade" infinite="yes" swipe="yes">
<tp-slider-arrow direction="previous"><button>&laquo; Previous</button></tp-slider-arrow>
Expand Down
62 changes: 62 additions & 0 deletions src/slider/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
tp-slider {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AvinashVinod03 Is this committed by mistake?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No @junaidbhura, as this is for the draggable functionality example.

display: block;
}

tp-slider-track {
display: block;
overflow-y: visible;
overflow-x: clip;
position: relative;
}

tp-slider-slides {
position: relative;
display: flex;
align-items: flex-start;
}
tp-slider:not([resizing=yes]) tp-slider-slides {
transition-duration: 0.6s;
transition-timing-function: cubic-bezier(0.42, 0, 0.58, 1);
}

tp-slider-slide {
flex: 0 0 100%;
scroll-snap-align: start;
}
tp-slider[flexible-height=yes]:not([initialized]) tp-slider-slide:not(:first-child) {
display: none;
}

tp-slider-nav {
display: flex;
gap: 10px;
}

tp-slider[behaviour=fade] {
/**
* We are using first of type and direct child here
* so that if there is a nested slider it does not affect
* the styles for the inner slider and is only applied to the
* parent.
*/
}
tp-slider[behaviour=fade] tp-slider-slides:first-of-type {
display: block;
}
tp-slider[behaviour=fade] tp-slider-slides:first-of-type > tp-slider-slide {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: auto;
transition-property: opacity, visibility;
transition-duration: 0.6s;
transition-timing-function: ease;
visibility: hidden;
opacity: 0;
}
tp-slider[behaviour=fade] tp-slider-slides:first-of-type > tp-slider-slide[active=yes] {
visibility: visible;
opacity: 1;
z-index: 1;
}/*# sourceMappingURL=style.css.map */
2 changes: 1 addition & 1 deletion src/slider/style.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 0 additions & 12 deletions src/slider/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,6 @@ tp-slider-slide {
}
}

.tp-slider--grabbing {
cursor: grabbing;
}

.tp-slider--grab {
cursor: grab;
}

.tp-slider--dragging {
user-select: none;
}

tp-slider-nav {
display: flex;
gap: 10px;
Expand Down
22 changes: 9 additions & 13 deletions src/slider/tp-slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,9 @@ export class TPSliderElement extends HTMLElement {
// Prevent the default behavior (e.g., text selection)
event.preventDefault();

// Apply the grabbing cursor class
this.classList.add( 'tp-slider--grabbing' );
this.classList.remove( 'tp-slider--grab' );

// Prevent text selection
this.classList.add( 'tp-slider--dragging' );
// Update styles for grabbing and disable text selection.
this.style.cursor = 'grabbing';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AvinashVinod03 Perhaps this should be attributes on the component itself. Please avoid using CSS in JS

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@junaidbhura do you want that we want to use classList.add and give styles with the css file itself ?

this.style.userSelect = 'none';
}

/**
Expand Down Expand Up @@ -197,10 +194,9 @@ export class TPSliderElement extends HTMLElement {
this.isDragging = false;
this.dragOffsetX = 0;

// Reset cursor and user-select styles
this.classList.remove( 'tp-slider--grabbing' );
this.classList.add( 'tp-slider--grab' );
this.classList.remove( 'tp-slider--dragging' );
// Update styles for releasing and re-enable text selection.
this.style.cursor = 'grab';
this.style.userSelect = '';
}
}

Expand All @@ -209,8 +205,8 @@ export class TPSliderElement extends HTMLElement {
*/
handleMouseOver() {
// Only styles when the draggable will be yes.
if ( this.getAttribute( 'draggable' ) === 'yes' ) {
this.classList.add( 'tp-slider--grab' );
if ( 'yes' === this.getAttribute( 'draggable' ) ) {
this.style.cursor = 'grab';
}
}

Expand All @@ -220,7 +216,7 @@ export class TPSliderElement extends HTMLElement {
handleMouseOut() {
// Reset cursor if not dragging.
if ( ! this.isDragging ) {
this.classList.remove( 'tp-slider--grab' );
this.style.cursor = '';
}
}

Expand Down