Skip to content

Commit 6dbe98a

Browse files
authored
Add Lookbook (#100)
- Add lookbook gem and related dependencies - Add ENABLE_LOOKBOOK environment variable, defaulting to true in local and dev environment - Mount lookbook if ENABLE_LOOKBOOK is set - Add component_preview layout for previewing partials without application header/footer - Add breadcrumb preview - Make partial paths explicit
1 parent 85a45e2 commit 6dbe98a

File tree

11 files changed

+131
-5
lines changed

11 files changed

+131
-5
lines changed

template-only-docs/Deployment.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ While following the [infrastructure template installation instructions](https://
3939
```
4040
1. Follow the infrastructure template instructions to configure [custom domains](https://github.com/navapbc/template-infra/blob/main/docs/infra/set-up-custom-domains.md) and [https support](https://github.com/navapbc/template-infra/blob/main/docs/infra/https-support.md).
4141
42+
## Enabling Lookbook on the dev environment
43+
44+
Lookbook is useful for sharing in progress components with teammates, including product managers and designers. To enable Lookbook on the dev environment, add an environment variable `ENABLE_LOOKBOOK` set to `true` in `/infra/<APP_NAME>/app-config/dev.tf` by adding the following code.
45+
46+
```terraform
47+
service_override_extra_environment_variables = {
48+
ENABLE_LOOKBOOK = "true"
49+
}
50+
```
51+
4252
## Deploying using another method
4353

4454
- AWS Cognito requires a lot of configuration to work correctly. See the Nava Platform infrastructure template for example configuration.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Lookbook
2+
3+
Lookbook is a tool for documenting and showcasing your application's components. It allows you to create a living style guide that can be shared with your team and stakeholders. It is particularly useful for frontend developers, designers, and product managers to see the components in action and understand how they can be used.
4+
5+
## Enabling Lookbook
6+
7+
To enable Lookbook in your application, you need to set the `ENABLE_LOOKBOOK` environment variable to `true`. Locally this can be done by adding the following line to your `.env` file:
8+
9+
```env
10+
ENABLE_LOOKBOOK=true
11+
```
12+
13+
If you are using the Nava Platform infrastructure template, you can enable Lookbook in the dev environment by adding the following code to `/infra/<APP_NAME>/app-config/dev.tf`:
14+
15+
```terraform
16+
service_override_extra_environment_variables = {
17+
ENABLE_LOOKBOOK = "true"
18+
}
19+
```
20+
21+
## Creating Lookbook previews
22+
23+
### Previewing partial components
24+
25+
When creating Lookbook previews for Rails partial components, remember to do the following:
26+
27+
1. Set the layout to `"component_preview"` to avoid rendering the application header and footer.
28+
2. In the render method call, specify the `template:` parameter explicitly, and include the underscore in front of the partial name. At the time of writing, Lookbook does not support rendering with `render "string"` or `render partial: "partial/name"`.
29+
30+
Here's an example of a Lookbook preview that follows these guidelines:
31+
32+
```ruby
33+
class ExamplePreview < Lookbook::Preview
34+
layout "component_preview"
35+
36+
def default
37+
render template: "application/_example", locals: {}
38+
end
39+
end
40+
```

template/{{app_name}}/Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ gem "faraday"
7171
# Create fake data (outside of dev/test block to support seeding lower environments)
7272
gem "faker", "~> 3.2"
7373

74+
gem "lookbook", ">= 2.3.9"
75+
7476
group :development, :test do
7577
gem "guard-rspec", require: false
7678

@@ -83,6 +85,10 @@ group :development, :test do
8385
# Testing framework
8486
gem "factory_bot_rails"
8587
gem "rspec-rails", "~> 6.1.0"
88+
89+
# Required by Lookbook
90+
gem "listen"
91+
gem "actioncable"
8692
end
8793

8894
group :development do

template/{{app_name}}/Gemfile.lock

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ GEM
132132
concurrent-ruby (1.3.5)
133133
connection_pool (2.5.0)
134134
crass (1.0.6)
135+
css_parser (1.21.1)
136+
addressable
135137
cssbundling-rails (1.4.1)
136138
railties (>= 6.0.0)
137139
date (3.4.1)
@@ -191,6 +193,8 @@ GEM
191193
guard (~> 2.1)
192194
guard-compat (~> 1.1)
193195
rspec (>= 2.99.0, < 4.0)
196+
htmlbeautifier (1.4.3)
197+
htmlentities (4.3.4)
194198
i18n (1.14.7)
195199
concurrent-ruby (~> 1.0)
196200
importmap-rails (2.1.0)
@@ -224,6 +228,18 @@ GEM
224228
loofah (2.24.0)
225229
crass (~> 1.0.2)
226230
nokogiri (>= 1.12.0)
231+
lookbook (2.3.9)
232+
activemodel
233+
css_parser
234+
htmlbeautifier (~> 1.3)
235+
htmlentities (~> 4.3.4)
236+
marcel (~> 1.0)
237+
railties (>= 5.0)
238+
redcarpet (~> 3.5)
239+
rouge (>= 3.26, < 5.0)
240+
view_component (>= 2.0)
241+
yard (~> 0.9)
242+
zeitwerk (~> 2.5)
227243
lumberjack (1.2.10)
228244
mail (2.8.1)
229245
mini_mime (>= 0.1.1)
@@ -351,13 +367,15 @@ GEM
351367
ffi (~> 1.0)
352368
rdoc (6.12.0)
353369
psych (>= 4.0.0)
370+
redcarpet (3.6.1)
354371
regexp_parser (2.10.0)
355372
reline (0.6.0)
356373
io-console (~> 0.5)
357374
responders (3.1.1)
358375
actionpack (>= 5.2)
359376
railties (>= 5.2)
360377
rexml (3.4.1)
378+
rouge (4.5.2)
361379
route_translator (14.2.0)
362380
actionpack (>= 6.1)
363381
activesupport (>= 6.1)
@@ -456,6 +474,10 @@ GEM
456474
unicode-emoji (4.0.4)
457475
uri (1.0.3)
458476
useragent (0.16.11)
477+
view_component (3.21.0)
478+
activesupport (>= 5.2.0, < 8.1)
479+
concurrent-ruby (~> 1.0)
480+
method_source (~> 1.0)
459481
warden (1.2.9)
460482
rack (>= 2.0.9)
461483
web-console (4.2.1)
@@ -470,6 +492,7 @@ GEM
470492
websocket-extensions (0.1.5)
471493
xpath (3.2.0)
472494
nokogiri (~> 1.8)
495+
yard (0.9.37)
473496
zeitwerk (2.7.2)
474497

475498
PLATFORMS
@@ -489,6 +512,7 @@ PLATFORMS
489512
x86_64-linux-musl
490513

491514
DEPENDENCIES
515+
actioncable
492516
active_storage_validations
493517
aws-sdk-cognitoidentityprovider (~> 1.88)
494518
aws-sdk-rails
@@ -507,6 +531,8 @@ DEPENDENCIES
507531
jbuilder
508532
jwt
509533
letter_opener
534+
listen
535+
lookbook (>= 2.3.9)
510536
pg (~> 1.1)
511537
pg-aws_rds_iam (~> 0.7.0)
512538
puma (>= 5.0)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class BreadcrumbsPreview < Lookbook::Preview
2+
layout "component_preview"
3+
4+
def default
5+
render template: "application/_breadcrumbs", locals: { crumbs: [
6+
{ name: "Passport applications", url: "default" }
7+
], current_name: "New passport application" }
8+
end
9+
end

template/{{app_name}}/app/views/application/_header.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
<% end %>
3030

3131
<li class="usa-nav__primary-item">
32-
<%= render partial: "language-toggle", locals: { container_class: "display-block desktop:display-none relative" } %>
32+
<%= render partial: "application/language-toggle", locals: { container_class: "display-block desktop:display-none relative" } %>
3333
</li>
3434
</ul>
3535
</div>
3636
</nav>
3737

38-
<%= render partial: "language-toggle", locals: { container_class: "usa-language-container display-none desktop:display-block" } %>
38+
<%= render partial: "application/language-toggle", locals: { container_class: "usa-language-container display-none desktop:display-block" } %>
3939
</div>
40-
</header>
40+
</header>

template/{{app_name}}/app/views/layouts/application.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
<body>
2222
<div class="display-flex flex-column minh-viewport">
23-
<%= render partial: 'header' %>
23+
<%= render partial: 'application/header' %>
2424

2525
<main id="main-content" class="grid-col-fill display-flex flex-column">
2626
<div class="grid-col-fill <%= yield :main_col_class %>">
2727
<section class="usa-section">
2828
<div class="grid-container">
29-
<%= render partial: 'flash' %>
29+
<%= render partial: 'application/flash' %>
3030

3131
<div class="grid-row grid-gap">
3232
<%= yield :before_content_col %>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<%# Layout for Lookbook previews of component partials %>
2+
<!DOCTYPE html>
3+
<html lang="<%= I18n.locale %>">
4+
<head>
5+
<meta name="viewport" content="width=device-width,initial-scale=1">
6+
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
7+
<%= javascript_importmap_tags %>
8+
<%= javascript_include_tag '@uswds/uswds/dist/js/uswds-init.min.js' %>
9+
</head>
10+
11+
<body>
12+
<div class="display-flex flex-column minh-viewport">
13+
<main id="main-content" class="grid-col-fill display-flex flex-column">
14+
<div class="grid-col-fill <%= yield :main_col_class %>">
15+
<div class="grid-container">
16+
<%= content_for?(:content) ? yield(:content) : yield %>
17+
</div>
18+
</div>
19+
</main>
20+
</div>
21+
22+
<%= javascript_include_tag '@uswds/uswds/dist/js/uswds.min.js' %>
23+
</body>
24+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Rails.application.configure do
2+
config.lookbook.preview_paths = [ Rails.root.join("app", "previews") ]
3+
end

template/{{app_name}}/config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# Keep the default /up rails endpoint.
88
get "up" => "rails/health#show"
99

10+
mount Lookbook::Engine, at: "/lookbook" if ENV["ENABLE_LOOKBOOK"].present?
11+
1012
# Support locale prefixes for these routes:
1113
localized do
1214
# Defines the root path route ("/")

0 commit comments

Comments
 (0)