|
| 1 | +--- |
| 2 | +title: Architecture component |
| 3 | +description: Rules and pattern to work with components |
| 4 | +image: images/cookbook/component_architecture.png |
| 5 | +tags: |
| 6 | + - javascript |
| 7 | + - symfony |
| 8 | +--- |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +In SymfonyUX 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). |
| 13 | +Those two packages allow you to create reusable components in your Symfony application. |
| 14 | +But the component architecture is not exclusive to Symfony, it is a design pattern that can be applied to any programming language or framework. |
| 15 | +And the js world already implement this architecture for long time, on many different frameworks like React, Vue, or Svelte. |
| 16 | +So, a set of rules and pattern has already be defined to work with components. This is why SymfonyUX try to be as close as possible to those rules. |
| 17 | +So let's see what are those rules! |
| 18 | + |
| 19 | +## 4 Rules |
| 20 | + |
| 21 | +### Composition |
| 22 | + |
| 23 | +A page is no longer just a page, but rather a collection of small, reusable components. |
| 24 | +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. |
| 25 | +The training list component could even be composed of smaller components, such as a training card component. |
| 26 | +The goal is to create the most atomic, and reusable components possible. |
| 27 | + |
| 28 | +#### How does it work into Symfony? |
| 29 | + |
| 30 | +In Symfony you can have a component Alert for example with the following template: |
| 31 | + |
| 32 | +```twig |
| 33 | +<div class="alert alert-{{ type }}"> |
| 34 | + <twig:Icon name="{{ icon }}" /> |
| 35 | + {{ message }} |
| 36 | +</div> |
| 37 | +``` |
| 38 | + |
| 39 | +So here you can see we have an alert component that his himself use an Icon component. |
| 40 | +Or you can make composition with the following syntax: |
| 41 | + |
| 42 | +```twig |
| 43 | +<twig:Card> |
| 44 | + <twig:CardHeader> |
| 45 | + <h2>My Card</h2> |
| 46 | + </twig:CardHeader> |
| 47 | + <twig:CardBody> |
| 48 | + <p>This is the content of my card.</p> |
| 49 | + </twig:CardBody> |
| 50 | +</twig:Card> |
| 51 | +``` |
| 52 | + |
| 53 | +So here we Card component, and we give to the content of this component mutliple other components. |
| 54 | + |
| 55 | +### Independence |
| 56 | + |
| 57 | +This is a really important rule, and not obvious. But your component should leave on his own context, |
| 58 | +he should not be aware of the rest of the page. You should to talk one component into a page, to another and it should work exactly the same. |
| 59 | +This rule make your component trully reusable. |
| 60 | + |
| 61 | +***How does it work into Symfony?*** |
| 62 | + |
| 63 | +Symfony keep the context of the page into the context of your component. So this your own responsability to follow this rules. |
| 64 | +But notice that if there are conflic between a variable from the context page and your component, your component context override the page context. |
| 65 | + |
| 66 | +### Props |
| 67 | + |
| 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. |
| 73 | + |
| 74 | +***How does it work into Symfony?*** |
| 75 | + |
| 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). |
| 77 | +We have the following template: |
| 78 | + |
| 79 | +```twig |
| 80 | +{% props type, icon, message %} |
| 81 | +
|
| 82 | +<div class="alert alert-{{ type }}"> |
| 83 | + <twig:Icon name="{{ icon }}" /> |
| 84 | + {{ message }} |
| 85 | +</div> |
| 86 | +``` |
| 87 | + |
| 88 | +Just like that we define three props for our Alert component. And know we can use like this: |
| 89 | + |
| 90 | +```twig |
| 91 | +<twig:Alert type="success" icon="check" message="Your account has been created." /> |
| 92 | +``` |
| 93 | + |
| 94 | +If your component anonymous but a class component, you can simply define props |
| 95 | +by adding property to your class. |
| 96 | + |
| 97 | +```php |
| 98 | +class Alert |
| 99 | +{ |
| 100 | + public string $type; |
| 101 | + public string $icon; |
| 102 | + public string $message; |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 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**. |
| 109 | + |
| 110 | +### State |
| 111 | + |
| 112 | +A state is pretty much like a prop but the main difference is a state can |
| 113 | +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. |
| 117 | + |
| 118 | +***How does it work into Symfony?*** |
| 119 | + |
| 120 | +In symfony you 2 different approach 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. |
| 122 | + |
| 123 | +```twig |
| 124 | +{% props label %} |
| 125 | +
|
| 126 | +<button data-controller="button" data-button-label="{{ label }}"> |
| 127 | + {{ label }} |
| 128 | +</button> |
| 129 | +``` |
| 130 | + |
| 131 | +And then you can define your controller like this: |
| 132 | + |
| 133 | +```js |
| 134 | +import { Controller } from 'stimulus'; |
| 135 | + |
| 136 | +export default class extends Controller { |
| 137 | + static values = { label: String }; |
| 138 | + |
| 139 | + connect() { |
| 140 | + this.element.textContent = this.labelValue; |
| 141 | + } |
| 142 | + |
| 143 | + loading() { |
| 144 | + this.element.textContent = 'Loading...'; |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +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 |
| 152 | +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: |
| 155 | + |
| 156 | +```php |
| 157 | +<?php |
| 158 | + |
| 159 | +#[AsLiveComponent] |
| 160 | +class Button |
| 161 | +{ |
| 162 | + #[LiveProp] |
| 163 | + public int $clicks = 0; |
| 164 | + |
| 165 | + public function increment() |
| 166 | + { |
| 167 | + $this->clicks++; |
| 168 | + |
| 169 | + $this->save(); |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +## Conclusion |
| 175 | + |
| 176 | +Even in Symfony, you can use the component architecture. |
| 177 | +Follow those rules help your front developpers working on codebase |
| 178 | +their are familiar with since those rules are already used in the js world. |
0 commit comments