solid-slider 1.1.0 → 1.2.6

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 CHANGED
@@ -1,6 +1,10 @@
1
- # <img width="27px" src="https://github.com/solidjs/solid-site/raw/master/src/assets/logo.png" alt="Solid logo"> &nbsp;Solid Slider
1
+ # Solid Slider
2
2
 
3
- A carousel/slider implementation in TypeScript for Solid using KeenSlider. keen-slider is a free library agnostic touch slider with native touch/swipe behavior and great performance. It comes with no dependencies, typescript support, multitouch support and is compatible with all common browsers.
3
+ [![size](https://img.shields.io/bundlephobia/minzip/solid-slider?style=for-the-badge)](https://bundlephobia.com/package/solid-slider)
4
+ [![size](https://img.shields.io/npm/v/solid-slider?style=for-the-badge)](https://www.npmjs.com/package/solid-slider)
5
+ ![npm](https://img.shields.io/npm/dw/solid-slider?style=for-the-badge)
6
+
7
+ A carousel/slider implementation in TypeScript for SolidJS. It's built on KeenSlider 6, an open-source library agnostic touch slider with native touch/swipe behavior and great performance. It comes with no dependencies, TypeScript support, multitouch support and is compatible with all common browsers.
4
8
 
5
9
  ## Installation
6
10
 
@@ -20,6 +24,28 @@ import createSlider from "solid-slider";
20
24
 
21
25
  You can find a functional demo of the slider with most features implemented here: https://codesandbox.io/s/solid-slider-j0x2g
22
26
 
27
+ ## Plugins
28
+
29
+ Plugins may be added directly via the createSlider primitive. You may add a KeenSlider plugin directly or built-in plugins shipped with this package. Currently an autoplay plugin is available that will assist with autoplaying actions in the slider. Simply add the plugins after the options parameter.
30
+
31
+ ### Autoplay
32
+
33
+ The autoplay function extends the slider with pausable playing. You can even supply a signal to control toggling autoplay. [Click here](https://codesandbox.io/s/solid-slider-autoplay-plugin-h2wphk?file=/src/index.tsx) for a demo of autoplay.
34
+
35
+ ```ts
36
+ import createSlider from "solid-slider";
37
+ import autoplay from "solid-slider/plugins/autoplay";
38
+
39
+ const [pause, togglePause] = createSignal(false);
40
+ const [slider] = createSlider(
41
+ { loop: true },
42
+ autoplay(2000, {
43
+ pause,
44
+ pauseOnDrag: true
45
+ })
46
+ );
47
+ ```
48
+
23
49
  ## Example
24
50
 
25
51
  The following is an example of how to create and then bind options using a directive.
@@ -58,11 +84,19 @@ const MyComponent = () => {
58
84
  };
59
85
  ```
60
86
 
87
+ ## Implementation
88
+
89
+ Solid Slider is meant to be a lightweight and compact wrapper of KeenSlider. It exposes helpers to make working with the slider convenient. Note that the when the slider mounts it assumes all children in the el are slides. You can override this functionality by passing in a "selector" value to target the specific slides you'd like to include.
90
+
91
+ Thie library exports it's own CSS which is the basic KeenSlider implementation for convenience. If you supply options as an accessor function, the slider will reactively update the configuration so that you don't have to destroy and recreate the slider. As of KeenSlider 6 plugins are now fully supported. You may supply them as a param in createSlider. Note that plugins are not reactively updated and must be supplied on creation.
92
+
61
93
  ## Changelog
62
94
 
63
95
  - 1.0.0 - Initial release
64
96
  - 1.0.3 - Changed the exported API to be slightly more flexible.
65
- - 1.1.0 - Upgraded to KeenSlider 6 and improved general reactivity.
97
+ - 1.1.1 - Upgraded to KeenSlider 6 and improved general reactivity.
98
+ - 1.2.5 - Added autoplay plugin and general plugin structure
99
+ - 1.2.6 - Maybe I should actually export the .css? That'd be a good idea, right?
66
100
 
67
101
  ## Keen Options API
68
102
 
package/dist/index.d.ts CHANGED
@@ -10,18 +10,17 @@ declare module "solid-js" {
10
10
  /**
11
11
  * Creates a slider powered by KeenSlider.
12
12
  *
13
- * @param {options} Options to initialize the slider with
14
- * @param {plugins} Plugins that enhance KeenSlider options
15
- * @returns {Object} An object of useful helpers
16
- * @returns {create} Register and creation function to call on setup
17
- * @returns {current} Current slide number
18
- * @returns {next} Function to trigger the next slide
19
- * @returns {prev} Function to trigger the previous slide
20
- * @returns {moveTo} Allow you to change the slider to a specific slide
21
- * @returns {refresh} Refresh trigger
22
- * @returns {details} Retrieve a list of SliderDetails
23
- * @returns {slider} Gain access to the slider itself
24
- * @returns {destroy} Destroy the entire slider (this is automatically handled)
13
+ * @param {Object} options Values to initialize the slider with
14
+ * @param {Array} plugins Extensions that enhance KeenSlider options
15
+ * @returns {[create: Function, helpers: Object]} Returns mount and helper methods
16
+ * @returns {Function} create Mounts the slider on provided element
17
+ * @returns {Function} helpers.current Current slide number
18
+ * @returns {Function} helpers.current Current slide number
19
+ * @returns {Function} helpers.next Function to trigger the next slide
20
+ * @returns {Function} helpers.prev Function to trigger the previous slide
21
+ * @returns {Function<Object>} helpers.details Provides details about the current slider
22
+ * @returns {Function} helpers.slider Returns the KeenSlider instance
23
+ * @returns {Function} helpers.destroy Manual destroy function
25
24
  *
26
25
  * @example
27
26
  * ```ts
@@ -29,7 +28,7 @@ declare module "solid-js" {
29
28
  * <div use:slider>...</div>
30
29
  * ```
31
30
  */
32
- declare const createSlider: <O = {}, P = {}, H extends string = KeenSliderHooks>(options?: KeenSliderOptions<O, P, H> | undefined, plugins?: KeenSliderPlugin<O, P, H>[] | undefined) => [create: (el: HTMLElement) => void, helpers: {
31
+ declare const createSlider: <O = {}, P = {}, H extends string = KeenSliderHooks>(options?: KeenSliderOptions<O, P, H> | Accessor<KeenSliderOptions<O, P, H>> | undefined, ...plugins: KeenSliderPlugin<O, P, H>[]) => [create: (el: HTMLElement) => void, helpers: {
33
32
  current: Accessor<number>;
34
33
  next: Accessor<void>;
35
34
  prev: Accessor<void>;
package/dist/index.js CHANGED
@@ -3,18 +3,17 @@ import KeenSlider from "keen-slider";
3
3
  /**
4
4
  * Creates a slider powered by KeenSlider.
5
5
  *
6
- * @param {options} Options to initialize the slider with
7
- * @param {plugins} Plugins that enhance KeenSlider options
8
- * @returns {Object} An object of useful helpers
9
- * @returns {create} Register and creation function to call on setup
10
- * @returns {current} Current slide number
11
- * @returns {next} Function to trigger the next slide
12
- * @returns {prev} Function to trigger the previous slide
13
- * @returns {moveTo} Allow you to change the slider to a specific slide
14
- * @returns {refresh} Refresh trigger
15
- * @returns {details} Retrieve a list of SliderDetails
16
- * @returns {slider} Gain access to the slider itself
17
- * @returns {destroy} Destroy the entire slider (this is automatically handled)
6
+ * @param {Object} options Values to initialize the slider with
7
+ * @param {Array} plugins Extensions that enhance KeenSlider options
8
+ * @returns {[create: Function, helpers: Object]} Returns mount and helper methods
9
+ * @returns {Function} create Mounts the slider on provided element
10
+ * @returns {Function} helpers.current Current slide number
11
+ * @returns {Function} helpers.current Current slide number
12
+ * @returns {Function} helpers.next Function to trigger the next slide
13
+ * @returns {Function} helpers.prev Function to trigger the previous slide
14
+ * @returns {Function<Object>} helpers.details Provides details about the current slider
15
+ * @returns {Function} helpers.slider Returns the KeenSlider instance
16
+ * @returns {Function} helpers.destroy Manual destroy function
18
17
  *
19
18
  * @example
20
19
  * ```ts
@@ -22,7 +21,7 @@ import KeenSlider from "keen-slider";
22
21
  * <div use:slider>...</div>
23
22
  * ```
24
23
  */
25
- const createSlider = (options, plugins) => {
24
+ const createSlider = (options, ...plugins) => {
26
25
  let slider;
27
26
  const [current, setCurrent] = createSignal(0);
28
27
  const destroy = () => slider && slider.destroy();
@@ -30,13 +29,18 @@ const createSlider = (options, plugins) => {
30
29
  const create = (el) => {
31
30
  el.classList.add("keen-slider");
32
31
  // @ts-ignore
33
- const opts = () => ({ selector: el.childNodes, ...options });
32
+ const opts = () => ({
33
+ selector: el.childNodes,
34
+ ...(typeof options == "function" ? options() : options)
35
+ });
34
36
  onMount(() => {
35
37
  slider = new KeenSlider(el, opts(), plugins);
36
- slider.on('slideChanged', () => setCurrent(slider.track.details.rel));
38
+ slider.on("slideChanged", () => setCurrent(slider.track.details.rel));
37
39
  });
38
40
  onCleanup(destroy);
39
- createEffect(on(() => options, () => slider && slider.update(opts())));
41
+ if (typeof options === "function") {
42
+ createEffect(on(() => options, () => slider && slider.update(opts())));
43
+ }
40
44
  };
41
45
  return [
42
46
  create,
@@ -44,7 +48,7 @@ const createSlider = (options, plugins) => {
44
48
  current,
45
49
  next: () => slider && slider.next(),
46
50
  prev: () => slider && slider.prev(),
47
- details: () => slider ? slider.track.details : {},
51
+ details: () => (slider ? slider.track.details : {}),
48
52
  slider: () => slider,
49
53
  moveTo: (id, duration = 250, absolute = false, easing) => slider?.moveToIdx(id, absolute, { duration, easing: easing }),
50
54
  destroy
@@ -0,0 +1,28 @@
1
+ import { Accessor } from "solid-js";
2
+ import { KeenSliderInstance } from "keen-slider";
3
+ /**
4
+ * Provides an autoplay plugin.
5
+ *
6
+ * @param {number} interval Interval in milliseconds for switching slides
7
+ * @param {object} options Options to customize the autoplay
8
+ * @param {Function} options.pause A pause signal to control the autoplay
9
+ * @param {boolean} options.pauseOnDrag Denotes of the autoplay should pause on drag.
10
+ * @param {object} options.animation Allows for control of duration and easing.
11
+ * @param {number} options.duration Duration for transitioning the slide.
12
+ * @param {number} options.easing Easing function for the transition animation.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const [create, { prev, next }] = createSlider();
17
+ * <div use:slider>...</div>
18
+ * ```
19
+ */
20
+ declare const autoplay: (interval: number | undefined, options: {
21
+ pause?: Accessor<boolean> | undefined;
22
+ pauseOnDrag?: boolean | undefined;
23
+ animation?: {
24
+ duration?: number | undefined;
25
+ easing?: ((t: number) => number) | undefined;
26
+ } | undefined;
27
+ }) => (slider: KeenSliderInstance) => void;
28
+ export default autoplay;
@@ -0,0 +1,33 @@
1
+ import { createEffect } from "solid-js";
2
+ import createTimer, { Schedule } from "@solid-primitives/timer";
3
+ /**
4
+ * Provides an autoplay plugin.
5
+ *
6
+ * @param {number} interval Interval in milliseconds for switching slides
7
+ * @param {object} options Options to customize the autoplay
8
+ * @param {Function} options.pause A pause signal to control the autoplay
9
+ * @param {boolean} options.pauseOnDrag Denotes of the autoplay should pause on drag.
10
+ * @param {object} options.animation Allows for control of duration and easing.
11
+ * @param {number} options.duration Duration for transitioning the slide.
12
+ * @param {number} options.easing Easing function for the transition animation.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const [create, { prev, next }] = createSlider();
17
+ * <div use:slider>...</div>
18
+ * ```
19
+ */
20
+ const autoplay = (interval = 1000, options) => {
21
+ return (slider) => {
22
+ let clear;
23
+ const start = () => {
24
+ clear = createTimer(() => slider.moveToIdx(slider.track.details.position + 1, true, options.animation), interval, Schedule.Interval);
25
+ };
26
+ // Pause the slider on drag
27
+ if (options.pauseOnDrag || true) {
28
+ slider.on("dragStarted", () => clear());
29
+ }
30
+ createEffect(() => (!options.pause || options.pause() === false ? start() : clear()));
31
+ };
32
+ };
33
+ export default autoplay;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "solid-slider",
3
- "version": "1.1.0",
4
- "description": "A slider/carousel component for Solid powered by KeenSlider.",
3
+ "version": "1.2.6",
4
+ "description": "A slider utility for SolidJS.",
5
5
  "author": "David Di Biase",
6
6
  "license": "ISC",
7
7
  "bugs": {
@@ -16,6 +16,11 @@
16
16
  },
17
17
  "main": "dist/index.js",
18
18
  "module": "dist/index.js",
19
+ "exports": {
20
+ ".": "./dist/index.js",
21
+ "./plugins/autoplay": "./dist/plugins/autoplay.js",
22
+ "./slider.css": "./dist/slider.css"
23
+ },
19
24
  "types": "dist/index.d.ts",
20
25
  "sideEffects": "false",
21
26
  "files": [
@@ -38,12 +43,13 @@
38
43
  "plugin"
39
44
  ],
40
45
  "dependencies": {
46
+ "@solid-primitives/timer": "^1.1.0",
41
47
  "keen-slider": "^6.6.3",
42
48
  "rimraf": "^3.0.2",
43
49
  "solid-js": "^1.3.5",
44
50
  "typescript": "^4.5.5"
45
51
  },
46
52
  "devDependencies": {
47
- "prettier": "^2.4.1"
53
+ "prettier": "^2.5.1"
48
54
  }
49
55
  }