@gem-sdk/components 2.0.0-dev.345 → 2.0.0-dev.347

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.
@@ -4,15 +4,41 @@ const AutoPlayPlugin = (slider)=>{
4
4
  let autoplay = slider.options.autoplay ?? {};
5
5
  let timeout;
6
6
  let mouseOver = false;
7
+ let inViewport = false;
8
+ let observer = null;
9
+ // Thêm sự kiện cho dot nếu có
10
+ const $dots = slider?.container?.closest('[data-component-type="component"]')?.querySelector('.carousel-dots');
11
+ // Callback của IntersectionObserver
12
+ function handleIntersection(entries) {
13
+ entries.forEach((entry)=>{
14
+ // Với threshold 0, chỉ cần một phần bất kỳ hiển thị sẽ đánh dấu isIntersecting
15
+ inViewport = entry.isIntersecting;
16
+ if (inViewport) {
17
+ nextTimeout();
18
+ } else {
19
+ clearNextTimeout();
20
+ }
21
+ });
22
+ }
23
+ function initIntersectionObserver() {
24
+ // Nếu đã có observer hoặc không có container thì không khởi tạo lại
25
+ if (observer || !slider.container) return;
26
+ observer = new IntersectionObserver(handleIntersection, {
27
+ root: null,
28
+ threshold: 0
29
+ });
30
+ observer.observe(slider.container);
31
+ }
7
32
  function clearNextTimeout() {
8
33
  clearTimeout(timeout);
9
34
  }
10
35
  function nextTimeout() {
11
- clearTimeout(timeout);
36
+ clearNextTimeout();
12
37
  timeout = setTimeout(()=>{
13
- if (mouseOver || !autoplay.enable) return;
38
+ // Nếu đang hover, autoplay bị tắt hoặc slider không trong viewport thì không chuyển slide
39
+ if (mouseOver || !autoplay.enable || !inViewport) return;
14
40
  slider.next();
15
- }, autoplay?.delay ?? 2000);
41
+ }, autoplay.delay ?? 2000);
16
42
  }
