Skip to content

Getting Started

Regor binds runtime behavior directly to DOM you already have, or to template content you provide at mount time.

This means you can start from static HTML, mount only where needed, and scale to multiple independent runtime islands on the same page.

Terminal window
yarn add regor
Terminal window
npm install regor
import { createApp, html, ref } from 'regor'
const app = createApp(
{
count: ref(0),
inc() {
this.count(this.count() + 1)
},
},
{
element: document.querySelector('#app')!,
template: html`
<section>
<button @click="inc">Increment</button>
<p>Count: <span r-text="count"></span></p>
</section>
`,
},
)
// optional teardown
// app.unbind()
// app.unmount()
<div id="app"></div>
<script type="module">
import {
createApp,
ref,
} from 'https://unpkg.com/regor/dist/regor.es2022.esm.prod.js'
createApp(
{
message: ref('Hello from CDN'),
},
{
element: document.querySelector('#app'),
template: '<h1 r-text="message"></h1>',
},
)
</script>
  1. Bind existing markup in place:
createApp({ userName: ref('Ada') }, { selector: '#profile-island' })
  1. Replace root content from template string:
createApp(
{ title: ref('Home') },
{
selector: '#app',
template: '<h1 r-text="title"></h1>',
},
)
  1. Replace root content from JSON template:
createApp(
{ label: ref('Save') },
{
element: document.querySelector('#app')!,
json: { t: 'button', a: { 'r-text': 'label' } },
},
)

ref for values and deep object conversion:

import { ref } from 'regor'
const count = ref(0)
count(1)
console.log(count())

sref for shallow object state:

import { sref } from 'regor'
const user = sref({ name: 'Ada', age: 30 })
user().name = 'Grace'
user({ ...user(), age: 31 })

Inside templates, refs are auto-unwrapped, so use count, user.name, not count() or .value.

import { createApp, defineComponent, ref } from 'regor'
const UserCard = defineComponent('<article><h3 r-text="name"></h3></article>', {
props: ['name'],
context: (head) => ({
name: head.props.name ?? 'Anonymous',
}),
})
createApp({
components: { UserCard },
activeName: ref('Ada Lovelace'),
})
<UserCard :name="activeName"></UserCard>
  1. r-text="expr" for text.
  2. :attr="expr" or r-bind:attr="expr" for attributes.
  3. .prop="expr" or :prop.prop="expr" for DOM properties.
  4. @event="handler" for events.
  5. r-if / r-else-if / r-else for conditional branches.
  6. r-for="item, #i in items" for loops.
  7. :context="{ ... }" / r-context="{ ... }" for object-style component input.
  8. {{ expr }} and [[ expr ]] interpolation (enabled by default).
  1. Overview
  2. Guide
  3. Directives
  4. API Reference
  5. Guide: Mounting