@aarongustafson/pull-to-refresh 1.0.1 → 1.1.1

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
@@ -23,6 +23,14 @@ A lightweight, customizable Web Component that adds pull-to-refresh functionalit
23
23
  - **Framework-agnostic**: Works with any framework or vanilla JavaScript
24
24
  - **Lightweight**: No dependencies, just native Web Components
25
25
  - **Shadow DOM**: Encapsulated styles that won't leak
26
+ - **TypeScript-ready**: Bundled definitions for editors and build tooling
27
+
28
+ ## TypeScript & Framework Support
29
+
30
+ - Bundled `.d.ts` files describe both `PullToRefreshElement` and `definePullToRefresh`, so modern editors provide autocomplete without extra config.
31
+ - Threshold/text/disabled props now reflect to attributes, keeping declarative templates in sync with imperative code.
32
+ - `_upgradeProperty` preserves pre-upgrade assignments—handy for SSR, hydration, and framework wrappers.
33
+ - Global `HTMLElementTagNameMap` augmentation lets TypeScript understand `document.querySelector('pull-to-refresh')` out of the box.
26
34
 
27
35
  ## Quick Start
28
36
 
package/define.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function definePullToRefresh(tagName?: string): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aarongustafson/pull-to-refresh",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "Web component that enables pull-to-refresh functionality.",
5
5
  "keywords": [
6
6
  "html",
@@ -20,22 +20,28 @@
20
20
  "type": "module",
21
21
  "main": "index.js",
22
22
  "module": "pull-to-refresh.js",
23
+ "types": "pull-to-refresh.d.ts",
23
24
  "customElements": "custom-elements.json",
24
25
  "exports": {
25
26
  ".": {
27
+ "types": "./pull-to-refresh.d.ts",
26
28
  "import": "./index.js"
27
29
  },
28
30
  "./pull-to-refresh.js": {
31
+ "types": "./pull-to-refresh.d.ts",
29
32
  "import": "./pull-to-refresh.js"
30
33
  },
31
34
  "./define.js": {
35
+ "types": "./define.d.ts",
32
36
  "import": "./define.js"
33
37
  },
34
38
  "./custom-elements.json": "./custom-elements.json"
35
39
  },
36
40
  "files": [
37
41
  "pull-to-refresh.js",
42
+ "pull-to-refresh.d.ts",
38
43
  "define.js",
44
+ "define.d.ts",
39
45
  "index.js",
40
46
  "translations.json",
41
47
  "custom-elements.json",
@@ -43,10 +49,9 @@
43
49
  "LICENSE"
44
50
  ],
45
51
  "engines": {
46
- "node": ">=14.0.0"
52
+ "node": ">=22.0.0"
47
53
  },
48
54
  "scripts": {
49
- "setup": "node scripts/setup.js",
50
55
  "test": "vitest",
51
56
  "test:ui": "vitest --ui",
52
57
  "test:run": "vitest run",
@@ -57,19 +62,20 @@
57
62
  "format:eslint": "eslint . --fix",
58
63
  "lint:prettier": "prettier \"**/*.js\" --check --ignore-path .gitignore",
59
64
  "format:prettier": "prettier \"**/*.js\" --write --ignore-path .gitignore",
60
- "version": "node scripts/update-demo-versions.js && git add demo/esm.html"
65
+ "version": "node scripts/update-demo-versions.js && git add demo/esm.html",
66
+ "postversion": "git push --follow-tags"
61
67
  },
62
68
  "devDependencies": {
63
- "@open-wc/eslint-config": "^13.0.0",
64
- "@testing-library/dom": "^10.4.1",
65
- "@testing-library/user-event": "^14.6.1",
66
- "@vitest/coverage-v8": "^4.0.10",
67
- "@vitest/ui": "^4.0.10",
68
- "eslint": "^9.39.1",
69
+ "@open-wc/eslint-config": "^13.1.0",
70
+ "@testing-library/dom": "^10.4.2",
71
+ "@testing-library/user-event": "^14.6.7",
72
+ "@vitest/coverage-v8": "^5.0.5",
73
+ "@vitest/ui": "^5.0.5",
74
+ "eslint": "^10.12.0",
69
75
  "eslint-config-prettier": "^10.1.8",
70
- "happy-dom": "^20.0.10",
71
- "prettier": "^3.6.2",
72
- "vitest": "^4.0.10"
76
+ "happy-dom": "^21.0.4",
77
+ "prettier": "^3.9.9",
78
+ "vitest": "^5.0.5"
73
79
  },
74
80
  "publishConfig": {
75
81
  "access": "public"
@@ -0,0 +1,34 @@
1
+ export interface PullToRefreshTranslationSet {
2
+ indicator: string;
3
+ release: string;
4
+ refreshing: string;
5
+ }
6
+
7
+ export interface PullToRefreshRefreshDetail {
8
+ complete: () => void;
9
+ }
10
+
11
+ export type PullToRefreshRefreshEvent = CustomEvent<PullToRefreshRefreshDetail>;
12
+
13
+ export declare class PullToRefreshElement extends HTMLElement {
14
+ static registerTranslations(
15
+ translations: Record<string, PullToRefreshTranslationSet>,
16
+ ): void;
17
+
18
+ threshold: number;
19
+ indicatorText: string;
20
+ releaseText: string;
21
+ refreshingText: string;
22
+ disabled: boolean;
23
+ disableSelection: boolean;
24
+ readonly indicatorHeight: number;
25
+
26
+ triggerRefresh(): Promise<void>;
27
+ completeRefresh(): void;
28
+ }
29
+
30
+ declare global {
31
+ interface HTMLElementTagNameMap {
32
+ 'pull-to-refresh': PullToRefreshElement;
33
+ }
34
+ }
@@ -50,58 +50,6 @@ export class PullToRefreshElement extends HTMLElement {
50
50
  ];
51
51
  }
52
52
 
53
- static styles = `
54
- :host {
55
- display: block;
56
- position: relative;
57
- height: 100vh;
58
- overflow: hidden;
59
- }
60
-
61
- :host([pulling][disable-selection]) ::slotted(*) {
62
- user-select: none;
63
- -webkit-user-select: none;
64
- }
65
-
66
- .ptr-container {
67
- height: 100%;
68
- overflow-y: auto;
69
- -webkit-overflow-scrolling: touch;
70
- position: relative;
71
- }
72
-
73
- .ptr-indicator {
74
- position: absolute;
75
- top: 0;
76
- left: 0;
77
- right: 0;
78
- height: var(--ptr-indicator-height, 3.125rem);
79
- display: flex;
80
- align-items: center;
81
- justify-content: center;
82
- background: var(--ptr-indicator-bg, ButtonFace);
83
- color: var(--ptr-indicator-color, ButtonText);
84
- font-size: var(--ptr-indicator-font-size, 0.875rem);
85
- transform: translateY(-3.125rem);
86
- transition: transform var(--ptr-transition-duration, 0.2s) ease;
87
- z-index: 1000;
88
- user-select: none;
89
- -webkit-user-select: none;
90
- }
91
-
92
- .ptr-indicator.active {
93
- /* Active state can be styled via CSS parts or custom properties */
94
- }
95
-
96
- .ptr-content {
97
- position: relative;
98
- }
99
-
100
- :host([disabled]) .ptr-container {
101
- touch-action: auto;
102
- }
103
- `;
104
-
105
53
  constructor() {
106
54
  super();
107
55
  this.attachShadow({ mode: 'open' });
@@ -112,17 +60,23 @@ export class PullToRefreshElement extends HTMLElement {
112
60
  this.isPulling = false;
113
61
  this.isPullingConfirmed = false;
114
62
  this.isRefreshing = false;
63
+ this.__listenersAttached = false;
64
+ this.__refreshTimeoutId = null;
65
+ this.__ariaLiveResetTimeoutId = null;
115
66
 
116
67
  // Cached DOM references (set after render)
117
68
  this._container = null;
118
69
  this._indicator = null;
70
+ this._indicatorTextEl = null;
119
71
 
120
72
  // Language detection
121
- this.__lang =
122
- this.getAttribute('lang') ||
123
- this.closest('[lang]')?.getAttribute('lang') ||
124
- document.documentElement.lang ||
125
- 'en';
73
+ this.__lang = 'en';
74
+
75
+ this.__pointerDownOptions = { passive: true };
76
+ this.__pointerMoveOptions = { passive: false };
77
+ this.__pointerUpOptions = { passive: true };
78
+ this.__pointerCancelOptions = { passive: true };
79
+ this.__scrollOptions = { passive: true };
126
80
 
127
81
  // Bind event handlers
128
82
  this.handleStart = this.handleStart.bind(this);
@@ -132,28 +86,67 @@ export class PullToRefreshElement extends HTMLElement {
132
86
  }
133
87
 
134
88
  connectedCallback() {
89
+ this.__upgradeProperty('threshold');
90
+ this.__upgradeProperty('indicatorText');
91
+ this.__upgradeProperty('releaseText');
92
+ this.__upgradeProperty('refreshingText');
93
+ this.__upgradeProperty('disabled');
94
+ this.__upgradeProperty('disableSelection');
95
+ this.__upgradeProperty('lang');
96
+ this.__lang = this.__resolveLang();
97
+
135
98
  this.render();
136
99
  this.setupEventListeners();
137
100
  }
138
101
 
139
102
  disconnectedCallback() {
140
103
  this.removeEventListeners();
104
+ this.__clearRefreshTimeout();
105
+ this.__clearAriaLiveResetTimeout();
141
106
  }
142
107
 
143
108
  attributeChangedCallback(name, oldValue, newValue) {
144
- if (oldValue !== newValue) {
145
- if (name === 'lang') {
146
- this.__lang = newValue || 'en';
147
- this.updateIndicatorText();
109
+ if (oldValue === newValue) {
110
+ return;
111
+ }
112
+
113
+ switch (name) {
114
+ case 'lang':
115
+ this.__lang = this.__resolveLang();
116
+ this.updateIndicatorText({ force: true });
117
+ break;
118
+ case 'indicator-text':
119
+ case 'release-text':
120
+ case 'refreshing-text':
121
+ this.updateIndicatorText({ force: true });
122
+ break;
123
+ case 'threshold': {
124
+ if (newValue !== null) {
125
+ const numericValue = Number(newValue);
126
+ if (!Number.isFinite(numericValue) || numericValue < 0) {
127
+ this.removeAttribute('threshold');
128
+ break;
129
+ }
130
+ }
131
+ this.updateIndicatorText({ force: true });
132
+ break;
148
133
  }
149
- if (name === 'disabled') {
134
+ case 'disabled':
150
135
  if (this.disabled) {
151
136
  this.removeEventListeners();
137
+ this.resetIndicator();
152
138
  } else {
153
139
  this.setupEventListeners();
140
+ this.updateIndicatorText({ force: true });
154
141
  }
155
- }
156
- this.updateIndicatorText();
142
+ break;
143
+ case 'disable-selection':
144
+ if (!this.disableSelection) {
145
+ this.removeAttribute('pulling');
146
+ }
147
+ break;
148
+ default:
149
+ break;
157
150
  }
158
151
  }
159
152
 
@@ -170,36 +163,88 @@ export class PullToRefreshElement extends HTMLElement {
170
163
  }
171
164
 
172
165
  setupEventListeners() {
173
- if (this.disabled) return;
166
+ if (this.disabled || this.__listenersAttached) {
167
+ return;
168
+ }
174
169
 
175
- this.addEventListener('pointerdown', this.handleStart, {
176
- passive: true,
177
- });
178
- this.addEventListener('pointermove', this.handleMove, {
179
- passive: false,
180
- });
181
- this.addEventListener('pointerup', this.handleEnd);
182
- this.addEventListener('pointercancel', this.handleEnd);
170
+ this.addEventListener(
171
+ 'pointerdown',
172
+ this.handleStart,
173
+ this.__pointerDownOptions,
174
+ );
175
+ this.addEventListener(
176
+ 'pointermove',
177
+ this.handleMove,
178
+ this.__pointerMoveOptions,
179
+ );
180
+ this.addEventListener(
181
+ 'pointerup',
182
+ this.handleEnd,
183
+ this.__pointerUpOptions,
184
+ );
185
+ this.addEventListener(
186
+ 'pointercancel',
187
+ this.handleEnd,
188
+ this.__pointerCancelOptions,
189
+ );
183
190
 
184
- const container = this.shadowRoot.querySelector('.ptr-container');
185
- if (container) {
186
- container.addEventListener('scroll', this.handleScroll);
191
+ if (!this._container) {
192
+ this._container = this.shadowRoot.querySelector('.ptr-container');
193
+ }
194
+ if (this._container) {
195
+ this._container.addEventListener(
196
+ 'scroll',
197
+ this.handleScroll,
198
+ this.__scrollOptions,
199
+ );
187
200
  }
201
+
202
+ this.__listenersAttached = true;
188
203
  }
189
204
 
190
205
  removeEventListeners() {
191
- this.removeEventListener('pointerdown', this.handleStart);
192
- this.removeEventListener('pointermove', this.handleMove);
193
- this.removeEventListener('pointerup', this.handleEnd);
194
- this.removeEventListener('pointercancel', this.handleEnd);
206
+ if (!this.__listenersAttached) {
207
+ return;
208
+ }
209
+
210
+ this.removeEventListener(
211
+ 'pointerdown',
212
+ this.handleStart,
213
+ this.__pointerDownOptions,
214
+ );
215
+ this.removeEventListener(
216
+ 'pointermove',
217
+ this.handleMove,
218
+ this.__pointerMoveOptions,
219
+ );
220
+ this.removeEventListener(
221
+ 'pointerup',
222
+ this.handleEnd,
223
+ this.__pointerUpOptions,
224
+ );
225
+ this.removeEventListener(
226
+ 'pointercancel',
227
+ this.handleEnd,
228
+ this.__pointerCancelOptions,
229
+ );
195
230
 
196
- if (!this._container) return;
231
+ if (this._container) {
232
+ this._container.removeEventListener(
233
+ 'scroll',
234
+ this.handleScroll,
235
+ this.__scrollOptions,
236
+ );
237
+ }
197
238
 
198
- this._container.removeEventListener('scroll', this.handleScroll);
239
+ this.__listenersAttached = false;
199
240
  }
200
241
 
201
242
  handleStart(e) {
243
+ if (!this._container) {
244
+ this._container = this.shadowRoot.querySelector('.ptr-container');
245
+ }
202
246
  if (
247
+ this._container &&
203
248
  this._container.scrollTop === 0 &&
204
249
  !this.isRefreshing &&
205
250
  !this.disabled
@@ -248,7 +293,9 @@ export class PullToRefreshElement extends HTMLElement {
248
293
  this.currentY = deltaY;
249
294
 
250
295
  if (this.currentY > 0) {
251
- e.preventDefault(); // Prevent scroll bounce
296
+ if (e.cancelable) {
297
+ e.preventDefault(); // Prevent scroll bounce
298
+ }
252
299
 
253
300
  const indicatorHeight = this.indicatorHeight;
254
301
  const translateY = Math.min(
@@ -256,15 +303,15 @@ export class PullToRefreshElement extends HTMLElement {
256
303
  indicatorHeight,
257
304
  );
258
305
 
259
- this._indicator.style.transform = `translateY(${translateY}px)`;
260
-
261
- if (this.currentY > this.threshold) {
262
- this._indicator.textContent = this.releaseText;
263
- this._indicator.classList.add('active');
264
- } else {
265
- this._indicator.textContent = this.indicatorText;
266
- this._indicator.classList.remove('active');
306
+ if (this._indicator) {
307
+ this._indicator.style.transform = `translateY(${translateY}px)`;
308
+ if (this.currentY > this.threshold) {
309
+ this._indicator.classList.add('active');
310
+ } else {
311
+ this._indicator.classList.remove('active');
312
+ }
267
313
  }
314
+ this.__updateIndicatorTextForState();
268
315
 
269
316
  this.dispatchEvent(
270
317
  new CustomEvent('ptr:pull-move', {
@@ -309,9 +356,14 @@ export class PullToRefreshElement extends HTMLElement {
309
356
  }
310
357
 
311
358
  async triggerRefresh() {
359
+ if (this.isRefreshing) {
360
+ return;
361
+ }
312
362
  this.isRefreshing = true;
313
- this._indicator.textContent = this.refreshingText;
314
- this._indicator.classList.add('active');
363
+ this.__updateIndicatorTextForState();
364
+ if (this._indicator) {
365
+ this._indicator.classList.add('active');
366
+ }
315
367
 
316
368
  const refreshEvent = new CustomEvent('ptr:refresh', {
317
369
  bubbles: true,
@@ -324,7 +376,7 @@ export class PullToRefreshElement extends HTMLElement {
324
376
  this.dispatchEvent(refreshEvent);
325
377
 
326
378
  // If no listener calls complete(), auto-complete after a delay
327
- setTimeout(() => {
379
+ this.__refreshTimeoutId = setTimeout(() => {
328
380
  if (this.isRefreshing) {
329
381
  this.completeRefresh();
330
382
  }
@@ -334,6 +386,7 @@ export class PullToRefreshElement extends HTMLElement {
334
386
  completeRefresh() {
335
387
  this.isRefreshing = false;
336
388
  this.resetIndicator();
389
+ this.__clearRefreshTimeout();
337
390
 
338
391
  this.dispatchEvent(
339
392
  new CustomEvent('ptr:refresh-complete', {
@@ -351,27 +404,44 @@ export class PullToRefreshElement extends HTMLElement {
351
404
 
352
405
  this._indicator.style.transform = `translateY(${-this.indicatorHeight}px)`;
353
406
  this._indicator.classList.remove('active');
354
- this._indicator.textContent = this.indicatorText;
407
+ this.__updateIndicatorTextForState();
355
408
 
356
409
  // Re-enable announcements after text is updated
357
410
  // Use setTimeout to ensure the text change happens first
358
- setTimeout(() => {
359
- this._indicator.setAttribute('aria-live', 'assertive');
411
+ this.__clearAriaLiveResetTimeout();
412
+ this.__ariaLiveResetTimeoutId = setTimeout(() => {
413
+ if (this._indicator) {
414
+ this._indicator.setAttribute('aria-live', 'assertive');
415
+ }
416
+ this.__ariaLiveResetTimeoutId = null;
360
417
  }, 0);
361
418
  }
362
419
 
363
- updateIndicatorText() {
364
- if (!this._indicator || this.isPulling || this.isRefreshing) return;
420
+ updateIndicatorText({ force = false } = {}) {
421
+ if (this.isPulling && !force && !this.isRefreshing) {
422
+ return;
423
+ }
365
424
 
366
- this._indicator.textContent = this.indicatorText;
425
+ this.__updateIndicatorTextForState();
367
426
  }
368
427
 
369
428
  get threshold() {
370
- return parseInt(this.getAttribute('threshold')) || 80;
429
+ const attrValue = this.getAttribute('threshold');
430
+ const parsed = parseInt(attrValue ?? '', 10);
431
+ return Number.isFinite(parsed) ? parsed : 80;
371
432
  }
372
433
 
373
434
  set threshold(value) {
374
- this.setAttribute('threshold', value);
435
+ if (value === null || value === undefined || value === '') {
436
+ this.removeAttribute('threshold');
437
+ return;
438
+ }
439
+ const numericValue = Number(value);
440
+ if (!Number.isFinite(numericValue) || numericValue < 0) {
441
+ this.removeAttribute('threshold');
442
+ return;
443
+ }
444
+ this.setAttribute('threshold', String(Math.round(numericValue)));
375
445
  }
376
446
 
377
447
  get indicatorText() {
@@ -380,6 +450,10 @@ export class PullToRefreshElement extends HTMLElement {
380
450
  }
381
451
 
382
452
  set indicatorText(value) {
453
+ if (value === null || value === undefined) {
454
+ this.removeAttribute('indicator-text');
455
+ return;
456
+ }
383
457
  this.setAttribute('indicator-text', value);
384
458
  }
385
459
 
@@ -389,6 +463,10 @@ export class PullToRefreshElement extends HTMLElement {
389
463
  }
390
464
 
391
465
  set releaseText(value) {
466
+ if (value === null || value === undefined) {
467
+ this.removeAttribute('release-text');
468
+ return;
469
+ }
392
470
  this.setAttribute('release-text', value);
393
471
  }
394
472
 
@@ -398,6 +476,10 @@ export class PullToRefreshElement extends HTMLElement {
398
476
  }
399
477
 
400
478
  set refreshingText(value) {
479
+ if (value === null || value === undefined) {
480
+ this.removeAttribute('refreshing-text');
481
+ return;
482
+ }
401
483
  this.setAttribute('refreshing-text', value);
402
484
  }
403
485
 
@@ -487,7 +569,9 @@ export class PullToRefreshElement extends HTMLElement {
487
569
  <style>${PullToRefreshElement.styles}</style>
488
570
  <div class="ptr-container">
489
571
  <div class="ptr-indicator" role="status" aria-live="assertive">
490
- <slot name="indicator">${this.indicatorText}</slot>
572
+ <slot name="indicator">
573
+ <span class="ptr-indicator-text">${this.indicatorText}</span>
574
+ </slot>
491
575
  </div>
492
576
  <div class="ptr-content">
493
577
  <slot></slot>
@@ -498,5 +582,69 @@ export class PullToRefreshElement extends HTMLElement {
498
582
  // Cache DOM references for efficiency
499
583
  this._container = this.shadowRoot.querySelector('.ptr-container');
500
584
  this._indicator = this.shadowRoot.querySelector('.ptr-indicator');
585
+ this._indicatorTextEl = this.shadowRoot.querySelector(
586
+ '.ptr-indicator-text',
587
+ );
588
+ this.__updateIndicatorTextForState();
589
+ }
590
+
591
+ __resolveLang() {
592
+ return (
593
+ this.getAttribute('lang') ||
594
+ this.closest('[lang]')?.getAttribute('lang') ||
595
+ document.documentElement.lang ||
596
+ 'en'
597
+ );
598
+ }
599
+
600
+ __setIndicatorText(text) {
601
+ if (!this._indicatorTextEl) {
602
+ return;
603
+ }
604
+ this._indicatorTextEl.textContent = text;
605
+ }
606
+
607
+ __updateIndicatorTextForState() {
608
+ if (!this._indicatorTextEl) {
609
+ return;
610
+ }
611
+
612
+ if (this.isRefreshing) {
613
+ this.__setIndicatorText(this.refreshingText);
614
+ return;
615
+ }
616
+
617
+ if (this.isPulling) {
618
+ if (this.currentY > this.threshold) {
619
+ this.__setIndicatorText(this.releaseText);
620
+ } else {
621
+ this.__setIndicatorText(this.indicatorText);
622
+ }
623
+ return;
624
+ }
625
+
626
+ this.__setIndicatorText(this.indicatorText);
627
+ }
628
+
629
+ __clearRefreshTimeout() {
630
+ if (this.__refreshTimeoutId !== null) {
631
+ clearTimeout(this.__refreshTimeoutId);
632
+ this.__refreshTimeoutId = null;
633
+ }
634
+ }
635
+
636
+ __clearAriaLiveResetTimeout() {
637
+ if (this.__ariaLiveResetTimeoutId !== null) {
638
+ clearTimeout(this.__ariaLiveResetTimeoutId);
639
+ this.__ariaLiveResetTimeoutId = null;
640
+ }
641
+ }
642
+
643
+ __upgradeProperty(prop) {
644
+ if (Object.prototype.hasOwnProperty.call(this, prop)) {
645
+ const value = this[prop];
646
+ delete this[prop];
647
+ this[prop] = value;
648
+ }
501
649
  }
502
650
  }