ripple 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,356 @@
1
+ # Ripple
2
+
3
+ > Currently, this project is still in early development, and should not be used in production.
4
+
5
+ Ripple is a TypeScript UI framework for the web.
6
+
7
+ I wrote Ripple as a love letter for frontend web – and this is largely a project that I built in less than a week, so it's very raw.
8
+
9
+ Personally, I ([@trueadm](https://github.com/trueadm)) have been involved in some truly amazing frontend frameworks along their journeys – from [Inferno](https://github.com/infernojs/inferno), where it all began, to [React](https://github.com/facebook/react) and the journey of React Hooks, to creating [Lexical](https://github.com/facebook/lexical), to [Svelte 5](https://github.com/sveltejs/svelte) and its new compiler and signal-based reactivity runtime. Along that journey, I collected ideas, and intriguing thoughts that may or may not pay off. Given my time between roles, I decided it was the best opportunity to try them out, and for open source to see what I was cooking.
10
+
11
+ Ripple was designed to be a JS/TS-first framework, rather than HTML-first. Ripple modules have their own `.ripple` extension and these modules
12
+ fully support TypeScript. By introducing a new extension, it affords Ripple to invent its own superset language, that plays really nicely with
13
+ TypeScript and JSX, but with a few interesting touches. In my experience, this has led to better DX not only for humans, but also for LLMs.
14
+
15
+ Right now, there will be plenty of bugs, things just won't work either and you'll find TODOs everywhere. At this stage, Ripple is more of an early alpha version of something that _might_ be, rather than something you should try and adopt. If anything, maybe some of the ideas can be shared and incubated back into other frameworks. There's also a lot of similarities with Svelte 5, and that's not by accident, that's because of my recent time working on Svelte 5.
16
+
17
+ ## Features
18
+
19
+ - **Reactive State Management**: Built-in reactivity with `$` prefixed variables
20
+ - **Component-Based Architecture**: Clean, reusable components with props and children
21
+ - **JSX-like Syntax**: Familiar templating with Ripple-specific enhancements
22
+ - **TypeScript Support**: Full TypeScript integration with type checking
23
+ - **VSCode Integration**: Rich editor support with diagnostics, syntax highlighting, and IntelliSense
24
+
25
+ ## Missing Features
26
+
27
+ - **SSR**: Ripple is currently an SPA only, this is because I haven't gotten around to it
28
+ - **Testing & Types**: The codebase is very raw with limited types (I've opted for JavaScript only to avoid build problems). There aren't any tests either – I've been using the `playground` directory to manually test things as I go
29
+
30
+ ## Quick Start
31
+
32
+ ### Installation
33
+
34
+ ```bash
35
+ pnpm i --save ripple
36
+ ```
37
+
38
+ You'll also need Vite and Ripple's Vite plugin to compile Ripple:
39
+
40
+ ```bash
41
+ pnpm i --save-dev vite-plugin-ripple
42
+ ```
43
+
44
+ You can see a working example in the [playground demo app](https://github.com/trueadm/ripple/tree/main/playground).
45
+
46
+ ### Mounting your app
47
+
48
+ You can use the `mount` API from the `ripple` package to render your Ripple component, using the `target`
49
+ option to specify what DOM element you want to render the component.
50
+
51
+ ```ts
52
+ // index.ts
53
+ import { mount } from 'ripple';
54
+ import { App } from '/App.ripple';
55
+
56
+ mount(App, {
57
+ props: {
58
+ title: 'Hello world!'
59
+ },
60
+ target: document.getElementById('root')
61
+ });
62
+ ```
63
+
64
+ ## Key Concepts
65
+
66
+ ### Components
67
+
68
+ Define reusable components with the `component` keyword. These are similar to functions in that they have `props`, but crucially,
69
+ they allow for a JSX-like syntax to be defined alongside standard TypeScript. That means you do not _return JSX_ like in other frameworks,
70
+ but you instead use it like a JavaScript statement, as shown:
71
+
72
+ ```ripple
73
+ component Button(props: { text: string, onClick: () => void }) {
74
+ <button onClick={props.onClick}>
75
+ {props.text}
76
+ </button>
77
+ }
78
+
79
+ // Usage
80
+ <Button text="Click me" onClick={() => console.log("Clicked!")} />
81
+ ```
82
+
83
+ ### Reactive Variables
84
+
85
+ Variables prefixed with `$` are automatically reactive:
86
+
87
+ ```ripple
88
+ let $name = "World";
89
+ let $count = 0;
90
+
91
+ // Updates automatically trigger re-renders
92
+ $count++;
93
+ ```
94
+
95
+ Object properties prefixed with `$` are also automatically reactive:
96
+
97
+ ```ripple
98
+ let counter = { $current: 0 };
99
+
100
+ // Updates automatically trigger re-renders
101
+ counter.$current++;
102
+ ```
103
+
104
+ Derived values are simply `$` variables that combined different parts of state:
105
+
106
+ ```ripple
107
+ let $count = 0;
108
+ let $double = $count * 2;
109
+ let $quadruple = $double * 2;
110
+ ```
111
+
112
+ That means `$count` itself might be derived if it were to reference another reactive property. For example:
113
+
114
+ ```ripple
115
+ component Counter({ $startingCount }) {
116
+ let $count = $startingCount;
117
+ let $double = $count * 2;
118
+ let $quadruple = $double * 2;
119
+ }
120
+ ```
121
+
122
+ Now given `$startingCount` is reactive, it would mean that `$count` might reset each time an incoming change to `$startingCount` occurs. That might not be desirable, so Ripple provides a way to `untrack` reactivity in those cases:
123
+
124
+ ```ripple
125
+ import { untrack } from 'ripple';
126
+
127
+ component Counter({ $startingCount }) {
128
+ let $count = untrack(() => $startingCount);
129
+ let $double = $count * 2;
130
+ let $quadruple = $double * 2;
131
+ }
132
+ ```
133
+
134
+ Now `$count` will only reactively create its value on initialization.
135
+
136
+ > Note: you cannot define reactive variables in module/global scope, they have to be created on access from an active component
137
+
138
+ ### Effects
139
+
140
+ When dealing with reactive state, you might want to be able to create side-effects based upon changes that happen upon updates.
141
+ To do this, you can use `effect`:
142
+
143
+ ```ripple
144
+ import { effect } from 'ripple';
145
+
146
+ component App() {
147
+ let $count = 0;
148
+
149
+ effect(() => {
150
+ console.log($count);
151
+ });
152
+
153
+ <button onClick={() => $count++}>Increment</button>
154
+ }
155
+ ```
156
+
157
+ ### Control flow
158
+
159
+ The JSX-like syntax might take some time to get used to if you're coming from another framework. For one, templating in Ripple
160
+ can only occur _inside_ a `component` body – you can't create JSX inside functions, or assign it to variables as an expression.
161
+
162
+ ```ripple
163
+ <div>
164
+ // you can create variables inside the template!
165
+ const str = "hello world";
166
+
167
+ console.log(str); // and function calls too!
168
+
169
+ debugger; // you can put breakpoints anywhere to help debugging!
170
+
171
+ {str}
172
+ </div>
173
+ ```
174
+
175
+ Note that strings inside the template need to be inside `{"string"}`, you can't do `<div>hello</div>` as Ripple
176
+ has no idea if `hello` is a string or maybe some JavaScript code that needs evaluating, so just ensure you wrap them
177
+ in curly braces. This shouldn't be an issue in the real-world anyway, as you'll likely use an i18n library that means
178
+ using JavaScript expressions regardless.
179
+
180
+ ### If statements
181
+
182
+ If blocks work seemlessly with Ripple's templating language, you can put them inside the JSX-like
183
+ statements, making control-flow far easier to read and reason with.
184
+
185
+ ```ripple
186
+ component Truthy({ x }) {
187
+ <div>
188
+ if (x) {
189
+ <span>
190
+ {"x is truthy"}
191
+ </span>
192
+ } else {
193
+ <span>
194
+ {"x is truthy"}
195
+ </span>
196
+ }
197
+ </div>
198
+ }
199
+ ```
200
+
201
+ ### For statements
202
+
203
+ You can render collections using a `for...of` block, and you don't need to specify a `key` prop like
204
+ other frameworks.
205
+
206
+ ```ripple
207
+ component ListView({ title, items }) {
208
+ <h2>{title}</h2>
209
+ <ul>
210
+ for (const item of items) {
211
+ <li>{item.text}</li>
212
+ }
213
+ </ul>
214
+ }
215
+ ```
216
+
217
+ ### Try statements
218
+
219
+ Try blocks work to building the foundation for **error boundaries**, when the runtime encounters
220
+ an error in the `try` block, you can easily render a fallback in the `catch` block.
221
+
222
+ ```ripple
223
+ import { reportError } from 'some-library';
224
+
225
+ component ErrorBoundary() {
226
+ <div>
227
+ try {
228
+ <ComponentThatFails />
229
+ } catch (e) {
230
+ reportError(e);
231
+
232
+ <div>{"An error occured! " + e.message}</div>
233
+ }
234
+ </div>
235
+ }
236
+ ```
237
+
238
+ ### Props
239
+
240
+ If you want a prop to be reactive, you should also give it a `$` prefix.
241
+
242
+ ```ripple
243
+ component Button(props: { $text: string, onClick: () => void }) {
244
+ <button onClick={props.onClick}>
245
+ {props.$text}
246
+ </button>
247
+ }
248
+
249
+ // Usage
250
+ <Button $text={some_text} onClick={() => console.log("Clicked!")} />
251
+ ```
252
+
253
+ ### Children
254
+
255
+ Use `$children` prop and the `<$component />` directive for component composition.
256
+
257
+ When you pass in children to a component, it gets implicitly passed as the `$children` prop, in the form of a component.
258
+
259
+ ```ripple
260
+ import type { Component } from 'ripple';
261
+
262
+ component Card(props: { $children: Component }) {
263
+ <div class="card">
264
+ <$component />
265
+ </div>
266
+ }
267
+
268
+ // Usage
269
+ <Card>
270
+ <p>{"Card content here"}</p>
271
+ </Card>
272
+ ```
273
+
274
+ You could also explicitly write the same code as shown:
275
+
276
+ ```ripple
277
+ import type { Component } from 'ripple';
278
+
279
+ component Card(props: { $children: Component }) {
280
+ <div class="card">
281
+ <$component />
282
+ </div>
283
+ }
284
+
285
+ // Usage with explicit component
286
+ <Card>
287
+ component $children() {
288
+ <p>{"Card content here"}</p>
289
+ }
290
+ </Card>
291
+ ```
292
+
293
+ ### Events
294
+
295
+ Like React, events are props that start with `on` and then continue with an uppercase character, such as:
296
+
297
+ - `onClick`
298
+ - `onPointerMove`
299
+ - `onPointerDown`
300
+ - `onKeyDown`
301
+
302
+ For `capture` phase events, just add `Capture` to the end of the prop name:
303
+
304
+ - `onClickCapture`
305
+ - `onPointerMoveCapture`
306
+ - `onPointerDownCapture`
307
+ - `onKeyDownCapture`
308
+
309
+ > Note: Some events are automatically delegated where possible by Ripple to improve runtime performance.
310
+
311
+ ### Styling
312
+
313
+ Ripple supports native CSS styling that is localized to the given component using the `<style>` element.
314
+
315
+ ```ripple
316
+ component MyComponent() {
317
+ <div class="container">
318
+ <h1>{"Hello World"}</h1>
319
+ </div>
320
+
321
+ <style>
322
+ .container {
323
+ background: blue;
324
+ padding: 1rem;
325
+ }
326
+
327
+ h1 {
328
+ color: white;
329
+ font-size: 2rem;
330
+ }
331
+ </style>
332
+ }
333
+ ```
334
+
335
+ > Note: the `<style>` element must be top-level within a `component`.
336
+
337
+ ## VSCode Extension
338
+
339
+ The Ripple VSCode extension provides:
340
+
341
+ - **Syntax Highlighting** for `.ripple` files
342
+ - **Real-time Diagnostics** for compilation errors
343
+ - **TypeScript Integration** for type checking
344
+ - **IntelliSense** for autocompletion
345
+
346
+ Clone the repository, and manually install the extension from the `packages/ripple-vscode-plugin/` directory.
347
+
348
+ ## Playground
349
+
350
+ Feel free to play around with how Ripple works. If you clone the repo, you can then:
351
+
352
+ ```bash
353
+ pnpm i && cd playground && pnpm dev
354
+ ```
355
+
356
+ The playground uses Ripple's Vite plugin, where you can play around with things inside the `playground/src` directory.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Ripple is a TypeScript UI framework for the web",
4
4
  "license": "MIT",
5
5
  "author": "Dominic Gannaway",
6
- "version": "0.2.2",
6
+ "version": "0.2.3",
7
7
  "type": "module",
8
8
  "module": "src/runtime/index.js",
9
9
  "main": "src/runtime/index.js",
@@ -345,11 +345,9 @@ const visitors = {
345
345
  );
346
346
 
347
347
  if (is_spreading) {
348
- if (spread_attributes.length === 0) {
349
- state.template.push(attr_value);
350
- } else {
351
- spread_attributes.push(b.prop('init', b.literal(name), attr_value));
352
- }
348
+ spread_attributes.push(b.prop('init', b.literal(name), attr_value));
349
+ } else {
350
+ state.template.push(attr_value);
353
351
  }
354
352
  };
355
353