Class: Panda::Core::Shared::HeaderComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/panda/core/shared/header_component.rb

Overview

Header component for HTML document head Handles title, meta tags, stylesheets, and JavaScript

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html_class: "", body_class: "", **attrs) ⇒ HeaderComponent

Returns a new instance of HeaderComponent.



9
10
11
12
13
# File 'app/components/panda/core/shared/header_component.rb', line 9

def initialize(html_class: "", body_class: "", **attrs)
  super()
  @html_class = html_class
  @body_class = body_class
end

Instance Attribute Details

#body_classObject (readonly)

Returns the value of attribute body_class.



15
16
17
# File 'app/components/panda/core/shared/header_component.rb', line 15

def body_class
  @body_class
end

#html_classObject (readonly)

Returns the value of attribute html_class.



15
16
17
# File 'app/components/panda/core/shared/header_component.rb', line 15

def html_class
  @html_class
end

Instance Method Details

#additional_head_contentObject

Render Panda::Core.config.additional_head_content with a view context.

Called with no receiver and no arguments, a host app's lambda has no view and no request, so content_security_policy_nonce is nil inside it and anything it injects (javascript_importmap_tags included) comes out un-nonced and is dropped by a strict script-src.

Two shapes are supported, so existing configuration keeps working:

# Arity 0 - evaluated against the view context, so view helpers and
# content_security_policy_nonce resolve directly.
config.additional_head_content = -> { javascript_importmap_tags(nonce: content_security_policy_nonce) }

# Arity 1 - the view context is yielded as an argument.
config.additional_head_content = ->(view) { view.javascript_importmap_tags(nonce: view.content_security_policy_nonce) }


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/components/panda/core/shared/header_component.rb', line 40

def additional_head_content
  content = Panda::Core.config.additional_head_content
  return nil unless content.respond_to?(:call)

  result =
    if content.arity == 0
      helpers.instance_exec(&content)
    else
      content.call(helpers)
    end

  result&.to_s&.html_safe
end

#html_langObject

Language for the attribute.

Screen readers use it to pick a pronunciation, so a page without one is read in whatever the reader's default happens to be.



21
22
23
# File 'app/components/panda/core/shared/header_component.rb', line 21

def html_lang
  I18n.locale.to_s.presence || "en"
end