|
| 1 | +# Components |
| 2 | + |
| 3 | +Components are the building blocks of an OpenScript application. They encapsulate the UI and logic for a specific part of your application. |
| 4 | + |
| 5 | +There are two primary ways to create components: **Class Components** and **Functional Components**. |
| 6 | + |
| 7 | +## Class Components |
| 8 | + |
| 9 | +Class components are the most feature-rich way to define components. They extend the `Component` class provided by `modular-openscriptjs`. |
| 10 | + |
| 11 | +### Basic Structure |
| 12 | + |
| 13 | +```javascript |
| 14 | +import { Component, app, ojs, state } from "modular-openscriptjs"; |
| 15 | + |
| 16 | +const h = app("h"); |
| 17 | + |
| 18 | +export default class MyComponent extends Component { |
| 19 | + constructor() { |
| 20 | + super(); |
| 21 | + // Initialize state |
| 22 | + this.counter = state(0); |
| 23 | + } |
| 24 | + |
| 25 | + render(...args) { |
| 26 | + return h.div( |
| 27 | + h.h1("My Component"), |
| 28 | + h.p(`Count: ${this.counter.value}`), |
| 29 | + ...args, |
| 30 | + ); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// Register the component |
| 35 | +ojs(MyComponent); |
| 36 | +``` |
| 37 | + |
| 38 | +### Key Features |
| 39 | + |
| 40 | +1. **Extends `Component`**: Must extend the base class. |
| 41 | +2. **`render()` Method**: Must implement a `render` method that returns the markup (usually via `h` instance). |
| 42 | +3. **State Management**: Can hold local state using `state()`. |
| 43 | +4. **`ojs(MyComponent)`**: Registers the component with the framework. |
| 44 | +5. **Passing Arguments**: The `render` method receives `...args` which can contain parent elements, attributes, or other data passed during rendering. Always spread `...args` in your root element or handle them appropriately. |
| 45 | + |
| 46 | +## Functional Components |
| 47 | + |
| 48 | +Functional components are simpler and are best used for presentational components that don't require complex state management or lifecycle hooks. |
| 49 | + |
| 50 | +### Basic Structure |
| 51 | + |
| 52 | +```javascript |
| 53 | +import { app } from "modular-openscriptjs"; |
| 54 | + |
| 55 | +const h = app("h"); |
| 56 | + |
| 57 | +export default function MyFunctionalComponent(props = {}) { |
| 58 | + return h.div( |
| 59 | + { class: "card" }, |
| 60 | + h.h2(props.title || "Default Title"), |
| 61 | + h.p(props.content), |
| 62 | + ); |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +_Note: Functional components are just regular JavaScript functions that return valid OpenScript markup._ |
| 67 | + |
| 68 | +## Naming Conventions |
| 69 | + |
| 70 | +- **Capitalized Names**: Component names (both class and function) **MUST** start with a Capital letter (e.g., `MyComponent`, `UserProfile`). |
| 71 | +- **Kebab-case in DOM**: When rendered, OpenScript converts the class name to kebab-case for the custom element tag (e.g., `MyComponent` -> `<ojs-my-component>`). |
| 72 | + |
| 73 | +## Event Listening |
| 74 | + |
| 75 | +OpenScript provides a declarative way to listen to events on elements. |
| 76 | + |
| 77 | +### Using `listeners` Object |
| 78 | + |
| 79 | +When creating an element with `h`, you can pass a `listeners` object in the attributes. |
| 80 | + |
| 81 | +```javascript |
| 82 | +h.button( |
| 83 | + { |
| 84 | + class: "btn", |
| 85 | + listeners: { |
| 86 | + click: this.handleClick.bind(this), |
| 87 | + mouseover: (e) => console.log("Hovered", e), |
| 88 | + }, |
| 89 | + }, |
| 90 | + "Click Me", |
| 91 | +); |
| 92 | +``` |
| 93 | + |
| 94 | +### Method Binding |
| 95 | + |
| 96 | +For class components, it's common to define methods for event handlers. Remember to `.bind(this)` or use arrow functions to preserve the correct `this` context. |
| 97 | + |
| 98 | +```javascript |
| 99 | +export default class Counter extends Component { |
| 100 | + // ... |
| 101 | + |
| 102 | + increment() { |
| 103 | + this.count.value++; |
| 104 | + } |
| 105 | + |
| 106 | + render() { |
| 107 | + return h.button( |
| 108 | + { |
| 109 | + listeners: { |
| 110 | + click: this.increment.bind(this), // Binding is crucial |
| 111 | + }, |
| 112 | + }, |
| 113 | + "+", |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +### Special Event Methods |
| 120 | + |
| 121 | +If your component class defines methods starting with `$_`, OpenScript automatically treats them as event listeners for the component instance itself (lifecycle events). |
| 122 | + |
| 123 | +- `$_mounted()`: Called when the component is added to the DOM. |
| 124 | +- `$_rendered()`: Called when the component is rendered. |
0 commit comments