Skip to content

Commit b083b76

Browse files
committed
minor #2072 [Cookbook] English / doc / code corrections (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Cookbook] English / doc / code corrections One commit per "category" `@WebMamba` I don't get the Stimulus button example... how would it work ? Commits ------- 85426fa [Cookbook] English / doc / code corrections
2 parents e59b665 + 85426fa commit b083b76

File tree

1 file changed

+50
-53
lines changed

1 file changed

+50
-53
lines changed
Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
title: Component architecture
3-
description: Rules and pattern to work with components
2+
title: Component Architecture
3+
description: Rules and patterns for working with components
44
image: images/cookbook/component-architecture.png
55
tags:
66
- JavaScript
@@ -11,25 +11,25 @@ published_at: '2024-08-02'
1111

1212
## Introduction
1313

14-
In Symfony UX exist two packages: [TwigComponents](https://symfony.com/bundles/ux-twig-component/current/index.html) and [LiveComponent](https://symfony.com/bundles/ux-live-component/current/index.html).
15-
Those two packages allow you to create reusable components in your Symfony application.
16-
But the component architecture is not exclusive to Symfony, it's a design pattern that can be applied to any programming language or framework.
17-
And the Javascript world already implements this architecture for long time, on many different frameworks like React, Vue, or Svelte.
18-
So, a set of rules and patterns has already be defined to work with components. This is why Symfony UX tries to be as close as possible to those rules.
19-
So, let's see what those rules are!
14+
In Symfony UX, there are two packages: [TwigComponents](https://symfony.com/bundles/ux-twig-component/current/index.html) and [LiveComponent](https://symfony.com/bundles/ux-live-component/current/index.html).
15+
These packages allow you to create reusable components in your Symfony application.
16+
However, component architecture is not exclusive to Symfony; it's a design pattern that can be applied to any programming language or framework.
17+
The JavaScript world has implemented this architecture for a long time, across many frameworks like React, Vue, or Svelte.
18+
A set of rules and patterns has already been defined for working with components. This is why Symfony UX tries to adhere closely to these rules.
19+
Let's explore what these rules are!
2020

2121
## 4 Rules
2222

2323
### Composition
2424

25-
A page is no longer just a page, but rather a collection of small, reusable components.
26-
These components can be assembled to form a page. For example, there could be a component for the title and another for the training list.
27-
The training list component could even be composed of smaller components, such as a training card component.
28-
The goal is to create the most atomic, and reusable components possible.
25+
A page is no longer just a page but rather a collection of small, reusable components.
26+
These components can be assembled to form a page. For example, there could be a component for the title and another for the training list.
27+
The training list component could even be composed of smaller components, such as a training card component.
28+
The goal is to create the most atomic and reusable components possible.
2929

30-
#### How does it work into Symfony?
30+
***How does it work in Symfony?***
3131

32-
In Symfony you can have a component Alert for example with the following template:
32+
In Symfony, you can have an `Alert` component, for example, with the following template:
3333

3434
```twig
3535
<div class="alert alert-{{ type }}">
@@ -38,8 +38,8 @@ In Symfony you can have a component Alert for example with the following templat
3838
</div>
3939
```
4040

41-
So here you can see we have an alert component that his himself use an Icon component.
42-
Or you can make composition with the following syntax:
41+
So here you can see we have an `Alert` component that itself uses an Icon component.
42+
Or you can compose with the following syntax:
4343

4444
```twig
4545
<twig:Card>
@@ -50,30 +50,29 @@ Or you can make composition with the following syntax:
5050
</twig:Card>
5151
```
5252

53-
So here we have a Card component, and we give to the content of this component two other components.
53+
So here we have a `Card` component, and we provide the content of this component with two other components.
5454

5555
### Independence
5656

57-
This is a really important rule, and not obvious. But your component should live on his own context,
58-
it should not be aware of the rest of the page. You should be able to take a component into a page, from another and it should work exactly the same.
57+
This is a really important rule and not an obvious one. Your component should live in its own context; it
58+
should not be aware of the rest of the page. You should be able to take a component from one page to another, and it should work exactly the same.
5959
This rule makes your component truly reusable.
6060

61-
***How does it work into Symfony?***
61+
***How does it work in Symfony?***
6262

63-
Symfony keeps the context of the page into the context of your component. So this your own responsibility to follow those rules.
64-
But notice that if there are conflicts between a variable from the context page and your component, your component context overrides the page context.
63+
Symfony keeps the context of the page within the context of your component. So it is your own responsibility to follow these rules.
64+
Note that if there are conflicts between a variable from the context page and your component, your component context overrides the page context.
6565

6666
### Props
6767

68-
Our component must remain independent, but we can customize it props.
69-
Let's take the example of a button component. You have your component that look on every page the same,
70-
the only change is the label. What you can do is to declare a prop `label` into your button component.
71-
And so now when you want to use your button component, you can pass the label you want as props. The component gonna take
72-
this props at his initialization and keep it all his life long.
68+
Our component must remain independent, but we can customize its props.
69+
For example, consider a button component. You want your component to look the same on every page, with the only change being the label.
70+
To do this, you can declare a `label` prop in your button component.
71+
When you use your button component, you can pass the label you want as a prop. The component will take this prop at initialization and keep it throughout its lifecycle.
7372

74-
***How does it work into Symfony?***
73+
***How does it work in Symfony?***
7574

76-
Let's take the example of the Alert component an [anonymous component](https://symfony.com/bundles/ux-twig-component/current/index.html#anonymous-components).
75+
Let's take the example of the `Alert` component as an [anonymous component](https://symfony.com/bundles/ux-twig-component/current/index.html#anonymous-components).
7776
We have the following template:
7877

7978
```twig
@@ -85,16 +84,16 @@ We have the following template:
8584
</div>
8685
```
8786

88-
Just like that we define three props for our Alert component. And know we can use like this:
87+
Just like that, we define three props for our `Alert` component. We can now use it like this:
8988

9089
```twig
9190
<twig:Alert type="success" icon="check" message="Your account has been created." />
9291
```
9392

94-
If your component anonymous but a class component, you can simply define props
95-
by adding property to your class.
93+
If your component is not anonymous but a class component, you can define props by adding properties to your class.
9694

9795
```php
96+
#[AsTwigComponent]
9897
class Alert
9998
{
10099
public string $type;
@@ -103,35 +102,32 @@ class Alert
103102
}
104103
```
105104

106-
There are something really important to notice with props. It's your props
107-
should only go into one direction from the parent to child. But your props should never
108-
go up. **If your child need to change something in the parent, you should use events**.
105+
There is something important to note with props: They should only flow in one direction, from parent to child. Props should never go up. **If your child needs to change something in the parent, you should use events.**
109106

110107
### State
111108

112-
A state is pretty much like a prop but the main difference is a state can
109+
A state is pretty much like a prop, but the main difference is that a state can
113110
change during the life of the component. Let's take the example of a button component.
114-
You can have a state `loading` that can be `true` or `false`. When the button is clicked
115-
the state `loading` can be set to `true` and the button can display a loader instead of the label.
116-
And when the loading is done, the state `loading` can be set to `false` and the button can display the label again.
111+
You can have a `loading` state that can be `true` or `false`. When the button is clicked
112+
the `loading` state can be set to `true`, and the button can display a loader instead of the label.
113+
When the loading is done, the `loading` state can be set to `false`, and the button can display the label again.
117114

118-
***How does it work into Symfony?***
115+
***How does it work in Symfony?***
119116

120-
In Symfony you have two different approaches to handle state. The first one is to use stimulus directly
121-
in to your component. What we recommend to do is to set a controller stimulus at the root of your component.
117+
In Symfony, you have two different approaches to handle state. The first is to use Stimulus directly in your component. We recommend setting a Stimulus controller at the root of your component.
122118

123119
```twig
124120
{% props label %}
125121
126-
<button data-controller="button" data-button-label="{{ label }}">
122+
<button data-controller="button" data-button-label-value="{{ label }}">
127123
{{ label }}
128124
</button>
129125
```
130126

131-
And then you can define your controller like this:
127+
Then, you can define your controller like this:
132128

133129
```js
134-
import { Controller } from 'stimulus';
130+
import { Controller } from '@hotwired/stimulus';
135131

136132
export default class extends Controller {
137133
static values = { label: String };
@@ -147,11 +143,11 @@ export default class extends Controller {
147143
```
148144

149145
The second approach is to use the [LiveComponent](https://symfony.com/bundles/ux-live-component/current/index.html) package.
150-
How to choose between the two? If your component don't need any backend logic
151-
for his state keep it simple and use stimulus approach. But if you need to handle
146+
How to choose between the two? If your component doesn't need any backend logic
147+
for its state, keep it simple and use the Stimulus approach. But if you need to handle
152148
backend logic for your state, use LiveComponent.
153-
With live component a live prop is a state. So if you want store the number of click on a button you can do
154-
the following component:
149+
With LiveComponent, a live prop is a state. So if you want to store the number of clicks on a button you can do
150+
so with the following component:
155151

156152
```php
157153
<?php
@@ -161,10 +157,11 @@ class Button
161157
{
162158
use DefaultActionTrait;
163159

160+
#[LiveProp]
164161
public int $clicks = 0;
165162

166163
#[LiveAction]
167-
public function increment()
164+
public function increment(): void
168165
{
169166
$this->clicks++;
170167

@@ -175,6 +172,6 @@ class Button
175172

176173
## Conclusion
177174

178-
Even in Symfony, you can use the component architecture.
179-
Follow those rules help your front developers working on codebase
180-
they are familiar with since those rules are already used in the JS world.
175+
Even in Symfony, you can use component architecture.
176+
Following these rules helps your front-end developers work on a codebase they are familiar with since these rules are
177+
already widely used in the JavaScript world.

0 commit comments

Comments
 (0)