think-drag-slider 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # think-drag-slider
2
+
3
+ A lightweight JavaScript utility for enabling click-and-drag horizontal scrolling on any element.
4
+
5
+ ## Features
6
+
7
+ - Adds drag-to-scroll behavior to one or more elements selected by CSS selector
8
+ - Adds helper classes automatically:
9
+ - `.drag-slider` on initialization
10
+ - `.active` while dragging
11
+ - Includes optional CSS for cursor states and disabling accidental text/image dragging
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install think-drag-slider
17
+ ```
18
+
19
+ ## Basic Usage
20
+
21
+ ```js
22
+ import dragSlider from 'think-drag-slider';
23
+ import 'think-drag-slider/dragSlider.css';
24
+
25
+ dragSlider('.my-scroll-row');
26
+ ```
27
+
28
+ ## HTML Example
29
+
30
+ ```html
31
+ <div class="my-scroll-row">
32
+ <article>Item 1</article>
33
+ <article>Item 2</article>
34
+ <article>Item 3</article>
35
+ </div>
36
+ ```
37
+
38
+ Make sure your container can scroll horizontally, for example with layout/overflow styles:
39
+
40
+ ```css
41
+ .my-scroll-row {
42
+ display: flex;
43
+ gap: 1rem;
44
+ overflow-x: auto;
45
+ white-space: nowrap;
46
+ }
47
+ ```
48
+
49
+ ## API
50
+
51
+ ### `dragSlider(target)`
52
+
53
+ - `target` (`string`): CSS selector used by `document.querySelectorAll`.
54
+ - Returns: `void`.
55
+
56
+ If no elements match, the function exits safely.
57
+
58
+ ## Behavior Notes
59
+
60
+ - Current implementation listens to mouse events (`mousedown`, `mousemove`, `mouseup`, `mouseleave`).
61
+ - Drag speed is multiplied internally for a faster feel.
62
+ - The utility currently logs drag distance to the console during dragging.
63
+
64
+ ## Included Files
65
+
66
+ - `dragSlider.js`
67
+ - `dragSlider.css`
68
+
69
+ ## Development
70
+
71
+ Run the default script:
72
+
73
+ ```bash
74
+ npm test
75
+ ```
76
+
77
+ (Currently this project has no automated tests configured.)
78
+
79
+ ## License
80
+
81
+ MIT
package/dragSlider.css ADDED
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @file dragSlider.css
3
+ * @description Cursor and interaction styles for draggable slider elements.
4
+ */
5
+
6
+ /**
7
+ * Root drag slider container.
8
+ */
9
+ .drag-slider {
10
+ position: relative;
11
+ cursor: grab;
12
+ scrollbar-width: none;
13
+ -ms-overflow-style: none;
14
+ }
15
+
16
+ .drag-slider::-webkit-scrollbar {
17
+ display: none;
18
+ }
19
+
20
+ /**
21
+ * Cursor feedback while dragging.
22
+ */
23
+ .drag-slider.active {
24
+ cursor: grabbing;
25
+ }
26
+
27
+ /**
28
+ * Disable text/image dragging and text selection on descendants.
29
+ * Repeated vendor-prefixed declarations are intentional for broad browser support.
30
+ */
31
+ .drag-slider * {
32
+ -webkit-user-select: none !important;
33
+ -ms-user-select: none !important;
34
+ user-select: none !important;
35
+ user-select: none;
36
+ -moz-user-select: none;
37
+ -webkit-user-drag: none;
38
+ -webkit-user-select: none;
39
+ -ms-user-select: none;
40
+ }
package/dragSlider.js ADDED
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Enables click-and-drag horizontal scrolling on matching elements.
3
+ *
4
+ * @param {string} target CSS selector used to find slider elements.
5
+ * @returns {void}
6
+ */
7
+ export default function dragSlider(target) {
8
+ const sliders = document.querySelectorAll(target);
9
+
10
+ if (!sliders.length) return;
11
+
12
+ sliders.forEach((slider) => {
13
+ let isDown = false;
14
+ let startX = 0;
15
+ let scrollLeft = 0;
16
+
17
+ slider.classList.add('drag-slider');
18
+
19
+ slider.addEventListener('mousedown', (e) => {
20
+ isDown = true;
21
+ slider.classList.add('active');
22
+ startX = e.pageX - slider.offsetLeft;
23
+ scrollLeft = slider.scrollLeft;
24
+ });
25
+ slider.addEventListener('mouseleave', () => {
26
+ isDown = false;
27
+ slider.classList.remove('active');
28
+ });
29
+ slider.addEventListener('mouseup', () => {
30
+ isDown = false;
31
+ slider.classList.remove('active');
32
+ });
33
+ slider.addEventListener('mousemove', (e) => {
34
+ if (!isDown) return;
35
+ e.preventDefault();
36
+ const x = e.pageX - slider.offsetLeft;
37
+ const walk = (x - startX) * 3; //scroll-fast
38
+ slider.scrollLeft = scrollLeft - walk;
39
+ console.log(walk);
40
+ });
41
+ });
42
+ }
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "think-drag-slider",
3
+ "version": "1.0.0",
4
+ "description": "A lightweight JavaScript utility for enabling click-and-drag horizontal scrolling on any element.",
5
+ "main": "dragSlider.js",
6
+ "scripts": {
7
+ "test": "echo \"No tests specified\" && exit 0"
8
+ },
9
+ "keywords": [
10
+ "slider",
11
+ "javascript",
12
+ "ui"
13
+ ],
14
+ "author": "Oleh Makedonskiy <dev@think.studio>",
15
+ "license": "MIT",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "https://github.com/dev-think-one/think-drag-slider.git"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/dev-think-one/think-drag-slider/issues"
22
+ },
23
+ "files": [
24
+ "dragSlider.js",
25
+ "dragSlider.css"
26
+ ]
27
+ }