### RippleJS For Loops Source: https://context7_llms Demonstrates the usage of for loops in RippleJS to iterate over arrays and render list items. Includes examples with and without an index. ```ripple component List({ items }) {
{"Card content here"}
{@message}
{'Loading...'}
break; // break is mandatory case 'success':{'Success!'}
break; case 'error':{'Error!'}
break; default:{'Unknown status'}
// No break needed for default } } import { track } from 'ripple'; component InteractiveStatus() { let status = track('loading'); // Reactive state // This switch block will update automatically when '@status' changes switch (@status) { case 'loading':{'Status: Loading...'}
break; case 'success':{'Status: Success!'}
break; case 'error':{'Status: Error!'}
break; default:{'Status: Unknown'}
} } // ❌ WRONG - Missing 'break' will cause a compilation error switch (value) { case 1:{'Case 1'}
case 2:{'Case 2'}
break; } ``` -------------------------------- ### Dynamic Elements in RippleJS Source: https://context7_llms Shows how to dynamically change HTML elements in RippleJS. The example toggles between a div and a span element when a button is clicked, demonstrating dynamic tag usage. ```ripple export component App() { let tag = track('div'); <@tag class="dynamic">{'Hello World'}@tag> } ``` -------------------------------- ### DOM References (Refs) in RippleJS Source: https://context7_llms Shows how to capture DOM element references in RippleJS using the ref syntax. The example includes both named and inline ref usage. ```ripple export component App() { let div = track(); const divRef = (node) => { @div = node; console.log("mounted", node); return () => { @div = undefined; console.log("unmounted", node); }; };{message}
} // ✅ CORRECT - Helper functions return data, not templates function getMessage() { return "Hello from function"; // Return data, not JSX } component App() {{'This content escapes the normal component tree.'}
{"Size: "}{mySet.size}
// Reactive size{"Has 2: "}{mySet.has(2)}
{"Direct usage: map has an item with key 2: "}{map.has(2)}
// reactive assignment let has = track(() => map.has(2));{"Assigned usage: map has an item with key 2: "}{@has}
} ``` -------------------------------- ### Create and Use TrackedObject in RippleJS Source: https://context7_llms Illustrates creating a TrackedObject using syntactic sugar or constructors and how its root-level properties are reactive. Supports standard Object properties and methods. ```ripple import { TrackedObject } from 'ripple'; // using syntactic sugar `#` const arr = #{a: 1, b: 2, c: 3}; // using the new constructor const arr = new TrackedObject({a: 1, b: 2, c: 3}); ``` ```ripple export component App() { const obj = #{a: 0} obj.a = 0;{'obj.a is: '}{obj.a}
{'obj.b is: '}{obj.b}
}
```
--------------------------------
### Manage Reactivity with Untrack and Track (Ripple)
Source: https://context7_llms
Illustrates fine-grained reactivity in Ripple.js using `track` for reactive variables and `untrack` to prevent effects from re-running when dependencies are accessed within it. It shows how to create tracked variables and effects.
```ripple
import { untrack, track, effect } from 'ripple';
let count = track(0);
let double = track(() => @count * 2);
let quadruple = track(() => @double * 2);
effect(() => {
// This effect will never fire again, as we've untracked the only dependency it has
console.log(untrack(() => @quadruple));
})
```
--------------------------------
### Ripple Component Definition
Source: https://context7_llms
Demonstrates how to define components in Ripple using the `component` keyword. Components are not functions returning JSX, but rather blocks of code with a specific structure. Includes basic usage with props and event handling.
```ripple
component Button(props: { text: string, onClick: () => void }) {
}
// Usage
export component App() {