### Install dependencies
Source: https://github.com/lookbook-hq/lookbook/blob/main/CONTRIBUTING.md
Commands to install necessary Ruby and JavaScript dependencies for development.
```bash
bundle install
```
```bash
bundle exec rspec
```
```bash
bundle exec appraisal install
```
```bash
npm install
```
```bash
npm install -g parcel
```
--------------------------------
### Start Bridgetown Development Server
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/README.md
Use this command to start the Bridgetown development server for Lookbook.
```shell
bin/bridgetown start
```
--------------------------------
### Install Dependencies with Bundler
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_extend/contributing.md
Use this command to install project dependencies. Ensure you have Bundler installed.
```bash
bundle install
```
--------------------------------
### ViewComponent Preview Class Example
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/components/view_component.md
This is an example of a ViewComponent preview class that extends `ViewComponent::Preview`. Lookbook automatically recognizes and renders these previews.
```ruby
class ExampleComponentPreview < ViewComponent::Preview
def with_default_title
render(ExampleComponent.new(title: "Example component default"))
end
end
```
--------------------------------
### Enable alphabetical sorting for examples
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/navigation.md
Set the sort_examples configuration option to true in your application configuration to sort preview examples alphabetically.
```rb
# config/application.rb
config.lookbook.sort_examples = true
```
--------------------------------
### Annotated Preview File Example
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/annotations.md
This example demonstrates how to use various annotations like @label, @display, @param, and @hidden within a Lookbook preview class to control rendering and navigation.
```ruby
# @label Basic Button
# @display bg_color "#fff"
class ButtonComponentPreview < Lookbook::Preview
# Primary button
# ?-------------
# This is the button style you should use for most things.
#
# @label Primary
def default
render ButtonComponent.new do
"Click me"
end
end
# Button with icon
# ?-------------
# This example uses dynamic preview parameters
# which can be edited live in the Lookbook UI
#
# @param text
# @param icon select [heart, cog, alert]
def icon(text: "Spread the love", icon: "heart")
render ButtonComponent.new(icon: icon) do
text
end
end
# Inverted button
# ?-------------
# For light-on-dark screens
#
# @display bg_color "#000"
def secondary
render ButtonComponent.new(style: :inverted) do
"Click me"
end
end
# Unicorn button
# ?-------------
# This button style is still a **work in progress**
# and so has been hidden from the navigation.
#
# @hidden
def unicorn
render ButtonComponent.new do
"Click me"
end
end
# @!group More examples
def short_text
render ButtonComponent.new do
"Go"
end
end
def long_text
render ButtonComponent.new do
"Click here to do this thing because it's the best way to do it"
end
end
def emoji_text
render ButtonComponent.new do
"👀📗"
end
end
# @!endgroup
end
```
--------------------------------
### Example URL for Dynamic Parameters
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/index.md
Demonstrates how to construct a URL to pass a specific value for a dynamic parameter to a preview scenario.
```text
/lookbook/inspect/example/with_dynamic_title?title=Custom+title
```
--------------------------------
### Configure Tab Frontmatter
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/pages/index.md
Examples of setting tab labels and ordering priority using frontmatter.
```yaml
---
label: Website
---
Tab page content here...
```
```yaml
---
label: Website
priority: 1
---
Tab page content here...
```
--------------------------------
### Notes Panel Content Example
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/annotations.md
This example illustrates how regular comment text, not starting with a tag, is rendered as Markdown in the Notes panel for a preview.
```ruby
class ProfileCardComponentPreview < Lookbook::Preview
# Profile Card
# ?-------------
# Use the default profile card component whenever you need to represent a user.
#
# All this text will be included in the Notes panel for this preview.
def default
end
end
```
--------------------------------
### Order Pages and Directories
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/pages/index.md
Example of using numeric prefixes to control the navigation order of pages and directories.
```text
test/components/docs/
├── 01_overview.md.erb
├── 02_implementation_notes/
│ ├── 01_slots.md.erb
│ └── 02_html_attributes.md.erb
└── 03_helpful_examples/
├── 01_basic_components.md.erb
└── 02_complex_components.md.erb
```
--------------------------------
### GET /previews.json
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/json_endpoints.md
Retrieves a list of all previews and their associated scenarios in JSON format.
```APIDOC
## GET /previews.json
### Description
This endpoint returns a JSON array containing all previews and their scenarios. Each preview object includes its name and a list of scenarios, where each scenario has a name and an inspect path.
### Method
GET
### Endpoint
/previews.json
### Response
#### Success Response (200)
- **Array** - A list of preview objects.
- **name** (string) - The name of the preview.
- **scenarios** (Array) - A list of scenarios for the preview.
- **name** (string) - The name of the scenario.
- **inspect_path** (string) - The path to inspect the scenario.
#### Response Example
```json
[
{
"name": "annotated",
"scenarios":
[
{
"name": "default",
"inspect_path": "/lookbook/inspect/foo/bar/annotated/default"
}
]
}
]
```
```
--------------------------------
### Panel View Partial Example
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_extend/panels/index.md
This is an example of a view partial for a custom inspector panel. It displays information about the current preview, its file path, and the number of scenarios.
```erb
Some information
You are looking at the '<%%= preview.label %>' preview
The preview file path is: '<%%= preview.full_path %>'
There are <%%= scenarios.size %> scenarios in this preview
```
--------------------------------
### Define Page Directory Structure
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/pages/index.md
Example directory structure for organizing documentation pages within the project.
```text
test/components/docs/
├── overview.md.erb
├── best_practices.md.erb
└── branding/
├── logos.md.erb
└── themes.md.erb
```
--------------------------------
### Default Preview Template
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/index.md
An example of a default template file for a 'default' scenario, showing how to render a component within an HTML structure.
```erb
<%%= render CellComponent.new %>
```
--------------------------------
### Create Multiple Groups Within a Preview Class
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/groups.md
This example illustrates how to define multiple distinct groups of scenarios within a single preview class. Each scenario can only belong to one group, and any scenarios not tagged with `@!group` will appear individually.
```ruby
class ExampleComponentPreview < ViewComponent::Preview
# @!group First Group
def first_scenario
# ...
end
def second_scenario
# ...
end
# @!endgroup
# @!group Second Group
def third_scenario
# ...
end
def fourth_scenario
# ...
end
# @!endgroup
def not_in_a_group
# ...
end
end
```
--------------------------------
### Create a Markdown Page with Frontmatter
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/pages/index.md
Example of a markdown page file using YAML frontmatter and ERB syntax.
```markdown
---
title: An example page
label: Nice example
---
This is an example page. If it has a `.md.erb` file extension its
contents will be run through a Markdown parser/renderer before display.
Fenced code blocks are fully supported and will be highlighted appropriately.
ERB can be used in here.
The template will be rendered **before** being parsed as Markdown.
You can can access data about the page using the `@page` variable.
The title of this page is "<%%= @page.title %>".
```
--------------------------------
### Set Up Appraisal for Gemset Testing
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_extend/contributing.md
Appraisal is used to test Lookbook against different versions of its dependencies. Run this command after installing gems.
```bash
bundle exec appraisal install
```
--------------------------------
### Param Tag Format and Options
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/params.md
Understand the format of the `@param` tag, which includes the parameter name, input type, an optional description, and optional options for customization. This example shows how to define parameters for a ButtonComponent.
```ruby
@param
```
```ruby
class ButtonComponentPreview < ViewComponent::Preview
# @param arrow toggle
# @param theme select { choices: [primary, secondary, danger] }
# @param content text "The text to display in the button"
def default(content: 'Click me', theme: 'primary', arrow: true)
render Elements::ButtonComponent.new(theme: theme, arrow: arrow) do
content
end
end
end
```
--------------------------------
### Example Standalone Preview Layout Template
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/layouts.md
This ERB template demonstrates a standalone preview layout. It utilizes display variables for dynamic background color and max-width, allowing per-preview customization.
```erb
">
Component Preview
<%%= stylesheet_pack_tag 'application', media: 'all' %>
<%%= javascript_pack_tag 'application' %>
">
<%%= yield %>
```
--------------------------------
### Render a select input template
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_extend/inputs/templates.md
Example of a system-provided select param input template using standard Rails form helpers.
```erb
<%%= select_tag(name,
options_for_select(choices || [], value),
**input_options,
"x-model": "value"
) %>
```
--------------------------------
### Supply Dynamic Params via URL
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/params.md
Provide values for dynamic parameters by appending them to the preview URL as query parameters. This example demonstrates how to set a custom title for the `with_dynamic_title` preview.
```http
/lookbook/inspect/example/with_dynamic_title?title=Custom+title
```
--------------------------------
### Group Scenarios Using @!group Tags
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/groups.md
This example demonstrates grouping the 'small', 'medium', and 'big' scenarios for the HeaderComponent using `@!group Sizes` and `@!endgroup` tags. Only the 'long_text' scenario remains ungrouped, resulting in a single preview for the size variations and a separate preview for the long text.
```ruby
class HeaderComponentPreview < ViewComponent::Preview
# @!group Sizes
def small
render HeaderComponent.new(size: 20) do
"Small header"
end
end
def medium
render :HeaderComponent.new(size: 30) do
"Medium header"
end
end
def big
render HeaderComponent.new(size: 44) do
"Big header"
end
end
# @!endgroup
def long_text
render HeaderComponent.new do
"This is a header with some long text within it that keeps on going on"
end
end
end
```
--------------------------------
### Identify Template Errors
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/troubleshooting.md
Example of the error message triggered by incompatible link_to helpers in preview layouts.
```text
No route matches {:action=>"preview", :controller=>"lookbook/previews", :path=>"path_to/current_component"}
```
--------------------------------
### Tag Usage Example
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/annotations.md
This snippet shows how to apply @display and @param tags to a method, along with a @label tag to the class, to control preview rendering and parameter definition.
```ruby
# @label Basic Button
class ButtonComponentPreview < Lookbook::Preview
# @display bg_color red
# @param icon select [heart, cog, alert]
def icon(icon: "heart")
render ButtonComponent.new(icon: icon) do
"Spread the love"
end
end
end
```
--------------------------------
### Define Dynamic Params in Preview Class
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/params.md
Set dynamic values from URL parameters by defining them as arguments in your preview class. This example shows how to define a dynamic title for an ExampleComponent.
```ruby
class ExampleComponentPreview < Lookbook::Preview
def with_dynamic_title(title: "Example component default")
render(ExampleComponent.new(title: title))
end
end
```
--------------------------------
### Define a Custom Label for a Scenario Group
Source: https://github.com/lookbook-hq/lookbook/blob/main/docs/src/_guide/previews/groups.md
This snippet shows how to assign a custom label, 'Some Nice Examples', to a group of scenarios using the `@!group