@pfern/elements 0.1.10 → 0.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/src/helpers.js ADDED
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Enable additional debug warnings.
3
+ *
4
+ * Set `process.env.ELEMENTSJS_DEBUG=true` (or `NODE_ENV=development`) to enable
5
+ * warnings for common mistakes like passive event handler returns.
6
+ */
7
+ export { DEBUG } from './core/elements.js'
8
+
9
+ /**
10
+ * Wrap a recursive pure function so it participates in replacement updates.
11
+ *
12
+ * Call the returned function again with new arguments to compute the next UI.
13
+ *
14
+ * @template {any[]} Args
15
+ * @param {(...args: Args) => any[]} fn
16
+ * @returns {(...args: Args) => any[]}
17
+ */
18
+ export { component } from './core/elements.js'
19
+
20
+ /**
21
+ * A map of all HTML/SVG tag helpers (plus `fragment`).
22
+ */
23
+ export { elements } from './core/elements.js'
24
+
25
+ /**
26
+ * Render a vnode into the DOM.
27
+ *
28
+ * This is typically called once on page load. After that, events that return
29
+ * vnodes perform replacement updates automatically.
30
+ *
31
+ * @param {any[]} vtree
32
+ * @param {HTMLElement | null} [container]
33
+ */
34
+ export { render } from './core/elements.js'
35
+
36
+ /**
37
+ * Serialize a vnode tree to an HTML string (SSR/SSG).
38
+ *
39
+ * This is the “stringification” counterpart to `render()`: it walks the same
40
+ * declarative vnode arrays and produces static HTML for build-time prerendering
41
+ * or server-side rendering.
42
+ *
43
+ * @param {*} vnode
44
+ * @returns {string}
45
+ */
46
+ export { toHtmlString } from './ssr.js'
47
+
48
+ /**
49
+ * Navigate to a URL path and update the window history object.
50
+ *
51
+ * This dispatches a `popstate` event after updating history so router-style
52
+ * apps can react without additional plumbing.
53
+ *
54
+ * @param {string} [path]
55
+ * @param {{ replace?: boolean }} [options]
56
+ */
57
+ export const navigate = (path, { replace = false } = {}) => {
58
+ if (typeof window === 'undefined') return
59
+
60
+ const url = new URL(path, window.location.origin)
61
+ const isSame =
62
+ url.pathname === window.location.pathname
63
+ && url.search === window.location.search
64
+ && url.hash === window.location.hash
65
+
66
+ if (isSame) return
67
+
68
+ const fn = replace ? 'replaceState' : 'pushState'
69
+ window.history[fn]({}, '', url)
70
+
71
+ try { window.dispatchEvent(new PopStateEvent('popstate')) }
72
+ catch { window.dispatchEvent(new Event('popstate')) }
73
+ }