Class: Daisy::Mockup::CodeComponent
- Inherits:
-
LocoMotion::BaseComponent
- Object
- ViewComponent::Base
- LocoMotion::BaseComponent
- Daisy::Mockup::CodeComponent
- Defined in:
- app/components/daisy/mockup/code_component.rb
Overview
The CodeComponent creates stylized code blocks for displaying code snippets, terminal output, or any text that benefits from monospace formatting. Common use cases include:
- Displaying code examples.
- Showing terminal commands and output.
- Highlighting important configuration settings.
- Creating interactive tutorials.
The component supports line prefixes (e.g., for line numbers or command prompts), syntax highlighting via CSS classes, and multi-line code blocks.
Constant Summary
Constants inherited from LocoMotion::BaseComponent
LocoMotion::BaseComponent::EMPTY_PART_IGNORED_TAGS, LocoMotion::BaseComponent::SELF_CLOSING_TAGS
Instance Attribute Summary
Attributes inherited from LocoMotion::BaseComponent
Instance Method Summary collapse
-
#before_render ⇒ Object
Sets up the component's CSS classes and HTML attributes.
-
#call ⇒ Object
Renders the code block with its lines or direct content.
-
#initialize(**kws) ⇒ CodeComponent
constructor
Creates a new code block component.
Methods inherited from LocoMotion::BaseComponent
build, #component_ref, #config_option, #cssify, define_modifier, define_modifiers, define_part, define_parts, define_size, define_sizes, #empty_part_content, #inspect, #part, register_component_initializer, register_component_setup, #rendered_css, #rendered_data, #rendered_html, #rendered_stimulus_controllers, #rendered_tag_name, renders_many, renders_one, set_component_name, #set_loco_parent, #strip_spaces
Methods included from LocoMotion::Concerns::InspectableComponent
Constructor Details
#initialize(**kws) ⇒ CodeComponent
Creates a new code block component.
116 117 118 119 120 |
# File 'app/components/daisy/mockup/code_component.rb', line 116 def initialize(**kws) super(**kws) @prefix = config_option(:prefix) end |
Instance Method Details
#before_render ⇒ Object
Sets up the component's CSS classes and HTML attributes.
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/components/daisy/mockup/code_component.rb', line 125 def before_render add_css(:component, "mockup-code") set_tag_name(:pre, :pre) set_tag_name(:code, :code) add_html(:pre, { "data-prefix": @prefix }) if @prefix # If the prefix is blank, add some left margin and hide the :before # pseudo-element used for the prefix add_css(:pre, "before:!hidden ml-6") if @prefix.blank? end |
#call ⇒ Object
Renders the code block with its lines or direct content.
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'app/components/daisy/mockup/code_component.rb', line 141 def call part(:component) do if lines.any? lines.each { |line| concat(line) } else part(:pre) do part(:code) do content end end end end end |