micra-rails 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4a6d0b13ecc6a8986ce5eea667182297f74cd8c5c8b32632e04b1b58f6cd4c26
4
+ data.tar.gz: f2d33530d1f24d31382a61a4bf1321d3a566d5ddb1cabe8906724afe7628fa07
5
+ SHA512:
6
+ metadata.gz: dbe6c5e23d82e90f2414e448db3a01bdd3015708bc940439e279cad84117fd4a6547d4a4a712f5323aa9d2479033c53cc7d6fff057f05391a1335c4e20967c25
7
+ data.tar.gz: c17d3344f4c6419d2c00c3f493b322473bc0d31bbe08cac15de3b9b1658fec75ff2e7ab805166a50b018d0a870b751fbc20cbf7738a77cbaf6159072f2733089
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ - Initial release skeleton.
6
+ - Helpers: `micra_component`, `micra_includes`, `micra_state`.
7
+ - Generator: `rails g micra:install` pins micra.js via importmap and adds a
8
+ `<%= micra_includes %>` line to the application layout.
9
+
10
+ [unreleased]: https://github.com/denisfl/micra-rails/compare/v0.0.0...HEAD
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Denis Fedosov-Ledovskikh
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,125 @@
1
+ # micra-rails
2
+
3
+ Rails integration for [Micra.js] — reactive UI in **~5 kB gzip**, no build step.
4
+
5
+ Drop reactive client state, declarative directives, and a cross-component event
6
+ bus into any Rails view — the way `data-controller` works for Stimulus, except
7
+ your state is reactive and your bindings are declarative.
8
+
9
+ ```erb
10
+ <%= micra_component :counter, count: 0 do %>
11
+ <button @click="dec">−</button>
12
+ <strong data-text="count"></strong>
13
+ <button @click="inc">+</button>
14
+ <% end %>
15
+ ```
16
+
17
+ ```js
18
+ // app/javascript/application.js
19
+ import * as Micra from "micra";
20
+
21
+ Micra.define("counter", {
22
+ state: { count: 0 },
23
+ inc() {
24
+ this.state.count++;
25
+ },
26
+ dec() {
27
+ this.state.count--;
28
+ },
29
+ });
30
+ ```
31
+
32
+ That's the whole API surface. Micra mounts on `DOMContentLoaded`, walks
33
+ `[data-component]`, and wires up reactive bindings.
34
+
35
+ ## Install
36
+
37
+ ```bash
38
+ bundle add micra-rails
39
+ bin/rails generate micra:install
40
+ ```
41
+
42
+ The generator:
43
+
44
+ - pins `micra` via importmap to jsDelivr CDN
45
+ - inserts `<%= micra_includes %>` into your application layout
46
+ - prints a quickstart counter example
47
+
48
+ ## Helpers
49
+
50
+ ### `micra_component(name, tag: :div, **props, &block)`
51
+
52
+ Wraps content in `<div data-component="<name>">` with `data-*` attributes for
53
+ each prop. Primitives (`String`, `Symbol`, `Numeric`, `Boolean`, `nil`) are
54
+ stringified; everything else (`Hash`, `Array`, ActiveRecord objects via
55
+ `as_json`) is JSON-encoded.
56
+
57
+ ```erb
58
+ <%= micra_component :user_card, user: @user.as_json, tag: :article do %>
59
+ <h3 data-text="user.name"></h3>
60
+ <p data-text="user.bio"></p>
61
+ <% end %>
62
+ ```
63
+
64
+ ### `micra_includes`
65
+
66
+ Outputs importmap tags + the `Micra.start()` boot script. Call once in your
67
+ application layout (the installer does this for you).
68
+
69
+ ### `micra_state(**props)`
70
+
71
+ Returns a hash of `data-*` attributes — useful when you want to attach state
72
+ to an existing element instead of wrapping it.
73
+
74
+ ```erb
75
+ <form id="signup" <%= micra_state(intent: "signup", token: form_token).to_html %>>
76
+ ...
77
+ </form>
78
+ ```
79
+
80
+ ## Why Micra over the alternatives
81
+
82
+ | | Micra.js | Stimulus | Alpine.js | Hotwire alone |
83
+ | -------------------------- | ---------------- | ----------------- | -------------- | ---------------- |
84
+ | Bundle (gzip) | ~5 kB | ~10 kB | ~14 kB | — |
85
+ | Reactive client state | ✓ | manual | ✓ | server-driven |
86
+ | Standard `data-*` syntax | ✓ | `data-controller` | `x-*` | `turbo-*` |
87
+ | Pairs with Turbo / Hotwire | ✓ | ✓ | works | native |
88
+ | Sweet spot | reactive islands | Rails behaviors | small reactive | server-driven UI |
89
+
90
+ Use **Stimulus** for behaviors that don't need client state.
91
+ Use **Micra** when you need reactive state on a Rails page.
92
+ Pair Micra with **Turbo Streams** for the full Hotwire stack with reactivity.
93
+
94
+ ## Production vs CDN
95
+
96
+ By default, `micra-rails` pins to jsDelivr:
97
+
98
+ ```ruby
99
+ pin "micra", to: "https://cdn.jsdelivr.net/npm/micra.js@2.1.0/dist/micra.esm.js"
100
+ ```
101
+
102
+ To self-host (no third-party CDN):
103
+
104
+ ```bash
105
+ bin/importmap pin micra --download
106
+ ```
107
+
108
+ This downloads `micra.esm.js` into `vendor/javascript/` and updates the pin.
109
+
110
+ ## Compatibility
111
+
112
+ | Rails | Status |
113
+ | ----- | ------------- |
114
+ | 8.x | ✓ |
115
+ | 7.2 | ✓ |
116
+ | 7.1 | ✓ |
117
+ | < 7.1 | not supported |
118
+
119
+ Requires Ruby ≥ 3.1.
120
+
121
+ ## License
122
+
123
+ MIT © Denis Fedosov-Ledovskikh
124
+
125
+ [Micra.js]: https://github.com/denisfl/micra.js
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/setup"
4
+ require "bundler/gem_tasks" # provides rake build, rake install, rake release
5
+ require "rake/testtask"
6
+
7
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
8
+ load "rails/tasks/engine.rake" if File.exist?(APP_RAKEFILE)
9
+
10
+ Rake::TestTask.new do |t|
11
+ t.libs << "test"
12
+ t.libs << "lib"
13
+ t.pattern = "test/**/*_test.rb"
14
+ t.verbose = false
15
+ end
16
+
17
+ task default: :test
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators/base"
4
+
5
+ module Micra
6
+ module Generators
7
+ class InstallGenerator < ::Rails::Generators::Base
8
+ desc "Installs Micra.js: pins via importmap and inserts <%= micra_includes %> into the application layout."
9
+
10
+ source_root File.expand_path("templates", __dir__)
11
+
12
+ def pin_micra_in_importmap
13
+ return unless File.exist?("config/importmap.rb")
14
+
15
+ line = %(pin "micra", to: "https://cdn.jsdelivr.net/npm/micra.js@#{Micra::Rails::MICRA_JS_VERSION}/dist/micra.esm.js", preload: true\n)
16
+
17
+ append_to_file "config/importmap.rb" do
18
+ "\n# Micra.js — reactive UI directives (<5 KB gzip)\n#{line}"
19
+ end
20
+ end
21
+
22
+ def inject_includes_in_layout
23
+ layout = "app/views/layouts/application.html.erb"
24
+ return unless File.exist?(layout)
25
+
26
+ unless File.read(layout).include?("micra_includes")
27
+ inject_into_file layout,
28
+ " <%= micra_includes %>\n",
29
+ before: " </head>\n"
30
+ end
31
+ end
32
+
33
+ def show_done_message
34
+ say ""
35
+ say "✔ Micra.js installed."
36
+ say ""
37
+ say " Try it in any view:"
38
+ say ""
39
+ say %( <%= micra_component :counter, count: 0 do %>)
40
+ say %( <button @click="dec">−</button>)
41
+ say %( <strong data-text="count"></strong>)
42
+ say %( <button @click="inc">+</button>)
43
+ say %( <% end %>)
44
+ say ""
45
+ say %( Then in JS (app/javascript/application.js or similar):)
46
+ say ""
47
+ say %( import * as Micra from "micra")
48
+ say %( Micra.define("counter", {)
49
+ say %( state: { count: 0 },)
50
+ say %( inc() { this.state.count++ },)
51
+ say %( dec() { this.state.count-- },)
52
+ say %( }))
53
+ say ""
54
+ say " Docs: https://denisfl.github.io/micra.js/"
55
+ say ""
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Micra
4
+ module Rails
5
+ # Mountable engine: registers the importmap pin and the view helpers.
6
+ class Engine < ::Rails::Engine
7
+ isolate_namespace Micra::Rails
8
+
9
+ initializer "micra.rails.importmap", before: "importmap" do |app|
10
+ next unless app.config.respond_to?(:importmap)
11
+
12
+ app.config.importmap.paths << root.join("config/importmap.rb")
13
+ end
14
+
15
+ initializer "micra.rails.helpers" do
16
+ ActiveSupport.on_load(:action_view) do
17
+ require "micra/rails/view_helpers"
18
+ include Micra::Rails::ViewHelpers
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Micra
4
+ module Rails
5
+ VERSION = "0.1.0"
6
+
7
+ # Tracks the Micra.js npm version we ship by default. The importmap pin
8
+ # uses this value. Bump it together with VERSION when wrapping a new
9
+ # upstream release.
10
+ MICRA_JS_VERSION = "2.1.0"
11
+ end
12
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Micra
4
+ module Rails
5
+ # View helpers for Micra.js components.
6
+ #
7
+ # <%= micra_component :counter, count: 0 do %>
8
+ # <button @click="dec">−</button>
9
+ # <strong data-text="count"></strong>
10
+ # <button @click="inc">+</button>
11
+ # <% end %>
12
+ #
13
+ # Produces:
14
+ #
15
+ # <div data-component="counter" data-count="0">
16
+ # <button @click="dec">−</button>
17
+ # <strong data-text="count"></strong>
18
+ # <button @click="inc">+</button>
19
+ # </div>
20
+ module ViewHelpers
21
+ # Renders a wrapper <%= tag %> with `data-component="<name>"` and
22
+ # `data-<key>="<value>"` for each prop. Non-string values are JSON-encoded
23
+ # so they round-trip via this.prop() in Micra.
24
+ def micra_component(name, tag: :div, **props, &block)
25
+ attributes = {"data-component" => name.to_s}
26
+
27
+ props.each do |key, value|
28
+ attributes["data-#{key.to_s.dasherize}"] = serialize_prop(value)
29
+ end
30
+
31
+ content_tag(tag, capture(&block), attributes)
32
+ end
33
+
34
+ # Drop into your application layout once. Outputs the importmap module
35
+ # script that boots Micra and a final <script>Micra.start()</script>.
36
+ def micra_includes
37
+ capture do
38
+ concat javascript_importmap_tags
39
+ concat javascript_tag(<<~JS, type: "module")
40
+ import * as Micra from "micra"
41
+ window.Micra = Micra
42
+ document.addEventListener("DOMContentLoaded", () => Micra.start())
43
+ JS
44
+ end
45
+ end
46
+
47
+ # Inline JSON props for a single component instance.
48
+ #
49
+ # <%= micra_state user: @user.as_json %>
50
+ #
51
+ # Equivalent to writing data-user='{"id":1,...}' yourself.
52
+ def micra_state(**props)
53
+ props.map { |k, v| ["data-#{k.to_s.dasherize}", serialize_prop(v)] }.to_h
54
+ end
55
+
56
+ private
57
+
58
+ def serialize_prop(value)
59
+ case value
60
+ when String, Symbol, Numeric, TrueClass, FalseClass, NilClass
61
+ value.to_s
62
+ else
63
+ value.to_json
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+ require "importmap-rails"
5
+
6
+ require "micra/rails/version"
7
+ require "micra/rails/engine"
8
+
9
+ module Micra
10
+ module Rails
11
+ # Convenience accessor used by ViewHelpers and the install generator.
12
+ # In production you may want to vendor the minified file instead of
13
+ # hitting jsDelivr — flip `Micra::Rails.vendored = true` and put the
14
+ # file at `app/assets/javascripts/micra.min.js`.
15
+ class << self
16
+ attr_accessor :vendored
17
+ end
18
+
19
+ self.vendored = false
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "micra/rails"
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: micra-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Denis Fedosov-Ledovskikh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-05-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '7.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '9.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '7.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '9.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: importmap-rails
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '1.0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '1.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '5.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '5.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: sqlite3
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '2.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '2.0'
75
+ description: |
76
+ Drop-in Rails helpers and an importmap pin for Micra.js — a 5 kB reactive
77
+ framework for server-rendered apps. Provides micra_component helper,
78
+ SSR-friendly props via data-* attributes, and a one-command installer.
79
+ email:
80
+ - denis@fedosov.me
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - CHANGELOG.md
86
+ - MIT-LICENSE
87
+ - README.md
88
+ - Rakefile
89
+ - lib/generators/micra/install/install_generator.rb
90
+ - lib/micra-rails.rb
91
+ - lib/micra/rails.rb
92
+ - lib/micra/rails/engine.rb
93
+ - lib/micra/rails/version.rb
94
+ - lib/micra/rails/view_helpers.rb
95
+ homepage: https://github.com/denisfl/micra-rails
96
+ licenses:
97
+ - MIT
98
+ metadata:
99
+ homepage_uri: https://github.com/denisfl/micra-rails
100
+ source_code_uri: https://github.com/denisfl/micra-rails
101
+ changelog_uri: https://github.com/denisfl/micra-rails/blob/master/CHANGELOG.md
102
+ rubygems_mfa_required: 'true'
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: 3.1.0
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubygems_version: 3.5.22
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Rails integration for Micra.js — reactive UI in ~5 kB, no build step
122
+ test_files: []