Class: Daisy::Mockup::CodeComponent

Inherits:
LocoMotion::BaseComponent show all
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

#config, #loco_parent

Instance Method Summary collapse

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

#build_inspect_string

Constructor Details

#initialize(**kws) ⇒ CodeComponent

Creates a new code block component.

Parameters:

  • kws (Hash)

    a customizable set of options

Options Hash (**kws):

  • prefix (String)

    Optional prefix for all lines (if not using individual line prefixes).

  • css (String)

    Additional CSS classes for styling. Common options include:

    • Size: w-full, max-w-3xl
    • Background: bg-base-300, bg-neutral
    • Text: text-sm, text-base-content


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_renderObject

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

#callObject

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