|
| 1 | +# morph.nvim |
| 2 | + |
| 3 | +Build interactive text user interfaces in Neovim with a React-like component model. |
| 4 | + |
| 5 | +## What is morph.nvim? |
| 6 | + |
| 7 | +morph.nvim lets you create dynamic, interactive buffers using familiar React patterns like components, state, and event handlers. Perfect for building custom UIs, forms, dashboards, or any interactive text-based interface within Neovim. |
| 8 | + |
| 9 | +## Quick Start |
| 10 | + |
| 11 | +```lua |
| 12 | +local Morph = require('morph') |
| 13 | +local h = Morph.h |
| 14 | + |
| 15 | +-- Create a simple counter component |
| 16 | +--- @param ctx morph.Ctx<{}, { count: integer }> |
| 17 | +local function Counter(ctx) |
| 18 | + if ctx.phase == 'mount' then |
| 19 | + ctx.state = { count = 0 } |
| 20 | + end |
| 21 | + |
| 22 | + return { |
| 23 | + h.Title({}, 'Count: '), |
| 24 | + h.Number({}, tostring(ctx.state.count)), |
| 25 | + '\n', |
| 26 | + h.Keyword({ |
| 27 | + nmap = { |
| 28 | + ['<CR>'] = function() |
| 29 | + ctx:update({ count = ctx.state.count + 1 }) |
| 30 | + return '' -- consume the keypress |
| 31 | + end |
| 32 | + } |
| 33 | + }, '[Press Enter to increment]') |
| 34 | + } |
| 35 | +end |
| 36 | + |
| 37 | +-- Render to current buffer |
| 38 | +local renderer = Morph.new() |
| 39 | +renderer:mount(h(Counter, {}, {})) |
| 40 | +``` |
| 41 | + |
| 42 | +## Key Features |
| 43 | + |
| 44 | +### 🎯 **Component-Based Architecture** |
| 45 | +Write reusable components with props, state, and lifecycle methods. Components can render other components, creating a composable hierarchy: |
| 46 | + |
| 47 | +```lua |
| 48 | +--- @param ctx morph.Ctx<{ todo: any, on_toggle: function }, {}> |
| 49 | +local function TodoItem(ctx) |
| 50 | + local todo = ctx.props.todo |
| 51 | + |
| 52 | + return { |
| 53 | + h('text', { |
| 54 | + hl = todo.done and 'Comment' or 'Normal', |
| 55 | + nmap = { |
| 56 | + ['<Space>'] = function() |
| 57 | + ctx.props.on_toggle(todo.id) |
| 58 | + return '' |
| 59 | + end |
| 60 | + } |
| 61 | + }, todo.done and '✓ ' or '○ '), |
| 62 | + todo.text |
| 63 | + } |
| 64 | +end |
| 65 | + |
| 66 | +--- @param ctx morph.Ctx<{}, { todos: table[] }> |
| 67 | +local function TodoList(ctx) |
| 68 | + if ctx.phase == 'mount' then |
| 69 | + ctx.state = { |
| 70 | + todos = { |
| 71 | + { id = 1, text = 'Learn morph.nvim', done = false }, |
| 72 | + { id = 2, text = 'Build awesome UI', done = false } |
| 73 | + } |
| 74 | + } |
| 75 | + end |
| 76 | + |
| 77 | + return { |
| 78 | + h.Title({}, 'My Todos'), |
| 79 | + '\n\n', |
| 80 | + vim.tbl_map(function(todo) |
| 81 | + return { |
| 82 | + h(TodoItem, { |
| 83 | + todo = todo, |
| 84 | + on_toggle = function(id) |
| 85 | + -- Update todo state... |
| 86 | + end |
| 87 | + }), |
| 88 | + '\n' |
| 89 | + } |
| 90 | + end, ctx.state.todos) |
| 91 | + } |
| 92 | +end |
| 93 | +``` |
| 94 | + |
| 95 | +### ⚡ **Efficient Reconciliation** |
| 96 | +Only updates what changed, using a diffing algorithm similar to React's virtual DOM. |
| 97 | + |
| 98 | +### 🎨 **Rich Text Styling** |
| 99 | +Apply highlight groups and extmarks with simple attributes: |
| 100 | + |
| 101 | +```lua |
| 102 | +h.ErrorMsg({ |
| 103 | + extmark = { |
| 104 | + virt_text = { { ' ← Error here', 'Comment' } } |
| 105 | + } |
| 106 | +}, 'Invalid input') |
| 107 | +``` |
| 108 | + |
| 109 | +### 🔥 **Interactive Event Handling** |
| 110 | +Respond to keypresses with mode-specific handlers: |
| 111 | + |
| 112 | +```lua |
| 113 | +h('text', { |
| 114 | + nmap = { ['<CR>'] = handle_enter }, |
| 115 | + imap = { ['<Tab>'] = handle_tab }, |
| 116 | + on_change = function(e) |
| 117 | + print('Text changed to:', e.text) |
| 118 | + end |
| 119 | +}, 'Interactive text') |
| 120 | +``` |
| 121 | + |
| 122 | +### 📝 **Text Change Detection** |
| 123 | +Automatically detect when users edit text within tags: |
| 124 | + |
| 125 | +```lua |
| 126 | +h('text', { |
| 127 | + on_change = function(e) |
| 128 | + -- e.text contains the new content |
| 129 | + validate_input(e.text) |
| 130 | + end |
| 131 | +}, 'Editable content') |
| 132 | +``` |
| 133 | + |
| 134 | +## Real-World Example |
| 135 | + |
| 136 | +```lua |
| 137 | +--- @param ctx morph.Ctx<{}, { query: string, results: table[] }> |
| 138 | +local function SearchForm(ctx) |
| 139 | + if ctx.phase == 'mount' then |
| 140 | + ctx.state = { query = '', results = {} } |
| 141 | + end |
| 142 | + |
| 143 | + return { |
| 144 | + h.Title({}, 'Search: '), |
| 145 | + h('text', { |
| 146 | + on_change = function(e) |
| 147 | + ctx:update({ |
| 148 | + query = e.text, |
| 149 | + results = performSearch(e.text) |
| 150 | + }) |
| 151 | + end |
| 152 | + }, ctx.state.query), |
| 153 | + '\n\n', |
| 154 | + |
| 155 | + -- Results |
| 156 | + vim.tbl_map(function(result) |
| 157 | + return { |
| 158 | + h.Directory({ |
| 159 | + nmap = { |
| 160 | + ['<CR>'] = function() |
| 161 | + vim.cmd('edit ' .. result.path) |
| 162 | + return '' |
| 163 | + end |
| 164 | + } |
| 165 | + }, result.name), |
| 166 | + '\n' |
| 167 | + } |
| 168 | + end, ctx.state.results) |
| 169 | + } |
| 170 | +end |
| 171 | +``` |
| 172 | + |
| 173 | +## Installation |
| 174 | + |
| 175 | +Since this is not a traditional Neovim "plugin" (it is a library), plugin authors are the intended consumers. Neovim does not have a good answer for automatic management of plugin dependencies. As such, it is recommended that library authors: |
| 176 | + |
| 177 | +1. Vendor morph.nvim in their plugin. Morph is implemented in a single file, so this should be relatively painless. However, there is no great way to check for updates. |
| 178 | + |
| 179 | +OR: |
| 180 | + |
| 181 | +2. Suggest to their users to add morph.nvim as a dependency in their package manager. The major con here is that breaking changes could be inadvertantly pulled in by users when they update their plugins. |
| 182 | + |
| 183 | +## Hyperscript Syntax |
| 184 | + |
| 185 | +morph.nvim uses a hyperscript-like syntax for creating elements, similar to React's JSX but in Lua: |
| 186 | + |
| 187 | +```lua |
| 188 | +-- Basic text element |
| 189 | +h('text', { hl = 'Comment' }, 'Hello world') |
| 190 | + |
| 191 | +-- Shorthand for highlight groups |
| 192 | +h.Comment({}, 'Hello world') -- equivalent to above |
| 193 | + |
| 194 | +-- Nested elements |
| 195 | +h('text', {}, { |
| 196 | + 'Outer text ', |
| 197 | + h.Keyword({}, 'highlighted'), |
| 198 | + ' more text' |
| 199 | +}) |
| 200 | + |
| 201 | +-- With event handlers |
| 202 | +h.Directory({ |
| 203 | + nmap = { |
| 204 | + ['<CR>'] = function() |
| 205 | + vim.cmd('edit ' .. filename) |
| 206 | + return '' -- consume keypress |
| 207 | + end |
| 208 | + } |
| 209 | +}, filename) |
| 210 | + |
| 211 | +-- With extmark options for advanced styling |
| 212 | +h.ErrorMsg({ |
| 213 | + extmark = { |
| 214 | + virt_text = { { ' ← Error here', 'Comment' } }, |
| 215 | + virt_text_pos = 'eol', |
| 216 | + priority = 100 |
| 217 | + } |
| 218 | +}, 'Invalid input') |
| 219 | + |
| 220 | +-- Arrays of elements |
| 221 | +{ |
| 222 | + h.Title({}, 'Header'), |
| 223 | + '\n', |
| 224 | + h.Normal({}, 'Body text'), |
| 225 | + '\n\n', |
| 226 | + h.Comment({}, 'Footer') |
| 227 | +} |
| 228 | +``` |
| 229 | + |
| 230 | +The `h` function creates virtual elements that get rendered to buffer text with associated extmarks for styling and interactivity. Use `h.HighlightGroup({...}, children)` as shorthand when you don't need conditional highlighting. The `extmark` attribute accepts any options supported by `nvim_buf_set_extmark()` for advanced text decoration. |
| 231 | + |
| 232 | +## API Reference |
| 233 | + |
| 234 | +### Core Functions |
| 235 | + |
| 236 | +- `Morph.new(bufnr?)` - Create a new renderer for a buffer |
| 237 | +- `renderer:mount(tree)` - Mount a component tree |
| 238 | +- `renderer:render(tree)` - Render static markup |
| 239 | +- `h(name, attributes?, children?)` - Create elements |
| 240 | + |
| 241 | +### Component Context |
| 242 | + |
| 243 | +- `ctx.props` - Component properties |
| 244 | +- `ctx.state` - Component state |
| 245 | +- `ctx.children` - Child elements |
| 246 | +- `ctx:update(newState)` - Update state and trigger re-render |
| 247 | +- `ctx.phase` - Current lifecycle phase ('mount', 'update', 'unmount') |
| 248 | + |
| 249 | +### Event Handlers |
| 250 | + |
| 251 | +- `nmap`, `imap`, `vmap`, `xmap`, `omap` - Mode-specific key handlers |
| 252 | +- `on_change` - Text change callback |
| 253 | + |
| 254 | +## Why morph.nvim? |
| 255 | + |
| 256 | +Building interactive UIs in Neovim traditionally requires managing buffer content, extmarks, keymaps, and autocmds manually. morph.nvim abstracts this complexity behind a declarative, component-based API that feels familiar to web developers while being optimized for Neovim's unique capabilities. |
| 257 | + |
| 258 | +## License (MIT) |
| 259 | + |
| 260 | +Copyright (c) 2025 jrapodaca@gmail.com |
| 261 | + |
| 262 | +Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 263 | +this software and associated documentation files (the "Software"), to deal in |
| 264 | +the Software without restriction, including without limitation the rights to |
| 265 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 266 | +of the Software, and to permit persons to whom the Software is furnished to do |
| 267 | +so, subject to the following conditions: |
| 268 | + |
| 269 | +The above copyright notice and this permission notice shall be included in all |
| 270 | +copies or substantial portions of the Software. |
| 271 | + |
| 272 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 273 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 274 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 275 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 276 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 277 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 278 | +SOFTWARE. |
0 commit comments