Class: Daisy::Mockup::CodeComponent

Inherits:
LocoMotion::BaseComponent show all
Defined in:
app/components/daisy/mockup/code_component.rb

Defined Under Namespace

Modules: Daisy

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`



113
114
115
116
117
# File 'app/components/daisy/mockup/code_component.rb', line 113

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.



122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/components/daisy/mockup/code_component.rb', line 122

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.



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/components/daisy/mockup/code_component.rb', line 138

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