ruby_wasm_ui 0.8.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.
Files changed (99) hide show
  1. checksums.yaml +7 -0
  2. data/.cursor/rules/ruby_comments.mdc +29 -0
  3. data/.github/workflows/playwright.yml +74 -0
  4. data/.github/workflows/rspec.yml +33 -0
  5. data/.node-version +1 -0
  6. data/CODE_OF_CONDUCT.md +132 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +218 -0
  9. data/Rakefile +4 -0
  10. data/docs/conditional-rendering.md +119 -0
  11. data/docs/lifecycle-hooks.md +75 -0
  12. data/docs/list-rendering.md +51 -0
  13. data/examples/Gemfile +5 -0
  14. data/examples/Gemfile.lock +41 -0
  15. data/examples/Makefile +15 -0
  16. data/examples/npm-packages/runtime/counter/index.html +28 -0
  17. data/examples/npm-packages/runtime/counter/index.rb +62 -0
  18. data/examples/npm-packages/runtime/counter/index.spec.js +42 -0
  19. data/examples/npm-packages/runtime/hello/index.html +28 -0
  20. data/examples/npm-packages/runtime/hello/index.rb +29 -0
  21. data/examples/npm-packages/runtime/hello/index.spec.js +53 -0
  22. data/examples/npm-packages/runtime/input/index.html +28 -0
  23. data/examples/npm-packages/runtime/input/index.rb +46 -0
  24. data/examples/npm-packages/runtime/input/index.spec.js +58 -0
  25. data/examples/npm-packages/runtime/list/index.html +27 -0
  26. data/examples/npm-packages/runtime/list/index.rb +33 -0
  27. data/examples/npm-packages/runtime/list/index.spec.js +46 -0
  28. data/examples/npm-packages/runtime/on_mounted_demo/index.html +40 -0
  29. data/examples/npm-packages/runtime/on_mounted_demo/index.rb +59 -0
  30. data/examples/npm-packages/runtime/on_mounted_demo/index.spec.js +50 -0
  31. data/examples/npm-packages/runtime/r_if_attribute_demo/index.html +34 -0
  32. data/examples/npm-packages/runtime/r_if_attribute_demo/index.rb +113 -0
  33. data/examples/npm-packages/runtime/r_if_attribute_demo/index.spec.js +140 -0
  34. data/examples/npm-packages/runtime/random_cocktail/index.html +27 -0
  35. data/examples/npm-packages/runtime/random_cocktail/index.rb +69 -0
  36. data/examples/npm-packages/runtime/random_cocktail/index.spec.js +101 -0
  37. data/examples/npm-packages/runtime/search_field/index.html +27 -0
  38. data/examples/npm-packages/runtime/search_field/index.rb +39 -0
  39. data/examples/npm-packages/runtime/search_field/index.spec.js +59 -0
  40. data/examples/npm-packages/runtime/todos/index.html +28 -0
  41. data/examples/npm-packages/runtime/todos/index.rb +239 -0
  42. data/examples/npm-packages/runtime/todos/index.spec.js +161 -0
  43. data/examples/npm-packages/runtime/todos/todos_repository.rb +23 -0
  44. data/examples/package.json +12 -0
  45. data/examples/src/counter/index.html +23 -0
  46. data/examples/src/counter/index.rb +60 -0
  47. data/lib/ruby_wasm_ui +1 -0
  48. data/lib/ruby_wasm_ui.rb +1 -0
  49. data/package-lock.json +100 -0
  50. data/package.json +32 -0
  51. data/packages/npm-packages/runtime/Gemfile +3 -0
  52. data/packages/npm-packages/runtime/Gemfile.lock +26 -0
  53. data/packages/npm-packages/runtime/README.md +5 -0
  54. data/packages/npm-packages/runtime/eslint.config.mjs +16 -0
  55. data/packages/npm-packages/runtime/package-lock.json +6668 -0
  56. data/packages/npm-packages/runtime/package.json +38 -0
  57. data/packages/npm-packages/runtime/rollup.config.mjs +89 -0
  58. data/packages/npm-packages/runtime/spec/ruby_wasm_ui/component_spec.rb +416 -0
  59. data/packages/npm-packages/runtime/spec/ruby_wasm_ui/dom/scheduler_spec.rb +98 -0
  60. data/packages/npm-packages/runtime/spec/ruby_wasm_ui/nodes_equal_spec.rb +190 -0
  61. data/packages/npm-packages/runtime/spec/ruby_wasm_ui/template/build_conditional_group_spec.rb +505 -0
  62. data/packages/npm-packages/runtime/spec/ruby_wasm_ui/template/build_for_group_spec.rb +377 -0
  63. data/packages/npm-packages/runtime/spec/ruby_wasm_ui/template/build_vdom_spec.rb +573 -0
  64. data/packages/npm-packages/runtime/spec/ruby_wasm_ui/template/parser_spec.rb +627 -0
  65. data/packages/npm-packages/runtime/spec/ruby_wasm_ui/utils/arrays_spec.rb +228 -0
  66. data/packages/npm-packages/runtime/spec/ruby_wasm_ui/utils/objects_spec.rb +127 -0
  67. data/packages/npm-packages/runtime/spec/ruby_wasm_ui/utils/props_spec.rb +205 -0
  68. data/packages/npm-packages/runtime/spec/ruby_wasm_ui/utils/strings_spec.rb +107 -0
  69. data/packages/npm-packages/runtime/spec/spec_helper.rb +16 -0
  70. data/packages/npm-packages/runtime/src/__tests__/sample.test.js +5 -0
  71. data/packages/npm-packages/runtime/src/index.js +37 -0
  72. data/packages/npm-packages/runtime/src/ruby_wasm_ui/app.rb +53 -0
  73. data/packages/npm-packages/runtime/src/ruby_wasm_ui/component.rb +215 -0
  74. data/packages/npm-packages/runtime/src/ruby_wasm_ui/dispatcher.rb +46 -0
  75. data/packages/npm-packages/runtime/src/ruby_wasm_ui/dom/attributes.rb +105 -0
  76. data/packages/npm-packages/runtime/src/ruby_wasm_ui/dom/destroy_dom.rb +63 -0
  77. data/packages/npm-packages/runtime/src/ruby_wasm_ui/dom/events.rb +40 -0
  78. data/packages/npm-packages/runtime/src/ruby_wasm_ui/dom/mount_dom.rb +108 -0
  79. data/packages/npm-packages/runtime/src/ruby_wasm_ui/dom/patch_dom.rb +237 -0
  80. data/packages/npm-packages/runtime/src/ruby_wasm_ui/dom/scheduler.rb +51 -0
  81. data/packages/npm-packages/runtime/src/ruby_wasm_ui/dom.rb +13 -0
  82. data/packages/npm-packages/runtime/src/ruby_wasm_ui/nodes_equal.rb +45 -0
  83. data/packages/npm-packages/runtime/src/ruby_wasm_ui/template/build_conditional_group.rb +150 -0
  84. data/packages/npm-packages/runtime/src/ruby_wasm_ui/template/build_for_group.rb +125 -0
  85. data/packages/npm-packages/runtime/src/ruby_wasm_ui/template/build_vdom.rb +220 -0
  86. data/packages/npm-packages/runtime/src/ruby_wasm_ui/template/parser.rb +134 -0
  87. data/packages/npm-packages/runtime/src/ruby_wasm_ui/template.rb +11 -0
  88. data/packages/npm-packages/runtime/src/ruby_wasm_ui/utils/arrays.rb +185 -0
  89. data/packages/npm-packages/runtime/src/ruby_wasm_ui/utils/objects.rb +37 -0
  90. data/packages/npm-packages/runtime/src/ruby_wasm_ui/utils/props.rb +25 -0
  91. data/packages/npm-packages/runtime/src/ruby_wasm_ui/utils/strings.rb +19 -0
  92. data/packages/npm-packages/runtime/src/ruby_wasm_ui/utils.rb +11 -0
  93. data/packages/npm-packages/runtime/src/ruby_wasm_ui/vdom.rb +84 -0
  94. data/packages/npm-packages/runtime/src/ruby_wasm_ui/version.rb +5 -0
  95. data/packages/npm-packages/runtime/src/ruby_wasm_ui.rb +14 -0
  96. data/packages/npm-packages/runtime/vitest.config.js +8 -0
  97. data/playwright.config.js +78 -0
  98. data/sig/ruby_wasm_ui.rbs +4 -0
  99. metadata +168 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35c9a72b39d103d9d57bb41e60d69830966d45faa673c926cd1709ed991ad389
