Skip to content

Commit

Permalink
Update first-component.md
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Sep 3, 2024
1 parent 9117488 commit abdbf26
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions guide/first-component.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Building your first component in Phlex

Let's start by building a simple Card component. A Card is a presentational component, it doesn't
really have any behavior. So it's going to be mostly for abstracting away the CSS classes and specific
markup. We'll use simple classes in our example, but you could imagine if you were using a utility
Lets start by building a simple Card component. A Card is a presentational component, it doesnt
really have any behavior. So its going to be mostly for abstracting away the CSS classes and specific
markup. Well use simple classes in our example, but you could imagine if you were using a utility
based CSS framework like Tailwind that you would be abstracting away a lot more.

```ruby
Expand All @@ -13,9 +13,10 @@ class Card < Phlex::HTML
end
```

Not the most exciting component, but it's a start. We can use it like this:
Not the most exciting component, but its a start. We can use it like this:

::: code-group

```ruby [view.rb]
class View < Phlex::HTML
def view_template
Expand All @@ -25,36 +26,39 @@ class View < Phlex::HTML
end
end
```

```html [output]
<div class="card">
<p>Card content</p>
</div>
```

:::

This is nice, but some of our cards will need a header section that has a title. Let's keep adding
This is nice, but some of our cards will need a header section that has a title. Lets keep adding
to our `Card` component so that it can have an optional header:

```ruby
class Card < Phlex::HTML
def view_template(&)
div(class: "card", &)
end
# [!code ++:8]
def header(&)

def header(&) # [!code ++:3]
div(class: "card-header", &)
end

def title(&)
def title(&) # [!code ++:3]
h2(class: "card-title", &)
end
end
```

We've added `header` and `title` methods to our `Card` component. Now when we render a card, we can
Weve added `header` and `title` methods to our `Card` component. Now when we render a card, we can
use the block argument, which will be the instance of our `Card`, and call our new methods:

::: code-group

```ruby [view.rb]
class View < Phlex::HTML
def view_template
Expand All @@ -68,6 +72,7 @@ class View < Phlex::HTML
end
end
```

```html [output]
<div class="card">
<div class="card-header">
Expand All @@ -76,10 +81,11 @@ end
<p>Card content</p>
</div>
```

:::

Ok, this is starting to look a bit better. But we also need a way to add an action button to our card.
This will need a bit more flexibility than just changing the content of the button. Let's see what
This will need a bit more flexibility than just changing the content of the button. Lets see what
that looks like:

```ruby
Expand All @@ -105,6 +111,7 @@ end
This will let us pass in attributes to the action button.

::: code-group

```ruby [view.rb]
class View < Phlex::HTML
def view_template
Expand All @@ -118,6 +125,7 @@ class View < Phlex::HTML
end
end
```

```html [output]
<div class="card">
<div class="card-header">
Expand All @@ -127,13 +135,14 @@ end
<a class="card-action" href="/">Action</a>
</div>
```

:::

This seems like it's working, but we actually have a bit of a problem. We can't specify a class for
This seems like its working, but we actually have a bit of a problem. We cant specify a class for
our action button without overriding the class provided in the `action` method. Any custom class or
classes we provide will clobber the default `card-action` class. Phlex provides a helper method to help
us deal with this situation called `mix`. It mixes attributes together, and is aware of token lists.
Let's update our `action` method to use `mix`:
Lets update our `action` method to use `mix`:

```ruby
def action(**attributes, &)
Expand All @@ -145,6 +154,7 @@ Now if we specify a custom class when we call our `action` method, it will be ad
instead of overriding it.

::: code-group

```ruby [view.rb]
class View < Phlex::HTML
def view_template
Expand All @@ -158,6 +168,7 @@ class View < Phlex::HTML
end
end
```

```html [output]
<div class="card">
<div class="card-header">
Expand All @@ -167,4 +178,5 @@ end
<p>Card content</p>
</div>
```

:::

0 comments on commit abdbf26

Please sign in to comment.