@zerolith/browser 0.0.0-stage → 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/LICENSE +201 -0
- package/README.md +36 -2
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/inview.d.ts +24 -0
- package/dist/inview.d.ts.map +1 -0
- package/dist/inview.js +62 -0
- package/dist/inview.js.map +1 -0
- package/dist/page.d.ts +13 -0
- package/dist/page.d.ts.map +1 -0
- package/dist/page.js +36 -0
- package/dist/page.js.map +1 -0
- package/dist/production/index.js +2 -0
- package/dist/production/index.js.map +1 -0
- package/dist/production/inview.js +2 -0
- package/dist/production/inview.js.map +1 -0
- package/dist/production/page.js +2 -0
- package/dist/production/page.js.map +1 -0
- package/dist/production/size.js +2 -0
- package/dist/production/size.js.map +1 -0
- package/dist/size.d.ts +27 -0
- package/dist/size.d.ts.map +1 -0
- package/dist/size.js +90 -0
- package/dist/size.js.map +1 -0
- package/package.json +51 -5
- package/src/index.ts +11 -0
- package/src/inview.ts +84 -0
- package/src/page.ts +46 -0
- package/src/size.ts +122 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,37 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zerolith/browser
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The page as signals: whether it is online and visible and the window's size, one signal each for the page, and an element's content box and whether it is in view, one per element, kept by observers that go with the enclosing owner.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { computed, html, signal } from "zerolith";
|
|
7
|
+
import { inView, online, size, windowSize } from "@zerolith/browser";
|
|
8
|
+
|
|
9
|
+
const status = computed(() => (online().get() ? "Online" : "Offline"));
|
|
10
|
+
const wide = computed(() => windowSize().get().width >= 960);
|
|
11
|
+
|
|
12
|
+
function Panel()
|
|
13
|
+
{
|
|
14
|
+
const root = signal<Element | undefined>(undefined);
|
|
15
|
+
const box = size(root);
|
|
16
|
+
const near = inView(root, { rootMargin: "200px" });
|
|
17
|
+
return html`<section ref=${root}>${computed(() => box.get().width)}px wide</section>`;
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- `online()`, `visible()`, and `windowSize()` follow the browser's events and are shared by the page.
|
|
22
|
+
- `size(target)` and `inView(target, options?)` take an element or the signal a `ref` fills.
|
|
23
|
+
- Each is still on the server: online, visible, and no size.
|
|
24
|
+
|
|
25
|
+
The guide is on the [docs site](https://zerolith.dev/guide/browser/).
|
|
26
|
+
|
|
27
|
+
Install with `npm install @zerolith/browser`. The API is frozen at 1.0: a breaking change comes only in a major, after a deprecation notice, and the [support policy](https://github.com/molexxxx/zerolith/blob/main/docs/policy/support.md) says how long a line gets fixes.
|
|
28
|
+
|
|
29
|
+
## Documentation
|
|
30
|
+
|
|
31
|
+
- The [browser guide](https://zerolith.dev/guide/browser/) explains it with examples that run as tests.
|
|
32
|
+
- The [reference](https://zerolith.dev/reference/zerolith-browser/) lists every export, generated from the source.
|
|
33
|
+
- New to zerolith? The [tutorial](https://zerolith.dev/learn/) builds an app step by step, and the [playground](https://zerolith.dev/playground/) runs code in the browser.
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
Apache-2.0. See `LICENSE`.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zerolith/browser: the page as signals. Whether it is online and visible
|
|
3
|
+
* and the window's size are one signal each for the page; an element's
|
|
4
|
+
* size and whether it is in view are one per element, kept by observers
|
|
5
|
+
* that go with the enclosing owner.
|
|
6
|
+
*/
|
|
7
|
+
export type { Size } from "./size.js";
|
|
8
|
+
export { size } from "./size.js";
|
|
9
|
+
export type { InViewOptions } from "./inview.js";
|
|
10
|
+
export { inView } from "./inview.js";
|
|
11
|
+
export { online, visible, windowSize } from "./page.js";
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,YAAY,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,YAAY,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/inview.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether an element is in view, as a signal kept by an
|
|
3
|
+
* IntersectionObserver.
|
|
4
|
+
*/
|
|
5
|
+
import { type ReadableSignal } from "zerolith";
|
|
6
|
+
/** Options for `inView`. */
|
|
7
|
+
export interface InViewOptions {
|
|
8
|
+
/** Space around the viewport that counts as in view, as an IntersectionObserver's `rootMargin`; "200px" starts loading a little ahead of the scroll. */
|
|
9
|
+
rootMargin?: string;
|
|
10
|
+
/** How much of the element must show, from 0 for any part of it to 1 for all of it. Defaults to 0. */
|
|
11
|
+
threshold?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Whether `target` intersects the viewport, as a signal: the element
|
|
15
|
+
* itself, or the signal a `ref` fills, which follows the element it holds
|
|
16
|
+
* and is false while it holds none. An IntersectionObserver keeps it
|
|
17
|
+
* current; it stops with the enclosing owner when there is one and lives
|
|
18
|
+
* for the page otherwise. The signal is false until the observer first
|
|
19
|
+
* reports, and where there is no observer, as on the server. A threshold
|
|
20
|
+
* outside 0 to 1, or a root margin the browser refuses, fails with
|
|
21
|
+
* `in_view_options_invalid`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function inView(target: Element | ReadableSignal<Element | undefined>, options?: InViewOptions): ReadableSignal<boolean>;
|
|
24
|
+
//# sourceMappingURL=inview.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inview.d.ts","sourceRoot":"","sources":["../src/inview.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAuB,KAAK,cAAc,EAAkE,MAAM,UAAU,CAAC;AAEpI,4BAA4B;AAC5B,MAAM,WAAW,aAAa;IAE1B,wJAAwJ;IACxJ,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,sGAAsG;IACtG,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAQD;;;;;;;;;GASG;AACH,wBAAgB,MAAM,CAAC,MAAM,EAAE,OAAO,GAAG,cAAc,CAAC,OAAO,GAAG,SAAS,CAAC,EAAE,OAAO,GAAE,aAAkB,GAAG,cAAc,CAAC,OAAO,CAAC,CAoDlI"}
|
package/dist/inview.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether an element is in view, as a signal kept by an
|
|
3
|
+
* IntersectionObserver.
|
|
4
|
+
*/
|
|
5
|
+
import { ZerolithError, diagnostic, effect, hasOwner, onDispose, signal } from "zerolith";
|
|
6
|
+
/** The error for options an IntersectionObserver refuses. */
|
|
7
|
+
function invalid(args) {
|
|
8
|
+
return new ZerolithError(diagnostic("in_view_options_invalid", "error", args));
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Whether `target` intersects the viewport, as a signal: the element
|
|
12
|
+
* itself, or the signal a `ref` fills, which follows the element it holds
|
|
13
|
+
* and is false while it holds none. An IntersectionObserver keeps it
|
|
14
|
+
* current; it stops with the enclosing owner when there is one and lives
|
|
15
|
+
* for the page otherwise. The signal is false until the observer first
|
|
16
|
+
* reports, and where there is no observer, as on the server. A threshold
|
|
17
|
+
* outside 0 to 1, or a root margin the browser refuses, fails with
|
|
18
|
+
* `in_view_options_invalid`.
|
|
19
|
+
*/
|
|
20
|
+
export function inView(target, options = {}) {
|
|
21
|
+
const threshold = options.threshold ?? 0;
|
|
22
|
+
const value = signal(false);
|
|
23
|
+
if (typeof IntersectionObserver === "undefined") {
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
let current = target instanceof Element ? target : undefined;
|
|
27
|
+
const init = { threshold };
|
|
28
|
+
if (options.rootMargin !== undefined) {
|
|
29
|
+
init.rootMargin = options.rootMargin;
|
|
30
|
+
}
|
|
31
|
+
let observer;
|
|
32
|
+
try {
|
|
33
|
+
observer = new IntersectionObserver((entries) => {
|
|
34
|
+
const entry = entries.findLast((candidate) => candidate.target === current);
|
|
35
|
+
if (entry !== undefined) {
|
|
36
|
+
value.set(entry.isIntersecting);
|
|
37
|
+
}
|
|
38
|
+
}, init);
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
throw invalid({ threshold: String(threshold), rootMargin: options.rootMargin ?? "" });
|
|
42
|
+
}
|
|
43
|
+
if (hasOwner()) {
|
|
44
|
+
onDispose(() => observer.disconnect());
|
|
45
|
+
}
|
|
46
|
+
if (target instanceof Element) {
|
|
47
|
+
observer.observe(target);
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
effect(() => {
|
|
51
|
+
const element = target.get();
|
|
52
|
+
current = element;
|
|
53
|
+
value.set(false);
|
|
54
|
+
if (element === undefined) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
observer.observe(element);
|
|
58
|
+
return () => observer.unobserve(element);
|
|
59
|
+
});
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=inview.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inview.js","sourceRoot":"","sources":["../src/inview.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAA4C,aAAa,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAWpI,6DAA6D;AAC7D,SAAS,OAAO,CAAC,IAAoB;IAEjC,OAAO,IAAI,aAAa,CAAC,UAAU,CAAC,yBAAyB,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;AACnF,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,MAAM,CAAC,MAAqD,EAAE,OAAO,GAAkB,EAAE;IAErG,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,IAAI,CAAC,CAAC;IACzC,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5B,IAAI,OAAO,oBAAoB,KAAK,WAAW,EAC/C,CAAC;QACG,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,IAAI,OAAO,GAAwB,MAAM,YAAY,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;IAClF,MAAM,IAAI,GAA6B,EAAE,SAAS,EAAE,CAAC;IACrD,IAAI,OAAO,CAAC,UAAU,KAAK,SAAS,EACpC,CAAC;QACG,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IACzC,CAAC;I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
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page's own state as signals shared by everyone who asks: whether it
|
|
3
|
+
* is online, whether it is visible, and the window's size.
|
|
4
|
+
*/
|
|
5
|
+
import { type ReadableSignal } from "zerolith";
|
|
6
|
+
import { type Size } from "./size.js";
|
|
7
|
+
/** Whether the browser is online, as `navigator.onLine` says, following the `online` and `offline` events; one signal for the page, started when first asked for, and true where there is no browser. */
|
|
8
|
+
export declare function online(): ReadableSignal<boolean>;
|
|
9
|
+
/** Whether the page is visible, following `visibilitychange`; one signal for the page, started when first asked for, and true where there is no document. */
|
|
10
|
+
export declare function visible(): ReadableSignal<boolean>;
|
|
11
|
+
/** The window's inner size, following `resize`, which a browser fires once per frame; one signal for the page, started when first asked for, and no size where there is no window. */
|
|
12
|
+
export declare function windowSize(): ReadableSignal<Size>;
|
|
13
|
+
//# sourceMappingURL=page.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../src/page.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,KAAK,cAAc,EAAU,MAAM,UAAU,CAAC;AACvD,OAAO,EAAQ,KAAK,IAAI,EAAY,MAAM,WAAW,CAAC;AAqBtD,yMAAyM;AACzM,wBAAgB,MAAM,IAAI,cAAc,CAAC,OAAO,CAAC,CAIhD;AAED,6JAA6J;AAC7J,wBAAgB,OAAO,IAAI,cAAc,CAAC,OAAO,CAAC,CAIjD;AAED,sLAAsL;AACtL,wBAAgB,UAAU,IAAI,cAAc,CAAC,IAAI,CAAC,CAIjD"}
|
package/dist/page.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page's own state as signals shared by everyone who asks: whether it
|
|
3
|
+
* is online, whether it is visible, and the window's size.
|
|
4
|
+
*/
|
|
5
|
+
import { signal } from "zerolith";
|
|
6
|
+
import { NONE, sameSize } from "./size.js";
|
|
7
|
+
let onlineSignal;
|
|
8
|
+
let visibleSignal;
|
|
9
|
+
let windowSizeSignal;
|
|
10
|
+
/** A signal of what `read` gives, read again on each of `events` from `target`; a still signal of `still` where there is no target, as on the server. */
|
|
11
|
+
function following(target, events, read, still, equals) {
|
|
12
|
+
if (target === undefined) {
|
|
13
|
+
return signal(still);
|
|
14
|
+
}
|
|
15
|
+
const value = signal(read(), equals === undefined ? undefined : { equals });
|
|
16
|
+
for (const event of events) {
|
|
17
|
+
target.addEventListener(event, () => value.set(read()));
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
/** Whether the browser is online, as `navigator.onLine` says, following the `online` and `offline` events; one signal for the page, started when first asked for, and true where there is no browser. */
|
|
22
|
+
export function online() {
|
|
23
|
+
onlineSignal ??= following(typeof window === "undefined" ? undefined : window, ["online", "offline"], () => navigator.onLine, true);
|
|
24
|
+
return onlineSignal;
|
|
25
|
+
}
|
|
26
|
+
/** Whether the page is visible, following `visibilitychange`; one signal for the page, started when first asked for, and true where there is no document. */
|
|
27
|
+
export function visible() {
|
|
28
|
+
visibleSignal ??= following(typeof document === "undefined" ? undefined : document, ["visibilitychange"], () => document.visibilityState === "visible", true);
|
|
29
|
+
return visibleSignal;
|
|
30
|
+
}
|
|
31
|
+
/** The window's inner size, following `resize`, which a browser fires once per frame; one signal for the page, started when first asked for, and no size where there is no window. */
|
|
32
|
+
export function windowSize() {
|
|
33
|
+
windowSizeSignal ??= following(typeof window === "undefined" ? undefined : window, ["resize"], () => ({ width: window.innerWidth, height: window.innerHeight }), NONE, sameSize);
|
|
34
|
+
return windowSizeSignal;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=page.js.map
|
package/dist/page.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page.js","sourceRoot":"","sources":["../src/page.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAuB,MAAM,EAAE,MAAM,UAAU,CAAC;A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AO,aAAa,CAAC;AACzB,CAAC;AAED,sLAAsL;AACtL,MAAM,UAAU,UAAU;IAEtB,gBAAgB,KAAK,SAAS,CAAC,OAAO,MAAM,KAAK,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IACjL,OAAO,gBAAgB,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["/**\n * @zerolith/browser: the page as signals. Whether it is online and visible\n * and the window's size are one signal each for the page; an element's\n * size and whether it is in view are one per element, kept by observers\n * that go with the enclosing owner.\n */\nexport type { Size } from \"./size.js\";\nexport { size } from \"./size.js\";\nexport type { InViewOptions } from \"./inview.js\";\nexport { inView } from \"./inview.js\";\nexport { online, visible, windowSize } from \"./page.js\";\n"],"names":[],"mappings":"AAOA,OAAS,SAAY,YAErB,OAAS,WAAc,cACvB,OAAS,OAAQ,QAAS,eAAkB"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{ZerolithError as e,diagnostic as t,effect as n,hasOwner as r,onDispose as i,signal as a}from"zerolith";function o(n){return new e(t(`in_view_options_invalid`,`error`,n))}export function inView(e,t={}){let s=t.threshold??0,c=a(!1);if(typeof IntersectionObserver>`u`)return c;let l=e instanceof Element?e:void 0,u={threshold:s};t.rootMargin!==void 0&&(u.rootMargin=t.rootMargin);let d;try{d=new IntersectionObserver(e=>{let t=e.findLast(e=>e.target===l);t!==void 0&&c.set(t.isIntersecting)},u)}catch{throw o({threshold:String(s),rootMargin:t.rootMargin??``})}return r()&&i(()=>d.disconnect()),e instanceof Element?(d.observe(e),c):(n(()=>{let t=e.get();if(l=t,c.set(!1),t!==void 0)return d.observe(t),()=>d.unobserve(t)}),c)}
|
|
2
|
+
//# sourceMappingURL=inview.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inview.js","sources":["../../src/inview.ts"],"sourcesContent":["/**\n * Whether an element is in view, as a signal kept by an\n * IntersectionObserver.\n */\nimport { type DiagnosticArgs, type ReadableSignal, ZerolithError, diagnostic, effect, hasOwner, onDispose, signal } from \"zerolith\";\n\n/** Options for `inView`. */\nexport interface InViewOptions\n{\n /** Space around the viewport that counts as in view, as an IntersectionObserver's `rootMargin`; \"200px\" starts loading a little ahead of the scroll. */\n rootMargin?: string;\n /** How much of the element must show, from 0 for any part of it to 1 for all of it. Defaults to 0. */\n threshold?: number;\n}\n\n/** The error for options an IntersectionObserver refuses. */\nfunction invalid(args: DiagnosticArgs): ZerolithError\n{\n return new ZerolithError(diagnostic(\"in_view_options_invalid\", \"error\", args));\n}\n\n/**\n * Whether `target` intersects the viewport, as a signal: the element\n * itself, or the signal a `ref` fills, which follows the element it holds\n * and is false while it holds none. An IntersectionObserver keeps it\n * current; it stops with the enclosing owner when there is one and lives\n * for the page otherwise. The signal is false until the observer first\n * reports, and where there is no observer, as on the server. A threshold\n * outside 0 to 1, or a root margin the browser refuses, fails with\n * `in_view_options_invalid`.\n */\nexport function inView(target: Element | ReadableSignal<Element | undefined>, options: InViewOptions = {}): ReadableSignal<boolean>\n{\n const threshold = options.threshold ?? 0;\n const value = signal(false);\n if (typeof IntersectionObserver === \"undefined\")\n {\n return value;\n }\n let current: Element | undefined = target instanceof Element ? target : undefined;\n const init: IntersectionObserverInit = { threshold };\n if (options.rootMargin !== undefined)\n {\n init.rootMargin = options.rootMargin;\n }\n let observer: IntersectionObserver;\n try\n {\n observer = new IntersectionObserver((entries) =>\n {\n const entry = entries.findLast((candidate) => candidate.target === current);\n if (entry !== undefined)\n {\n value.set(entry.isIntersecting);\n }\n }, init);\n }\n catch\n {\n throw invalid({ threshold: String(threshold), rootMargin: options.rootMargin ?? \"\" });\n }\n if (hasOwner())\n {\n onDispose(() => observer.disconnect());\n }\n if (target instanceof Element)\n {\n observer.observe(target);\n return value;\n }\n effect(() =>\n {\n const element = target.get();\n current = element;\n value.set(false);\n if (element === undefined)\n {\n return undefined;\n }\n observer.observe(element);\n return () => observer.unobserve(element);\n });\n return value;\n}\n"],"names":["invalid","ZerolithError","diagnostic","args","threshold","options","value","signal","current","target","init","observer","entry","entries","candidate","hasOwner","onDispose","effect","element"],"mappings":"AAIA,OAAmD,mBAAe,gBAAY,YAAQ,cAAU,eAAW,gBAAc,WAYzH,SAASA,EAAQ,EAAoB,CAEjC,OAAO,IAAIC,EAAcC,EAAW,0BAA2B,QAASC,CAAI,CAAC,CACjF,CAYA,OAAM,SAAU,OAAO,EAAuD,EAAyB,CAAA,EAAE,CAErG,IAAMC,EAAYC,EAAQ,WAAa,EACjCC,EAAQC,EAAO,EAAK,EAC1B,GAAI,OAAO,qBAAyB,IAEhC,OAAOD,EAEX,IAAIE,EAA+BC,aAAkB,QAAUA,EAAS,OAClEC,EAAiC,CAAE,WAAS,EAC9CL,EAAQ,aAAe,SAEvB,EAAK,WAAaA,EAAQ,YAE9B,IAAIM,EACJ,GACA,CACI,EAAW,IAAI,qBAAsB,GAAW,CAE5C,IAAMC,EAAQC,EAAQ,SAAU,GAAcC,EAAU,SAAWN,CAAO,EACtEI,IAAU,QAEVN,EAAM,IAAIM,EAAM,cAAc,CAEtC,EAAGF,CAAI,CACX,MAEA,CACI,MAAMV,EAAQ,CAAE,UAAW,OAAOI,CAAS,EAAG,WAAYC,EAAQ,YAAc,EAAE,CAAE,CACxF,CAsBA,OArBIU,EAAQ,GAERC,MAAgBL,EAAS,WAAU,CAAE,EAErCF,aAAkB,SAElBE,EAAS,QAAQF,CAAM,EAChBH,IAEXW,MAAY,CAER,IAAMC,EAAUT,EAAO,IAAG,EAG1B,GAFA,EAAUS,EACVZ,EAAM,IAAI,EAAK,EACXY,IAAY,OAKhB,OADAP,EAAS,QAAQO,CAAO,MACXP,EAAS,UAAUO,CAAO,CAC3C,CAAC,EACMZ,EACX"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{signal as e}from"zerolith";import{NONE as t,sameSize as n}from"./size.js";let r,i,a;function o(t,n,r,i,a){if(t===void 0)return e(i);let o=e(r(),a===void 0?void 0:{equals:a});for(let e of n)t.addEventListener(e,()=>o.set(r()));return o}export function online(){return r??=o(typeof window>`u`?void 0:window,[`online`,`offline`],()=>navigator.onLine,!0),r}export function visible(){return i??=o(typeof document>`u`?void 0:document,[`visibilitychange`],()=>document.visibilityState===`visible`,!0),i}export function windowSize(){return a??=o(typeof window>`u`?void 0:window,[`resize`],()=>({width:window.innerWidth,height:window.innerHeight}),t,n),a}
|
|
2
|
+
//# sourceMappingURL=page.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page.js","sources":["../../src/page.ts"],"sourcesContent":["/**\n * The page's own state as signals shared by everyone who asks: whether it\n * is online, whether it is visible, and the window's size.\n */\nimport { type ReadableSignal, signal } from \"zerolith\";\nimport { NONE, type Size, sameSize } from \"./size.js\";\n\nlet onlineSignal: ReadableSignal<boolean> | undefined;\nlet visibleSignal: ReadableSignal<boolean> | undefined;\nlet windowSizeSignal: ReadableSignal<Size> | undefined;\n\n/** A signal of what `read` gives, read again on each of `events` from `target`; a still signal of `still` where there is no target, as on the server. */\nfunction following<T>(target: EventTarget | undefined, events: readonly string[], read: () => T, still: T, equals?: (a: T, b: T) => boolean): ReadableSignal<T>\n{\n if (target === undefined)\n {\n return signal(still);\n }\n const value = signal(read(), equals === undefined ? undefined : { equals });\n for (const event of events)\n {\n target.addEventListener(event, () => value.set(read()));\n }\n return value;\n}\n\n/** Whether the browser is online, as `navigator.onLine` says, following the `online` and `offline` events; one signal for the page, started when first asked for, and true where there is no browser. */\nexport function online(): ReadableSignal<boolean>\n{\n onlineSignal ??= following(typeof window === \"undefined\" ? undefined : window, [\"online\", \"offline\"], () => navigator.onLine, true);\n return onlineSignal;\n}\n\n/** Whether the page is visible, following `visibilitychange`; one signal for the page, started when first asked for, and true where there is no document. */\nexport function visible(): ReadableSignal<boolean>\n{\n visibleSignal ??= following(typeof document === \"undefined\" ? undefined : document, [\"visibilitychange\"], () => document.visibilityState === \"visible\", true);\n return visibleSignal;\n}\n\n/** The window's inner size, following `resize`, which a browser fires once per frame; one signal for the page, started when first asked for, and no size where there is no window. */\nexport function windowSize(): ReadableSignal<Size>\n{\n windowSizeSignal ??= following(typeof window === \"undefined\" ? undefined : window, [\"resize\"], () => ({ width: window.innerWidth, height: window.innerHeight }), NONE, sameSize);\n return windowSizeSignal;\n}\n"],"names":["onlineSignal","visibleSignal","windowSizeSignal","following","target","signal","still","value","read","equals","event","events","NONE","sameSize"],"mappings":"AAIA,OAA8B,gBAAc,WAC5C,OAAS,UAAiB,kBAAgB,YAE1C,IAAIA,EACAC,EACAC,EAGJ,SAASC,EAAa,EAAiC,EAA2B,EAAe,EAAU,EAAgC,CAEvI,GAAIC,IAAW,OAEX,OAAOC,EAAOC,CAAK,EAEvB,IAAMC,EAAQF,EAAOG,EAAI,EAAIC,IAAW,OAAY,OAAY,CAAE,QAAM,CAAE,EAC1E,IAAK,IAAMC,KAASC,EAEhBP,EAAO,iBAAiBM,MAAaH,EAAM,IAAIC,EAAI,CAAE,CAAC,EAE1D,OAAOD,CACX,CAGA,OAAM,SAAU,QAAM,CAGlB,MADA,KAAiBJ,EAAU,OAAO,OAAW,IAAc,OAAY,OAAQ,CAAC,SAAU,SAAS,MAAS,UAAU,OAAQ,EAAI,EAC3HH,CACX,CAGA,OAAM,SAAU,SAAO,CAGnB,MADA,KAAkBG,EAAU,OAAO,SAAa,IAAc,OAAY,SAAU,CAAC,kBAAkB,MAAS,SAAS,kBAAoB,UAAW,EAAI,EACrJF,CACX,CAGA,OAAM,SAAU,YAAU,CAGtB,MADA,KAAqBE,EAAU,OAAO,OAAW,IAAc,OAAY,OAAQ,CAAC,QAAQ,OAAU,CAAE,MAAO,OAAO,WAAY,OAAQ,OAAO,WAAW,GAAKS,EAAMC,CAAQ,EACxKX,CACX"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{effect as e,hasOwner as t,onDispose as n,signal as r}from"zerolith";export const NONE={width:0,height:0};export function sameSize(e,t){return e.width===t.width&&e.height===t.height}const i=new Map;let a;function o(e){for(let t of e){let e=i.get(t.target);if(e!==void 0){let n={width:t.contentRect.width,height:t.contentRect.height};for(let t of e)t(n)}}}function s(e,t){a??=new ResizeObserver(o);let n=i.get(e);return n===void 0&&(n=new Set,i.set(e,n),a.observe(e)),n.add(t),()=>{n.delete(t),n.size===0&&(i.delete(e),a?.unobserve(e))}}function c(e){if(!e.isConnected)return NONE;let t=getComputedStyle(e);return{width:Math.max(0,e.clientWidth-parseFloat(t.paddingLeft)-parseFloat(t.paddingRight)),height:Math.max(0,e.clientHeight-parseFloat(t.paddingTop)-parseFloat(t.paddingBottom))}}export function size(i){let a=r(NONE,{equals:sameSize});if(typeof ResizeObserver>`u`)return a;let o=e=>a.set(e);if(i instanceof Element){a.set(c(i));let e=s(i,o);return t()&&n(e),a}return e(()=>{let e=i.get();if(e===void 0)a.set(NONE);else return a.set(c(e)),s(e,o)}),a}
|
|
2
|
+
//# sourceMappingURL=size.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"size.js","sources":["../../src/size.ts"],"sourcesContent":["/**\n * An element's content box as a signal, kept by one ResizeObserver shared\n * by every measured element.\n */\nimport { type ReadableSignal, effect, hasOwner, onDispose, signal } from \"zerolith\";\n\n/** A width and a height in CSS pixels. */\nexport interface Size\n{\n /** The width in CSS pixels. */\n readonly width: number;\n /** The height in CSS pixels. */\n readonly height: number;\n}\n\n/** No size: what an element has before it is measured, and what the server has. */\nexport const NONE: Size = { width: 0, height: 0 };\n\n/** True when two sizes are the same, so a resize to the same size changes nothing. */\nexport function sameSize(a: Size, b: Size): boolean\n{\n return a.width === b.width && a.height === b.height;\n}\n\n/** Who follows each element's content box, so one observer serves every measured element. */\nconst listeners = new Map<Element, Set<(box: Size) => void>>();\nlet observer: ResizeObserver | undefined;\n\n/** Hands each reported element's content box to those following it. */\nfunction report(entries: ResizeObserverEntry[]): void\n{\n for (const entry of entries)\n {\n const set = listeners.get(entry.target);\n if (set !== undefined)\n {\n const box = { width: entry.contentRect.width, height: entry.contentRect.height };\n for (const listener of set)\n {\n listener(box);\n }\n }\n }\n}\n\n/** Starts `listener` on `element`'s content box through the shared observer and returns what stops it, which unobserves the element once nobody follows it. */\nfunction watch(element: Element, listener: (box: Size) => void): () => void\n{\n observer ??= new ResizeObserver(report);\n let set = listeners.get(element);\n if (set === undefined)\n {\n set = new Set();\n listeners.set(element, set);\n observer.observe(element);\n }\n set.add(listener);\n return () =>\n {\n set.delete(listener);\n if (set.size === 0)\n {\n listeners.delete(element);\n observer?.unobserve(element);\n }\n };\n}\n\n/** The content box of `element` from its layout now, the client box less the padding, or no size while it is not in the document. */\nfunction contentBox(element: Element): Size\n{\n if (!element.isConnected)\n {\n return NONE;\n }\n const style = getComputedStyle(element);\n return {\n width: Math.max(0, element.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight)),\n height: Math.max(0, element.clientHeight - parseFloat(style.paddingTop) - parseFloat(style.paddingBottom))\n };\n}\n\n/**\n * The content box of `target` as a signal: the element itself, or the\n * signal a `ref` fills, which measures the element it holds and is no size\n * while it holds none. An element not yet in the document is no size\n * until the observer first reports it. One ResizeObserver, shared by every\n * measured element, keeps the signal current; it stops with the enclosing\n * owner when there is one and lives for the page otherwise. Where there is\n * no observer, as on the server, the signal is no size.\n */\nexport function size(target: Element | ReadableSignal<Element | undefined>): ReadableSignal<Size>\n{\n const value = signal(NONE, { equals: sameSize });\n if (typeof ResizeObserver === \"undefined\")\n {\n return value;\n }\n const follow = (box: Size): void => value.set(box);\n if (target instanceof Element)\n {\n value.set(contentBox(target));\n const stop = watch(target, follow);\n if (hasOwner())\n {\n onDispose(stop);\n }\n return value;\n }\n effect(() =>\n {\n const element = target.get();\n if (element === undefined)\n {\n value.set(NONE);\n return undefined;\n }\n value.set(contentBox(element));\n return watch(element, follow);\n });\n return value;\n}\n"],"names":["a","b","listeners","observer","report","entry","entries","set","box","listener","watch","element","contentBox","style","value","signal","follow","target","stop","hasOwner","onDispose","effect"],"mappings":"AAIA,OAA8B,YAAQ,cAAU,eAAW,gBAAc,WAYzE,OAAO,MAAM,KAAa,CAAE,MAAO,EAAG,OAAQ,CAAC,EAG/C,OAAM,SAAU,SAAS,EAAS,EAAO,CAErC,OAAOA,EAAE,QAAUC,EAAE,OAASD,EAAE,SAAWC,EAAE,MACjD,CAGA,MAAMC,EAAY,IAAI,IACtB,IAAIC,EAGJ,SAASC,EAAO,EAA8B,CAE1C,IAAK,IAAMC,KAASC,EACpB,CACI,IAAMC,EAAML,EAAU,IAAIG,EAAM,MAAM,EACtC,GAAIE,IAAQ,OACZ,CACI,IAAMC,EAAM,CAAE,MAAOH,EAAM,YAAY,MAAO,OAAQA,EAAM,YAAY,MAAM,EAC9E,IAAK,IAAMI,KAAYF,EAEnBE,EAASD,CAAG,CAEpB,CACJ,CACJ,CAGA,SAASE,EAAM,EAAkB,EAA6B,CAE1D,IAAa,IAAI,eAAeN,CAAM,EACtC,IAAIG,EAAML,EAAU,IAAIS,CAAO,EAQ/B,OAPIJ,IAAQ,SAER,EAAM,IAAI,IACVL,EAAU,IAAIS,EAASJ,CAAG,EAC1BJ,EAAS,QAAQQ,CAAO,GAE5BJ,EAAI,IAAIE,CAAQ,MACJ,CAERF,EAAI,OAAOE,CAAQ,EACfF,EAAI,OAAS,IAEbL,EAAU,OAAOS,CAAO,EACxBR,GAAU,UAAUQ,CAAO,EAEnC,CACJ,CAGA,SAASC,EAAW,EAAgB,CAEhC,GAAI,CAACD,EAAQ,YAET,OAAO,KAEX,IAAME,EAAQ,iBAAiBF,CAAO,EACtC,MAAO,CACH,MAAO,KAAK,IAAI,EAAGA,EAAQ,YAAc,WAAWE,EAAM,WAAW,EAAI,WAAWA,EAAM,YAAY,CAAC,EACvG,OAAQ,KAAK,IAAI,EAAGF,EAAQ,aAAe,WAAWE,EAAM,UAAU,EAAI,WAAWA,EAAM,aAAa,CAAC,CAC5G,CACL,CAWA,OAAM,SAAU,KAAK,EAAqD,CAEtE,IAAMC,EAAQC,EAAO,KAAM,CAAE,OAAQ,QAAQ,CAAE,EAC/C,GAAI,OAAO,eAAmB,IAE1B,OAAOD,EAEX,IAAME,EAAU,GAAoBF,EAAM,IAAIN,CAAG,EACjD,GAAIS,aAAkB,QACtB,CACIH,EAAM,IAAIF,EAAWK,CAAM,CAAC,EAC5B,IAAMC,EAAOR,EAAMO,EAAQD,CAAM,EAKjC,OAJIG,EAAQ,GAERC,EAAUF,CAAI,EAEXJ,CACX,CAYA,OAXAO,MAAY,CAER,IAAMV,EAAUM,EAAO,IAAG,EAC1B,GAAIN,IAAY,OAEZG,EAAM,IAAI,IAAI,OAIlB,OADAA,EAAM,IAAIF,EAAWD,CAAO,CAAC,EACtBD,EAAMC,EAASK,CAAM,CAChC,CAAC,EACMF,CACX"}
|
package/dist/size.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An element's content box as a signal, kept by one ResizeObserver shared
|
|
3
|
+
* by every measured element.
|
|
4
|
+
*/
|
|
5
|
+
import { type ReadableSignal } from "zerolith";
|
|
6
|
+
/** A width and a height in CSS pixels. */
|
|
7
|
+
export interface Size {
|
|
8
|
+
/** The width in CSS pixels. */
|
|
9
|
+
readonly width: number;
|
|
10
|
+
/** The height in CSS pixels. */
|
|
11
|
+
readonly height: number;
|
|
12
|
+
}
|
|
13
|
+
/** No size: what an element has before it is measured, and what the server has. */
|
|
14
|
+
export declare const NONE: Size;
|
|
15
|
+
/** True when two sizes are the same, so a resize to the same size changes nothing. */
|
|
16
|
+
export declare function sameSize(a: Size, b: Size): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* The content box of `target` as a signal: the element itself, or the
|
|
19
|
+
* signal a `ref` fills, which measures the element it holds and is no size
|
|
20
|
+
* while it holds none. An element not yet in the document is no size
|
|
21
|
+
* until the observer first reports it. One ResizeObserver, shared by every
|
|
22
|
+
* measured element, keeps the signal current; it stops with the enclosing
|
|
23
|
+
* owner when there is one and lives for the page otherwise. Where there is
|
|
24
|
+
* no observer, as on the server, the signal is no size.
|
|
25
|
+
*/
|
|
26
|
+
export declare function size(target: Element | ReadableSignal<Element | undefined>): ReadableSignal<Size>;
|
|
27
|
+
//# sourceMappingURL=size.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"size.d.ts","sourceRoot":"","sources":["../src/size.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,KAAK,cAAc,EAAuC,MAAM,UAAU,CAAC;AAEpF,0CAA0C;AAC1C,MAAM,WAAW,IAAI;IAEjB,+BAA+B;IAC/B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,gCAAgC;IAChC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CAC3B;AAED,mFAAmF;AACnF,eAAO,MAAM,IAAI,EAAE,IAA8B,CAAC;AAElD,sFAAsF;AACtF,wBAAgB,QAAQ,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,IAAI,GAAG,OAAO,CAGlD;AA4DD;;;;;;;;GAQG;AACH,wBAAgB,IAAI,CAAC,MAAM,EAAE,OAAO,GAAG,cAAc,CAAC,OAAO,GAAG,SAAS,CAAC,GAAG,cAAc,CAAC,IAAI,CAAC,CA8BhG"}
|
package/dist/size.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An element's content box as a signal, kept by one ResizeObserver shared
|
|
3
|
+
* by every measured element.
|
|
4
|
+
*/
|
|
5
|
+
import { effect, hasOwner, onDispose, signal } from "zerolith";
|
|
6
|
+
/** No size: what an element has before it is measured, and what the server has. */
|
|
7
|
+
export const NONE = { width: 0, height: 0 };
|
|
8
|
+
/** True when two sizes are the same, so a resize to the same size changes nothing. */
|
|
9
|
+
export function sameSize(a, b) {
|
|
10
|
+
return a.width === b.width && a.height === b.height;
|
|
11
|
+
}
|
|
12
|
+
/** Who follows each element's content box, so one observer serves every measured element. */
|
|
13
|
+
const listeners = new Map();
|
|
14
|
+
let observer;
|
|
15
|
+
/** Hands each reported element's content box to those following it. */
|
|
16
|
+
function report(entries) {
|
|
17
|
+
for (const entry of entries) {
|
|
18
|
+
const set = listeners.get(entry.target);
|
|
19
|
+
if (set !== undefined) {
|
|
20
|
+
const box = { width: entry.contentRect.width, height: entry.contentRect.height };
|
|
21
|
+
for (const listener of set) {
|
|
22
|
+
listener(box);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** Starts `listener` on `element`'s content box through the shared observer and returns what stops it, which unobserves the element once nobody follows it. */
|
|
28
|
+
function watch(element, listener) {
|
|
29
|
+
observer ??= new ResizeObserver(report);
|
|
30
|
+
let set = listeners.get(element);
|
|
31
|
+
if (set === undefined) {
|
|
32
|
+
set = new Set();
|
|
33
|
+
listeners.set(element, set);
|
|
34
|
+
observer.observe(element);
|
|
35
|
+
}
|
|
36
|
+
set.add(listener);
|
|
37
|
+
return () => {
|
|
38
|
+
set.delete(listener);
|
|
39
|
+
if (set.size === 0) {
|
|
40
|
+
listeners.delete(element);
|
|
41
|
+
observer?.unobserve(element);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** The content box of `element` from its layout now, the client box less the padding, or no size while it is not in the document. */
|
|
46
|
+
function contentBox(element) {
|
|
47
|
+
if (!element.isConnected) {
|
|
48
|
+
return NONE;
|
|
49
|
+
}
|
|
50
|
+
const style = getComputedStyle(element);
|
|
51
|
+
return {
|
|
52
|
+
width: Math.max(0, element.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight)),
|
|
53
|
+
height: Math.max(0, element.clientHeight - parseFloat(style.paddingTop) - parseFloat(style.paddingBottom))
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* The content box of `target` as a signal: the element itself, or the
|
|
58
|
+
* signal a `ref` fills, which measures the element it holds and is no size
|
|
59
|
+
* while it holds none. An element not yet in the document is no size
|
|
60
|
+
* until the observer first reports it. One ResizeObserver, shared by every
|
|
61
|
+
* measured element, keeps the signal current; it stops with the enclosing
|
|
62
|
+
* owner when there is one and lives for the page otherwise. Where there is
|
|
63
|
+
* no observer, as on the server, the signal is no size.
|
|
64
|
+
*/
|
|
65
|
+
export function size(target) {
|
|
66
|
+
const value = signal(NONE, { equals: sameSize });
|
|
67
|
+
if (typeof ResizeObserver === "undefined") {
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
const follow = (box) => value.set(box);
|
|
71
|
+
if (target instanceof Element) {
|
|
72
|
+
value.set(contentBox(target));
|
|
73
|
+
const stop = watch(target, follow);
|
|
74
|
+
if (hasOwner()) {
|
|
75
|
+
onDispose(stop);
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
effect(() => {
|
|
80
|
+
const element = target.get();
|
|
81
|
+
if (element === undefined) {
|
|
82
|
+
value.set(NONE);
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
value.set(contentBox(element));
|
|
86
|
+
return watch(element, follow);
|
|
87
|
+
});
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=size.js.map
|
package/dist/size.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"size.js","sourceRoot":"","sources":["../src/size.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAuB,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAWpF,mFAAmF;AACnF,MAAM,CAAC,MAAM,IAAI,GAAS,EAAE,KAAK,EAAE,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CAAC,CAAC;QAC9B,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACnC,IAAI,QAAQ,EAAE,EACd,CAAC;YACG,SAAS,CAAC,IAAI,CAAC,CAAC;QACpB,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IACD,MAAM,CAAC,GAAG,EAAE;QAER,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,EAAE,CAAC;QAC7B,IAAI,OAAO,KAAK,SAAS,EACzB,CAAC;YACG,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YAChB,OAAO,SAAS,CAAC;QACrB,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAC;QAC/B,OAAO,KAAK,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAClC,CAAC,CAAC,CAAC;IACH,OAAO,KAAK,CAAC;AACjB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@zerolith/browser",
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "The page as signals for zerolith: whether it is online and visible and the window's size, shared by the page, and an element's size and presence in view",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": "molexxxx",
|
|
7
|
+
"homepage": "https://zerolith.dev/",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/molexxxx/zerolith.git",
|
|
11
|
+
"directory": "packages/browser"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/molexxxx/zerolith/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"zerolith",
|
|
18
|
+
"signals",
|
|
19
|
+
"browser",
|
|
20
|
+
"resize",
|
|
21
|
+
"intersection",
|
|
22
|
+
"visibility"
|
|
23
|
+
],
|
|
24
|
+
"type": "module",
|
|
25
|
+
"sideEffects": false,
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=24.0.0"
|
|
28
|
+
},
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"development": "./dist/index.js",
|
|
33
|
+
"default": "./dist/production/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"src",
|
|
40
|
+
"LICENSE",
|
|
41
|
+
"README.md"
|
|
42
|
+
],
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsc -p tsconfig.build.json && node ../../scripts/production.ts"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"zerolith": "^1.2.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"zerolith": "^1.2.0"
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @zerolith/browser: the page as signals. Whether it is online and visible
|
|
3
|
+
* and the window's size are one signal each for the page; an element's
|
|
4
|
+
* size and whether it is in view are one per element, kept by observers
|
|
5
|
+
* that go with the enclosing owner.
|
|
6
|
+
*/
|
|
7
|
+
export type { Size } from "./size.js";
|
|
8
|
+
export { size } from "./size.js";
|
|
9
|
+
export type { InViewOptions } from "./inview.js";
|
|
10
|
+
export { inView } from "./inview.js";
|
|
11
|
+
export { online, visible, windowSize } from "./page.js";
|
package/src/inview.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether an element is in view, as a signal kept by an
|
|
3
|
+
* IntersectionObserver.
|
|
4
|
+
*/
|
|
5
|
+
import { type DiagnosticArgs, type ReadableSignal, ZerolithError, diagnostic, effect, hasOwner, onDispose, signal } from "zerolith";
|
|
6
|
+
|
|
7
|
+
/** Options for `inView`. */
|
|
8
|
+
export interface InViewOptions
|
|
9
|
+
{
|
|
10
|
+
/** Space around the viewport that counts as in view, as an IntersectionObserver's `rootMargin`; "200px" starts loading a little ahead of the scroll. */
|
|
11
|
+
rootMargin?: string;
|
|
12
|
+
/** How much of the element must show, from 0 for any part of it to 1 for all of it. Defaults to 0. */
|
|
13
|
+
threshold?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The error for options an IntersectionObserver refuses. */
|
|
17
|
+
function invalid(args: DiagnosticArgs): ZerolithError
|
|
18
|
+
{
|
|
19
|
+
return new ZerolithError(diagnostic("in_view_options_invalid", "error", args));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Whether `target` intersects the viewport, as a signal: the element
|
|
24
|
+
* itself, or the signal a `ref` fills, which follows the element it holds
|
|
25
|
+
* and is false while it holds none. An IntersectionObserver keeps it
|
|
26
|
+
* current; it stops with the enclosing owner when there is one and lives
|
|
27
|
+
* for the page otherwise. The signal is false until the observer first
|
|
28
|
+
* reports, and where there is no observer, as on the server. A threshold
|
|
29
|
+
* outside 0 to 1, or a root margin the browser refuses, fails with
|
|
30
|
+
* `in_view_options_invalid`.
|
|
31
|
+
*/
|
|
32
|
+
export function inView(target: Element | ReadableSignal<Element | undefined>, options: InViewOptions = {}): ReadableSignal<boolean>
|
|
33
|
+
{
|
|
34
|
+
const threshold = options.threshold ?? 0;
|
|
35
|
+
const value = signal(false);
|
|
36
|
+
if (typeof IntersectionObserver === "undefined")
|
|
37
|
+
{
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
let current: Element | undefined = target instanceof Element ? target : undefined;
|
|
41
|
+
const init: IntersectionObserverInit = { threshold };
|
|
42
|
+
if (options.rootMargin !== undefined)
|
|
43
|
+
{
|
|
44
|
+
init.rootMargin = options.rootMargin;
|
|
45
|
+
}
|
|
46
|
+
let observer: IntersectionObserver;
|
|
47
|
+
try
|
|
48
|
+
{
|
|
49
|
+
observer = new IntersectionObserver((entries) =>
|
|
50
|
+
{
|
|
51
|
+
const entry = entries.findLast((candidate) => candidate.target === current);
|
|
52
|
+
if (entry !== undefined)
|
|
53
|
+
{
|
|
54
|
+
value.set(entry.isIntersecting);
|
|
55
|
+
}
|
|
56
|
+
}, init);
|
|
57
|
+
}
|
|
58
|
+
catch
|
|
59
|
+
{
|
|
60
|
+
throw invalid({ threshold: String(threshold), rootMargin: options.rootMargin ?? "" });
|
|
61
|
+
}
|
|
62
|
+
if (hasOwner())
|
|
63
|
+
{
|
|
64
|
+
onDispose(() => observer.disconnect());
|
|
65
|
+
}
|
|
66
|
+
if (target instanceof Element)
|
|
67
|
+
{
|
|
68
|
+
observer.observe(target);
|
|
69
|
+
return value;
|
|
70
|
+
}
|
|
71
|
+
effect(() =>
|
|
72
|
+
{
|
|
73
|
+
const element = target.get();
|
|
74
|
+
current = element;
|
|
75
|
+
value.set(false);
|
|
76
|
+
if (element === undefined)
|
|
77
|
+
{
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
observer.observe(element);
|
|
81
|
+
return () => observer.unobserve(element);
|
|
82
|
+
});
|
|
83
|
+
return value;
|
|
84
|
+
}
|
package/src/page.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page's own state as signals shared by everyone who asks: whether it
|
|
3
|
+
* is online, whether it is visible, and the window's size.
|
|
4
|
+
*/
|
|
5
|
+
import { type ReadableSignal, signal } from "zerolith";
|
|
6
|
+
import { NONE, type Size, sameSize } from "./size.js";
|
|
7
|
+
|
|
8
|
+
let onlineSignal: ReadableSignal<boolean> | undefined;
|
|
9
|
+
let visibleSignal: ReadableSignal<boolean> | undefined;
|
|
10
|
+
let windowSizeSignal: ReadableSignal<Size> | undefined;
|
|
11
|
+
|
|
12
|
+
/** A signal of what `read` gives, read again on each of `events` from `target`; a still signal of `still` where there is no target, as on the server. */
|
|
13
|
+
function following<T>(target: EventTarget | undefined, events: readonly string[], read: () => T, still: T, equals?: (a: T, b: T) => boolean): ReadableSignal<T>
|
|
14
|
+
{
|
|
15
|
+
if (target === undefined)
|
|
16
|
+
{
|
|
17
|
+
return signal(still);
|
|
18
|
+
}
|
|
19
|
+
const value = signal(read(), equals === undefined ? undefined : { equals });
|
|
20
|
+
for (const event of events)
|
|
21
|
+
{
|
|
22
|
+
target.addEventListener(event, () => value.set(read()));
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Whether the browser is online, as `navigator.onLine` says, following the `online` and `offline` events; one signal for the page, started when first asked for, and true where there is no browser. */
|
|
28
|
+
export function online(): ReadableSignal<boolean>
|
|
29
|
+
{
|
|
30
|
+
onlineSignal ??= following(typeof window === "undefined" ? undefined : window, ["online", "offline"], () => navigator.onLine, true);
|
|
31
|
+
return onlineSignal;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Whether the page is visible, following `visibilitychange`; one signal for the page, started when first asked for, and true where there is no document. */
|
|
35
|
+
export function visible(): ReadableSignal<boolean>
|
|
36
|
+
{
|
|
37
|
+
visibleSignal ??= following(typeof document === "undefined" ? undefined : document, ["visibilitychange"], () => document.visibilityState === "visible", true);
|
|
38
|
+
return visibleSignal;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The window's inner size, following `resize`, which a browser fires once per frame; one signal for the page, started when first asked for, and no size where there is no window. */
|
|
42
|
+
export function windowSize(): ReadableSignal<Size>
|
|
43
|
+
{
|
|
44
|
+
windowSizeSignal ??= following(typeof window === "undefined" ? undefined : window, ["resize"], () => ({ width: window.innerWidth, height: window.innerHeight }), NONE, sameSize);
|
|
45
|
+
return windowSizeSignal;
|
|
46
|
+
}
|
package/src/size.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An element's content box as a signal, kept by one ResizeObserver shared
|
|
3
|
+
* by every measured element.
|
|
4
|
+
*/
|
|
5
|
+
import { type ReadableSignal, effect, hasOwner, onDispose, signal } from "zerolith";
|
|
6
|
+
|
|
7
|
+
/** A width and a height in CSS pixels. */
|
|
8
|
+
export interface Size
|
|
9
|
+
{
|
|
10
|
+
/** The width in CSS pixels. */
|
|
11
|
+
readonly width: number;
|
|
12
|
+
/** The height in CSS pixels. */
|
|
13
|
+
readonly height: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** No size: what an element has before it is measured, and what the server has. */
|
|
17
|
+
export const NONE: Size = { width: 0, height: 0 };
|
|
18
|
+
|
|
19
|
+
/** True when two sizes are the same, so a resize to the same size changes nothing. */
|
|
20
|
+
export function sameSize(a: Size, b: Size): boolean
|
|
21
|
+
{
|
|
22
|
+
return a.width === b.width && a.height === b.height;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Who follows each element's content box, so one observer serves every measured element. */
|
|
26
|
+
const listeners = new Map<Element, Set<(box: Size) => void>>();
|
|
27
|
+
let observer: ResizeObserver | undefined;
|
|
28
|
+
|
|
29
|
+
/** Hands each reported element's content box to those following it. */
|
|
30
|
+
function report(entries: ResizeObserverEntry[]): void
|
|
31
|
+
{
|
|
32
|
+
for (const entry of entries)
|
|
33
|
+
{
|
|
34
|
+
const set = listeners.get(entry.target);
|
|
35
|
+
if (set !== undefined)
|
|
36
|
+
{
|
|
37
|
+
const box = { width: entry.contentRect.width, height: entry.contentRect.height };
|
|
38
|
+
for (const listener of set)
|
|
39
|
+
{
|
|
40
|
+
listener(box);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Starts `listener` on `element`'s content box through the shared observer and returns what stops it, which unobserves the element once nobody follows it. */
|
|
47
|
+
function watch(element: Element, listener: (box: Size) => void): () => void
|
|
48
|
+
{
|
|
49
|
+
observer ??= new ResizeObserver(report);
|
|
50
|
+
let set = listeners.get(element);
|
|
51
|
+
if (set === undefined)
|
|
52
|
+
{
|
|
53
|
+
set = new Set();
|
|
54
|
+
listeners.set(element, set);
|
|
55
|
+
observer.observe(element);
|
|
56
|
+
}
|
|
57
|
+
set.add(listener);
|
|
58
|
+
return () =>
|
|
59
|
+
{
|
|
60
|
+
set.delete(listener);
|
|
61
|
+
if (set.size === 0)
|
|
62
|
+
{
|
|
63
|
+
listeners.delete(element);
|
|
64
|
+
observer?.unobserve(element);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The content box of `element` from its layout now, the client box less the padding, or no size while it is not in the document. */
|
|
70
|
+
function contentBox(element: Element): Size
|
|
71
|
+
{
|
|
72
|
+
if (!element.isConnected)
|
|
73
|
+
{
|
|
74
|
+
return NONE;
|
|
75
|
+
}
|
|
76
|
+
const style = getComputedStyle(element);
|
|
77
|
+
return {
|
|
78
|
+
width: Math.max(0, element.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight)),
|
|
79
|
+
height: Math.max(0, element.clientHeight - parseFloat(style.paddingTop) - parseFloat(style.paddingBottom))
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The content box of `target` as a signal: the element itself, or the
|
|
85
|
+
* signal a `ref` fills, which measures the element it holds and is no size
|
|
86
|
+
* while it holds none. An element not yet in the document is no size
|
|
87
|
+
* until the observer first reports it. One ResizeObserver, shared by every
|
|
88
|
+
* measured element, keeps the signal current; it stops with the enclosing
|
|
89
|
+
* owner when there is one and lives for the page otherwise. Where there is
|
|
90
|
+
* no observer, as on the server, the signal is no size.
|
|
91
|
+
*/
|
|
92
|
+
export function size(target: Element | ReadableSignal<Element | undefined>): ReadableSignal<Size>
|
|
93
|
+
{
|
|
94
|
+
const value = signal(NONE, { equals: sameSize });
|
|
95
|
+
if (typeof ResizeObserver === "undefined")
|
|
96
|
+
{
|
|
97
|
+
return value;
|
|
98
|
+
}
|
|
99
|
+
const follow = (box: Size): void => value.set(box);
|
|
100
|
+
if (target instanceof Element)
|
|
101
|
+
{
|
|
102
|
+
value.set(contentBox(target));
|
|
103
|
+
const stop = watch(target, follow);
|
|
104
|
+
if (hasOwner())
|
|
105
|
+
{
|
|
106
|
+
onDispose(stop);
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
effect(() =>
|
|
111
|
+
{
|
|
112
|
+
const element = target.get();
|
|
113
|
+
if (element === undefined)
|
|
114
|
+
{
|
|
115
|
+
value.set(NONE);
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
value.set(contentBox(element));
|
|
119
|
+
return watch(element, follow);
|
|
120
|
+
});
|
|
121
|
+
return value;
|
|
122
|
+
}
|