@aarongustafson/pull-to-refresh 1.0.0 → 1.1.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 +13 -0
- package/package.json +11 -5
- package/pull-to-refresh.d.ts +36 -0
- package/pull-to-refresh.js +251 -132
package/README.md
CHANGED
|
@@ -9,6 +9,11 @@ A lightweight, customizable Web Component that adds pull-to-refresh functionalit
|
|
|
9
9
|
|
|
10
10
|
[Live Demo](https://aarongustafson.github.io/pull-to-refresh/demo/) ([Source](./demo/index.html))
|
|
11
11
|
|
|
12
|
+
### Additional Demos
|
|
13
|
+
|
|
14
|
+
- [esm.sh CDN](https://aarongustafson.github.io/pull-to-refresh/demo/esm.html) ([Source](./demo/esm.html)) - Loading from esm.sh
|
|
15
|
+
- [unpkg CDN](https://aarongustafson.github.io/pull-to-refresh/demo/unpkg.html) ([Source](./demo/unpkg.html)) - Loading from unpkg
|
|
16
|
+
|
|
12
17
|
## Features
|
|
13
18
|
|
|
14
19
|
- **Touch-optimized**: Smooth pull gesture with momentum and feedback
|
|
@@ -18,6 +23,14 @@ A lightweight, customizable Web Component that adds pull-to-refresh functionalit
|
|
|
18
23
|
- **Framework-agnostic**: Works with any framework or vanilla JavaScript
|
|
19
24
|
- **Lightweight**: No dependencies, just native Web Components
|
|
20
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.
|
|
21
34
|
|
|
22
35
|
## Quick Start
|
|
23
36
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aarongustafson/pull-to-refresh",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Web component that enables pull-to-refresh functionality.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"html",
|
|
@@ -20,13 +20,16 @@
|
|
|
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
|
".": {
|
|
26
|
-
"import": "./index.js"
|
|
27
|
+
"import": "./index.js",
|
|
28
|
+
"types": "./pull-to-refresh.d.ts"
|
|
27
29
|
},
|
|
28
30
|
"./pull-to-refresh.js": {
|
|
29
|
-
"import": "./pull-to-refresh.js"
|
|
31
|
+
"import": "./pull-to-refresh.js",
|
|
32
|
+
"types": "./pull-to-refresh.d.ts"
|
|
30
33
|
},
|
|
31
34
|
"./define.js": {
|
|
32
35
|
"import": "./define.js"
|
|
@@ -35,6 +38,7 @@
|
|
|
35
38
|
},
|
|
36
39
|
"files": [
|
|
37
40
|
"pull-to-refresh.js",
|
|
41
|
+
"pull-to-refresh.d.ts",
|
|
38
42
|
"define.js",
|
|
39
43
|
"index.js",
|
|
40
44
|
"translations.json",
|
|
@@ -43,7 +47,7 @@
|
|
|
43
47
|
"LICENSE"
|
|
44
48
|
],
|
|
45
49
|
"engines": {
|
|
46
|
-
"node": ">=
|
|
50
|
+
"node": ">=20.0.0"
|
|
47
51
|
},
|
|
48
52
|
"scripts": {
|
|
49
53
|
"setup": "node scripts/setup.js",
|
|
@@ -56,7 +60,9 @@
|
|
|
56
60
|
"lint:eslint": "eslint .",
|
|
57
61
|
"format:eslint": "eslint . --fix",
|
|
58
62
|
"lint:prettier": "prettier \"**/*.js\" --check --ignore-path .gitignore",
|
|
59
|
-
"format:prettier": "prettier \"**/*.js\" --write --ignore-path .gitignore"
|
|
63
|
+
"format:prettier": "prettier \"**/*.js\" --write --ignore-path .gitignore",
|
|
64
|
+
"version": "node scripts/update-demo-versions.js && git add demo/esm.html",
|
|
65
|
+
"postversion": "git push --follow-tags"
|
|
60
66
|
},
|
|
61
67
|
"devDependencies": {
|
|
62
68
|
"@open-wc/eslint-config": "^13.0.0",
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
export declare function definePullToRefresh(tagName?: string): boolean;
|
|
31
|
+
|
|
32
|
+
declare global {
|
|
33
|
+
interface HTMLElementTagNameMap {
|
|
34
|
+
'pull-to-refresh': PullToRefreshElement;
|
|
35
|
+
}
|
|
36
|
+
}
|
package/pull-to-refresh.js
CHANGED
|
@@ -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
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,57 +163,95 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
170
163
|
}
|
|
171
164
|
|
|
172
165
|
setupEventListeners() {
|
|
173
|
-
if (this.disabled)
|
|
166
|
+
if (this.disabled || this.__listenersAttached) {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
174
169
|
|
|
175
|
-
this.addEventListener(
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
|
|
185
|
-
|
|
186
|
-
container.addEventListener('scroll', this.handleScroll);
|
|
191
|
+
if (!this._container) {
|
|
192
|
+
this._container = this.shadowRoot.querySelector('.ptr-container');
|
|
187
193
|
}
|
|
194
|
+
if (this._container) {
|
|
195
|
+
this._container.addEventListener(
|
|
196
|
+
'scroll',
|
|
197
|
+
this.handleScroll,
|
|
198
|
+
this.__scrollOptions,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
this.__listenersAttached = true;
|
|
188
203
|
}
|
|
189
204
|
|
|
190
205
|
removeEventListeners() {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
this.removeEventListener('pointercancel', this.handleEnd);
|
|
206
|
+
if (!this.__listenersAttached) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
195
209
|
|
|
196
|
-
|
|
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
|
+
);
|
|
230
|
+
|
|
231
|
+
if (this._container) {
|
|
232
|
+
this._container.removeEventListener(
|
|
233
|
+
'scroll',
|
|
234
|
+
this.handleScroll,
|
|
235
|
+
this.__scrollOptions,
|
|
236
|
+
);
|
|
237
|
+
}
|
|
197
238
|
|
|
198
|
-
this.
|
|
239
|
+
this.__listenersAttached = false;
|
|
199
240
|
}
|
|
200
241
|
|
|
201
242
|
handleStart(e) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
pointerType: e.pointerType,
|
|
206
|
-
clientY: e.clientY,
|
|
207
|
-
target: e.target,
|
|
208
|
-
});
|
|
243
|
+
if (!this._container) {
|
|
244
|
+
this._container = this.shadowRoot.querySelector('.ptr-container');
|
|
245
|
+
}
|
|
209
246
|
if (
|
|
247
|
+
this._container &&
|
|
210
248
|
this._container.scrollTop === 0 &&
|
|
211
249
|
!this.isRefreshing &&
|
|
212
250
|
!this.disabled
|
|
213
251
|
) {
|
|
214
|
-
console.log('[PTR] Starting pull (pending direction confirmation)');
|
|
215
252
|
this.isPulling = true;
|
|
216
253
|
this.isPullingConfirmed = false;
|
|
217
254
|
this.startY = e.clientY;
|
|
218
|
-
} else {
|
|
219
|
-
console.log('[PTR] Not starting pull', {
|
|
220
|
-
scrollTop: this._container.scrollTop,
|
|
221
|
-
isRefreshing: this.isRefreshing,
|
|
222
|
-
disabled: this.disabled,
|
|
223
|
-
});
|
|
224
255
|
}
|
|
225
256
|
}
|
|
226
257
|
|
|
@@ -229,25 +260,16 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
229
260
|
|
|
230
261
|
const deltaY = e.clientY - this.startY;
|
|
231
262
|
|
|
232
|
-
console.log('[PTR] handleMove', {
|
|
233
|
-
clientY: e.clientY,
|
|
234
|
-
startY: this.startY,
|
|
235
|
-
delta: deltaY,
|
|
236
|
-
isPullingConfirmed: this.isPullingConfirmed,
|
|
237
|
-
});
|
|
238
|
-
|
|
239
263
|
// If direction not yet confirmed, check if user is pulling down
|
|
240
264
|
if (!this.isPullingConfirmed) {
|
|
241
265
|
// Need some movement to determine direction (at least 5px)
|
|
242
266
|
if (Math.abs(deltaY) > 5) {
|
|
243
267
|
if (deltaY < 0) {
|
|
244
268
|
// Upward pull - cancel the gesture
|
|
245
|
-
console.log('[PTR] Upward pull detected, canceling');
|
|
246
269
|
this.isPulling = false;
|
|
247
270
|
return;
|
|
248
271
|
} else {
|
|
249
272
|
// Downward pull - confirm and proceed
|
|
250
|
-
console.log('[PTR] Downward pull confirmed');
|
|
251
273
|
this.isPullingConfirmed = true;
|
|
252
274
|
|
|
253
275
|
// Prevent text selection during pull if disable-selection is set
|
|
@@ -271,7 +293,9 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
271
293
|
this.currentY = deltaY;
|
|
272
294
|
|
|
273
295
|
if (this.currentY > 0) {
|
|
274
|
-
e.
|
|
296
|
+
if (e.cancelable) {
|
|
297
|
+
e.preventDefault(); // Prevent scroll bounce
|
|
298
|
+
}
|
|
275
299
|
|
|
276
300
|
const indicatorHeight = this.indicatorHeight;
|
|
277
301
|
const translateY = Math.min(
|
|
@@ -279,15 +303,15 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
279
303
|
indicatorHeight,
|
|
280
304
|
);
|
|
281
305
|
|
|
282
|
-
this._indicator
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
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
|
+
}
|
|
290
313
|
}
|
|
314
|
+
this.__updateIndicatorTextForState();
|
|
291
315
|
|
|
292
316
|
this.dispatchEvent(
|
|
293
317
|
new CustomEvent('ptr:pull-move', {
|
|
@@ -300,11 +324,6 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
300
324
|
}
|
|
301
325
|
|
|
302
326
|
handleEnd() {
|
|
303
|
-
console.log('[PTR] handleEnd', {
|
|
304
|
-
isPulling: this.isPulling,
|
|
305
|
-
currentY: this.currentY,
|
|
306
|
-
threshold: this.threshold,
|
|
307
|
-
});
|
|
308
327
|
if (!this.isPulling) return;
|
|
309
328
|
|
|
310
329
|
this.isPulling = false;
|
|
@@ -337,10 +356,14 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
337
356
|
}
|
|
338
357
|
|
|
339
358
|
async triggerRefresh() {
|
|
340
|
-
|
|
359
|
+
if (this.isRefreshing) {
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
341
362
|
this.isRefreshing = true;
|
|
342
|
-
this.
|
|
343
|
-
this._indicator
|
|
363
|
+
this.__updateIndicatorTextForState();
|
|
364
|
+
if (this._indicator) {
|
|
365
|
+
this._indicator.classList.add('active');
|
|
366
|
+
}
|
|
344
367
|
|
|
345
368
|
const refreshEvent = new CustomEvent('ptr:refresh', {
|
|
346
369
|
bubbles: true,
|
|
@@ -353,7 +376,7 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
353
376
|
this.dispatchEvent(refreshEvent);
|
|
354
377
|
|
|
355
378
|
// If no listener calls complete(), auto-complete after a delay
|
|
356
|
-
setTimeout(() => {
|
|
379
|
+
this.__refreshTimeoutId = setTimeout(() => {
|
|
357
380
|
if (this.isRefreshing) {
|
|
358
381
|
this.completeRefresh();
|
|
359
382
|
}
|
|
@@ -363,6 +386,7 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
363
386
|
completeRefresh() {
|
|
364
387
|
this.isRefreshing = false;
|
|
365
388
|
this.resetIndicator();
|
|
389
|
+
this.__clearRefreshTimeout();
|
|
366
390
|
|
|
367
391
|
this.dispatchEvent(
|
|
368
392
|
new CustomEvent('ptr:refresh-complete', {
|
|
@@ -380,27 +404,44 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
380
404
|
|
|
381
405
|
this._indicator.style.transform = `translateY(${-this.indicatorHeight}px)`;
|
|
382
406
|
this._indicator.classList.remove('active');
|
|
383
|
-
this.
|
|
407
|
+
this.__updateIndicatorTextForState();
|
|
384
408
|
|
|
385
409
|
// Re-enable announcements after text is updated
|
|
386
410
|
// Use setTimeout to ensure the text change happens first
|
|
387
|
-
|
|
388
|
-
|
|
411
|
+
this.__clearAriaLiveResetTimeout();
|
|
412
|
+
this.__ariaLiveResetTimeoutId = setTimeout(() => {
|
|
413
|
+
if (this._indicator) {
|
|
414
|
+
this._indicator.setAttribute('aria-live', 'assertive');
|
|
415
|
+
}
|
|
416
|
+
this.__ariaLiveResetTimeoutId = null;
|
|
389
417
|
}, 0);
|
|
390
418
|
}
|
|
391
419
|
|
|
392
|
-
updateIndicatorText() {
|
|
393
|
-
if (
|
|
420
|
+
updateIndicatorText({ force = false } = {}) {
|
|
421
|
+
if (this.isPulling && !force && !this.isRefreshing) {
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
394
424
|
|
|
395
|
-
this.
|
|
425
|
+
this.__updateIndicatorTextForState();
|
|
396
426
|
}
|
|
397
427
|
|
|
398
428
|
get threshold() {
|
|
399
|
-
|
|
429
|
+
const attrValue = this.getAttribute('threshold');
|
|
430
|
+
const parsed = parseInt(attrValue ?? '', 10);
|
|
431
|
+
return Number.isFinite(parsed) ? parsed : 80;
|
|
400
432
|
}
|
|
401
433
|
|
|
402
434
|
set threshold(value) {
|
|
403
|
-
|
|
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)));
|
|
404
445
|
}
|
|
405
446
|
|
|
406
447
|
get indicatorText() {
|
|
@@ -409,6 +450,10 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
409
450
|
}
|
|
410
451
|
|
|
411
452
|
set indicatorText(value) {
|
|
453
|
+
if (value === null || value === undefined) {
|
|
454
|
+
this.removeAttribute('indicator-text');
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
412
457
|
this.setAttribute('indicator-text', value);
|
|
413
458
|
}
|
|
414
459
|
|
|
@@ -418,6 +463,10 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
418
463
|
}
|
|
419
464
|
|
|
420
465
|
set releaseText(value) {
|
|
466
|
+
if (value === null || value === undefined) {
|
|
467
|
+
this.removeAttribute('release-text');
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
421
470
|
this.setAttribute('release-text', value);
|
|
422
471
|
}
|
|
423
472
|
|
|
@@ -427,6 +476,10 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
427
476
|
}
|
|
428
477
|
|
|
429
478
|
set refreshingText(value) {
|
|
479
|
+
if (value === null || value === undefined) {
|
|
480
|
+
this.removeAttribute('refreshing-text');
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
430
483
|
this.setAttribute('refreshing-text', value);
|
|
431
484
|
}
|
|
432
485
|
|
|
@@ -516,7 +569,9 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
516
569
|
<style>${PullToRefreshElement.styles}</style>
|
|
517
570
|
<div class="ptr-container">
|
|
518
571
|
<div class="ptr-indicator" role="status" aria-live="assertive">
|
|
519
|
-
<slot name="indicator"
|
|
572
|
+
<slot name="indicator">
|
|
573
|
+
<span class="ptr-indicator-text">${this.indicatorText}</span>
|
|
574
|
+
</slot>
|
|
520
575
|
</div>
|
|
521
576
|
<div class="ptr-content">
|
|
522
577
|
<slot></slot>
|
|
@@ -527,5 +582,69 @@ export class PullToRefreshElement extends HTMLElement {
|
|
|
527
582
|
// Cache DOM references for efficiency
|
|
528
583
|
this._container = this.shadowRoot.querySelector('.ptr-container');
|
|
529
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
|
+
}
|
|
530
649
|
}
|
|
531
650
|
}
|