Class: Shadcn::Sidebar::Component

Inherits:
ApplicationViewComponent show all
Defined in:
app/components/shadcn/sidebar/component.rb

Overview

The panel itself — upstream's Sidebar (vendor/shadcn/ui/sidebar.tsx:154), not its provider, which is Sidebar::Provider::Component.

Upstream renders three different trees here and picks between them while rendering. Two of those choices are knowable on a server and one is not:

collapsible: :none  a bare panel, decided here
isMobile            a Sheet, decided by `matchMedia` — so not here

The mobile branch is the controller's, applied to this same DOM without moving it. See decisions/02-javascript.md.

Constant Summary

Constants inherited from ApplicationViewComponent

ApplicationViewComponent::CONTENTS_STYLE, ApplicationViewComponent::VOID_TAGS

Instance Attribute Summary collapse

Attributes inherited from ApplicationViewComponent

#attributes

Instance Method Summary collapse

Methods inherited from ApplicationViewComponent

default_tag, #merged_style, #render_element, #shadcn_t, slot_name, #style_variants, #tag_name

Constructor Details

#initialize(side: :left, variant: :sidebar, collapsible: :offcanvas, **attributes) ⇒ Component

Returns a new instance of Component.



25
26
27
28
29
30
# File 'app/components/shadcn/sidebar/component.rb', line 25

def initialize(side: :left, variant: :sidebar, collapsible: :offcanvas, **attributes)
  @side = side&.to_sym || :left
  @variant = variant&.to_sym || :sidebar
  @collapsible = collapsible&.to_sym || :offcanvas
  super(**attributes)
end

Instance Attribute Details

#collapsibleObject (readonly)

Returns the value of attribute collapsible.



23
24
25
# File 'app/components/shadcn/sidebar/component.rb', line 23

def collapsible
  @collapsible
end

#sideObject (readonly)

Returns the value of attribute side.



23
24
25
# File 'app/components/shadcn/sidebar/component.rb', line 23

def side
  @side
end

#variantObject (readonly)

Returns the value of attribute variant.



23
24
25
# File 'app/components/shadcn/sidebar/component.rb', line 23

def variant
  @variant
end

Instance Method Details

#callObject



59
60
61
62
63
# File 'app/components/shadcn/sidebar/component.rb', line 59

def call
  return render_element(body: content) if bare?

  render_element(body: safe_join([ overlay, gap, container ]))
end

#css_classes(extra = nil) ⇒ Object

collapsible: :none is a different element with different classes, not the same one with a flag (sidebar.tsx:166-180).



51
52
53
54
55
56
57
# File 'app/components/shadcn/sidebar/component.rb', line 51

def css_classes(extra = nil)
  return super if !bare?

  ShadcnViewComponent.cn(
    "flex h-full w-(--sidebar-width) flex-col bg-sidebar text-sidebar-foreground", extra
  )
end

#element_attributes(**defaults) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/components/shadcn/sidebar/component.rb', line 32

def element_attributes(**defaults)
  return super(**defaults) if bare?

  super(**{
    "data-shadcn--sidebar-target" => "sidebar",
    "data-state" => "expanded",
    # Upstream fills this only while collapsed (sidebar.tsx:212) — the
    # classes match `group-data-[collapsible=offcanvas]:…`, so a value here
    # while expanded would style an open panel as a closed one. The value
    # to collapse *to* therefore has to live somewhere else.
    "data-collapsible" => "",
    "data-sidebar-collapsible" => collapsible,
    "data-variant" => variant,
    "data-side" => side
  }.merge(defaults))
end