`) onto the webpage. It outlines the typical setup for a Riot.js application.
```HTML
```
--------------------------------
### Mount Riot.js Components Using Various Selectors
Source: https://v3.riotjs.vercel.app/v2/guide
This example demonstrates the flexibility of the `riot.mount()` method, showing how to mount components using different selector types. It covers mounting all custom tags on the page, mounting a specific element by its ID, and mounting multiple components by providing a comma-separated list of tag names.
```JavaScript
// mount all custom tags on the page
riot.mount('*')
// mount an element with a specific id
riot.mount('#my-element')
// mount selected elements
riot.mount('todo, forum, comments')
```
--------------------------------
### React Todo Application Example
Source: https://v3.riotjs.vercel.app/v2/ru/compare
This JavaScript code demonstrates a simple Todo application built with React. It showcases the use of `React.createClass` for component definition, state management with `getInitialState` and `setState`, event handling for input changes and form submission, and rendering with JSX, which mixes HTML directly within JavaScript.
```JavaScript
var TodoList = React.createClass({
render: function() {
var createItem = function(itemText) {
return {itemText};
};
return {this.props.items.map(createItem)}
;
}
});
var TodoApp = React.createClass({
getInitialState: function() {
return {items: [], text: ''};
},
onChange: function(e) {
this.setState({text: e.target.value});
},
handleSubmit: function(e) {
e.preventDefault();
var nextItems = this.state.items.concat([this.state.text]);
var nextText = '';
this.setState({items: nextItems, text: nextText});
},
render: function() {
return (
TODO
);
}
});
React.render(, mountNode);
```
--------------------------------
### Riot.js Custom Tag Indentation Rules
Source: https://v3.riotjs.vercel.app/v2/guide
This snippet illustrates the critical indentation rules for defining Riot.js custom tags within tag files. It shows that tags must start at the beginning of the line without any leading whitespace for proper compilation, contrasting working and failing examples.
```Riot.js
```
--------------------------------
### Configure and Compile Riot.js Tags with ECMAScript 6 (ES6) Preprocessor
Source: https://v3.riotjs.vercel.app/ja/guide/compiler
This section details how to enable ES6 (Babel) as a preprocessor for Riot.js tags. It includes an example ES6 tag, installation commands for Babel presets and core, the required .babelrc configuration, and various compilation commands for ES6 and ES5 output.
```JavaScript
{ test }
const type = 'JavaScript'
this.test = `This is ${type}`
```
```Shell
npm install babel-preset-es2015-riot --save-dev
```
```Shell
npm install babel-core -g
```
```JSON
{ "presets": ["es2015-riot"] }
```
```Shell
riot --type es6 source.tag
```
```Shell
riot tags/folder dist/es6.tags.js
```
```Shell
babel es6.tags.js --out-file tags.js
```
--------------------------------
### Riot.js Custom Tag Definition Example
Source: https://v3.riotjs.vercel.app/v2/es
This snippet demonstrates how to define a custom component in Riot.js using a single file. It combines HTML for structure, scoped CSS for styling, and JavaScript for logic, showcasing a 'todo' component with an input form and a list.
```Riot.js
{ opts.title }
```
--------------------------------
### Riot.js: Handling Multiple Events with `riot.observable` After Spaced Events Removal
Source: https://v3.riotjs.vercel.app/guide/migration-from-riot2
To achieve significant performance improvements, Riot.js 3's `riot.observable` no longer supports spaced event names (e.g., 'start stop') for binding multiple events simultaneously. Instead, developers must bind each event individually using separate `.on()` calls. Listening to `*` for all events remains supported.
```javascript
var el = riot.observable()
// not supported
el.on('start stop', function() { /* */ })
el.trigger('start stop')
// use this instead
function cb() {}
el
.on('start', cb)
.on('stop', cb)
el
.trigger('start')
.trigger('stop')
```
--------------------------------
### Riot.js API Documentation
Source: https://v3.riotjs.vercel.app/v2/zh/api
Comprehensive API documentation for manually creating Riot.js tag instances using `riot.tag()` and the experimental `riot.Tag` class. It details parameters, usage, and important limitations for each method.
```APIDOC
riot.tag(tagName, html, [css], [attrs], [constructor])
- Manually defines a new custom tag without using the compiler.
- Parameters:
- `tagName`: (string) The name of the tag.
- `html`: (string) The page layout with expressions.
- `css`: (string, optional) The tag's CSS.
- `attrs`: (string, optional) String of tag attributes.
- `constructor`: (function, optional) Initialization function called before tag expressions are evaluated and before the tag is loaded.
- Limitations (due to bypassing compiler):
1. Self-closing tags are not supported.
2. Unquoted expressions are not supported (e.g., must be `value="{ val }"` not `value={ val }`).
3. Boolean attributes are not supported (e.g., must be `__checked="{ flag }"` not `checked={ flag }`).
4. ES6 method shorthand forms are not supported.
5. Image sources must use `riot-src` (e.g., `
`) to avoid illegal server requests.
6. Inline styles must use `riot-style` (e.g., `riot-style="color: { color }"`) for IE support.
riot.Tag(impl, conf, innerHTML)
- Experimental API allowing developers to access internal Tag instances for advanced custom tag creation.
- Parameters:
- `impl`: (object) An object containing tag implementation details.
- `tmpl`: (string) The tag template.
- `fn(opts)`: (function) Callback function called when the mount event occurs.
- `attrs`: (object) A key-value container object with HTML attributes for the root tag.
- `conf`: (object) Configuration options for the tag instance.
- `root`: (DOM node) The DOM node where the tag template will be loaded.
- `opts`: (object) Tag parameters.
- `isLoop`: (boolean) Whether used in a loop tag.
- `hasImpl`: (boolean) Whether already registered with `riot.tag`.
- `item`: (any) The loop item bound to this instance in a loop.
- `innerHTML`: (string) HTML content used to replace embedded `yield` tags in the template.
- Usage Note: Generally not recommended unless `riot.tag` or other Riot methods cannot meet specific requirements.
```
--------------------------------
### Riot.js In-Browser Tag Compilation Setup
Source: https://v3.riotjs.vercel.app/v2/guide/compiler
This snippet demonstrates how to define and mount Riot.js custom tags directly within an HTML page. It uses `type="riot/tag"` for inlined and external tag definitions, includes the Riot.js compiler, and mounts all tags using `riot.mount('*')`.
```HTML
```
--------------------------------
### Initialize Global Riot.js Admin Module Interface
Source: https://v3.riotjs.vercel.app/v1
This JavaScript snippet sets up a single global 'admin' variable using `riot.observable`. It acts as the core interface for the application, allowing functions to be bound as new modules or a configuration object to start the application, returning the API instance when called without arguments.
```JavaScript
var global = is_node ? exports : window,
instance
global.admin = riot.observable(function(arg) {
if (!arg) return instance
if ($.isFunction(arg)) {
admin.on("ready", arg)
} else {
instance = new Admin(arg)
instance.on("ready", function() {
admin.trigger("ready", instance)
})
}
})
```
--------------------------------
### Riot.js Application Structure with Custom Tags
Source: https://v3.riotjs.vercel.app/index
This snippet illustrates how to assemble a Riot.js application by embedding custom tags directly into the HTML body. It also shows the `riot.mount` function used to initialize and mount all custom tags (`*`) on the page, passing an optional `api` object for global data or services.
```HTML
Acme community
```
--------------------------------
### Mounting All Riot.js Custom Tags
Source: https://v3.riotjs.vercel.app/v2/api
Illustrates the use of the special '*' selector with `riot.mount()` to automatically mount all custom tags present on the page. This is a convenient way to initialize all Riot.js components without specifying each one individually.
```JavaScript
riot.mount('*')
```
--------------------------------
### Run Riot.js CLI with Custom Configuration File
Source: https://v3.riotjs.vercel.app/guide/compiler
This command demonstrates how to execute the Riot.js command-line interface, explicitly pointing it to a custom configuration file named `riot.config.js`. This allows for overriding default settings and defining custom build behaviors.
```Shell
riot --config riot.config
```
--------------------------------
### Install Babel 5 Globally for Riot.js ES6
Source: https://v3.riotjs.vercel.app/v2/guide/compiler
This command installs Babel version 5.8 globally via npm, which is used by Riot.js to transform ECMAScript 6 (ES6) code into compatible JavaScript for broader browser support.
```Shell
npm install babel@5.8 -g
```
--------------------------------
### Server-side Rendering with riot.render
Source: https://v3.riotjs.vercel.app/ja/api
Example of rendering a Riot.js tag to HTML on the server-side using `riot.render`. This method is synchronous and returns the rendered HTML.
```JavaScript
// "my-tag"をHTMLにレンダリング
var mytag = require('my-tag')
riot.render(mytag, { foo: 'bar' })
```
--------------------------------
### Riot.js Mounting Tags with Options
Source: https://v3.riotjs.vercel.app/guide
Illustrates how to mount a Riot.js tag to the page using `riot.mount()`, passing initial configuration options to the parent tag.
```JavaScript
```
--------------------------------
### Riot.js CLI: New Component Creation Option
Source: https://v3.riotjs.vercel.app/v2/release-notes
This snippet documents the `--new` option available in the Riot.js command-line interface (CLI). This option simplifies the process of creating new Riot.js components by providing a quick way to scaffold the necessary files and structure.
```APIDOC
riot --new [componentName]
- Purpose: Creates a new Riot.js component.
- Parameters:
- --new: Flag to initiate new component creation.
- [componentName]: (Optional) The name of the component to create. If omitted, the CLI might prompt for a name or create a default structure.
- Usage: Streamlines the initial setup for new Riot.js components, improving developer workflow.
```
--------------------------------
### Server-Side Rendering with `riot.render`
Source: https://v3.riotjs.vercel.app/api
An example of using `riot.render` on the server-side to convert a Riot.js tag into an HTML string. This is useful for server-side rendering (SSR) applications.
```JavaScript
// render "my-tag" to html
var mytag = require('my-tag')
riot.render(mytag, { foo: 'bar' })
```