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

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`



106
107
108
109
110
# File 'app/components/daisy/mockup/code_component.rb', line 106

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.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/components/daisy/mockup/code_component.rb', line 115

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.



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/components/daisy/mockup/code_component.rb', line 131

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