@aarongustafson/pull-to-refresh 1.1.3 → 1.2.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 +354 -369
- package/custom-elements.json +139 -12
- package/define.d.ts +2 -0
- package/package.json +8 -3
- package/pull-to-refresh.d.ts +78 -6
- package/pull-to-refresh.js +211 -35
package/README.md
CHANGED
|
@@ -6,46 +6,33 @@
|
|
|
6
6
|
[](https://github.com/aarongustafson/pull-to-refresh/blob/main/LICENSE)
|
|
7
7
|
[](https://github.com/aarongustafson/pull-to-refresh/blob/main/pull-to-refresh.d.ts)
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
`<pull-to-refresh>` adds a downward pull gesture to scrollable content. At the
|
|
10
|
+
top of its scroll container, pulling past the threshold and releasing fires
|
|
11
|
+
`ptr:refresh`. Your application loads data, updates content, and reports the
|
|
12
|
+
result; the component doesn’t fetch data or reload the page.
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
Supply a native Refresh button for keyboard use and a useful no-JavaScript
|
|
15
|
+
alternative. The example below uses a form that reloads the document before
|
|
16
|
+
enhancement or if registration fails. The component doesn’t create a button.
|
|
12
17
|
|
|
13
|
-
|
|
18
|
+
## Demos
|
|
14
19
|
|
|
15
|
-
|
|
20
|
+
- [Live demo](https://aarongustafson.github.io/pull-to-refresh/demo/) ([source](./demo/index.html))
|
|
21
|
+
- [esm.sh: manual registration](https://aarongustafson.github.io/pull-to-refresh/demo/esm.html) ([source](./demo/esm.html))
|
|
22
|
+
- [unpkg: automatic registration](https://aarongustafson.github.io/pull-to-refresh/demo/unpkg.html) ([source](./demo/unpkg.html))
|
|
16
23
|
|
|
17
|
-
|
|
18
|
-
|
|
24
|
+
The live demo covers [a basic handler](https://aarongustafson.github.io/pull-to-refresh/demo/#basic),
|
|
25
|
+
[failure and retry](https://aarongustafson.github.io/pull-to-refresh/demo/#custom-text),
|
|
26
|
+
[selection](https://aarongustafson.github.io/pull-to-refresh/demo/#disable-selection),
|
|
27
|
+
and [translated examples](https://aarongustafson.github.io/pull-to-refresh/demo/#localization).
|
|
19
28
|
|
|
20
|
-
##
|
|
29
|
+
## Installation and registration
|
|
21
30
|
|
|
22
|
-
|
|
23
|
-
- **Customizable**: Adjust threshold, text, and styling via attributes and CSS custom properties
|
|
24
|
-
- **Event-driven**: Rich event API for complete control over the refresh lifecycle
|
|
25
|
-
- **Accessible**: Semantic HTML and ARIA-friendly
|
|
26
|
-
- **Framework-agnostic**: Works with any framework or vanilla JavaScript
|
|
27
|
-
- **Lightweight**: No dependencies, just native Web Components
|
|
28
|
-
- **Shadow DOM**: Encapsulated styles that won't leak
|
|
29
|
-
- **TypeScript-ready**: Bundled definitions for editors and build tooling
|
|
30
|
-
|
|
31
|
-
## TypeScript & Framework Support
|
|
32
|
-
|
|
33
|
-
- Bundled `.d.ts` files describe both `PullToRefreshElement` and `definePullToRefresh`, so modern editors provide autocomplete without extra config.
|
|
34
|
-
- Threshold/text/disabled props now reflect to attributes, keeping declarative templates in sync with imperative code.
|
|
35
|
-
- `_upgradeProperty` preserves pre-upgrade assignments—handy for SSR, hydration, and framework wrappers.
|
|
36
|
-
- Global `HTMLElementTagNameMap` augmentation lets TypeScript understand `document.querySelector('pull-to-refresh')` out of the box.
|
|
37
|
-
|
|
38
|
-
## Quick Start
|
|
39
|
-
|
|
40
|
-
### Installation
|
|
41
|
-
|
|
42
|
-
```bash
|
|
31
|
+
```sh
|
|
43
32
|
npm install @aarongustafson/pull-to-refresh
|
|
44
33
|
```
|
|
45
34
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
**Option 1: Manual registration**
|
|
35
|
+
The main import exports the class without registering an element:
|
|
49
36
|
|
|
50
37
|
```javascript
|
|
51
38
|
import { PullToRefreshElement } from '@aarongustafson/pull-to-refresh';
|
|
@@ -53,14 +40,17 @@ import { PullToRefreshElement } from '@aarongustafson/pull-to-refresh';
|
|
|
53
40
|
customElements.define('pull-to-refresh', PullToRefreshElement);
|
|
54
41
|
```
|
|
55
42
|
|
|
56
|
-
|
|
43
|
+
You can register the class under another tag name; update your HTML to match.
|
|
44
|
+
|
|
45
|
+
For automatic registration in a browser, import the guarded helper instead:
|
|
57
46
|
|
|
58
47
|
```javascript
|
|
59
48
|
import '@aarongustafson/pull-to-refresh/define.js';
|
|
60
|
-
// Registers <pull-to-refresh> when customElements is available
|
|
61
49
|
```
|
|
62
50
|
|
|
63
|
-
|
|
51
|
+
Importing `define.js` also calls `definePullToRefresh()`. The helper skips an
|
|
52
|
+
already registered tag and returns `true` when a registry is available, or
|
|
53
|
+
`false` otherwise. Calling it explicitly doesn’t defer that initial registration:
|
|
64
54
|
|
|
65
55
|
```javascript
|
|
66
56
|
import { definePullToRefresh } from '@aarongustafson/pull-to-refresh/define.js';
|
|
@@ -68,394 +58,389 @@ import { definePullToRefresh } from '@aarongustafson/pull-to-refresh/define.js';
|
|
|
68
58
|
definePullToRefresh();
|
|
69
59
|
```
|
|
70
60
|
|
|
71
|
-
You can also
|
|
61
|
+
You can also load `define.js` with a module script:
|
|
72
62
|
|
|
73
63
|
```html
|
|
74
64
|
<script
|
|
75
|
-
src="./node_modules/@aarongustafson/pull-to-refresh/define.js"
|
|
76
65
|
type="module"
|
|
66
|
+
src="./node_modules/@aarongustafson/pull-to-refresh/define.js"
|
|
77
67
|
></script>
|
|
78
68
|
```
|
|
79
69
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
<pull-to-refresh>
|
|
84
|
-
<div class="content">
|
|
85
|
-
<h1>My Content</h1>
|
|
86
|
-
<p>Pull down from the top to refresh!</p>
|
|
87
|
-
</div>
|
|
88
|
-
</pull-to-refresh>
|
|
70
|
+
The class is also exported from
|
|
71
|
+
`@aarongustafson/pull-to-refresh/pull-to-refresh.js`. Bundled
|
|
72
|
+
[type declarations](./pull-to-refresh.d.ts) describe the public API.
|
|
89
73
|
|
|
90
|
-
|
|
91
|
-
import '@aarongustafson/pull-to-refresh';
|
|
92
|
-
|
|
93
|
-
const ptr = document.querySelector('pull-to-refresh');
|
|
94
|
-
|
|
95
|
-
ptr.addEventListener('ptr:refresh', (e) => {
|
|
96
|
-
// Perform your refresh logic (fetch data, etc.)
|
|
97
|
-
fetch('/api/data')
|
|
98
|
-
.then((response) => response.json())
|
|
99
|
-
.then((data) => {
|
|
100
|
-
// Update your content
|
|
101
|
-
updateContent(data);
|
|
102
|
-
// Signal completion
|
|
103
|
-
e.detail.complete();
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
</script>
|
|
107
|
-
```
|
|
74
|
+
## Basic usage
|
|
108
75
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
| Attribute | Type | Default | Description |
|
|
114
|
-
| ------------------- | ------- | ------------- | ------------------------------------------- |
|
|
115
|
-
| `threshold` | number | `80` | Distance in pixels to trigger refresh |
|
|
116
|
-
| `indicator-text` | string | localized | Text shown when pulling down |
|
|
117
|
-
| `release-text` | string | localized | Text shown when ready to release |
|
|
118
|
-
| `refreshing-text` | string | localized | Text shown while refreshing |
|
|
119
|
-
| `lang` | string | auto-detected | Language code for localization |
|
|
120
|
-
| `disabled` | boolean | `false` | Disables pull-to-refresh functionality |
|
|
121
|
-
| `disable-selection` | boolean | `false` | Prevents text selection during pull gesture |
|
|
122
|
-
|
|
123
|
-
**Note:** The text attributes use automatic localization based on the `lang` attribute or auto-detection. See [Localization](#-localization-i18n) below.
|
|
124
|
-
|
|
125
|
-
### Properties
|
|
126
|
-
|
|
127
|
-
All attributes are also available as properties:
|
|
128
|
-
|
|
129
|
-
```javascript
|
|
130
|
-
const ptr = document.querySelector('pull-to-refresh');
|
|
131
|
-
ptr.threshold = 120;
|
|
132
|
-
ptr.indicatorText = 'Swipe down';
|
|
133
|
-
ptr.disabled = true;
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
### Events
|
|
137
|
-
|
|
138
|
-
| Event | Detail | Description |
|
|
139
|
-
| ---------------------- | ------------------------ | ------------------------------- |
|
|
140
|
-
| `ptr:pull-start` | `{}` | Fired when pull gesture starts |
|
|
141
|
-
| `ptr:pull-move` | `{ distance: number }` | Fired during pull gesture |
|
|
142
|
-
| `ptr:pull-end` | `{}` | Fired when pull gesture ends |
|
|
143
|
-
| `ptr:refresh` | `{ complete: Function }` | Fired when refresh is triggered |
|
|
144
|
-
| `ptr:refresh-complete` | `{}` | Fired when refresh completes |
|
|
145
|
-
|
|
146
|
-
**Important**: Call `event.detail.complete()` in your `ptr:refresh` handler to signal completion:
|
|
147
|
-
|
|
148
|
-
```javascript
|
|
149
|
-
ptr.addEventListener('ptr:refresh', (e) => {
|
|
150
|
-
doAsyncWork().then(() => {
|
|
151
|
-
e.detail.complete(); // Call this when done
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
If you don't call `complete()`, the component will auto-complete after 2 seconds.
|
|
157
|
-
|
|
158
|
-
### Slots
|
|
159
|
-
|
|
160
|
-
| Slot | Description |
|
|
161
|
-
| ----------- | --------------------------------- |
|
|
162
|
-
| (default) | Your scrollable content |
|
|
163
|
-
| `indicator` | Optional custom indicator element |
|
|
164
|
-
|
|
165
|
-
### CSS Custom Properties
|
|
166
|
-
|
|
167
|
-
| Property | Default | Description |
|
|
168
|
-
| --------------------------- | ------------ | --------------------------------- |
|
|
169
|
-
| `--ptr-indicator-height` | `3.125rem` | Height of the indicator area |
|
|
170
|
-
| `--ptr-indicator-bg` | `ButtonFace` | Background color of indicator |
|
|
171
|
-
| `--ptr-indicator-color` | `ButtonText` | Text color of indicator |
|
|
172
|
-
| `--ptr-indicator-font-size` | `0.875rem` | Font size of indicator text |
|
|
173
|
-
| `--ptr-transition-duration` | `0.2s` | Duration of indicator transitions |
|
|
174
|
-
|
|
175
|
-
### Methods
|
|
176
|
-
|
|
177
|
-
| Method | Description |
|
|
178
|
-
| ------------------- | -------------------------------------------------------------------------------- |
|
|
179
|
-
| `completeRefresh()` | Manually complete the refresh (alternative to calling `event.detail.complete()`) |
|
|
180
|
-
|
|
181
|
-
## Examples
|
|
182
|
-
|
|
183
|
-
### Custom Threshold
|
|
184
|
-
|
|
185
|
-
```html
|
|
186
|
-
<pull-to-refresh threshold="120">
|
|
187
|
-
<div>Content here</div>
|
|
188
|
-
</pull-to-refresh>
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
### Localization
|
|
192
|
-
|
|
193
|
-
The component automatically detects the language and displays localized text:
|
|
76
|
+
This complete example generates a number locally, so it doesn’t need an API.
|
|
77
|
+
Serve it over HTTP with the installed package at the path shown. Replace the
|
|
78
|
+
delayed update with your application’s work.
|
|
194
79
|
|
|
195
80
|
```html
|
|
196
|
-
|
|
197
|
-
<
|
|
198
|
-
<
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
<div>Contenu ici</div>
|
|
204
|
-
</pull-to-refresh>
|
|
205
|
-
|
|
206
|
-
<!-- Japanese -->
|
|
207
|
-
<pull-to-refresh lang="ja">
|
|
208
|
-
<div>ここにコンテンツ</div>
|
|
81
|
+
<pull-to-refresh id="refresh-content">
|
|
82
|
+
<h2>Latest number</h2>
|
|
83
|
+
<p>Number: <span id="number">0</span></p>
|
|
84
|
+
<p id="refresh-status" role="status">Ready</p>
|
|
85
|
+
<form id="refresh-form">
|
|
86
|
+
<button type="submit">Refresh</button>
|
|
87
|
+
</form>
|
|
209
88
|
</pull-to-refresh>
|
|
210
|
-
```
|
|
211
89
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
1. The element's `lang` attribute
|
|
215
|
-
2. The nearest ancestor's `lang` attribute
|
|
216
|
-
3. The document's `lang` attribute
|
|
217
|
-
4. Default: English
|
|
90
|
+
<script type="module">
|
|
91
|
+
import './node_modules/@aarongustafson/pull-to-refresh/define.js';
|
|
218
92
|
|
|
219
|
-
|
|
93
|
+
const component = document.getElementById('refresh-content');
|
|
94
|
+
const number = document.getElementById('number');
|
|
95
|
+
const status = document.getElementById('refresh-status');
|
|
96
|
+
const form = document.getElementById('refresh-form');
|
|
97
|
+
let pending = false;
|
|
220
98
|
|
|
221
|
-
|
|
222
|
-
|
|
99
|
+
form.addEventListener('submit', (event) => {
|
|
100
|
+
event.preventDefault();
|
|
101
|
+
if (!pending) component.triggerRefresh();
|
|
102
|
+
});
|
|
223
103
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
104
|
+
component.addEventListener('ptr:refresh', async (event) => {
|
|
105
|
+
if (pending) {
|
|
106
|
+
event.detail.complete();
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
pending = true;
|
|
110
|
+
status.textContent = 'Refreshing...';
|
|
111
|
+
try {
|
|
112
|
+
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
113
|
+
const nextNumber = Math.floor(Math.random() * 1000);
|
|
114
|
+
number.textContent = nextNumber;
|
|
115
|
+
status.textContent = 'Complete! Use Refresh or pull again.';
|
|
116
|
+
} catch {
|
|
117
|
+
status.textContent =
|
|
118
|
+
'Refresh failed. Your previous number is unchanged. Use Refresh to try again.';
|
|
119
|
+
} finally {
|
|
120
|
+
pending = false;
|
|
121
|
+
event.detail.complete();
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
</script>
|
|
231
125
|
```
|
|
232
126
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
127
|
+
The handler leaves the previous number visible while loading and on failure.
|
|
128
|
+
The button stays available and focus stays where the user left it. Translate
|
|
129
|
+
the button and application status messages alongside your content.
|
|
130
|
+
|
|
131
|
+
For a network update, check `response.ok`, validate the response, and prepare
|
|
132
|
+
the new content before replacing the existing data. Use `textContent` for
|
|
133
|
+
untrusted text. Report failures visibly and provide a retry action.
|
|
134
|
+
|
|
135
|
+
## Behavior and completion
|
|
136
|
+
|
|
137
|
+
- The host defaults to `height: 100vh`, with an internal vertical scroll
|
|
138
|
+
container. Set the host’s height to fit your layout.
|
|
139
|
+
- A downward movement greater than 5 pixels confirms a pull. Release must
|
|
140
|
+
exceed `threshold` to trigger refresh; matching it exactly isn’t enough.
|
|
141
|
+
- A browser `pointercancel` abandons the pull, restores selection and the
|
|
142
|
+
resting indicator, and emits `ptr:pull-end`. It never starts a refresh or
|
|
143
|
+
emits `ptr:refresh-complete` for the canceled gesture, even beyond threshold.
|
|
144
|
+
- `disabled` removes gesture listeners and resets the indicator. It doesn’t
|
|
145
|
+
disable your button or prevent a direct `triggerRefresh()` call.
|
|
146
|
+
- `disable-selection` suppresses selection on slotted content during a
|
|
147
|
+
confirmed pull. Release removes the `pulling` attribute and restores
|
|
148
|
+
selection styling. Removing `disable-selection` also removes `pulling`.
|
|
149
|
+
- Without upgrade, authored content and forms remain ordinary HTML.
|
|
150
|
+
- On disconnect, the component removes its gesture/scroll listeners and
|
|
151
|
+
clears its timers and interaction state. Pending completion callbacks become
|
|
152
|
+
inactive, without a completion event. It doesn’t cancel your application’s
|
|
153
|
+
work; cancel or guard your own pending updates when removing the component.
|
|
154
|
+
|
|
155
|
+
Call `event.detail.complete()` in `finally` for both success and failure.
|
|
156
|
+
Each callback belongs to the action that supplied it. Repeated calls and
|
|
157
|
+
callbacks from older actions do nothing. `completeRefresh()` instead completes
|
|
158
|
+
whichever action is current. Either path ends that action and fires
|
|
159
|
+
`ptr:refresh-complete` once; neither means the data update succeeded.
|
|
160
|
+
|
|
161
|
+
By default, the component schedules automatic completion **two seconds after
|
|
162
|
+
dispatching `ptr:refresh`**, even when a listener is still working. That timer does not wait
|
|
163
|
+
for a promise returned by your handler. `triggerRefresh()`’s returned promise
|
|
164
|
+
also doesn’t await your work. Keep application loading and outcome messages
|
|
165
|
+
separate from `ptr:refresh-complete`, and guard overlapping work.
|
|
166
|
+
|
|
167
|
+
Set `refresh-timeout="0"` (or `component.refreshTimeout = 0`) to keep the
|
|
168
|
+
interaction refreshing until explicit completion. Always call the event’s
|
|
169
|
+
`complete()` callback in `finally`, and provide application-owned error and
|
|
170
|
+
retry feedback. Removing the component also cancels its interaction, not the
|
|
171
|
+
underlying request. A positive `refresh-timeout` sets a different delay in
|
|
172
|
+
milliseconds. The component captures the delay before dispatching each refresh
|
|
173
|
+
event; changes apply to the next action, not an already running one.
|
|
174
|
+
|
|
175
|
+
## Attributes and properties
|
|
176
|
+
|
|
177
|
+
Property setters reflect their corresponding attributes. Pre-upgrade property
|
|
178
|
+
assignments are preserved.
|
|
179
|
+
|
|
180
|
+
| Attribute | Property | Default | Behavior |
|
|
181
|
+
| -------------------- | ------------------- | ----------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
182
|
+
| `threshold` | `threshold` | `80` | Pull distance in pixels. Invalid or negative values remove the attribute and restore the default. Attribute reads use integer parsing; the setter rounds valid numbers. |
|
|
183
|
+
| `refresh-timeout` | `refreshTimeout` | `2000` | Automatic completion delay in integer milliseconds, from 0 to 2147483647. Zero requires explicit completion. Changes affect the next action. |
|
|
184
|
+
| `indicator-message` | `indicatorMessage` | `null` | Authored message while pulling; a nonempty value overrides other sources. |
|
|
185
|
+
| `release-message` | `releaseMessage` | `null` | Authored message beyond the threshold. |
|
|
186
|
+
| `refreshing-message` | `refreshingMessage` | `null` | Authored message while the interaction is refreshing. |
|
|
187
|
+
| `indicator-text` | `indicatorText` | Localized | Deprecated override while pulling; retained for migration. |
|
|
188
|
+
| `release-text` | `releaseText` | Localized | Deprecated override beyond the threshold; retained for migration. |
|
|
189
|
+
| `refreshing-text` | `refreshingText` | Localized | Deprecated override while refreshing; retained for migration. |
|
|
190
|
+
| `lang` | `lang` | Inherited or `en` | Language used to select indicator messages. |
|
|
191
|
+
| `disabled` | `disabled` | `false` | Boolean attribute; presence disables gesture listeners. |
|
|
192
|
+
| `disable-selection` | `disableSelection` | `false` | Boolean attribute; presence suppresses selection during a confirmed pull. |
|
|
193
|
+
|
|
194
|
+
The new message properties return the authored attribute value (`string | null`),
|
|
195
|
+
not the resolved message. Assigning a string reflects it to the attribute;
|
|
196
|
+
assigning `null` or `undefined` removes it. Supply nonempty, complete literal
|
|
197
|
+
strings. The component doesn’t interpolate tokens or insert data into messages;
|
|
198
|
+
any braces remain literal text.
|
|
199
|
+
Empty or whitespace-only message values are invalid; remove the attribute to
|
|
200
|
+
use the next source.
|
|
201
|
+
|
|
202
|
+
The legacy `indicatorText`, `releaseText`, and `refreshingText` getters return
|
|
203
|
+
the resolved, last-working effective messages, not their authored attributes.
|
|
204
|
+
Invalid configuration leaves those returned messages unchanged.
|
|
205
|
+
Set legacy text properties to `null` or `undefined` to remove an override.
|
|
206
|
+
Empty legacy text attributes also fall through. Set `threshold` to `null`,
|
|
207
|
+
`undefined`, or `''` to restore its default.
|
|
208
|
+
See [Localization](#localization-i18n) for message precedence and language selection.
|
|
209
|
+
|
|
210
|
+
`refreshTimeout` returns the effective numeric delay. Set it to `null` or
|
|
211
|
+
`undefined` to restore the default. Invalid property assignments throw
|
|
212
|
+
`RangeError` without changing the existing value. Invalid attributes, including
|
|
213
|
+
empty values, log an error and are removed, restoring the default.
|
|
214
|
+
|
|
215
|
+
## Interaction state
|
|
216
|
+
|
|
217
|
+
`isRefreshing` is an observation-only boolean. It starts as `false`, including
|
|
218
|
+
before connection, and stays false while pulling. An accepted refresh sets it
|
|
219
|
+
to `true` before dispatching `ptr:refresh`. Completion sets it to `false` before
|
|
220
|
+
`ptr:refresh-complete`; disconnect cancels the interaction and sets it to false
|
|
221
|
+
without a completion event. Reconnecting starts idle.
|
|
222
|
+
|
|
223
|
+
This state describes the component’s interaction, not your application’s
|
|
224
|
+
loading, progress, failure, or success. With automatic completion enabled, it
|
|
225
|
+
can become false while your request is still pending. Keep application state
|
|
226
|
+
and error/retry feedback separate.
|
|
227
|
+
|
|
228
|
+
TypeScript declares `isRefreshing` as `readonly`. The existing runtime field
|
|
229
|
+
remains writable for compatibility, but assigning it bypasses timers, indicator
|
|
230
|
+
updates, and events. Use `triggerRefresh()`, `completeRefresh()`, or the event’s
|
|
231
|
+
completion callback instead. The existing `isPulling`, `isPullingConfirmed`,
|
|
232
|
+
`currentY`, and `startY` fields remain untouched implementation details, not
|
|
233
|
+
additional supported state APIs.
|
|
234
|
+
|
|
235
|
+
## Methods and events
|
|
236
|
+
|
|
237
|
+
| Method | Behavior |
|
|
238
|
+
| --------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
239
|
+
| `triggerRefresh()` | Starts a connected, idle interaction and dispatches `ptr:refresh`. Does nothing while disconnected or already refreshing. Works independently of `disabled`; doesn’t wait for application work. |
|
|
240
|
+
| `completeRefresh()` | Ends the current interaction once, resets the indicator, clears the timer, and dispatches `ptr:refresh-complete`. Does nothing when idle. |
|
|
241
|
+
| `refreshMessages(): boolean` | Rereads shared configuration and resolves messages for this connected instance. Returns `false` when disconnected or when configuration is invalid. See [Localization](#localization-i18n). |
|
|
242
|
+
| `PullToRefreshElement.registerTranslations(translations)` | Deprecated. Adds or replaces whole base-language entries for indicator messages. |
|
|
243
|
+
|
|
244
|
+
The established `ptr:*` namespace is retained for compatibility. There are no
|
|
245
|
+
`pull-to-refresh:*` aliases, so one action doesn’t notify two sets of listeners.
|
|
246
|
+
Alternate registration tags use the same event names.
|
|
247
|
+
|
|
248
|
+
All five events are dispatched synchronously on the host with `bubbles: true`,
|
|
249
|
+
`composed: true`, and `cancelable: false`. They support delegation across an
|
|
250
|
+
enclosing shadow root. `preventDefault()` does not stop a pull or refresh.
|
|
251
|
+
Initialization and message/configuration changes do not emit these events.
|
|
252
|
+
|
|
253
|
+
| Event | Detail | Timing and frequency |
|
|
254
|
+
| ---------------------- | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
255
|
+
| `ptr:pull-start` | `null` | Once when downward movement greater than 5 pixels confirms a pull, before that move updates the indicator. |
|
|
256
|
+
| `ptr:pull-move` | `{ distance: number }` | On each confirmed positive-distance move, after indicator position and text update. Distance is a snapshot in CSS pixels, not a live state object. |
|
|
257
|
+
| `ptr:pull-end` | `null` | When an active pointer ends or is canceled, even if the pull was unconfirmed or below threshold. Pulling is already false; refresh/reset and distance cleanup follow dispatch. Cancellation only resets; it never triggers refresh. |
|
|
258
|
+
| `ptr:refresh` | `{ complete: () => void }` | A gesture or `triggerRefresh()` starts an interaction, after refreshing state and indicator text update. See [completion behavior](#behavior-and-completion). |
|
|
259
|
+
| `ptr:refresh-complete` | `null` | Explicit or automatic completion, after refreshing state is false and the indicator has reset. This is not a data-success notification. |
|
|
260
|
+
|
|
261
|
+
TypeScript infers event details for `addEventListener()` and
|
|
262
|
+
`removeEventListener()`, including when importing only `define.js`. The exported
|
|
263
|
+
`PullToRefreshEventMap`, `PullToRefreshPullMoveDetail`, and
|
|
264
|
+
`PullToRefreshRefreshDetail` types describe the same contract. Native events
|
|
265
|
+
such as `keydown` retain their usual types; application-defined event names
|
|
266
|
+
remain supported.
|
|
267
|
+
|
|
268
|
+
`indicatorHeight` returns the indicator’s current rendered height in CSS pixels
|
|
269
|
+
(zero before rendering or when it has no layout). Gesture positioning measures
|
|
270
|
+
its untransformed layout height once at the start of each pull, not on every
|
|
271
|
+
move, so scaled ancestors don’t distort local CSS offsets. Height changes during a
|
|
272
|
+
pull apply to the next gesture; the resting position follows its CSS height
|
|
273
|
+
automatically, including font-size changes.
|
|
244
274
|
|
|
245
|
-
|
|
246
|
-
<div>Content here</div>
|
|
247
|
-
</pull-to-refresh>
|
|
248
|
-
```
|
|
275
|
+
## Localization (i18n)
|
|
249
276
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
make the other unreadable. Custom properties override these defaults and
|
|
253
|
-
inherit from the host. A custom slotted indicator supplies its own markup
|
|
254
|
-
and styles. Test your complete pair, including nested text, in each supported
|
|
255
|
-
scheme and user palette. Automatic forced-color adjustment remains enabled.
|
|
256
|
-
The indicator uses different authored or localized messages for pulling,
|
|
257
|
-
release, and loading, rather than color alone.
|
|
258
|
-
|
|
259
|
-
The demos use `CanvasText` on `Canvas` for reading surfaces and status
|
|
260
|
-
messages, `ButtonText` on `ButtonFace` for code and buttons, and
|
|
261
|
-
`MarkText` on `Mark` for marked examples. They preserve native keyboard focus.
|
|
262
|
-
Each standalone demo includes a native Refresh form: enhancement runs the
|
|
263
|
-
existing refresh handler; without JavaScript or a successful module load,
|
|
264
|
-
the form reloads the document. The component does not generate a keyboard
|
|
265
|
-
button. Supply one for tasks that must not depend on a pull gesture.
|
|
266
|
-
Keep existing data visible during loading or failure, provide a readable
|
|
267
|
-
error and retry action, and call `event.detail.complete()` when your handler
|
|
268
|
-
finishes. Completion reports the end of the interaction, not success.
|
|
269
|
-
|
|
270
|
-
Recorded-palette substitution and browser emulation are regression checks,
|
|
271
|
-
not proof of native high-contrast repainting or assistive-technology support.
|
|
272
|
-
The recorded study’s 19 distinct palettes pass the tested demo pairs; bundled
|
|
273
|
-
Ubuntu WebKit has a known dark `ButtonText`/`ButtonFace` contrast limitation
|
|
274
|
-
outside that dataset. The browser suite tracks it as a narrowly scoped expected
|
|
275
|
-
failure instead of replacing every platform’s native pair.
|
|
276
|
-
|
|
277
|
-
### Custom Messages
|
|
277
|
+
Use message attributes to match your application’s vocabulary before adding
|
|
278
|
+
translations:
|
|
278
279
|
|
|
279
280
|
```html
|
|
280
281
|
<pull-to-refresh
|
|
281
|
-
indicator-
|
|
282
|
-
release-
|
|
283
|
-
refreshing-
|
|
282
|
+
indicator-message="↓ Pull to check for updates"
|
|
283
|
+
release-message="↻ Release to check"
|
|
284
|
+
refreshing-message="⏳ Checking for updates..."
|
|
284
285
|
>
|
|
285
|
-
|
|
286
|
+
<!-- Your content and native Refresh form -->
|
|
286
287
|
</pull-to-refresh>
|
|
287
288
|
```
|
|
288
289
|
|
|
289
|
-
###
|
|
290
|
+
### Shared document defaults
|
|
290
291
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
try {
|
|
294
|
-
const response = await fetch('/api/latest');
|
|
295
|
-
const data = await response.json();
|
|
296
|
-
renderData(data);
|
|
297
|
-
} catch (error) {
|
|
298
|
-
console.error('Refresh failed:', error);
|
|
299
|
-
} finally {
|
|
300
|
-
e.detail.complete();
|
|
301
|
-
}
|
|
302
|
-
});
|
|
303
|
-
```
|
|
304
|
-
|
|
305
|
-
### Disabled State
|
|
292
|
+
Add one inline JSON script before the components connect to share messages
|
|
293
|
+
throughout that document:
|
|
306
294
|
|
|
307
295
|
```html
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
// Or toggle programmatically
|
|
315
|
-
ptr.disabled = true;
|
|
296
|
+
<script type="application/json" data-pull-to-refresh-config>
|
|
297
|
+
{
|
|
298
|
+
"indicator-message": "↓ Pull to check for updates",
|
|
299
|
+
"release-message": "↻ Release to check",
|
|
300
|
+
"refreshing-message": "⏳ Checking for updates..."
|
|
301
|
+
}
|
|
316
302
|
</script>
|
|
317
303
|
```
|
|
318
304
|
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
The component
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
305
|
+
Keep the `data-pull-to-refresh-config` marker empty. The JSON must be an object
|
|
306
|
+
containing only `indicator-message`, `release-message`, and
|
|
307
|
+
`refreshing-message`; each supplied value
|
|
308
|
+
must be a nonempty string. You can omit keys to use the fallback for those
|
|
309
|
+
messages. Don’t add language dictionaries, scopes, a `src`, or multiple
|
|
310
|
+
configuration scripts. Shared defaults apply regardless of an instance’s
|
|
311
|
+
language; each embedded document needs its own configuration.
|
|
312
|
+
|
|
313
|
+
The component resolves each message separately, in this order:
|
|
314
|
+
|
|
315
|
+
1. An instance `*-message` attribute containing a valid message
|
|
316
|
+
2. A nonempty legacy instance `*-text` attribute
|
|
317
|
+
3. A shared document JSON string
|
|
318
|
+
4. A registered base-language key
|
|
319
|
+
5. A bundled base-language key
|
|
320
|
+
6. The English message
|
|
321
|
+
|
|
322
|
+
After changing the JSON, call `component.refreshMessages()` on each connected
|
|
323
|
+
instance that needs the new messages. The method returns `true` on success.
|
|
324
|
+
It returns `false` when the instance is disconnected. Invalid configuration
|
|
325
|
+
also returns `false`, reports the failure with `console.error`, and retains the
|
|
326
|
+
entire last valid message set without partial updates. If shared or instance
|
|
327
|
+
configuration is invalid on initial connection, the component logs the failure,
|
|
328
|
+
keeps its initial English messages, and still enhances the native content.
|
|
329
|
+
Correct the configuration and call `refreshMessages()` to recover.
|
|
330
|
+
It doesn’t start or complete a refresh, dispatch events, move focus, or change
|
|
331
|
+
interaction state.
|
|
332
|
+
|
|
333
|
+
### Language fallback and legacy migration
|
|
334
|
+
|
|
335
|
+
Language selection uses the first nonempty value from the element’s `lang`,
|
|
336
|
+
`closest('[lang]')`, the document language, and then `en`. The closest match
|
|
337
|
+
includes the element itself: an empty `lang` on the element or its nearest
|
|
338
|
+
matching ancestor falls through to the document language, not a more distant
|
|
339
|
+
ancestor. The component resolves inherited
|
|
340
|
+
language when it connects; changing an ancestor’s language alone doesn’t update
|
|
341
|
+
an already connected indicator. Changing the element’s `lang` or calling
|
|
342
|
+
`refreshMessages()` resolves the language again.
|
|
343
|
+
Regional codes such as `es-MX` use their base language. Unknown codes fall back
|
|
344
|
+
to English. See [translations.json](./translations.json) for the built-in
|
|
345
|
+
messages and supported codes.
|
|
346
|
+
|
|
347
|
+
The built-in catalogs, `registerTranslations()`, and the existing `*-text`
|
|
348
|
+
attributes and properties are deprecated but retained. They emit no deprecation
|
|
349
|
+
warnings; removal is reserved for a future major version. For new code, use
|
|
350
|
+
instance messages and shared document defaults. Rename `indicator-text`,
|
|
351
|
+
`release-text`, and `refreshing-text` to their `*-message` equivalents, keeping
|
|
352
|
+
the complete strings.
|
|
353
|
+
|
|
354
|
+
If you still use registered catalogs, register them before the elements connect:
|
|
365
355
|
|
|
366
356
|
```javascript
|
|
367
357
|
import { PullToRefreshElement } from '@aarongustafson/pull-to-refresh';
|
|
368
358
|
|
|
369
|
-
// Add a new language
|
|
370
|
-
PullToRefreshElement.registerTranslations({
|
|
371
|
-
nl: {
|
|
372
|
-
indicator: '↓ Trek om te vernieuwen',
|
|
373
|
-
release: '↻ Loslaten om te vernieuwen',
|
|
374
|
-
refreshing: '⏳ Vernieuwen...',
|
|
375
|
-
},
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
// Override existing translations
|
|
379
359
|
PullToRefreshElement.registerTranslations({
|
|
380
360
|
en: {
|
|
381
|
-
indicator: '
|
|
382
|
-
release: '
|
|
383
|
-
refreshing: '⏳
|
|
361
|
+
indicator: '↓ Pull to check for updates',
|
|
362
|
+
release: '↻ Release to check',
|
|
363
|
+
refreshing: '⏳ Checking for updates...',
|
|
384
364
|
},
|
|
385
365
|
});
|
|
386
|
-
```
|
|
387
|
-
|
|
388
|
-
### Per-Instance Overrides
|
|
389
|
-
|
|
390
|
-
You can always override translations for individual instances:
|
|
391
|
-
|
|
392
|
-
```html
|
|
393
|
-
<pull-to-refresh
|
|
394
|
-
lang="es"
|
|
395
|
-
indicator-text="Custom Spanish text"
|
|
396
|
-
release-text="Custom release text"
|
|
397
|
-
>
|
|
398
|
-
<!-- Content -->
|
|
399
|
-
</pull-to-refresh>
|
|
400
|
-
```
|
|
401
|
-
|
|
402
|
-
## Testing
|
|
403
|
-
|
|
404
|
-
```bash
|
|
405
|
-
npm test # Run tests in watch mode
|
|
406
|
-
npm run test:run # Run tests once
|
|
407
|
-
npm run test:ui # Open Vitest UI
|
|
408
|
-
npm run test:coverage # Generate coverage report
|
|
409
|
-
```
|
|
410
|
-
|
|
411
|
-
## Development
|
|
412
366
|
|
|
413
|
-
|
|
414
|
-
npm install # Install dependencies
|
|
415
|
-
npm run setup # Run setup wizard (first time)
|
|
416
|
-
npm run lint # Lint code
|
|
417
|
-
npm run format # Format code
|
|
367
|
+
customElements.define('pull-to-refresh', PullToRefreshElement);
|
|
418
368
|
```
|
|
419
369
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
370
|
+
Registration replaces whole base-language entries; it doesn’t merge a new
|
|
371
|
+
entry with the previously registered one. A missing key falls back to the
|
|
372
|
+
bundled base-language key, then English. Register base codes such as `pt`, not
|
|
373
|
+
`pt-BR`, because lookup uses the base code. Registration alone doesn’t redraw
|
|
374
|
+
connected indicators; call `refreshMessages()` after registering to update them.
|
|
423
375
|
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
376
|
+
Only the indicator messages are translated by the component. Authors translate
|
|
377
|
+
content, buttons, loading messages, errors, and retry instructions. The
|
|
378
|
+
[Spanish, French, and Japanese demos](./demo/index.html#localization) show
|
|
379
|
+
authored instance messages and shared defaults in embedded documents.
|
|
428
380
|
|
|
429
|
-
|
|
381
|
+
## Styling
|
|
430
382
|
|
|
431
|
-
|
|
383
|
+
| Custom property | Default | Purpose |
|
|
384
|
+
| --------------------------- | ------------ | ------------------------------------------------------------- |
|
|
385
|
+
| `--ptr-indicator-height` | `3.125rem` | Indicator area height, measured at the start of each gesture. |
|
|
386
|
+
| `--ptr-indicator-bg` | `ButtonFace` | Indicator background. |
|
|
387
|
+
| `--ptr-indicator-color` | `ButtonText` | Indicator foreground. |
|
|
388
|
+
| `--ptr-indicator-font-size` | `0.875rem` | Indicator text size. |
|
|
389
|
+
| `--ptr-transition-duration` | `0.2s` | Indicator transition duration. |
|
|
432
390
|
|
|
433
|
-
|
|
434
|
-
// Auto-define
|
|
435
|
-
import '@aarongustafson/pull-to-refresh';
|
|
436
|
-
|
|
437
|
-
// Class only
|
|
438
|
-
import { PullToRefreshElement } from '@aarongustafson/pull-to-refresh/pull-to-refresh.js';
|
|
391
|
+
Set foreground and background together:
|
|
439
392
|
|
|
440
|
-
|
|
441
|
-
|
|
393
|
+
```css
|
|
394
|
+
pull-to-refresh {
|
|
395
|
+
--ptr-indicator-bg: #102030;
|
|
396
|
+
--ptr-indicator-color: #ffffff;
|
|
397
|
+
}
|
|
442
398
|
|
|
443
|
-
|
|
444
|
-
|
|
399
|
+
@media (prefers-reduced-motion: reduce) {
|
|
400
|
+
pull-to-refresh {
|
|
401
|
+
--ptr-transition-duration: 0s;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
445
404
|
```
|
|
446
405
|
|
|
447
|
-
|
|
406
|
+
Test the pair in light, dark, and user-selected palettes. Automatic forced-color
|
|
407
|
+
adjustment remains enabled. Messages distinguish pulling, release, and loading
|
|
408
|
+
without relying on color alone.
|
|
409
|
+
|
|
410
|
+
The default slot holds your content. The optional `indicator` slot replaces the
|
|
411
|
+
default message with authored markup and styles; your application must update
|
|
412
|
+
that custom indicator’s state text. There are no exposed CSS parts.
|
|
413
|
+
|
|
414
|
+
## Accessibility and compatibility
|
|
415
|
+
|
|
416
|
+
The indicator uses `role="status"` with assertive live announcements; the
|
|
417
|
+
component temporarily turns announcements off while resetting it. Supply a
|
|
418
|
+
separate visible application status, preserve existing data on failure, and
|
|
419
|
+
keep a native Refresh action. The gesture alone isn’t a keyboard alternative.
|
|
420
|
+
Test focus, announcements, touch scrolling, and browser pull-to-refresh behavior
|
|
421
|
+
on the devices and assistive technologies your application supports.
|
|
422
|
+
|
|
423
|
+
The runtime uses Custom Elements, Shadow DOM, modules with JSON import
|
|
424
|
+
attributes, and Pointer Events. If registration cannot load, the authored
|
|
425
|
+
content and native form remain available. Browser emulation and the recorded
|
|
426
|
+
palette checks aren’t proof of native high-contrast repainting or screen-reader
|
|
427
|
+
behavior. The browser suite tracks a known Ubuntu WebKit dark
|
|
428
|
+
`ButtonText`/`ButtonFace` contrast limitation as an expected failure.
|
|
429
|
+
|
|
430
|
+
## Development and testing
|
|
431
|
+
|
|
432
|
+
```sh
|
|
433
|
+
npm install
|
|
434
|
+
npm run lint
|
|
435
|
+
npm run format
|
|
436
|
+
npm run test:run
|
|
437
|
+
npm run test:browser
|
|
438
|
+
```
|
|
448
439
|
|
|
449
|
-
|
|
440
|
+
`npm test` runs watch mode; `npm run test:ui` opens the Vitest UI, and
|
|
441
|
+
`npm run test:coverage` collects coverage. See [CONTRIBUTING.md](./CONTRIBUTING.md)
|
|
442
|
+
for contributor guidance and [the demo guide](./demo/README.md) for ownership.
|
|
450
443
|
|
|
451
444
|
## License
|
|
452
445
|
|
|
453
|
-
MIT
|
|
454
|
-
|
|
455
|
-
## Credits
|
|
456
|
-
|
|
457
|
-
Created by [Aaron Gustafson](https://github.com/aarongustafson)
|
|
458
|
-
|
|
459
|
-
---
|
|
460
|
-
|
|
461
|
-
**Try it out!** Check out the [live demo](https://aarongustafson.github.io/pull-to-refresh/demo/index.html) 🚀
|
|
446
|
+
MIT — see [LICENSE](./LICENSE).
|