Skip to content

Commit

Permalink
🛠️ Component: Set up view_component ComponentPreviews
Browse files Browse the repository at this point in the history
- #1187

This is an intermediary step before we look into `LookBook` or similar,
but it:

1. Makes it possible to define component previews in
   `spec/components/previews`
2. Creates a basic layout for showing Components
3. Adds a `CardComponentPreview`
4. Sprouts an `ApplicationComponent` which we can start to massage into
   playing well with tailwind's class syntax
  • Loading branch information
zspencer committed Mar 6, 2023
1 parent e02407f commit 17ddcdc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
19 changes: 19 additions & 0 deletions app/components/application_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ApplicationComponent < ViewComponent::Base
attr_accessor :data
attr_writer :classes

def initialize(data: {}, classes: "")
self.data = data
self.classes = classes
end

# @todo this should gracefully merge left passed in data
def attributes(classes: "")
tag_builder.tag_options(data: data, class: self.classes(classes))
end

# @todo how do we want to handle when tailwind is given conflicting classes? i.e. `mt-5 mt-3`
def classes(others = "")
"#{@classes} #{others}".strip.split.compact.uniq.join(" ")
end
end
2 changes: 1 addition & 1 deletion app/components/card_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div <%= data_attributes %> class="grid grid-cols-1 grid-rows-2 grid-flow-col shadow rounded-lg px-3 bg-white">
<div <%== attributes(classes: "grid grid-cols-1 grid-rows-2 grid-flow-col shadow rounded-lg px-3 bg-white") %>>
<%= content %>
</div>
11 changes: 1 addition & 10 deletions app/components/card_component.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
class CardComponent < ViewComponent::Base
attr_accessor :data

def initialize(data: {})
self.data = data
end

def data_attributes
@data_attributes ||= tag_builder.tag_options(data: data)&.html_safe
end
class CardComponent < ApplicationComponent
end
15 changes: 15 additions & 0 deletions app/views/layouts/component_preview.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
</head>

<body>
<%= yield %>
</body>
</html>
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ class Application < Rails::Application

config.i18n.load_path += Dir[Rails.root.join("config", "locales", "**", "*.{rb,yml}")]
config.i18n.load_path += Dir[Rails.root.join("app", "furniture", "**", "locales", "**", "*.{rb,yml}")]

config.view_component.preview_paths << "#{Rails.root}/spec/components/previews"
config.view_component.default_preview_layout = "component_preview"
end
end
11 changes: 11 additions & 0 deletions spec/components/previews/card_component_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CardComponentPreview < ViewComponent::Preview
def card
render(CardComponent.new(data: {fancy: :pants}, classes: "m-2")) do
<<~HTML.chomp.html_safe # rubocop:disable Rails/OutputSafety
<h3 class='p-2'>Hey There</h3>
<p class='p-2'>You can put stuff in me!</p>
HTML
end
end
end

0 comments on commit 17ddcdc

Please sign in to comment.