Class: LcpRuby::Display::Renderers::CopyCode

Inherits:
BaseRenderer show all
Defined in:
lib/lcp_ruby/display/renderers/copy_code.rb

Instance Method Summary collapse

Methods inherited from BaseRenderer

#link_producing?

Instance Method Details

#render(value, options = {}, record: nil, view_context: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lcp_ruby/display/renderers/copy_code.rb', line 5

def render(value, options = {}, record: nil, view_context: nil)
  return "" if value.blank?

  text = value.to_s
  button_label = options["button_label"] ||
    I18n.t("lcp_ruby.actions.copy", default: "Copy")

  view_context.(
    :div,
    class: "lcp-copy-code",
    data: { controller: "clipboard" }
  ) do
    code = view_context.(:code, text, class: "lcp-copy-code__value")
    button = view_context.button_tag(
      button_label,
      type: "button",
      class: "lcp-copy-code__btn",
      data: {
        action: "clipboard#copy",
        clipboard_text_value: text
      }
    )
    view_context.safe_join([ code, button ])
  end
end