Class: Proscenium::ViewComponent::ReactComponent

Inherits:
Base
  • Object
show all
Includes:
ReactComponentable
Defined in:
lib/proscenium/view_component/react_component.rb

Overview

Renders a <div> for use with React components, with data attributes specifying the component path and props.

If a content block is given, that content will be rendered inside the component, allowing for a “loading” UI. If no block is given, then a “loading…” text will be rendered. It is intended that the component is mounted to this div, and the loading UI will then be replaced with the component’s rendered output.

Instance Method Summary collapse

Methods inherited from Base

inherited, sideload_assets

Methods included from CssModules

included

Instance Method Details

#callObject



16
17
18
19
20
# File 'lib/proscenium/view_component/react_component.rb', line 16

def call
  tag.send root_tag, data: data_attributes do
    tag.div content || 'loading...'
  end
end

#virtual_pathObject



22
23
24
25
26
27
# File 'lib/proscenium/view_component/react_component.rb', line 22

def virtual_path
  @virtual_path ||= begin
    paths = Proscenium::Resolver.resolve(self.class.source_path.sub_ext('.jsx').to_s)
    paths.is_a?(Array) ? paths.find { |x| x.ends_with?('.js') } : paths
  end
end