@tempots/client 0.0.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/LICENSE.txt +202 -0
- package/README.md +204 -0
- package/index.cjs +1 -0
- package/index.d.ts +308 -0
- package/index.js +327 -0
- package/package.json +40 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# @tempots/client
|
|
2
|
+
|
|
3
|
+
Client-side hydration utilities for Tempo applications with SSR support.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @tempots/client
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @tempots/client
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- **Hydration**: Attach client-side interactivity to server-rendered HTML
|
|
16
|
+
- **Islands Architecture**: Partial hydration with lazy loading strategies
|
|
17
|
+
- **Multiple Hydration Strategies**: immediate, idle, visible, and media query-based
|
|
18
|
+
- **Full Tempo Compatibility**: Works seamlessly with Tempo signals and components
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Quick Start with `startClient()`
|
|
23
|
+
|
|
24
|
+
The simplest way to initialize your client-side application:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
// entry-client.ts
|
|
28
|
+
import { startClient } from '@tempots/client'
|
|
29
|
+
import { App, Counter, TodoList } from './app'
|
|
30
|
+
|
|
31
|
+
startClient({
|
|
32
|
+
app: () => App(), // App to render in client-only mode
|
|
33
|
+
islands: { Counter, TodoList }, // Islands to hydrate
|
|
34
|
+
debug: true, // Optional: enable debug logging
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`startClient()` automatically:
|
|
39
|
+
- Detects SSR vs client-only mode
|
|
40
|
+
- Initializes islands with their hydration strategies
|
|
41
|
+
- Sets up HMR cleanup (when available)
|
|
42
|
+
|
|
43
|
+
### Basic Hydration (Low-Level)
|
|
44
|
+
|
|
45
|
+
For more control, use the low-level `hydrate()` function:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { hydrate } from '@tempots/client'
|
|
49
|
+
import { App } from './App'
|
|
50
|
+
|
|
51
|
+
// Server-rendered HTML is already in the DOM
|
|
52
|
+
const container = document.getElementById('app')!
|
|
53
|
+
|
|
54
|
+
// Hydrate with client-side interactivity
|
|
55
|
+
const cleanup = hydrate(App(), container)
|
|
56
|
+
|
|
57
|
+
// Later, to cleanup all event listeners:
|
|
58
|
+
cleanup()
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Islands Architecture
|
|
62
|
+
|
|
63
|
+
Islands allow you to hydrate only specific interactive components while keeping the rest as static HTML.
|
|
64
|
+
|
|
65
|
+
#### Server-side: Mark islands
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { html } from '@tempots/dom'
|
|
69
|
+
import {
|
|
70
|
+
ISLAND_ATTR,
|
|
71
|
+
ISLAND_OPTIONS_ATTR,
|
|
72
|
+
ISLAND_HYDRATE_ATTR,
|
|
73
|
+
} from '@tempots/client'
|
|
74
|
+
|
|
75
|
+
// Or use the islandMarker helper
|
|
76
|
+
import { islandMarker } from '@tempots/client'
|
|
77
|
+
|
|
78
|
+
const Page = () => html.div(
|
|
79
|
+
html.h1('Static content - no JS shipped'),
|
|
80
|
+
|
|
81
|
+
// This counter will be hydrated as an island
|
|
82
|
+
html.div(
|
|
83
|
+
...islandMarker('Counter', { initial: 10 }, 'visible'),
|
|
84
|
+
// Server-rendered content
|
|
85
|
+
html.button('-'),
|
|
86
|
+
html.span('10'),
|
|
87
|
+
html.button('+'),
|
|
88
|
+
),
|
|
89
|
+
)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### Client-side: Initialize islands
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import { initIslands } from '@tempots/client'
|
|
96
|
+
import { Counter } from './islands/Counter'
|
|
97
|
+
|
|
98
|
+
// Initialize all islands found in the document
|
|
99
|
+
const cleanup = initIslands({
|
|
100
|
+
Counter,
|
|
101
|
+
})
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Hydration Strategies
|
|
105
|
+
|
|
106
|
+
Islands support multiple hydration strategies to optimize loading performance:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
// Hydrate immediately when JS loads
|
|
110
|
+
islandMarker('Counter', options, 'immediate')
|
|
111
|
+
|
|
112
|
+
// Hydrate when browser is idle (requestIdleCallback)
|
|
113
|
+
islandMarker('Counter', options, 'idle')
|
|
114
|
+
|
|
115
|
+
// Hydrate when scrolled into view (IntersectionObserver)
|
|
116
|
+
islandMarker('Counter', options, 'visible')
|
|
117
|
+
|
|
118
|
+
// Hydrate when media query matches
|
|
119
|
+
islandMarker('Counter', options, { media: '(min-width: 768px)' })
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## API
|
|
123
|
+
|
|
124
|
+
### `startClient(options)`
|
|
125
|
+
|
|
126
|
+
High-level function to initialize the Tempo client with automatic SSR detection.
|
|
127
|
+
|
|
128
|
+
**Options:**
|
|
129
|
+
- `app?: () => Renderable` - App component for client-only rendering
|
|
130
|
+
- `islands: IslandRegistry` - Map of island names to component factories
|
|
131
|
+
- `container?: string | HTMLElement` - Container selector or element (default: `"#app"`)
|
|
132
|
+
- `providers?: Providers` - Providers to inject during hydration
|
|
133
|
+
- `debug?: boolean` - Enable debug logging (default: `false`)
|
|
134
|
+
|
|
135
|
+
**Returns:** `() => void` - Cleanup function
|
|
136
|
+
|
|
137
|
+
**Example:**
|
|
138
|
+
```typescript
|
|
139
|
+
startClient({
|
|
140
|
+
app: () => App(),
|
|
141
|
+
islands: { Counter, TodoList },
|
|
142
|
+
container: '#app',
|
|
143
|
+
debug: true,
|
|
144
|
+
})
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### `hydrate(renderable, container, options?)`
|
|
148
|
+
|
|
149
|
+
Hydrates server-rendered HTML with client-side interactivity.
|
|
150
|
+
|
|
151
|
+
**Parameters:**
|
|
152
|
+
- `renderable: Renderable` - The Tempo renderable (should match server render)
|
|
153
|
+
- `container: HTMLElement` - Container with server-rendered content
|
|
154
|
+
- `options?: HydrateOptions` - Optional configuration
|
|
155
|
+
|
|
156
|
+
**Options:**
|
|
157
|
+
- `providers?: Providers` - Providers to inject during hydration
|
|
158
|
+
- `removeMarkers?: boolean` - Remove hydration markers after hydration (default: true)
|
|
159
|
+
|
|
160
|
+
**Returns:** `() => void` - Cleanup function
|
|
161
|
+
|
|
162
|
+
### `hydrateIsland(element, component, componentOptions, hydrateOptions?)`
|
|
163
|
+
|
|
164
|
+
Hydrates a single island element.
|
|
165
|
+
|
|
166
|
+
**Parameters:**
|
|
167
|
+
- `element: HTMLElement` - The island container element
|
|
168
|
+
- `component: (options: O) => Renderable` - Component factory
|
|
169
|
+
- `componentOptions: O` - Options to pass to the component
|
|
170
|
+
- `hydrateOptions?: IslandHydrateOptions` - Optional configuration
|
|
171
|
+
|
|
172
|
+
**Returns:** `() => void` - Cleanup function
|
|
173
|
+
|
|
174
|
+
### `initIslands(registry, options?)`
|
|
175
|
+
|
|
176
|
+
Scans the document for islands and hydrates them based on their strategy.
|
|
177
|
+
|
|
178
|
+
**Parameters:**
|
|
179
|
+
- `registry: IslandRegistry` - Map of island names to component factories
|
|
180
|
+
- `options?: IslandHydrateOptions` - Optional configuration
|
|
181
|
+
|
|
182
|
+
**Returns:** `() => void` - Cleanup function for all islands
|
|
183
|
+
|
|
184
|
+
### `islandMarker(name, options, strategy?)`
|
|
185
|
+
|
|
186
|
+
Creates attributes for marking an island during server-side rendering.
|
|
187
|
+
|
|
188
|
+
**Parameters:**
|
|
189
|
+
- `name: string` - Island name (must match registry key)
|
|
190
|
+
- `options: unknown` - Options to serialize
|
|
191
|
+
- `strategy?: HydrationStrategy` - When to hydrate (default: 'visible')
|
|
192
|
+
|
|
193
|
+
**Returns:** `Array<{ name: string; value: string }>` - Attribute list
|
|
194
|
+
|
|
195
|
+
## Constants
|
|
196
|
+
|
|
197
|
+
- `ISLAND_ATTR` - Attribute name for island markers (`data-tempo-island`)
|
|
198
|
+
- `ISLAND_HYDRATE_ATTR` - Attribute for hydration strategy (`data-tempo-hydrate`)
|
|
199
|
+
- `ISLAND_OPTIONS_ATTR` - Attribute for serialized options (`data-tempo-options`)
|
|
200
|
+
- `HYDRATION_ID_ATTR` - Attribute for hydration IDs (`data-tts-id`)
|
|
201
|
+
|
|
202
|
+
## License
|
|
203
|
+
|
|
204
|
+
Apache-2.0
|
package/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("@tempots/core"),a=require("@tempots/dom");function y(r,e,t={}){const{providers:n={},removeMarkers:i=!0}=t,s=N(e),o=new p.DisposalScope,c=new d(e.ownerDocument,e,void 0,n,s,0),h=p.withScope(o,()=>r.render(c));return i&&g(e),(l=!1)=>{o.dispose(),h(l)}}function N(r){const e=new Map;return r.querySelectorAll(`[${a.HYDRATION_ID_ATTR}]`).forEach(n=>{const i=n.getAttribute(a.HYDRATION_ID_ATTR);i&&e.set(i,n)}),e}function g(r){r.querySelectorAll(`[${a.HYDRATION_ID_ATTR}]`).forEach(t=>{t.removeAttribute(a.HYDRATION_ID_ATTR),t.removeAttribute("data-tts-node")})}class d{constructor(e,t,n,i,s,o){this.document=e,this.element=t,this.reference=n,this.providers=i,this.hydrationMap=s,this.childIndex=o}makeChildElement=(e,t)=>{const n=this.element.children;for(let s=this.childIndex;s<n.length;s++){const o=n[s];if(o.tagName.toLowerCase()===e.toLowerCase())return this.childIndex=s+1,new d(this.document,o,void 0,this.providers,this.hydrationMap,0)}console.warn(`Hydration mismatch: could not find element <${e}> in container`);const i=t!==void 0?this.document.createElementNS(t,e):this.document.createElement(e);return this.appendOrInsert(i),new d(this.document,i,void 0,this.providers,this.hydrationMap,0)};makeChildText=e=>{const t=this.element.childNodes;for(let i=this.childIndex;i<t.length;i++){const s=t[i];if(s.nodeType===Node.TEXT_NODE)return this.childIndex=i+1,s.textContent!==e&&(s.textContent=e),new d(this.document,this.element,s,this.providers,this.hydrationMap,this.childIndex)}const n=this.document.createTextNode(e);return this.appendOrInsert(n),new d(this.document,this.element,n,this.providers,this.hydrationMap,this.childIndex)};setText=e=>{this.reference&&(this.reference.nodeValue=e)};getText=()=>this.reference?.nodeValue??this.element.textContent??"";makeRef=()=>{const e=this.element.childNodes;for(let n=this.childIndex;n<e.length;n++){const i=e[n];if(i.nodeType===Node.TEXT_NODE&&(i.textContent===""||i.textContent===null))return this.childIndex=n+1,new d(this.document,this.element,i,this.providers,this.hydrationMap,this.childIndex)}const t=this.document.createTextNode("");return this.appendOrInsert(t),new d(this.document,this.element,t,this.providers,this.hydrationMap,this.childIndex)};makePortal=e=>{const t=typeof e=="string"?this.document.querySelector(e):e;if(t==null)throw new Error(`Cannot find element by selector for portal: ${e}`);return new d(this.document,t,void 0,this.providers,this.hydrationMap,0)};appendOrInsert=e=>{this.reference===void 0?this.element.appendChild(e):this.element.insertBefore(e,this.reference)};setProvider=(e,t,n)=>new d(this.document,this.element,this.reference,{...this.providers,[e]:[t,n]},this.hydrationMap,this.childIndex);getProvider=e=>{if(this.providers[e]===void 0)throw new Error(`Provider not found: ${String(e)}`);const[t,n]=this.providers[e];return{value:t,onUse:n}};clear=e=>{e&&(this.reference!==void 0?this.reference.parentNode?.removeChild(this.reference):this.element.parentNode?.removeChild(this.element))};addClasses=e=>{this.element.classList.add(...e)};removeClasses=e=>{this.element.classList.remove(...e)};getClasses=()=>Array.from(this.element.classList);on=(e,t,n)=>{const i=s=>t(s,this);return this.element.addEventListener(e,i,n),s=>{s&&this.element.removeEventListener(e,i,n)}};isBrowserDOM=()=>!1;isBrowser=()=>!0;isHeadlessDOM=()=>!1;isHeadless=()=>!1;setStyle=(e,t)=>{this.element.style[e]=t};getStyle=e=>this.element.style[e];makeAccessors=e=>{const t=this.element;return{get:()=>t[e],set:n=>{t[e]=n}}}}const u="data-tempo-island",I="data-tempo-hydrate",A="data-tempo-options";function S(r,e,t,n={}){r.innerHTML="";const i=new a.BrowserContext(r.ownerDocument,r,void 0,n.providers??{}),s=new p.DisposalScope,o=p.withScope(s,()=>e(t).render(i));return(c=!1)=>{s.dispose(),o(c)}}function D(r,e,t){if(e==="immediate")return t(),()=>{};if(e==="idle")if("requestIdleCallback"in window){const n=requestIdleCallback(t);return()=>cancelIdleCallback(n)}else{const n=setTimeout(t,1);return()=>clearTimeout(n)}if(e==="visible"){const n=new IntersectionObserver(i=>{for(const s of i)if(s.isIntersecting){n.disconnect(),t();break}},{rootMargin:"50px"});return n.observe(r),()=>n.disconnect()}if(typeof e=="object"&&"media"in e){const n=window.matchMedia(e.media);if(n.matches)return t(),()=>{};const i=s=>{s.matches&&(n.removeEventListener("change",i),t())};return n.addEventListener("change",i),()=>n.removeEventListener("change",i)}return t(),()=>{}}function O(r){return!r||r==="immediate"||r==="load"?"immediate":r==="idle"?"idle":r==="visible"?"visible":r.startsWith("media:")?{media:r.slice(6).trim()}:"immediate"}function v(r,e={}){const t=[];return document.querySelectorAll(`[${u}]`).forEach(i=>{const s=i,o=s.getAttribute(u);if(!o){console.warn("[Tempo Islands] Element missing island name:",s);return}const c=r[o];if(!c){console.warn(`[Tempo Islands] Component "${o}" not found in registry`);return}const h=s.getAttribute(A);let l={};if(h)try{l=JSON.parse(h)}catch(f){console.warn(`[Tempo Islands] Failed to parse options for "${o}":`,f)}const m=s.getAttribute(I),T=O(m),w=D(s,T,()=>{const f=S(s,c,l,e);t.push(f),s.removeAttribute(u),s.removeAttribute(I),s.removeAttribute(A)});t.push(w)}),()=>{t.forEach(i=>i())}}function R(r,e={},t="visible"){const n=typeof t=="object"?`media:${t.media}`:t;return[{name:u,value:r},{name:A,value:JSON.stringify(e)},{name:I,value:n}]}function b(r){const{app:e,islands:t,container:n="#app",providers:i={},debug:s=!1}=r,o=s?m=>console.log(`[Tempo] ${m}`):()=>{},c=typeof n=="string"?document.querySelector(n):n;if(!c)return console.error(`[Tempo] Could not find container: ${typeof n=="string"?n:"(element)"}`),()=>{};const h=c.querySelector(`[${u}]`)!==null;let l;if(h)o("SSR mode: Initializing islands..."),l=v(t,{providers:i}),o("Islands initialized! Static content stays static.");else if(e){o("Client-only mode: Rendering app...");const m=new a.BrowserContext(document,c,void 0,i),T=new p.DisposalScope,w=p.withScope(T,()=>e().render(m)),f=v(t,{providers:i});l=()=>{f(),T.dispose(),w(!0)},o("App rendered and islands initialized!")}else o("No SSR content detected, initializing islands only..."),l=v(t,{providers:i});return l}Object.defineProperty(exports,"HYDRATION_ID_ATTR",{enumerable:!0,get:()=>a.HYDRATION_ID_ATTR});exports.HydrationContext=d;exports.ISLAND_ATTR=u;exports.ISLAND_HYDRATE_ATTR=I;exports.ISLAND_OPTIONS_ATTR=A;exports.hydrate=y;exports.hydrateIsland=S;exports.initIslands=v;exports.islandMarker=R;exports.startClient=b;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { Renderable, Providers, ProviderMark, Clear, BrowserContext, DOMContext, HYDRATION_ID_ATTR, HeadlessContext } from '@tempots/dom';
|
|
2
|
+
/**
|
|
3
|
+
* Options for client-side hydration.
|
|
4
|
+
* @public
|
|
5
|
+
*/
|
|
6
|
+
export interface HydrateOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Providers to inject during hydration.
|
|
9
|
+
*/
|
|
10
|
+
providers?: Providers;
|
|
11
|
+
/**
|
|
12
|
+
* Whether to remove hydration markers after hydration completes.
|
|
13
|
+
* Defaults to true.
|
|
14
|
+
*/
|
|
15
|
+
removeMarkers?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Hydrates server-rendered HTML with client-side interactivity.
|
|
19
|
+
*
|
|
20
|
+
* This function takes a server-rendered container and attaches event handlers
|
|
21
|
+
* and reactive updates to the existing DOM nodes without re-creating them.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```typescript
|
|
25
|
+
* import { hydrate } from '@tempots/client'
|
|
26
|
+
* import { App } from './App'
|
|
27
|
+
*
|
|
28
|
+
* // Server-rendered HTML is already in the DOM
|
|
29
|
+
* // Hydrate it with client-side interactivity
|
|
30
|
+
* const cleanup = hydrate(App(), document.getElementById('app')!)
|
|
31
|
+
*
|
|
32
|
+
* // Later, to remove all event listeners:
|
|
33
|
+
* cleanup()
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @param renderable - The Renderable to hydrate with (should match server render).
|
|
37
|
+
* @param container - The container element with server-rendered content.
|
|
38
|
+
* @param options - Hydration options.
|
|
39
|
+
* @returns A cleanup function that removes all event listeners and disposes signals.
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
export declare function hydrate(renderable: Renderable, container: HTMLElement, options?: HydrateOptions): () => void;
|
|
43
|
+
/**
|
|
44
|
+
* A DOMContext implementation for hydration that reuses existing DOM nodes.
|
|
45
|
+
*
|
|
46
|
+
* During hydration, instead of creating new DOM elements, this context
|
|
47
|
+
* finds and reuses existing elements that were rendered on the server.
|
|
48
|
+
* Event handlers are attached to the existing elements.
|
|
49
|
+
*
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
export declare class HydrationContext implements DOMContext {
|
|
53
|
+
readonly document: Document;
|
|
54
|
+
readonly element: HTMLElement;
|
|
55
|
+
readonly reference: Node | undefined;
|
|
56
|
+
readonly providers: Providers;
|
|
57
|
+
private readonly hydrationMap;
|
|
58
|
+
private childIndex;
|
|
59
|
+
constructor(document: Document, element: HTMLElement, reference: Node | undefined, providers: Providers, hydrationMap: Map<string, HTMLElement>, childIndex: number);
|
|
60
|
+
/**
|
|
61
|
+
* Finds the next child element instead of creating a new one.
|
|
62
|
+
* Falls back to creating a new element if hydration fails.
|
|
63
|
+
*/
|
|
64
|
+
readonly makeChildElement: (tagName: string, namespace: string | undefined) => DOMContext;
|
|
65
|
+
/**
|
|
66
|
+
* Finds the next text node instead of creating a new one.
|
|
67
|
+
*/
|
|
68
|
+
readonly makeChildText: (text: string) => DOMContext;
|
|
69
|
+
readonly setText: (text: string) => void;
|
|
70
|
+
readonly getText: () => string;
|
|
71
|
+
readonly makeRef: () => DOMContext;
|
|
72
|
+
readonly makePortal: (selector: string | HTMLElement) => DOMContext;
|
|
73
|
+
private readonly appendOrInsert;
|
|
74
|
+
readonly setProvider: <T>(mark: ProviderMark<T>, value: T, onUse: undefined | (() => void)) => DOMContext;
|
|
75
|
+
readonly getProvider: <T>(mark: ProviderMark<T>) => {
|
|
76
|
+
value: T;
|
|
77
|
+
onUse: (() => void) | undefined;
|
|
78
|
+
};
|
|
79
|
+
readonly clear: (removeTree: boolean) => void;
|
|
80
|
+
readonly addClasses: (tokens: string[]) => void;
|
|
81
|
+
readonly removeClasses: (tokens: string[]) => void;
|
|
82
|
+
readonly getClasses: () => string[];
|
|
83
|
+
readonly on: <E>(event: string, listener: (event: E, ctx: HydrationContext) => void, options?: AddEventListenerOptions) => Clear;
|
|
84
|
+
readonly isBrowserDOM: () => this is BrowserContext;
|
|
85
|
+
readonly isBrowser: () => this is BrowserContext;
|
|
86
|
+
readonly isHeadlessDOM: () => this is HeadlessContext;
|
|
87
|
+
readonly isHeadless: () => this is HeadlessContext;
|
|
88
|
+
readonly setStyle: (name: string, value: string) => void;
|
|
89
|
+
readonly getStyle: (name: string) => string;
|
|
90
|
+
readonly makeAccessors: (name: string) => {
|
|
91
|
+
get(): unknown;
|
|
92
|
+
set(value: unknown): void;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Attribute name for island markers.
|
|
97
|
+
* @public
|
|
98
|
+
*/
|
|
99
|
+
export declare const ISLAND_ATTR = "data-tempo-island";
|
|
100
|
+
/**
|
|
101
|
+
* Attribute name for island hydration strategy.
|
|
102
|
+
* @public
|
|
103
|
+
*/
|
|
104
|
+
export declare const ISLAND_HYDRATE_ATTR = "data-tempo-hydrate";
|
|
105
|
+
/**
|
|
106
|
+
* Attribute name for serialized island options.
|
|
107
|
+
* @public
|
|
108
|
+
*/
|
|
109
|
+
export declare const ISLAND_OPTIONS_ATTR = "data-tempo-options";
|
|
110
|
+
/**
|
|
111
|
+
* Hydration strategy for islands.
|
|
112
|
+
*
|
|
113
|
+
* - `"immediate"` - Hydrate as soon as possible (client:load)
|
|
114
|
+
* - `"idle"` - Hydrate when the browser is idle (client:idle)
|
|
115
|
+
* - `"visible"` - Hydrate when scrolled into view (client:visible)
|
|
116
|
+
* - `"media"` - Hydrate when a media query matches
|
|
117
|
+
*
|
|
118
|
+
* @public
|
|
119
|
+
*/
|
|
120
|
+
export type HydrationStrategy = "immediate" | "idle" | "visible" | {
|
|
121
|
+
media: string;
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Options for island hydration.
|
|
125
|
+
* @public
|
|
126
|
+
*/
|
|
127
|
+
export interface IslandHydrateOptions {
|
|
128
|
+
/**
|
|
129
|
+
* Providers to inject during hydration.
|
|
130
|
+
*/
|
|
131
|
+
providers?: Providers;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Island component factory type.
|
|
135
|
+
* Options are received from JSON deserialization and typed as `unknown`.
|
|
136
|
+
* Components should cast options to their expected type internally.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```typescript
|
|
140
|
+
* interface CounterOptions { initial?: number }
|
|
141
|
+
*
|
|
142
|
+
* const Counter: IslandComponent = (options) => {
|
|
143
|
+
* const { initial = 0 } = (options ?? {}) as CounterOptions;
|
|
144
|
+
* return html.div(/* ... *\/)
|
|
145
|
+
* }
|
|
146
|
+
* ```
|
|
147
|
+
* @public
|
|
148
|
+
*/
|
|
149
|
+
export type IslandComponent = (options: unknown) => Renderable;
|
|
150
|
+
/**
|
|
151
|
+
* Island component registry type.
|
|
152
|
+
* Maps island names to their component factories.
|
|
153
|
+
* @public
|
|
154
|
+
*/
|
|
155
|
+
export type IslandRegistry = Record<string, IslandComponent>;
|
|
156
|
+
/**
|
|
157
|
+
* Hydrates a single island element with the given component.
|
|
158
|
+
*
|
|
159
|
+
* Unlike full hydration, islands are re-rendered fresh on the client.
|
|
160
|
+
* The server-rendered HTML is replaced with the interactive component.
|
|
161
|
+
* This is simpler and more reliable than trying to match existing DOM.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```typescript
|
|
165
|
+
* import { hydrateIsland } from '@tempots/client'
|
|
166
|
+
* import { Counter } from './islands/Counter'
|
|
167
|
+
*
|
|
168
|
+
* const element = document.querySelector('[data-tempo-island="Counter"]')
|
|
169
|
+
* const options = JSON.parse(element.dataset.tempoProps || '{}')
|
|
170
|
+
*
|
|
171
|
+
* hydrateIsland(element, Counter, options)
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* @param element - The island container element.
|
|
175
|
+
* @param component - The component factory function.
|
|
176
|
+
* @param componentOptions - Options to pass to the component.
|
|
177
|
+
* @param hydrateOptions - Hydration options.
|
|
178
|
+
* @returns A cleanup function.
|
|
179
|
+
* @public
|
|
180
|
+
*/
|
|
181
|
+
export declare function hydrateIsland<O>(element: HTMLElement, component: (options: O) => Renderable, componentOptions: O, hydrateOptions?: IslandHydrateOptions): () => void;
|
|
182
|
+
/**
|
|
183
|
+
* Initializes all islands in the document using the provided registry.
|
|
184
|
+
*
|
|
185
|
+
* This function scans the document for elements with `data-tempo-island` attributes
|
|
186
|
+
* and hydrates them using the corresponding component from the registry.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```typescript
|
|
190
|
+
* import { initIslands } from '@tempots/client'
|
|
191
|
+
* import { Counter } from './islands/Counter'
|
|
192
|
+
* import { TodoList } from './islands/TodoList'
|
|
193
|
+
*
|
|
194
|
+
* // Initialize all islands
|
|
195
|
+
* const cleanup = initIslands({
|
|
196
|
+
* Counter,
|
|
197
|
+
* TodoList,
|
|
198
|
+
* })
|
|
199
|
+
*
|
|
200
|
+
* // Later, to cleanup all islands:
|
|
201
|
+
* cleanup()
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* @param registry - A map of island names to component factories.
|
|
205
|
+
* @param options - Hydration options.
|
|
206
|
+
* @returns A cleanup function that disposes all islands.
|
|
207
|
+
* @public
|
|
208
|
+
*/
|
|
209
|
+
export declare function initIslands(registry: IslandRegistry, options?: IslandHydrateOptions): () => void;
|
|
210
|
+
/**
|
|
211
|
+
* Creates an island marker for server-side rendering.
|
|
212
|
+
*
|
|
213
|
+
* This function returns attributes that should be added to the island's
|
|
214
|
+
* root element during SSR to enable client-side hydration.
|
|
215
|
+
*
|
|
216
|
+
* @example
|
|
217
|
+
* ```typescript
|
|
218
|
+
* // In your SSR template
|
|
219
|
+
* const Counter = (options: CounterOptions) => {
|
|
220
|
+
* return html.div(
|
|
221
|
+
* ...islandMarker('Counter', options, 'visible'),
|
|
222
|
+
* // ... counter implementation
|
|
223
|
+
* )
|
|
224
|
+
* }
|
|
225
|
+
* ```
|
|
226
|
+
*
|
|
227
|
+
* @param name - The island component name (must match registry key).
|
|
228
|
+
* @param options - Options to serialize for client-side hydration.
|
|
229
|
+
* @param strategy - When to hydrate the island.
|
|
230
|
+
* @returns An array of attribute setters for the island element.
|
|
231
|
+
* @public
|
|
232
|
+
*/
|
|
233
|
+
export declare function islandMarker(name: string, options?: unknown, strategy?: HydrationStrategy): Array<{
|
|
234
|
+
name: string;
|
|
235
|
+
value: string;
|
|
236
|
+
}>;
|
|
237
|
+
/**
|
|
238
|
+
* Options for the startClient function.
|
|
239
|
+
* @public
|
|
240
|
+
*/
|
|
241
|
+
export interface ClientOptions<R extends IslandRegistry> {
|
|
242
|
+
/**
|
|
243
|
+
* App component for client-only rendering.
|
|
244
|
+
* If provided and no SSR content is detected, this will be rendered.
|
|
245
|
+
*/
|
|
246
|
+
app?: () => Renderable;
|
|
247
|
+
/**
|
|
248
|
+
* Island component registry.
|
|
249
|
+
* Maps island names to their component factories.
|
|
250
|
+
*/
|
|
251
|
+
islands: R;
|
|
252
|
+
/**
|
|
253
|
+
* Container selector or element.
|
|
254
|
+
* @default "#app"
|
|
255
|
+
*/
|
|
256
|
+
container?: string | HTMLElement;
|
|
257
|
+
/**
|
|
258
|
+
* Providers to inject during hydration.
|
|
259
|
+
*/
|
|
260
|
+
providers?: Providers;
|
|
261
|
+
/**
|
|
262
|
+
* Enable debug logging.
|
|
263
|
+
* @default false
|
|
264
|
+
*/
|
|
265
|
+
debug?: boolean;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Initializes the Tempo client with automatic SSR detection.
|
|
269
|
+
*
|
|
270
|
+
* This is a high-level convenience function that handles:
|
|
271
|
+
* - Container detection
|
|
272
|
+
* - SSR vs client-only mode detection
|
|
273
|
+
* - Island initialization
|
|
274
|
+
* - HMR cleanup (when available)
|
|
275
|
+
*
|
|
276
|
+
* For more control, use the lower-level `initIslands()` and `hydrateIsland()` functions directly.
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```typescript
|
|
280
|
+
* // Minimal usage - islands only
|
|
281
|
+
* import { startClient } from '@tempots/client'
|
|
282
|
+
* import { Counter, TodoList } from './islands'
|
|
283
|
+
*
|
|
284
|
+
* startClient({
|
|
285
|
+
* islands: { Counter, TodoList }
|
|
286
|
+
* })
|
|
287
|
+
* ```
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```typescript
|
|
291
|
+
* // With client-only fallback
|
|
292
|
+
* import { startClient } from '@tempots/client'
|
|
293
|
+
* import { App, Counter } from './app'
|
|
294
|
+
*
|
|
295
|
+
* startClient({
|
|
296
|
+
* app: () => App(),
|
|
297
|
+
* islands: { Counter },
|
|
298
|
+
* debug: true
|
|
299
|
+
* })
|
|
300
|
+
* ```
|
|
301
|
+
*
|
|
302
|
+
* @param options - Client configuration options.
|
|
303
|
+
* @returns A cleanup function that disposes all islands and removes event listeners.
|
|
304
|
+
* @public
|
|
305
|
+
*/
|
|
306
|
+
export declare function startClient<R extends IslandRegistry>(options: ClientOptions<R>): () => void;
|
|
307
|
+
export type { Renderable, Providers };
|
|
308
|
+
export { HYDRATION_ID_ATTR };
|
package/index.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { DisposalScope as S, withScope as y } from "@tempots/core";
|
|
2
|
+
import { HYDRATION_ID_ATTR as f, BrowserContext as g } from "@tempots/dom";
|
|
3
|
+
import { HYDRATION_ID_ATTR as q } from "@tempots/dom";
|
|
4
|
+
function D(r, e, t = {}) {
|
|
5
|
+
const { providers: n = {}, removeMarkers: i = !0 } = t, s = A(e), o = new S(), d = new c(
|
|
6
|
+
e.ownerDocument,
|
|
7
|
+
e,
|
|
8
|
+
void 0,
|
|
9
|
+
n,
|
|
10
|
+
s,
|
|
11
|
+
0
|
|
12
|
+
), h = y(o, () => r.render(d));
|
|
13
|
+
return i && N(e), (l = !1) => {
|
|
14
|
+
o.dispose(), h(l);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function A(r) {
|
|
18
|
+
const e = /* @__PURE__ */ new Map();
|
|
19
|
+
return r.querySelectorAll(`[${f}]`).forEach((n) => {
|
|
20
|
+
const i = n.getAttribute(f);
|
|
21
|
+
i && e.set(i, n);
|
|
22
|
+
}), e;
|
|
23
|
+
}
|
|
24
|
+
function N(r) {
|
|
25
|
+
r.querySelectorAll(`[${f}]`).forEach((t) => {
|
|
26
|
+
t.removeAttribute(f), t.removeAttribute("data-tts-node");
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
class c {
|
|
30
|
+
constructor(e, t, n, i, s, o) {
|
|
31
|
+
this.document = e, this.element = t, this.reference = n, this.providers = i, this.hydrationMap = s, this.childIndex = o;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Finds the next child element instead of creating a new one.
|
|
35
|
+
* Falls back to creating a new element if hydration fails.
|
|
36
|
+
*/
|
|
37
|
+
makeChildElement = (e, t) => {
|
|
38
|
+
const n = this.element.children;
|
|
39
|
+
for (let s = this.childIndex; s < n.length; s++) {
|
|
40
|
+
const o = n[s];
|
|
41
|
+
if (o.tagName.toLowerCase() === e.toLowerCase())
|
|
42
|
+
return this.childIndex = s + 1, new c(
|
|
43
|
+
this.document,
|
|
44
|
+
o,
|
|
45
|
+
void 0,
|
|
46
|
+
this.providers,
|
|
47
|
+
this.hydrationMap,
|
|
48
|
+
0
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
console.warn(
|
|
52
|
+
`Hydration mismatch: could not find element <${e}> in container`
|
|
53
|
+
);
|
|
54
|
+
const i = t !== void 0 ? this.document.createElementNS(t, e) : this.document.createElement(e);
|
|
55
|
+
return this.appendOrInsert(i), new c(
|
|
56
|
+
this.document,
|
|
57
|
+
i,
|
|
58
|
+
void 0,
|
|
59
|
+
this.providers,
|
|
60
|
+
this.hydrationMap,
|
|
61
|
+
0
|
|
62
|
+
);
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Finds the next text node instead of creating a new one.
|
|
66
|
+
*/
|
|
67
|
+
makeChildText = (e) => {
|
|
68
|
+
const t = this.element.childNodes;
|
|
69
|
+
for (let i = this.childIndex; i < t.length; i++) {
|
|
70
|
+
const s = t[i];
|
|
71
|
+
if (s.nodeType === Node.TEXT_NODE)
|
|
72
|
+
return this.childIndex = i + 1, s.textContent !== e && (s.textContent = e), new c(
|
|
73
|
+
this.document,
|
|
74
|
+
this.element,
|
|
75
|
+
s,
|
|
76
|
+
this.providers,
|
|
77
|
+
this.hydrationMap,
|
|
78
|
+
this.childIndex
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
const n = this.document.createTextNode(e);
|
|
82
|
+
return this.appendOrInsert(n), new c(
|
|
83
|
+
this.document,
|
|
84
|
+
this.element,
|
|
85
|
+
n,
|
|
86
|
+
this.providers,
|
|
87
|
+
this.hydrationMap,
|
|
88
|
+
this.childIndex
|
|
89
|
+
);
|
|
90
|
+
};
|
|
91
|
+
setText = (e) => {
|
|
92
|
+
this.reference && (this.reference.nodeValue = e);
|
|
93
|
+
};
|
|
94
|
+
getText = () => this.reference?.nodeValue ?? this.element.textContent ?? "";
|
|
95
|
+
makeRef = () => {
|
|
96
|
+
const e = this.element.childNodes;
|
|
97
|
+
for (let n = this.childIndex; n < e.length; n++) {
|
|
98
|
+
const i = e[n];
|
|
99
|
+
if (i.nodeType === Node.TEXT_NODE && (i.textContent === "" || i.textContent === null))
|
|
100
|
+
return this.childIndex = n + 1, new c(
|
|
101
|
+
this.document,
|
|
102
|
+
this.element,
|
|
103
|
+
i,
|
|
104
|
+
this.providers,
|
|
105
|
+
this.hydrationMap,
|
|
106
|
+
this.childIndex
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
const t = this.document.createTextNode("");
|
|
110
|
+
return this.appendOrInsert(t), new c(
|
|
111
|
+
this.document,
|
|
112
|
+
this.element,
|
|
113
|
+
t,
|
|
114
|
+
this.providers,
|
|
115
|
+
this.hydrationMap,
|
|
116
|
+
this.childIndex
|
|
117
|
+
);
|
|
118
|
+
};
|
|
119
|
+
makePortal = (e) => {
|
|
120
|
+
const t = typeof e == "string" ? this.document.querySelector(e) : e;
|
|
121
|
+
if (t == null)
|
|
122
|
+
throw new Error(
|
|
123
|
+
`Cannot find element by selector for portal: ${e}`
|
|
124
|
+
);
|
|
125
|
+
return new c(
|
|
126
|
+
this.document,
|
|
127
|
+
t,
|
|
128
|
+
void 0,
|
|
129
|
+
this.providers,
|
|
130
|
+
this.hydrationMap,
|
|
131
|
+
0
|
|
132
|
+
);
|
|
133
|
+
};
|
|
134
|
+
appendOrInsert = (e) => {
|
|
135
|
+
this.reference === void 0 ? this.element.appendChild(e) : this.element.insertBefore(e, this.reference);
|
|
136
|
+
};
|
|
137
|
+
setProvider = (e, t, n) => new c(
|
|
138
|
+
this.document,
|
|
139
|
+
this.element,
|
|
140
|
+
this.reference,
|
|
141
|
+
{ ...this.providers, [e]: [t, n] },
|
|
142
|
+
this.hydrationMap,
|
|
143
|
+
this.childIndex
|
|
144
|
+
);
|
|
145
|
+
getProvider = (e) => {
|
|
146
|
+
if (this.providers[e] === void 0)
|
|
147
|
+
throw new Error(`Provider not found: ${String(e)}`);
|
|
148
|
+
const [t, n] = this.providers[e];
|
|
149
|
+
return { value: t, onUse: n };
|
|
150
|
+
};
|
|
151
|
+
clear = (e) => {
|
|
152
|
+
e && (this.reference !== void 0 ? this.reference.parentNode?.removeChild(this.reference) : this.element.parentNode?.removeChild(this.element));
|
|
153
|
+
};
|
|
154
|
+
addClasses = (e) => {
|
|
155
|
+
this.element.classList.add(...e);
|
|
156
|
+
};
|
|
157
|
+
removeClasses = (e) => {
|
|
158
|
+
this.element.classList.remove(...e);
|
|
159
|
+
};
|
|
160
|
+
getClasses = () => Array.from(this.element.classList);
|
|
161
|
+
on = (e, t, n) => {
|
|
162
|
+
const i = (s) => t(s, this);
|
|
163
|
+
return this.element.addEventListener(e, i, n), (s) => {
|
|
164
|
+
s && this.element.removeEventListener(e, i, n);
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
// HydrationContext runs in a browser environment, so isBrowser() returns true
|
|
168
|
+
// to ensure browser-specific code paths (like Location provider) work correctly.
|
|
169
|
+
// Note: isBrowserDOM returns false since this is not a BrowserContext instance.
|
|
170
|
+
isBrowserDOM = () => !1;
|
|
171
|
+
isBrowser = () => !0;
|
|
172
|
+
isHeadlessDOM = () => !1;
|
|
173
|
+
isHeadless = () => !1;
|
|
174
|
+
setStyle = (e, t) => {
|
|
175
|
+
this.element.style[e] = t;
|
|
176
|
+
};
|
|
177
|
+
getStyle = (e) => this.element.style[e];
|
|
178
|
+
makeAccessors = (e) => {
|
|
179
|
+
const t = this.element;
|
|
180
|
+
return {
|
|
181
|
+
get: () => t[e],
|
|
182
|
+
set: (n) => {
|
|
183
|
+
t[e] = n;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const u = "data-tempo-island", I = "data-tempo-hydrate", T = "data-tempo-options";
|
|
189
|
+
function x(r, e, t, n = {}) {
|
|
190
|
+
r.innerHTML = "";
|
|
191
|
+
const i = new g(
|
|
192
|
+
r.ownerDocument,
|
|
193
|
+
r,
|
|
194
|
+
void 0,
|
|
195
|
+
n.providers ?? {}
|
|
196
|
+
), s = new S(), o = y(s, () => e(t).render(i));
|
|
197
|
+
return (d = !1) => {
|
|
198
|
+
s.dispose(), o(d);
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function E(r, e, t) {
|
|
202
|
+
if (e === "immediate")
|
|
203
|
+
return t(), () => {
|
|
204
|
+
};
|
|
205
|
+
if (e === "idle")
|
|
206
|
+
if ("requestIdleCallback" in window) {
|
|
207
|
+
const n = requestIdleCallback(t);
|
|
208
|
+
return () => cancelIdleCallback(n);
|
|
209
|
+
} else {
|
|
210
|
+
const n = setTimeout(t, 1);
|
|
211
|
+
return () => clearTimeout(n);
|
|
212
|
+
}
|
|
213
|
+
if (e === "visible") {
|
|
214
|
+
const n = new IntersectionObserver(
|
|
215
|
+
(i) => {
|
|
216
|
+
for (const s of i)
|
|
217
|
+
if (s.isIntersecting) {
|
|
218
|
+
n.disconnect(), t();
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
},
|
|
222
|
+
{ rootMargin: "50px" }
|
|
223
|
+
);
|
|
224
|
+
return n.observe(r), () => n.disconnect();
|
|
225
|
+
}
|
|
226
|
+
if (typeof e == "object" && "media" in e) {
|
|
227
|
+
const n = window.matchMedia(e.media);
|
|
228
|
+
if (n.matches)
|
|
229
|
+
return t(), () => {
|
|
230
|
+
};
|
|
231
|
+
const i = (s) => {
|
|
232
|
+
s.matches && (n.removeEventListener("change", i), t());
|
|
233
|
+
};
|
|
234
|
+
return n.addEventListener("change", i), () => n.removeEventListener("change", i);
|
|
235
|
+
}
|
|
236
|
+
return t(), () => {
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function M(r) {
|
|
240
|
+
return !r || r === "immediate" || r === "load" ? "immediate" : r === "idle" ? "idle" : r === "visible" ? "visible" : r.startsWith("media:") ? { media: r.slice(6).trim() } : "immediate";
|
|
241
|
+
}
|
|
242
|
+
function w(r, e = {}) {
|
|
243
|
+
const t = [];
|
|
244
|
+
return document.querySelectorAll(`[${u}]`).forEach((i) => {
|
|
245
|
+
const s = i, o = s.getAttribute(u);
|
|
246
|
+
if (!o) {
|
|
247
|
+
console.warn("[Tempo Islands] Element missing island name:", s);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const d = r[o];
|
|
251
|
+
if (!d) {
|
|
252
|
+
console.warn(
|
|
253
|
+
`[Tempo Islands] Component "${o}" not found in registry`
|
|
254
|
+
);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
const h = s.getAttribute(T);
|
|
258
|
+
let l = {};
|
|
259
|
+
if (h)
|
|
260
|
+
try {
|
|
261
|
+
l = JSON.parse(h);
|
|
262
|
+
} catch (m) {
|
|
263
|
+
console.warn(
|
|
264
|
+
`[Tempo Islands] Failed to parse options for "${o}":`,
|
|
265
|
+
m
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
const a = s.getAttribute(I), p = M(a), v = E(s, p, () => {
|
|
269
|
+
const m = x(
|
|
270
|
+
s,
|
|
271
|
+
d,
|
|
272
|
+
l,
|
|
273
|
+
e
|
|
274
|
+
);
|
|
275
|
+
t.push(m), s.removeAttribute(u), s.removeAttribute(I), s.removeAttribute(T);
|
|
276
|
+
});
|
|
277
|
+
t.push(v);
|
|
278
|
+
}), () => {
|
|
279
|
+
t.forEach((i) => i());
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
function L(r, e = {}, t = "visible") {
|
|
283
|
+
const n = typeof t == "object" ? `media:${t.media}` : t;
|
|
284
|
+
return [
|
|
285
|
+
{ name: u, value: r },
|
|
286
|
+
{ name: T, value: JSON.stringify(e) },
|
|
287
|
+
{ name: I, value: n }
|
|
288
|
+
];
|
|
289
|
+
}
|
|
290
|
+
function C(r) {
|
|
291
|
+
const {
|
|
292
|
+
app: e,
|
|
293
|
+
islands: t,
|
|
294
|
+
container: n = "#app",
|
|
295
|
+
providers: i = {},
|
|
296
|
+
debug: s = !1
|
|
297
|
+
} = r, o = s ? (a) => console.log(`[Tempo] ${a}`) : () => {
|
|
298
|
+
}, d = typeof n == "string" ? document.querySelector(n) : n;
|
|
299
|
+
if (!d)
|
|
300
|
+
return console.error(`[Tempo] Could not find container: ${typeof n == "string" ? n : "(element)"}`), () => {
|
|
301
|
+
};
|
|
302
|
+
const h = d.querySelector(`[${u}]`) !== null;
|
|
303
|
+
let l;
|
|
304
|
+
if (h)
|
|
305
|
+
o("SSR mode: Initializing islands..."), l = w(t, { providers: i }), o("Islands initialized! Static content stays static.");
|
|
306
|
+
else if (e) {
|
|
307
|
+
o("Client-only mode: Rendering app...");
|
|
308
|
+
const a = new g(document, d, void 0, i), p = new S(), v = y(p, () => e().render(a)), m = w(t, { providers: i });
|
|
309
|
+
l = () => {
|
|
310
|
+
m(), p.dispose(), v(!0);
|
|
311
|
+
}, o("App rendered and islands initialized!");
|
|
312
|
+
} else
|
|
313
|
+
o("No SSR content detected, initializing islands only..."), l = w(t, { providers: i });
|
|
314
|
+
return l;
|
|
315
|
+
}
|
|
316
|
+
export {
|
|
317
|
+
q as HYDRATION_ID_ATTR,
|
|
318
|
+
c as HydrationContext,
|
|
319
|
+
u as ISLAND_ATTR,
|
|
320
|
+
I as ISLAND_HYDRATE_ATTR,
|
|
321
|
+
T as ISLAND_OPTIONS_ATTR,
|
|
322
|
+
D as hydrate,
|
|
323
|
+
x as hydrateIsland,
|
|
324
|
+
w as initIslands,
|
|
325
|
+
L as islandMarker,
|
|
326
|
+
C as startClient
|
|
327
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tempots/client",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./index.cjs",
|
|
6
|
+
"module": "./index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./index.js",
|
|
11
|
+
"require": "./index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"description": "Client-side hydration utilities for Tempo SSR applications",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"tempo",
|
|
18
|
+
"tempots",
|
|
19
|
+
"hydration",
|
|
20
|
+
"client",
|
|
21
|
+
"ssr"
|
|
22
|
+
],
|
|
23
|
+
"homepage": "https://github.com/fponticelli/tempots",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/fponticelli/tempots/issues"
|
|
26
|
+
},
|
|
27
|
+
"author": {
|
|
28
|
+
"name": "Franco Ponticelli",
|
|
29
|
+
"email": "franco.ponticelli@gmail.com",
|
|
30
|
+
"url": "https://github.com/fponticelli"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/fponticelli/tempots.git"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@tempots/core": "^2.1.0",
|
|
38
|
+
"@tempots/dom": "^36.0.0"
|
|
39
|
+
}
|
|
40
|
+
}
|