17
43
  function onMouseOver() {
18
44
  mouseOver = true;
@@ -23,11 +49,15 @@ const AutoPlayPlugin = (slider)=>{
23
49
  nextTimeout();
24
50
  }
25
51
  slider.on('created', ()=>{
26
- if (autoplay.pauseOnHover) {
52
+ $dots?.addEventListener('mouseover', onMouseOver);
53
+ $dots?.addEventListener('mouseout', onMouseOut);
54
+ // Nếu tùy chọn pauseOnHover bật thì thêm sự kiện hover cho container slider
55
+ if (autoplay.pauseOnHover && slider.container) {
27
56
  slider.container.addEventListener('mouseover', onMouseOver);
28
57
  slider.container.addEventListener('mouseout', onMouseOut);
29
58
  }
30
- if (slider.options.autoplay?.enable) {
59
+ if (autoplay.enable) {
60
+ initIntersectionObserver();
31
61
  nextTimeout();
32
62
  }
33
63
  });
@@ -35,29 +65,40 @@ const AutoPlayPlugin = (slider)=>{
35
65
  slider.on('animationEnded', nextTimeout);
36
66
  slider.on('updated', (newOptions)=>{
37
67
  autoplay = newOptions.options?.autoplay ?? {};
38
- nextTimeout();
39
- });
40
- slider.on('destroyed', ()=>{
41
- slider.container.removeEventListener('mouseover', onMouseOver);
42
- slider.container.removeEventListener('mouseout', onMouseOut);
68
+ if (autoplay.enable) {
69
+ initIntersectionObserver();
70
+ nextTimeout();
71
+ }
43
72
  });
44
73
  slider.on('optionsChanged', (newOptions)=>{
45
- const $dots = slider?.container?.closest('[data-component-type="component"]')?.querySelector('.carousel-dots');
46
- $dots?.addEventListener('mouseover', onMouseOver);
47
- $dots?.addEventListener('mouseout', onMouseOut);
48
- if (newOptions.options.autoplay?.pauseOnHover !== autoplay.pauseOnHover) {
74
+ if (newOptions.options.autoplay?.pauseOnHover !== autoplay.pauseOnHover && slider.container) {
49
75
  if (newOptions.options.autoplay?.pauseOnHover) {
50
76
  slider.container.addEventListener('mouseover', onMouseOver);
51
77
  slider.container.addEventListener('mouseout', onMouseOut);
52
- }
53
- if (!newOptions.options.autoplay?.pauseOnHover) {
78
+ } else {
54
79
  slider.container.removeEventListener('mouseover', onMouseOver);
55
80
  slider.container.removeEventListener('mouseout', onMouseOut);
56
81
  mouseOver = false;
57
82
  }
58
83
  }
59
- nextTimeout();
60
84
  autoplay = newOptions.options.autoplay ?? {};
85
+ if (autoplay.enable) {
86
+ initIntersectionObserver();
87
+ nextTimeout();
88
+ }
89
+ });
90
+ slider.on('destroyed', ()=>{
91
+ if (slider.container) {
92
+ slider.container.removeEventListener('mouseover', onMouseOver);
93
+ slider.container.removeEventListener('mouseout', onMouseOut);
94
+ }
95
+ $dots?.removeEventListener('mouseover', onMouseOver);
96
+ $dots?.removeEventListener('mouseout', onMouseOut);
97
+ clearNextTimeout();
98
+ if (observer) {
99
+ observer.disconnect();
100
+ observer = null;
101
+ }
61
102
  });
62
103
  };
63
104
 
@@ -7,11 +7,12 @@ var getClass = require('./common/getClass.js');
7
7
 
8
8
  const ProductListItem = (props)=>{
9
9
  const { pageContext, children } = props;
10
- const { productItemClass } = getClass.getClass(props);
10
+ const { productItemClass, gpProductClass } = getClass.getClass(props);
11
11
  return core.template /* liquid */ `
12
12
  <gp-product
13
13
  gp-context='{"variantSelected": {{ variant | json | escape }}, "quantity": 1 , "formId": "{{ product_form_id }}"}'
14
14
  gp-data='{"variantSelected": {{ variant | json | escape }}, "quantity": 1, "productUrl":{{ product.url | json | escape }}, "productHandle":{{ product.handle | json | escape }}, "collectionUrl": {{ collection.url | json | escape }}, "collectionHandle": {{ collection.handle | json | escape }},"pageContext" : ${JSON.stringify(pageContext)}}'
15
+ class="${gpProductClass}"
15
16
  >
16
17
  {%- form 'product', product, id: product_form_id, class: 'form contents ${productItemClass}', data-type: 'add-to-cart-form', autocomplete: 'off' -%}
17
18
  <input type="hidden" name="id" value="{{ variant.id }}" />
@@ -17,19 +17,23 @@ const getClass = (props)=>{
17
17
  const productListWrapClass = system.createClass({
18
18
  'gp-relative gp-flex': true
19
19
  });
20
+ const gpProductClass = system.createClass({
21
+ 'gp-contents': setting?.sameHeight
22
+ });
20
23
  const carouselClass = system.createClass({
21
24
  'gp-justify-center': true,
22
25
  'gp-overflow-hidden': !setting?.sameHeight
23
26
  });
24
27
  const productItemClass = system.createClass({
25
- 'gp-contents [&_*[data-same-height-display-contents="true"]]:gp-contents [&_*[data-component-label="Row"]]:gp-grid [&_*[data-component-label="Row"]]:gp-grid-rows-[subgrid] [&_*[data-component-label="Row"]]:gp-gap-y-0 [&_*[data-component-label="Row"]]:gp-grid-col-[subgrid]': setting?.sameHeight,
28
+ 'gp-contents [&_*[data-same-height-display-contents="true"]]:gp-contents [&_*[data-same-height-subgrid-container="true"]]:gp-grid [&_*[data-same-height-subgrid-container="true"]]:gp-grid-rows-[subgrid] [&_*[data-same-height-subgrid-container="true"]]:gp-gap-y-0 [&_*[data-same-height-subgrid-container="true"]]:gp-grid-col-[subgrid]': setting?.sameHeight,
26
29
  [`gp-child-item-${builderProps?.uid}`]: true
27
30
  });
28
31
  return {
29
32
  gridLayoutClass,
30
33
  productItemClass,
31
34
  carouselClass,
32
- productListWrapClass
35
+ productListWrapClass,
36
+ gpProductClass
33
37
  };
34
38
  };
35
39
 
@@ -2,15 +2,41 @@ const AutoPlayPlugin = (slider)=>{
2
2
  let autoplay = slider.options.autoplay ?? {};
3
3
  let timeout;
4
4
  let mouseOver = false;
5
+ let inViewport = false;
6
+ let observer = null;
7
+ // Thêm sự kiện cho dot nếu có
8
+ const $dots = slider?.container?.closest('[data-component-type="component"]')?.querySelector('.carousel-dots');
9
+ // Callback của IntersectionObserver
10
+ function handleIntersection(entries) {
11
+ entries.forEach((entry)=>{
12
+ // Với threshold 0, chỉ cần một phần bất kỳ hiển thị sẽ đánh dấu isIntersecting
13
+ inViewport = entry.isIntersecting;
14
+ if (inViewport) {
15
+ nextTimeout();
16
+ } else {
17
+ clearNextTimeout();
18
+ }
19
+ });
20
+ }
21
+ function initIntersectionObserver() {
22
+ // Nếu đã có observer hoặc không có container thì không khởi tạo lại
23
+ if (observer || !slider.container) return;
24
+ observer = new IntersectionObserver(handleIntersection, {
25
+ root: null,
26
+ threshold: 0
27
+ });
28
+ observer.observe(slider.container);
29
+ }
5
30
  function clearNextTimeout() {
6
31
  clearTimeout(timeout);
7
32
  }
8
33
  function nextTimeout() {
9
- clearTimeout(timeout);
34
+ clearNextTimeout();
10
35
  timeout = setTimeout(()=>{
11
- if (mouseOver || !autoplay.enable) return;
36
+ // Nếu đang hover, autoplay bị tắt hoặc slider không trong viewport thì không chuyển slide
37
+ if (mouseOver || !autoplay.enable || !inViewport) return;
12
38
  slider.next();
13
- }, autoplay?.delay ?? 2000);
39
+ }, autoplay.delay ?? 2000);
14
40
  }
15
41
  function onMouseOver() {
16
42
  mouseOver = true;
@@ -21,11 +47,15 @@ const AutoPlayPlugin = (slider)=>{
21
47
  nextTimeout();
22
48
  }
23
49
  slider.on('created', ()=>{
24
- if (autoplay.pauseOnHover) {
50
+ $dots?.addEventListener('mouseover', onMouseOver);
51
+ $dots?.addEventListener('mouseout', onMouseOut);
52
+ // Nếu tùy chọn pauseOnHover bật thì thêm sự kiện hover cho container slider
53
+ if (autoplay.pauseOnHover && slider.container) {
25
54
  slider.container.addEventListener('mouseover', onMouseOver);
26
55
  slider.container.addEventListener('mouseout', onMouseOut);
27
56
  }
28
- if (slider.options.autoplay?.enable) {
57
+ if (autoplay.enable) {
58
+ initIntersectionObserver();
29
59
  nextTimeout();
30
60
  }
31
61
  });
@@ -33,29 +63,40 @@ const AutoPlayPlugin = (slider)=>{
33
63
  slider.on('animationEnded', nextTimeout);
34
64
  slider.on('updated', (newOptions)=>{
35
65
  autoplay = newOptions.options?.autoplay ?? {};
36
- nextTimeout();
37
- });
38
- slider.on('destroyed', ()=>{
39
- slider.container.removeEventListener('mouseover', onMouseOver);
40
- slider.container.removeEventListener('mouseout', onMouseOut);
66
+ if (autoplay.enable) {
67
+ initIntersectionObserver();
68
+ nextTimeout();
69
+ }
41
70
  });
42
71
  slider.on('optionsChanged', (newOptions)=>{
43
- const $dots = slider?.container?.closest('[data-component-type="component"]')?.querySelector('.carousel-dots');
44
- $dots?.addEventListener('mouseover', onMouseOver);
45
- $dots?.addEventListener('mouseout', onMouseOut);
46
- if (newOptions.options.autoplay?.pauseOnHover !== autoplay.pauseOnHover) {
72
+ if (newOptions.options.autoplay?.pauseOnHover !== autoplay.pauseOnHover && slider.container) {
47
73
  if (newOptions.options.autoplay?.pauseOnHover) {
48
74
  slider.container.addEventListener('mouseover', onMouseOver);
49
75
  slider.container.addEventListener('mouseout', onMouseOut);
50
- }
51
- if (!newOptions.options.autoplay?.pauseOnHover) {
76
+ } else {
52
77
  slider.container.removeEventListener('mouseover', onMouseOver);
53
78
  slider.container.removeEventListener('mouseout', onMouseOut);
54
79
  mouseOver = false;
55
80
  }
56
81
  }
57
- nextTimeout();
58
82
  autoplay = newOptions.options.autoplay ?? {};
83
+ if (autoplay.enable) {
84
+ initIntersectionObserver();
85
+ nextTimeout();
86
+ }
87
+ });
88
+ slider.on('destroyed', ()=>{
89
+ if (slider.container) {
90
+ slider.container.removeEventListener('mouseover', onMouseOver);
91
+ slider.container.removeEventListener('mouseout', onMouseOut);
92
+ }
93
+ $dots?.removeEventListener('mouseover', onMouseOver);
94
+ $dots?.removeEventListener('mouseout', onMouseOut);
95
+ clearNextTimeout();
96
+ if (observer) {
97
+ observer.disconnect();
98
+ observer = null;
99
+ }
59
100
  });
60
101
  };
61
102
 
@@ -3,11 +3,12 @@ import { getClass } from './common/getClass.js';
3
3
 
4
4
  const ProductListItem = (props)=>{
5
5
  const { pageContext, children } = props;
6
- const { productItemClass } = getClass(props);
6
+ const { productItemClass, gpProductClass } = getClass(props);
7
7
  return template /* liquid */ `
8
8
  <gp-product
9
9
  gp-context='{"variantSelected": {{ variant | json | escape }}, "quantity": 1 , "formId": "{{ product_form_id }}"}'
10
10
  gp-data='{"variantSelected": {{ variant | json | escape }}, "quantity": 1, "productUrl":{{ product.url | json | escape }}, "productHandle":{{ product.handle | json | escape }}, "collectionUrl": {{ collection.url | json | escape }}, "collectionHandle": {{ collection.handle | json | escape }},"pageContext" : ${JSON.stringify(pageContext)}}'
11
+ class="${gpProductClass}"
11
12
  >
12
13
  {%- form 'product', product, id: product_form_id, class: 'form contents ${productItemClass}', data-type: 'add-to-cart-form', autocomplete: 'off' -%}
13
14
  <input type="hidden" name="id" value="{{ variant.id }}" />
@@ -15,19 +15,23 @@ const getClass = (props)=>{
15
15
  const productListWrapClass = createClass({
16
16
  'gp-relative gp-flex': true
17
17
  });
18
+ const gpProductClass = createClass({
19
+ 'gp-contents': setting?.sameHeight
20
+ });
18
21
  const carouselClass = createClass({
19
22
  'gp-justify-center': true,
20
23
  'gp-overflow-hidden': !setting?.sameHeight
21
24
  });
22
25
  const productItemClass = createClass({
23
- 'gp-contents [&_*[data-same-height-display-contents="true"]]:gp-contents [&_*[data-component-label="Row"]]:gp-grid [&_*[data-component-label="Row"]]:gp-grid-rows-[subgrid] [&_*[data-component-label="Row"]]:gp-gap-y-0 [&_*[data-component-label="Row"]]:gp-grid-col-[subgrid]': setting?.sameHeight,
26
+ 'gp-contents [&_*[data-same-height-display-contents="true"]]:gp-contents [&_*[data-same-height-subgrid-container="true"]]:gp-grid [&_*[data-same-height-subgrid-container="true"]]:gp-grid-rows-[subgrid] [&_*[data-same-height-subgrid-container="true"]]:gp-gap-y-0 [&_*[data-same-height-subgrid-container="true"]]:gp-grid-col-[subgrid]': setting?.sameHeight,
24
27
  [`gp-child-item-${builderProps?.uid}`]: true
25
28
  });
26
29
  return {
27
30
  gridLayoutClass,
28
31
  productItemClass,
29
32
  carouselClass,
30
- productListWrapClass
33
+ productListWrapClass,
34
+ gpProductClass
31
35
  };
32
36
  };
33
37
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gem-sdk/components",
3
- "version": "2.0.0-dev.345",
3
+ "version": "2.0.0-dev.347",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "main": "dist/cjs/index.js",