All Projects → tobymao → snabberb

tobymao / snabberb

Licence: MIT license
A simple component view framework for Ruby Opal based on Snabbdom

Programming Languages

ruby
36898 projects - #4 most used programming language
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to snabberb

CleanUI
Android library to create beautiful, clean and minimal UIs.
Stars: ✭ 19 (-53.66%)
Mutual labels:  components, views
amplify-ui
A multi-framework Design System to provide a solid foundation for building UI!
Stars: ✭ 487 (+1087.8%)
Mutual labels:  components
lookbook
A tool to help browse, develop, test & document ViewComponents in Ruby on Rails apps 🛠
Stars: ✭ 420 (+924.39%)
Mutual labels:  components
vortex-components
⚛️Vortex Components is a collection of React Components that helps developers build Ethereum Dapps powered by the Vortex Redux Store.
Stars: ✭ 14 (-65.85%)
Mutual labels:  components
vue-mui
Mobile UI elements for Vue 2.0
Stars: ✭ 16 (-60.98%)
Mutual labels:  components
design-system
The Baloise Design System consists of reusable components and a clearly defined visual style, that can be assembled together to build any number of applications.
Stars: ✭ 40 (-2.44%)
Mutual labels:  components
commix
Micro-framework for data-driven composable system architectures
Stars: ✭ 46 (+12.2%)
Mutual labels:  components
tailwind-antd-react-kit
UI Components and helpers for frontend development using Tailwind + Ant Design and React.js
Stars: ✭ 27 (-34.15%)
Mutual labels:  components
rainbow
一款极简单的Vue UI组件库
Stars: ✭ 14 (-65.85%)
Mutual labels:  components
tsstyled
A small, fast, and simple CSS-in-JS solution for React.
Stars: ✭ 52 (+26.83%)
Mutual labels:  components
torchx
TorchX is a universal job launcher for PyTorch applications. TorchX is designed to have fast iteration time for training/research and support for E2E production ML pipelines when you're ready.
Stars: ✭ 165 (+302.44%)
Mutual labels:  components
vue.rb
Ruby bindings for Vue.js
Stars: ✭ 26 (-36.59%)
Mutual labels:  opal
hyperapp-styled-components
styled-components for hyperapp in under 3kb
Stars: ✭ 17 (-58.54%)
Mutual labels:  components
vue-patternfly
PatternFly 3 components for Vue 3
Stars: ✭ 50 (+21.95%)
Mutual labels:  components
components
Reusable React components used by HospitalRun
Stars: ✭ 109 (+165.85%)
Mutual labels:  components
craft-helper
A collection of useful Craft CMS macros and components.
Stars: ✭ 23 (-43.9%)
Mutual labels:  components
hsx
Static HTML sites with JSX and webpack (no React).
Stars: ✭ 15 (-63.41%)
Mutual labels:  components
v-svg-icons
Svg icons for VueJS.
Stars: ✭ 36 (-12.2%)
Mutual labels:  components
wexond-ui
List of reusable React components following Wexond design patterns
Stars: ✭ 24 (-41.46%)
Mutual labels:  components
taroify
Taroify 是移动端组件库 Vant 的 Taro 版本,两者基于相同的视觉规范,提供一致的 API 接口,助力开发者快速搭建小程序应用。
Stars: ✭ 420 (+924.39%)
Mutual labels:  components

Snabberb

Snabberb is a simple Ruby view framework built on Opal and Snabbdom.

You can write reactive views in plain Ruby that compile to efficient Javascript.

Inline Example

require 'opal'
require 'snabberb'

class TextBox < Snabberb::Component
  needs :text
  needs :selected, default: false, store: true

  def render
    onclick = lambda do
      store(:selected, !@selected)
    end

    style = {
      cursor: 'pointer',
      border: 'solid 1px rgba(0,0,0,0.2)',
    }

    style['background-color'] = 'lightblue' if @selected

    h(:div, { style: style, on: { click: onclick } }, [
      h(:div, @text)
    ])
  end
end


# Assuming you have a DOM element with ID=app
TextBox.attach('app', text: 'hello world')

# Or you can get the HTML string for isomorphic applications
TextBox.html(text: 'hello world')

Examples

Rack App

Roda App with HTML Prerendering

18xx Board Game Engine

Usage

Creating DOM Elements With h

Subclass Snabberb::Component and override #render to build divs using #h.

Render should only return one root element.

#h takes either a DOM symbol (:div, :span, :a, ...) or another Snabberb::Component class.

...
class DomExample < Snabberb::Component
  def render
    h(:div)
  end
end

class ComponentExample < Snabberb::Component
  def render
    h(OtherComponent)
  end
end

Like Snabbdom, #h with DOM elements can take props which take the form of a dict.

...
class PropsExample < Snabberb::Component
  def render
    h(:div, { style: { display: 'inline-block' }, class: { selected: true } })
  end
end

Components do not take props, instead they take needs which are dependent arguments.

...
class PassingNeedsExample < Snabberb::Component
  def render
    h(ChildComponent, need1: 1, need2: 2)
  end
end

#h can also be nested with a child or multiple children.

...
class NestedExample < Snabberb::Component
  def render
    h(:div, [
      h(ChildComponent, need1: 1, need2: 2),
      h(:div, { style: { width: '100px' } }, [
        h(:div, 'hello'),
      ])
    ])
  end
end

Needs

Components can define needs which allow parent components to pass down arguments. They can also be stateful which allows changes to propogate easily throughout the application.

Needs are by default required. They can be set with default values. Needs are accesible with instance variables that are automatically set.

...
class NeedsExample < Snabberb::Component
  needs :name
  needs :value, default: 0, store: true

  def render
    onclick = lambda do
      store(:value, @value + 1)
    end

    h(:div, [
      h(:div, @name),
      h(:div, { on: { click: onclick} }, @value),
    ])
  end
end

When simple state changes must be tracked, a need can define store: true. This will use the stored value of this key which is set on the root node. The precedence of need values is stored > passed needs > default value.

Needs can be set with #store which will trigger a view update. Snabberb uses Snabbdom to update the DOM, so only the differences in the DOM are changed.

Prerendering

You can prerender your HTML by calling

Snabberb.prerender_script('LayoutClass', 'ApplicationClass', 'application_id', javascript_include_tags: '', **needs)

A detailed example can be found in the Roda example.

Generating HTML from a File

You can generate HTML from a component with a file.

Snabberb.html_script('path/to/my_component.rb', **needs)

This reads in the ruby file at the path and generates javascript that calls html on the CamelCased version of the file name.

Installation

Add this line to your application's Gemfile:

gem 'snabberb'

And then execute:

$ bundle

Or install it yourself as:

$ gem install snabberb

Development

bundle install
bundle exec rake

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/tobymao/snabberb.

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].