Clearwater is a rich front-end framework for building fast, reasonable, and easily composable browser applications in Ruby. It renders to a virtual DOM and applies the virtual DOM to the browser's actual DOM to update only what has changed on the page.
If you're using this as a standalone application then just install via:
$ gem install clearwater
If you're using rails then add these lines to your application's Gemfile:
gem 'clearwater'
gem 'opal-rails' # Only needed for Rails appsClearwater has three distinct parts:
- The component: the presenter and template engine
- The router: the dispatcher and control
- The application: the "Go" button
The Component
class Blog
include Clearwater::Component
# The render method defines what gets rendered. It needs to return a
# virtual-DOM element using the element DSL.
def render
# Element DSL is tag_name(attributes, content)
# The attributes can be omitted if you don't need to customize the element.
div({ id: 'foo' }, [
# Content types:
h1('Heading'), # Another element
'Hello, world!', # String
123, # Numeric
nil, # Absolutely nothing
ArticleIndex.new # Another component
])
end
endThe Router
router = Clearwater::Router.new do
route 'blog' => Blog.new do
route 'new_article' => NewArticle.new
route ':article_id' => ArticleReader.new
end
endYou can also use Clearwater as part of the Rails asset pipeline. First create your application:
# file: app/assets/javascripts/application.rb
require 'opal' # Not necessary if you load Opal from a CDN
require 'clearwater'
class Layout
include Clearwater::Component
def render
div({ id: 'app' }, [
header({ class_name: 'main-header' }, [
h1('Hello, world!'),
]),
outlet, # This is what renders subordinate routes
])
end
end
class Articles
include Clearwater::Component
def render
div({ id: 'articles-container '}, [
input({ class_name: 'search-articles', onkeyup: method(:search) }),
ul({ id: 'articles-index' }, articles.map { |article|
ArticlesListItem.new(article)
}),
outlet, # This is what renders subordinate routes (e.g. Article)
])
end
def articles
@articles ||= MyStore.fetch_articles
if @query
@articles.select { |article| article.match?(@query) }
else
@articles
end
end
def search(event)
@query = event.target.value
call # Rerender the app
end
end
class ArticlesListItem
include Clearwater::Component
attr_reader :article
def initialize(article)
@article = article
end
def render
# Note the "key" key in this hash. This is a hint to the virtual DOM that
# if this node is moved around, it can still reuse the same element.
li({ key: article.id, class_name: 'article' }, [
# The Link component will rerender the app for the new URL on click
Link.new({ href: "/articles/#{article.id}" }, article.title),
time({ class_name: 'timestamp' }, article.timestamp.strftime('%m/%d/%Y')),
])
end
end
class Article
include Clearwater::Component
def render
# In addition to using HTML5 tag names as methods, you can use the `tag`
# method with a query selector to generate a tag with those attributes.
tag('article.selected-article', nil, [
h1({ class_name: 'article-title' }, article.title),
time({ class_name: 'article-timestamp' }, article.timestamp.strftime('%m-%d-%Y')),
section({ class_name: 'article-body' }, article.body),
])
end
def article
# params[:article_id] is the section of the URL that contains what would be
# the `:article_id` parameter in the router below.
MyStore.article(params[:article_id])
end
def match? query
query.split.all? { |token|
title.include?(token) || body.include?(token)
}
end
end
module MyStore
extend self
DB = 5.times.map do |n|
OpenStruct.new(id: n, timestamp: Time.new, title: "Random thoughts n.#{n}", body: 'Some deep stuff')
end
def fetch_articles
DB
end
def article(id)
id = id.to_i
DB.find {|a| a.id == id}
end
end
router = Clearwater::Router.new do
route 'articles' => Articles.new do
route ':article_id' => Article.new
end
end
MyApp = Clearwater::Application.new(
component: Layout.new,
router: router,
element: $document.body # This is the default target element
)
MyApp.call # Render the app.- Fork it
- Branch it
- Hack it
- Save it
- Commit it
- Push it
- Pull-request it
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
This Code of Conduct is adapted from the Contributor Covenant, version 1.0.0, available at http://contributor-covenant.org/version/1/0/0/
Copyright (c) 2014-2015 Jamie Gaskins
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.