4
+ data.tar.gz: 62fd13431aff9e10a4bd29de95cc1457efd365004f9b82717207014658ea4204
5
+ SHA512:
6
+ metadata.gz: f8384fb1deb73e52d0ab70c8444b459716db2b9ea01094fa87ce557bd970c38360b6853e8c1005d95c232f35148e71311bc510badfdd94ed04ce9bf5bace2ba8
7
+ data.tar.gz: dea729cadcb8b017fe8a7772364af85c168f9bf816dda4bd030f0e237796007231dd4b286e708e586d8a6f5b66b75afce2743aa703ab86179bcbc5b161cdb7c2
@@ -0,0 +1,29 @@
1
+ When writing or modifying Ruby code:
2
+
3
+ 1. Comments and Documentation
4
+ - All comments MUST be written in English
5
+ - Use YARD documentation style for methods and classes
6
+ - Include @param and @return tags for method documentation
7
+ - Keep comments clear and concise
8
+
9
+ 2. Documentation Examples:
10
+
11
+ Good:
12
+ ```ruby
13
+ # Create a new component instance
14
+ # @param props [Hash] Component properties
15
+ def initialize(props = {})
16
+ # Initialize component state
17
+ @state = {}
18
+ end
19
+ ```
20
+
21
+ 3. Documentation Requirements:
22
+ - Document all public methods
23
+ - Include type information for parameters and return values
24
+ - Use descriptive English names for methods and variables
25
+ - Add context when the code's purpose isn't immediately clear
26
+ description:
27
+ globs:
28
+ alwaysApply: false
29
+ ---
@@ -0,0 +1,74 @@
1
+ name: Playwright Tests
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+
9
+ jobs:
10
+ test:
11
+ timeout-minutes: 60
12
+ runs-on: ubuntu-latest
13
+
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+
17
+ - name: Setup Node.js
18
+ uses: actions/setup-node@v4
19
+ with:
20
+ node-version: "24.x"
21
+ cache: "npm"
22
+
23
+ - name: Install dependencies
24
+ run: npm ci
25
+
26
+ - name: Get installed Playwright version
27
+ id: playwright-version
28
+ run: echo "PLAYWRIGHT_VERSION=$(node -e "console.log(require('./node_modules/@playwright/test/package.json').version)")" >> $GITHUB_ENV
29
+
30
+ - name: Cache Playwright browsers
31
+ uses: actions/cache@v4
32
+ id: playwright-cache
33
+ with:
34
+ path: ~/.cache/ms-playwright
35
+ key: playwright-browsers-${{ env.PLAYWRIGHT_VERSION }}-${{ runner.os }}
36
+
37
+ - name: Install Playwright Browsers (Chromium only)
38
+ if: steps.playwright-cache.outputs.cache-hit != 'true'
39
+ run: npx playwright install chromium --with-deps
40
+
41
+ - name: Cache runtime node_modules
42
+ uses: actions/cache@v4
43
+ with:
44
+ path: packages/npm-packages/runtime/node_modules
45
+ key: runtime-node-modules-${{ hashFiles('packages/npm-packages/runtime/package-lock.json') }}
46
+
47
+ - name: Install runtime dependencies
48
+ run: |
49
+ cd packages/npm-packages/runtime
50
+ npm ci
51
+
52
+ - name: Build runtime package
53
+ run: |
54
+ cd packages/npm-packages/runtime
55
+ npm run build:dev
56
+
57
+ - name: Run Playwright tests
58
+ run: npm run test
59
+
60
+ - name: Upload Playwright Report
61
+ uses: actions/upload-artifact@v4
62
+ if: always()
63
+ with:
64
+ name: playwright-report
65
+ path: playwright-report/
66
+ retention-days: 30
67
+
68
+ - name: Upload test results
69
+ uses: actions/upload-artifact@v4
70
+ if: always() && hashFiles('test-results/**/*') != ''
71
+ with:
72
+ name: test-results
73
+ path: test-results/
74
+ retention-days: 30
@@ -0,0 +1,33 @@
1
+ name: RSpec
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ pull_request:
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ matrix:
14
+ ruby-version: ["3.4.0"]
15
+
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+
19
+ - name: Set up Ruby
20
+ uses: ruby/setup-ruby@v1
21
+ with:
22
+ ruby-version: ${{ matrix.ruby-version }}
23
+ bundler-cache: true
24
+
25
+ - name: Install dependencies
26
+ run: |
27
+ cd packages/npm-packages/runtime
28
+ bundle install
29
+
30
+ - name: Run RSpec
31
+ run: |
32
+ cd packages/npm-packages/runtime
33
+ bundle exec rspec
data/.node-version ADDED
@@ -0,0 +1 @@
1
+ 23.11.0
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 t0yohei
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,218 @@
1
+ # ruby-wasm-ui
2
+
3
+ A modern web frontend framework for Ruby using [ruby.wasm](https://github.com/ruby/ruby.wasm). Write reactive web applications using familiar Ruby syntax and patterns.
4
+
5
+ **⚠️ Warning: This library is currently under development and subject to frequent breaking changes. Please use with caution and expect API changes in future versions.**
6
+
7
+ ## Features
8
+
9
+ - **Reactive State Management**: Simple, predictable state updates with actions
10
+ - **Virtual DOM**: Efficient DOM updates using a virtual DOM implementation
11
+ - **Event Handling**: Intuitive event system with Ruby lambdas
12
+ - **Component Architecture**: Build reusable components with clean separation of concerns
13
+ - **Lifecycle Hooks**: Manage component lifecycle with hooks like `on_mounted`
14
+ - **Ruby Syntax**: Write frontend applications using Ruby instead of JavaScript
15
+
16
+ ## Quick Start
17
+
18
+ Create an HTML file:
19
+
20
+ ```html
21
+ <!DOCTYPE html>
22
+ <html>
23
+ <head>
24
+ <script src="https://unpkg.com/ruby-wasm-ui@0.8.1"></script>
25
+ <script defer type="text/ruby" src="app.rb"></script>
26
+ </head>
27
+ <body>
28
+ <div id="app"></div>
29
+ </body>
30
+ </html>
31
+ ```
32
+
33
+ Create `app.rb`:
34
+
35
+ ```ruby
36
+ require "js"
37
+
38
+ # Define a Counter component
39
+ CounterComponent = RubyWasmUi.define_component(
40
+ # Initialize component state
41
+ state: ->(props) {
42
+ { count: props[:count] || 0 }
43
+ },
44
+
45
+ # Render the counter component
46
+ template: ->() {
47
+ RubyWasmUi::Template::Parser.parse_and_eval(<<~HTML, binding)
48
+ <div>
49
+ <div>{state[:count]}</div>
50
+ <!-- Both ButtonComponent and button-component are valid -->
51
+ <ButtonComponent
52
+ label="Increment"
53
+ on="{ click_button: -> { increment } }">
54
+ </ButtonComponent>
55
+ <button-component
56
+ label="Decrement"
57
+ on="{ click_button: -> { decrement } }"
58
+ />
59
+ </div>
60
+ HTML
61
+ },
62
+
63
+ # Component methods
64
+ methods: {
65
+ increment: ->() {
66
+ update_state(count: state[:count] + 1)
67
+ },
68
+ decrement: ->() {
69
+ update_state(count: state[:count] - 1)
70
+ }
71
+ }
72
+ )
73
+
74
+ # Button component - reusable button with click handler
75
+ ButtonComponent = RubyWasmUi.define_component(
76
+ template: ->() {
77
+ RubyWasmUi::Template::Parser.parse_and_eval(<<~HTML, binding)
78
+ <button on="{ click: ->() { emit('click_button') } }">
79
+ {props[:label]}
80
+ </button>
81
+ HTML
82
+ }
83
+ )
84
+
85
+ # Create and mount the app
86
+ app = RubyWasmUi::App.create(CounterComponent, count: 5)
87
+ app_element = JS.global[:document].getElementById("app")
88
+ app.mount(app_element)
89
+ ```
90
+
91
+ ## Using as a Gem
92
+
93
+ You can also use `ruby_wasm_ui` as a Ruby gem with `rbwasm` to build your application as a WASM file.
94
+
95
+ ### Setup
96
+
97
+ 1. Add `ruby_wasm_ui` to your `Gemfile`:
98
+
99
+ ```ruby
100
+ # frozen_string_literal: true
101
+
102
+ source "https://rubygems.org"
103
+
104
+ gem "ruby_wasm_ui"
105
+ ```
106
+
107
+ 2. Install dependencies:
108
+
109
+ ```bash
110
+ bundle install
111
+ ```
112
+
113
+ ### Building Your Application
114
+
115
+ 1. Build Ruby WASM:
116
+
117
+ ```bash
118
+ bundle exec rbwasm build --ruby-version 3.4 -o ruby.wasm
119
+ ```
120
+
121
+ 2. Pack your application files:
122
+
123
+ ```bash
124
+ bundle exec rbwasm pack ruby.wasm --dir ./src::./src -o src.wasm
125
+ ```
126
+
127
+ This command packs your Ruby files from the `./src` directory into the WASM file.
128
+
129
+ ### Creating Your HTML File
130
+
131
+ Create an HTML file in the `src` directory that loads the WASM file:
132
+
133
+ ```html
134
+ <!DOCTYPE html>
135
+ <html lang="en">
136
+ <head>
137
+ <meta charset="UTF-8" />
138
+ <title>My App</title>
139
+ <script type="module">
140
+ import { DefaultRubyVM } from "https://cdn.jsdelivr.net/npm/@ruby/wasm-wasi@2.7.2/dist/browser/+esm";
141
+ const response = await fetch("../src.wasm");
142
+ const module = await WebAssembly.compileStreaming(response);
143
+ const { vm } = await DefaultRubyVM(module);
144
+ vm.evalAsync(`
145
+ require "ruby_wasm_ui"
146
+ require_relative './src/app.rb'
147
+ `);
148
+ </script>
149
+ </head>
150
+ <body>
151
+ <div id="app"></div>
152
+ </body>
153
+ </html>
154
+ ```
155
+
156
+ ### Example Project Structure
157
+
158
+ ```
159
+ my-app/
160
+ ├── Gemfile
161
+ ├── src.wasm
162
+ └── src/
163
+ ├── app.rb
164
+ └── index.html
165
+ ```
166
+
167
+ Your `src/app.rb` file can use `ruby_wasm_ui` just like in the Quick Start example:
168
+
169
+ ```ruby
170
+ require "js"
171
+
172
+ CounterComponent = RubyWasmUi.define_component(
173
+ state: ->(props) {
174
+ { count: props[:count] || 0 }
175
+ },
176
+ template: ->() {
177
+ RubyWasmUi::Template::Parser.parse_and_eval(<<~HTML, binding)
178
+ <div>
179
+ <div>{state[:count]}</div>
180
+ <button on="{ click: -> { increment } }">Increment</button>
181
+ </div>
182
+ HTML
183
+ },
184
+ methods: {
185
+ increment: ->() {
186
+ update_state(count: state[:count] + 1)
187
+ }
188
+ }
189
+ )
190
+
191
+ app = RubyWasmUi::App.create(CounterComponent, count: 0)
192
+ app_element = JS.global[:document].getElementById("app")
193
+ app.mount(app_element)
194
+ ```
195
+
196
+ See the [examples](examples) directory for a complete working example.
197
+
198
+ ## Documentation
199
+
200
+ - [Conditional Rendering with r-if](docs/conditional-rendering.md)
201
+ - [List Rendering with r-for](docs/list-rendering.md)
202
+ - [Lifecycle Hooks](docs/lifecycle-hooks.md)
203
+
204
+ ## Development
205
+
206
+ This project is currently under active development. To run the examples locally:
207
+
208
+ ```bash
209
+ # Run production examples
210
+ npm run serve:examples
211
+
212
+ # Run development examples
213
+ npm run serve:examples:dev
214
+ ```
215
+
216
+ ## License
217
+
218
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,119 @@
1
+ # Conditional Rendering with r-if
2
+
3
+ ruby-wasm-ui provides the `r-if` directive for conditional rendering of elements based on state or computed values:
4
+
5
+ ```ruby
6
+ # Component demonstrating r-if conditional rendering
7
+ ConditionalComponent = RubyWasmUi.define_component(
8
+ state: ->() {
9
+ {
10
+ show_message: false,
11
+ counter: 0
12
+ }
13
+ },
14
+
15
+ template: ->() {
16
+ RubyWasmUi::Template::Parser.parse_and_eval(<<~HTML, binding)
17
+ <div>
18
+ <!-- Simple boolean condition -->
19
+ <div r-if="{state[:show_message]}">
20
+ <p>This message is conditionally rendered!</p>
21
+ </div>
22
+
23
+ <!-- Using r-if, r-elsif, r-else for multiple conditions -->
24
+ <div r-if="{state[:counter] > 0}">
25
+ <p>Counter is positive: {state[:counter]}</p>
26
+ </div>
27
+
28
+ <div r-elsif="{state[:counter] < 0}">
29
+ <p>Counter is negative: {state[:counter]}</p>
30
+ </div>
31
+
32
+ <div r-else>
33
+ <p>Counter is zero</p>
34
+ </div>
35
+
36
+ <!-- Toggle button -->
37
+ <button on="{click: ->() { toggle_message }}">
38
+ {state[:show_message] ? "Hide" : "Show"} Message
39
+ </button>
40
+ </div>
41
+ HTML
42
+ },
43
+
44
+ methods: {
45
+ toggle_message: ->() {
46
+ update_state(show_message: !state[:show_message])
47
+ }
48
+ }
49
+ )
50
+ ```
51
+
52
+ ## r-if, r-elsif, r-else Syntax
53
+
54
+ The conditional directives work together to create if-elsif-else chains:
55
+
56
+ ### Basic Usage
57
+
58
+ - **`r-if="{condition}"`**: Renders the element when condition is truthy
59
+ - **`r-elsif="{condition}"`**: Renders when previous conditions are falsy and this condition is truthy
60
+ - **`r-else`**: Renders when all previous conditions are falsy (no condition needed)
61
+
62
+ ### Expression Evaluation
63
+
64
+ All conditional expressions are evaluated as Ruby code within curly braces `{}`:
65
+
66
+ - Use any valid Ruby expression that returns a truthy or falsy value
67
+ - Access component state with `state[:key]`
68
+ - Access props with `props[:key]`
69
+ - Support for comparison operators (`>`, `<`, `==`, `!=`, etc.)
70
+ - Support for logical operators (`&&`, `||`, `!`)
71
+ - Support for method calls and complex expressions
72
+
73
+ ### Conditional Chain Rules
74
+
75
+ 1. **Sequential Processing**: Conditions are evaluated in order (r-if → r-elsif → r-else)
76
+ 2. **Mutual Exclusivity**: Only one element in a conditional chain will render
77
+ 3. **Grouping**: Consecutive conditional elements form a single conditional group
78
+ 4. **Breaking**: A new `r-if` breaks the current conditional chain and starts a new one
79
+
80
+ ### Advanced Example
81
+
82
+ ```ruby
83
+ # Real-world example with loading states and data
84
+ LoadingComponent = RubyWasmUi.define_component(
85
+ state: ->() {
86
+ {
87
+ is_loading: false,
88
+ data: nil,
89
+ error: nil
90
+ }
91
+ },
92
+
93
+ template: ->() {
94
+ RubyWasmUi::Template::Parser.parse_and_eval(<<~HTML, binding)
95
+ <div>
96
+ <!-- Loading state -->
97
+ <p r-if="{state[:is_loading]}">Loading...</p>
98
+
99
+ <!-- Error state -->
100
+ <div r-elsif="{state[:error]}" style="color: red;">
101
+ <p>Error: {state[:error]}</p>
102
+ <button on="{click: ->() { retry_load }}">Retry</button>
103
+ </div>
104
+
105
+ <!-- Success state with data -->
106
+ <div r-elsif="{state[:data]}">
107
+ <h2>{state[:data][:title]}</h2>
108
+ <p>{state[:data][:description]}</p>
109
+ </div>
110
+
111
+ <!-- Initial state (no loading, no error, no data) -->
112
+ <button r-else on="{click: ->() { load_data }}">
113
+ Load Data
114
+ </button>
115
+ </div>
116
+ HTML
117
+ }
118
+ )
119
+ ```