@opsydyn/astro-foldkit 0.2.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/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # Changelog
2
+
3
+ ## [0.2.1](https://github.com/opsydyn/fold-kit-experiments/compare/astro-foldkit-v0.2.0...astro-foldkit-v0.2.1) (2026-06-12)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * correct repository url for npm provenance ([723fc7f](https://github.com/opsydyn/fold-kit-experiments/commit/723fc7f932ab6bbfd077b2a49df4904624ff89d2))
9
+
10
+ ## [0.2.0](https://github.com/opsydyn/fold-kit-experiments/compare/astro-foldkit-v0.1.0...astro-foldkit-v0.2.0) (2026-06-12)
11
+
12
+
13
+ ### Features
14
+
15
+ * :arrow_up: bump foldkit to 0.108.0 and migrate runtime API ([7441538](https://github.com/opsydyn/fold-kit-experiments/commit/744153844de11b096e2434e7e678d1149238d1b0))
16
+ * :rotating_light: add Effect language service and linteffect rules ([c2fad21](https://github.com/opsydyn/fold-kit-experiments/commit/c2fad2198616a0df333bb41e8fe0aaa9af9c433c))
17
+ * :sparkles: add prop to remove foldkit meta data optionally ([2ae7989](https://github.com/opsydyn/fold-kit-experiments/commit/2ae798977b7e78706bf329bef394f6e1604e2f73))
18
+ * :sparkles: add props to fold kit ([de04151](https://github.com/opsydyn/fold-kit-experiments/commit/de04151cd0bbe54c1fc4e36af34836fb5c39d4c3))
19
+ * :sparkles: charts updates ([cd052d9](https://github.com/opsydyn/fold-kit-experiments/commit/cd052d9e0bccd7994ec2f4283fb3e2ccff9bc030))
20
+ * :sparkles: foldkit upgrade ([cafc368](https://github.com/opsydyn/fold-kit-experiments/commit/cafc36852352e66c7446e06701eaa87103279ff0))
21
+ * :sparkles: mono repo refactor ([18079de](https://github.com/opsydyn/fold-kit-experiments/commit/18079de599b3848a50b89bfe897ad47f50f43919))
22
+ * :sparkles: prepare package for npm ([3d5850b](https://github.com/opsydyn/fold-kit-experiments/commit/3d5850bf536b2d10ae1483bd108a2eda89de321a))
23
+ * :sparkles: tests for package ([086808d](https://github.com/opsydyn/fold-kit-experiments/commit/086808daafdc5331609fcf34fa1734d7d2d5b28f))
24
+
25
+
26
+ ### Bug Fixes
27
+
28
+ * :white_check_mark: resolve all biome lint errors ([b09399d](https://github.com/opsydyn/fold-kit-experiments/commit/b09399dc0c9e68a2cca0fc53c0a1b59c2b517c90))
package/README.md ADDED
@@ -0,0 +1,245 @@
1
+ # @opsydyn/astro-foldkit
2
+
3
+ Astro integration and renderer for [FoldKit](https://foldkit.dev).
4
+
5
+ FoldKit is an Elm Architecture runtime built on [Effect](https://effect.website). This package registers FoldKit as an Astro renderer so you can drop any FoldKit app into a `.astro` page as a component and hydrate it with `client:load`.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install @opsydyn/astro-foldkit
11
+ # peer deps
12
+ npm install astro foldkit
13
+ ```
14
+
15
+ ## Setup
16
+
17
+ Add the integration to `astro.config.ts`:
18
+
19
+ ```ts
20
+ import { defineConfig } from 'astro/config'
21
+ import foldkit from '@opsydyn/astro-foldkit'
22
+
23
+ export default defineConfig({
24
+ integrations: [foldkit()],
25
+ })
26
+ ```
27
+
28
+ ## Defining an app
29
+
30
+ Use `defineApp` to register a FoldKit app for lazy loading. The loader returns your `main.ts` module which must export a value that satisfies `AppConfig`.
31
+
32
+ ```ts
33
+ // src/apps/counter/app.ts
34
+ import { defineApp } from '@opsydyn/astro-foldkit/define-app'
35
+
36
+ export default defineApp(() => import('./main'))
37
+ ```
38
+
39
+ ```ts
40
+ // src/apps/counter/main.ts
41
+ export const Model = null
42
+ export const init = () => [0, []] as const
43
+ export const update = (model: number, message: 'Inc' | 'Dec') =>
44
+ [message === 'Inc' ? model + 1 : model - 1, []] as const
45
+ export const view = (model: number) => ({ /* foldkit view tree */ })
46
+ ```
47
+
48
+ Use the app in an Astro page:
49
+
50
+ ```astro
51
+ ---
52
+ import Counter from '../apps/counter/app'
53
+ ---
54
+ <Counter client:load />
55
+ ```
56
+
57
+ ## Passing props
58
+
59
+ `defineApp` accepts a type parameter for the props your FoldKit app expects. This makes the component callable with typed attributes in `.astro` files.
60
+
61
+ ```ts
62
+ // src/apps/greeting/app.ts
63
+ import { defineApp } from '@opsydyn/astro-foldkit/define-app'
64
+ import type { Name } from './model'
65
+
66
+ export default defineApp<{ name: Name }>(() => import('./main'))
67
+ ```
68
+
69
+ Props are forwarded from Astro's `<astro-island>` serialisation into your `init` function. Declare `init` to accept `props: unknown` and validate at the boundary with Effect Schema:
70
+
71
+ ```ts
72
+ // src/apps/greeting/model.ts
73
+ import { Schema } from 'effect'
74
+
75
+ export const Name = Schema.String.pipe(Schema.brand('Name'))
76
+ export type Name = typeof Name.Type
77
+
78
+ const Props = Schema.Struct({ name: Name })
79
+
80
+ export const init = (props: unknown): readonly [Model, readonly []] => {
81
+ const { name } = Schema.decodeUnknownSync(Props)(props)
82
+ return [name, []]
83
+ }
84
+ ```
85
+
86
+ Pass the branded value from the Astro page:
87
+
88
+ ```astro
89
+ ---
90
+ import { Schema } from 'effect'
91
+ import GreetingApp from '../apps/greeting/app'
92
+ import { Name } from '../apps/greeting/model'
93
+
94
+ const name = Schema.decodeSync(Name)(Astro.url.searchParams.get('name') ?? 'World')
95
+ ---
96
+ <GreetingApp client:load name={name} />
97
+ ```
98
+
99
+ The TypeScript types flow end-to-end: the `Name` brand is required at the Astro call site, and `Schema.decodeUnknownSync` re-validates the serialised value at the client hydration boundary.
100
+
101
+ ## Built-in props
102
+
103
+ The renderer intercepts one reserved prop before forwarding anything to your app. It is never seen by `init`, `update`, or `view`.
104
+
105
+ ### `noMeta`
106
+
107
+ Prevents the FoldKit program from overwriting `document.title`. Use this whenever you embed an app as an island on a page that manages its own title — without it, the island's view tree will update the browser tab title on every render tick.
108
+
109
+ ```astro
110
+ <DashboardChart client:load noMeta data={chartData} />
111
+ ```
112
+
113
+ Both `noMeta` and `noMeta={true}` are accepted. The prop is stripped before `init` receives the rest of your props.
114
+
115
+ ## Architecture
116
+
117
+ ### Imperative shell, functional core
118
+
119
+ The Astro page is the **imperative shell**: it performs side effects (HTTP fetches, query-param reads, URL parsing) and validates raw values into branded types via `Schema.decodeSync`. The shell hands only clean, typed values into the component.
120
+
121
+ The FoldKit app is the **functional core**: its `init`, `update`, and `view` functions are pure. They receive already-validated props, run a closed message loop, and produce a view tree — with no side effects outside of declared `Command`s and `Subscription`s.
122
+
123
+ ### Data flow
124
+
125
+ ```mermaid
126
+ flowchart TD
127
+ subgraph shell ["Astro SSR (imperative shell)"]
128
+ A[fetch / query params] --> B["Schema.decodeSync(Brand)"]
129
+ B --> C[branded prop]
130
+ end
131
+
132
+ subgraph island ["Astro serialisation"]
133
+ C --> D["&lt;astro-island props='...'&gt;"]
134
+ end
135
+
136
+ subgraph hydration ["Client hydration — client.ts"]
137
+ D --> E["config.init(props)"]
138
+ E --> F["Schema.decodeUnknownSync(Props)"]
139
+ F --> G[initial Model]
140
+ end
141
+
142
+ subgraph core ["FoldKit runtime (functional core)"]
143
+ G --> H[update loop]
144
+ H --> I[view]
145
+ I --> J[DOM patch]
146
+ K["Subscription\n(animationFrame, events)"] --> H
147
+ end
148
+ ```
149
+
150
+ The double-decode is intentional: `Schema.decodeSync` at the Astro boundary ensures the page cannot render with invalid data; `Schema.decodeUnknownSync` at the FoldKit boundary re-validates after JSON round-trip through the island serialisation, so the functional core never receives unverified input.
151
+
152
+ ## AppConfig
153
+
154
+ The module returned by your loader must export:
155
+
156
+ | Export | Type | Description |
157
+ | :------- | :------------------------------------------------------------- | :---------------------------------------------- |
158
+ | `Model` | `unknown` | Initial model type marker |
159
+ | `init` | `(props: unknown) => readonly [Model, ReadonlyArray<Command>]` | Initial state from props and startup commands |
160
+ | `update` | `(model, message) => readonly [Model, ReadonlyArray<Command>]` | Pure state transition |
161
+ | `view` | `(model) => Document` | Render the current model to a FoldKit view tree |
162
+
163
+ ## Exports
164
+
165
+ | Entry point | Description |
166
+ | :---------------------------------- | :-------------------------------------- |
167
+ | `@opsydyn/astro-foldkit` | Default Astro integration (`foldkit()`) |
168
+ | `@opsydyn/astro-foldkit/define-app` | `defineApp` helper and `AppConfig` type |
169
+
170
+ ## Embedding
171
+
172
+ `Runtime.embed` gives you a typed handle to the running program so you can clean it up on unmount and push live data in without remounting. This integration uses `embed` internally — the `astro:unmount` event calls `handle.dispose()` to tear down subscriptions and animation-frame loops when Astro navigates away.
173
+
174
+ ### Declaring ports
175
+
176
+ Ports are typed channels declared in your app's `main.ts`. Pass a `Schema` for each port so values are validated at the boundary.
177
+
178
+ ```ts
179
+ // src/apps/dashboard/main.ts
180
+ import { Port } from 'foldkit'
181
+ import { Schema } from 'effect'
182
+
183
+ export const ports = {
184
+ inbound: {
185
+ data: Port.inbound(Schema.Array(DataPointSchema)),
186
+ },
187
+ outbound: {
188
+ selection: Port.outbound(Schema.NullOr(Schema.String)),
189
+ },
190
+ }
191
+ ```
192
+
193
+ Pass the ports map to `makeApplication` alongside your `init`, `update`, and `view`:
194
+
195
+ ```ts
196
+ import { Runtime } from 'foldkit'
197
+
198
+ const program = Runtime.makeApplication({
199
+ Model,
200
+ init,
201
+ update,
202
+ view,
203
+ container,
204
+ devTools: false,
205
+ ports,
206
+ })
207
+ ```
208
+
209
+ ### Live prop updates
210
+
211
+ Once embedded, push new data through an inbound port instead of remounting the component. This is particularly useful in Astro View Transition flows where the page shell re-runs but the island should preserve its running state:
212
+
213
+ ```ts
214
+ const handle = Runtime.embed(program)
215
+
216
+ // Called when Astro soft-navigates back to this page with new data
217
+ handle.ports.data.send(nextDataPoints)
218
+ ```
219
+
220
+ ### Outbound subscriptions
221
+
222
+ Subscribe to outbound ports to react to events inside the program from the host page. Port names are flat on `handle.ports` regardless of whether they were declared under `inbound` or `outbound`:
223
+
224
+ ```ts
225
+ const unsubscribe = handle.ports.selection.subscribe((id) => {
226
+ // sync selection state to the URL or another island
227
+ history.replaceState({}, '', `?selected=${id ?? ''}`)
228
+ })
229
+
230
+ element.addEventListener('astro:unmount', () => {
231
+ unsubscribe()
232
+ handle.dispose()
233
+ }, { once: true })
234
+ ```
235
+
236
+ ## Peer dependencies
237
+
238
+ | Package | Version |
239
+ | :-------- | :--------- |
240
+ | `astro` | `≥ 5.0` |
241
+ | `foldkit` | `≥ 0.108` |
242
+
243
+ ## License
244
+
245
+ MIT
@@ -0,0 +1,8 @@
1
+ import { n as FoldkitApp } from "./types-CuEHzdpH.mjs";
2
+
3
+ //#region src/client.d.ts
4
+ declare const _default: (element: HTMLElement) => (component: FoldkitApp, props: Record<string, unknown>, _slots: Record<string, unknown>, _meta: {
5
+ client: string;
6
+ }) => Promise<void>;
7
+ //#endregion
8
+ export { _default as default };
@@ -0,0 +1,32 @@
1
+ import { Runtime } from "foldkit";
2
+ //#region src/client-helpers.ts
3
+ /** Returns true when the `noMeta` prop should suppress document metadata writes. */
4
+ const shouldSkipMetadata = (props) => props.noMeta === true || props.noMeta === "";
5
+ /**
6
+ * Wraps a foldkit view function so the Document it returns always carries
7
+ * `initialTitle` as its title, preventing the runtime from overwriting
8
+ * `document.title` when the app is embedded as an island on a page that
9
+ * owns its own title.
10
+ */
11
+ const makeNoMetaView = (view, initialTitle) => (model) => ({
12
+ ...view(model),
13
+ title: initialTitle
14
+ });
15
+ //#endregion
16
+ //#region src/client.ts
17
+ var client_default = (element) => async (component, props, _slots, _meta) => {
18
+ const config = await component.load();
19
+ element.id ||= element.getAttribute("uid") ?? crypto.randomUUID();
20
+ const baseView = config.view;
21
+ const view = shouldSkipMetadata(props) ? makeNoMetaView(baseView, document.title) : baseView;
22
+ const program = Runtime.makeApplication({
23
+ ...config,
24
+ init: () => config.init(props),
25
+ view,
26
+ container: element
27
+ });
28
+ const handle = Runtime.embed(program);
29
+ element.addEventListener("astro:unmount", () => handle.dispose(), { once: true });
30
+ };
31
+ //#endregion
32
+ export { client_default as default };
@@ -0,0 +1,6 @@
1
+ import { n as FoldkitApp, t as AppConfig } from "./types-CuEHzdpH.mjs";
2
+
3
+ //#region src/define-app.d.ts
4
+ declare function defineApp<Props extends Record<string, unknown> = Record<string, unknown>>(load: () => Promise<AppConfig>): FoldkitApp<Props>;
5
+ //#endregion
6
+ export { type AppConfig, type FoldkitApp, defineApp };
@@ -0,0 +1,9 @@
1
+ //#region src/define-app.ts
2
+ function defineApp(load) {
3
+ return Object.assign((_props) => {}, {
4
+ __foldkit: true,
5
+ load
6
+ });
7
+ }
8
+ //#endregion
9
+ export { defineApp };
@@ -0,0 +1,6 @@
1
+ import { AstroIntegration } from "astro";
2
+
3
+ //#region src/index.d.ts
4
+ declare function foldkit(): AstroIntegration;
5
+ //#endregion
6
+ export { foldkit as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,18 @@
1
+ import { fileURLToPath } from "node:url";
2
+ import { foldkit as foldkit$1 } from "@foldkit/vite-plugin";
3
+ //#region src/index.ts
4
+ function foldkit() {
5
+ return {
6
+ name: "astro-foldkit",
7
+ hooks: { "astro:config:setup": ({ addRenderer, updateConfig }) => {
8
+ addRenderer({
9
+ name: "astro-foldkit",
10
+ clientEntrypoint: fileURLToPath(new URL("./client.mjs", import.meta.url)),
11
+ serverEntrypoint: fileURLToPath(new URL("./server.mjs", import.meta.url))
12
+ });
13
+ updateConfig({ vite: { plugins: [foldkit$1()] } });
14
+ } }
15
+ };
16
+ }
17
+ //#endregion
18
+ export { foldkit as default };
@@ -0,0 +1,7 @@
1
+ import { NamedSSRLoadedRendererValue } from "astro";
2
+
3
+ //#region src/server.d.ts
4
+ declare function check(Component: unknown): Promise<boolean>;
5
+ declare const renderer: NamedSSRLoadedRendererValue;
6
+ //#endregion
7
+ export { check, renderer as default };
@@ -0,0 +1,15 @@
1
+ //#region src/server.ts
2
+ async function check(Component) {
3
+ return Component != null && (typeof Component === "object" || typeof Component === "function") && "__foldkit" in Component && Component.__foldkit === true;
4
+ }
5
+ async function renderToStaticMarkup() {
6
+ return { html: "" };
7
+ }
8
+ const renderer = {
9
+ name: "astro-foldkit",
10
+ check,
11
+ renderToStaticMarkup,
12
+ supportsAstroStaticSlot: true
13
+ };
14
+ //#endregion
15
+ export { check, renderer as default };
@@ -0,0 +1,14 @@
1
+ //#region src/types.d.ts
2
+ type AppConfig = {
3
+ readonly Model: unknown;
4
+ readonly init: (...args: ReadonlyArray<unknown>) => readonly [unknown, ReadonlyArray<unknown>];
5
+ readonly update: (model: any, message: any) => readonly [any, ReadonlyArray<any>];
6
+ readonly view: (model: any) => unknown;
7
+ };
8
+ type FoldkitApp<Props extends Record<string, unknown> = Record<string, unknown>> = {
9
+ (props?: Props): void;
10
+ readonly __foldkit: true;
11
+ readonly load: () => Promise<AppConfig>;
12
+ };
13
+ //#endregion
14
+ export { FoldkitApp as n, AppConfig as t };
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "@opsydyn/astro-foldkit",
3
+ "version": "0.2.1",
4
+ "description": "Astro integration and renderer for FoldKit.",
5
+ "publishConfig": {
6
+ "access": "public"
7
+ },
8
+ "sideEffects": false,
9
+ "type": "module",
10
+ "main": "./dist/index.mjs",
11
+ "types": "./dist/index.d.mts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.mts",
15
+ "import": "./dist/index.mjs"
16
+ },
17
+ "./define-app": {
18
+ "types": "./dist/define-app.d.mts",
19
+ "import": "./dist/define-app.mjs"
20
+ }
21
+ },
22
+ "typesVersions": {
23
+ "*": {
24
+ "define-app": [
25
+ "./dist/define-app.d.mts"
26
+ ]
27
+ }
28
+ },
29
+ "files": [
30
+ "CHANGELOG.md",
31
+ "dist",
32
+ "README.md"
33
+ ],
34
+ "scripts": {
35
+ "build": "tsdown",
36
+ "typecheck": "tsc --noEmit",
37
+ "test": "bun test",
38
+ "test:unit": "bun test test/unit",
39
+ "test:integration": "bun test test/integration",
40
+ "check": "biome check .",
41
+ "check:fix": "biome check --write ."
42
+ },
43
+ "keywords": [
44
+ "astro",
45
+ "astro-integration",
46
+ "foldkit",
47
+ "elm-architecture",
48
+ "effect"
49
+ ],
50
+ "author": "Alan P Currie",
51
+ "license": "MIT",
52
+ "repository": {
53
+ "type": "git",
54
+ "url": "git+https://github.com/opsydyn/fold-kit-experiments.git",
55
+ "directory": "packages/astro-foldkit"
56
+ },
57
+ "bugs": {
58
+ "url": "https://github.com/opsydyn/fold-kit-experiments/issues"
59
+ },
60
+ "engines": {
61
+ "node": ">=22.0.0"
62
+ },
63
+ "peerDependencies": {
64
+ "astro": ">=5.0.0",
65
+ "foldkit": ">=0.108.0"
66
+ },
67
+ "dependencies": {
68
+ "@foldkit/vite-plugin": "^0.5.1"
69
+ },
70
+ "devDependencies": {
71
+ "@types/node": "^22.0.0",
72
+ "astro": "^7.0.0-alpha.1",
73
+ "bun-types": "^1.3.14",
74
+ "fast-check": "^4.8.0",
75
+ "foldkit": "^0.108.0",
76
+ "tsdown": "^0.21.8",
77
+ "typescript": "^6.0.3"
78
+ }
79
+ }