-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
tp-slider { | ||
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 */ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,11 @@ export class TPSliderElement extends HTMLElement { | |
'per-view', | ||
'step', | ||
'responsive', | ||
'draggable', | ||
]; | ||
protected isDragging: boolean = false; | ||
protected dragStartX: number = 0; | ||
protected dragOffsetX: number = 0; | ||
|
||
/** | ||
* Constructor. | ||
|
@@ -68,6 +72,9 @@ export class TPSliderElement extends HTMLElement { | |
// Touch listeners. | ||
this.addEventListener( 'touchstart', this.handleTouchStart.bind( this ), { passive: true } ); | ||
this.addEventListener( 'touchend', this.handleTouchEnd.bind( this ) ); | ||
|
||
// Check for draggable initialization. | ||
this.checkDraggable(); | ||
} | ||
|
||
/** | ||
|
@@ -91,7 +98,126 @@ export class TPSliderElement extends HTMLElement { | |
*/ | ||
static get observedAttributes(): string[] { | ||
// Observed attributes. | ||
return [ 'current-slide', 'flexible-height', 'infinite', 'swipe', 'per-view', 'step' ]; | ||
return [ 'current-slide', 'flexible-height', 'infinite', 'swipe', 'per-view', 'step', 'draggable' ]; | ||
} | ||
|
||
/** | ||
* Check and initialize draggable behavior. | ||
*/ | ||
checkDraggable() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @AvinashVinod03 I wonder if draggable should be implied by |
||
// Check if draggable attribute is set to "yes". | ||
if ( 'yes' === this.getAttribute( 'draggable' ) ) { | ||
this.initializeDraggableEvents(); | ||
} | ||
} | ||
|
||
/** | ||
* Initialize draggable event listeners. | ||
*/ | ||
initializeDraggableEvents() { | ||
// Add event listeners to handle dragging behavior (start, move, stop) for the slider. | ||
this.addEventListener( 'mousedown', this.handleMouseDown.bind( this ) ); | ||
this.addEventListener( 'mousemove', this.handleMouseMove.bind( this ) ); | ||
this.addEventListener( 'mouseup', this.handleMouseUp.bind( this ) ); | ||
this.addEventListener( 'mouseleave', this.handleMouseUp.bind( this ) ); | ||
|
||
// Add cursor changes for grab on hover | ||
this.addEventListener( 'mouseover', this.handleMouseOver.bind( this ) ); | ||
this.addEventListener( 'mouseout', this.handleMouseOut.bind( this ) ); | ||
} | ||
|
||
/** | ||
* Mouse down event - Start dragging. | ||
* | ||
* @param {MouseEvent} event - The mouse event object. | ||
*/ | ||
handleMouseDown( event: MouseEvent ) { | ||
// Set dragging state to true | ||
this.isDragging = true; | ||
|
||
// Store the initial X position when the mouse is pressed | ||
this.dragStartX = event.clientX; | ||
|
||
// Prevent the default behavior (e.g., text selection) | ||
event.preventDefault(); | ||
|
||
// Update styles for grabbing and disable text selection. | ||
this.style.cursor = 'grabbing'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
} | ||
|
||
/** | ||
* Mouse move event - Drag the slider. | ||
* | ||
* @param {MouseEvent} event - The mouse event object. | ||
*/ | ||
handleMouseMove( event: MouseEvent ) { | ||
// Check if the dragging is active | ||
if ( this.isDragging ) { | ||
const deltaX: number = this.dragStartX - event.clientX; | ||
this.dragOffsetX = deltaX; | ||
|
||
// Find the slides container element and apply the transformation | ||
const slidesContainer: TPSliderSlidesElement | null = this.querySelector( 'tp-slider-slides' ); | ||
|
||
// Ensure slidesContainer exists before applying the transformation | ||
if ( slidesContainer ) { | ||
slidesContainer.style.transform = `translateX(${ this.dragOffsetX }px)`; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Mouse up or mouse leave event - End dragging. | ||
*/ | ||
handleMouseUp() { | ||
// Check if the dragging is active | ||
if ( this.isDragging ) { | ||
this.isDragging = false; | ||
|
||
// Find the slides container element and apply the transformation | ||
const slidesContainer: TPSliderSlidesElement | null = this.querySelector( 'tp-slider-slides' ); | ||
|
||
// Ensure slidesContainer exists before applying the transformation | ||
if ( slidesContainer ) { | ||
slidesContainer.style.transform = `translateX(0)`; | ||
|
||
// Determine the next or previous slide based on the drag offset | ||
if ( 100 < this.dragOffsetX ) { | ||
this.next(); | ||
} else if ( -100 > this.dragOffsetX ) { | ||
this.previous(); | ||
} | ||
} | ||
|
||
// Reset draggable state | ||
this.isDragging = false; | ||
this.dragOffsetX = 0; | ||
|
||
// Update styles for releasing and re-enable text selection. | ||
this.style.cursor = 'grab'; | ||
this.style.userSelect = ''; | ||
} | ||
} | ||
|
||
/** | ||
* Handle mouse over - Set cursor to grab. | ||
*/ | ||
handleMouseOver() { | ||
// Only styles when the draggable will be yes. | ||
if ( 'yes' === this.getAttribute( 'draggable' ) ) { | ||
this.style.cursor = 'grab'; | ||
} | ||
} | ||
|
||
/** | ||
* Handle mouse out - Reset cursor. | ||
*/ | ||
handleMouseOut() { | ||
// Reset cursor if not dragging. | ||
if ( ! this.isDragging ) { | ||
this.style.cursor = ''; | ||
} | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.