|
| 1 | +# MarsBased Angular 17 Style Guide |
| 2 | + |
| 3 | +### 🚧 This guide is focused on Angular 17, for older versions see [here](./angular-guidelines.md) 🚧 |
| 4 | + |
| 5 | +We follow the official and opinionated [Angular coding style guide](https://angular.dev/style-guide) as the primary source of best practices and conventions. |
| 6 | + |
| 7 | +## Standalone Components |
| 8 | + |
| 9 | +In the latest versions of Angular, it is recommended to use standalone components. These are components that do not have a direct relationship with other components and can be used independently. Previously, all elements were organized in modules, and using an element from a module meant loading the entire module. |
| 10 | + |
| 11 | +Now, with standalone components, each component is used individually, which improves application load time and performance. |
| 12 | + |
| 13 | +## Single Responsibility Services with State Management |
| 14 | + |
| 15 | +In Angular, services should adhere to the Single Responsibility Principle, meaning each service should focus on a specific domain or functionality. By doing so, services become easier to maintain, test, and reuse. |
| 16 | + |
| 17 | +Additionally, these services should manage their state using signals, allowing components to subscribe to state changes efficiently. Signals provide a reactive way to handle state, enabling components to subscribe to state changes and automatically update when the state changes. |
| 18 | + |
| 19 | +## Smart and Dumb (Presentational) components |
| 20 | + |
| 21 | +The only goal of a presentational component is to keep the UI logic isolated. It doesn't have access to injected business services. It communicates using @Input and @Output data flows.Presentational components communicate using Input and Output only. |
| 22 | + |
| 23 | +A smart component, or smart container, injects business services and keeps a more complex state. Its goal is to orchestrate the communication and behaviour between data services and presentational components. Smart components communicate to the rest of the app using injected services, Input an Outputs. |
| 24 | + |
| 25 | +Having a separation between smart and presentational components adds the advantages: |
| 26 | + |
| 27 | +- Easier to write and more focused tests. |
| 28 | +- Isolated UI state. |
| 29 | +- Better component reusability. |
| 30 | + |
| 31 | +## Structuring Application Architecture Using Routes |
| 32 | + |
| 33 | +In Angular, organizing the application's architecture by following the routes can enhance readability, maintainability, and scalability. Similar to other frameworks, using a route-based folder structure helps developers quickly locate components associated with specific routes, improving overall development efficiency. |
| 34 | + |
| 35 | +### Folder Structure |
| 36 | + |
| 37 | +Within the src directory, create a routes folder that mirrors the application's routes. Each route should have its own folder containing all related components, services, etc. This structure helps keep the project organized and makes it easier to manage and scale the application as it grows. |
| 38 | + |
| 39 | +Here's an example of how to structure the folders for an application with a users route: |
| 40 | + |
| 41 | +``` |
| 42 | +src/ |
| 43 | + app/ |
| 44 | + routes/ |
| 45 | + users/ |
| 46 | + # users components |
| 47 | +``` |
| 48 | + |
| 49 | +## State Interface |
| 50 | + |
| 51 | +Maintaining common interface components, such as modals, popups, or loading spinners, in a parent component common to the entire application is a recommended practice. This approach helps in managing the application's UI state efficiently and ensures a consistent look and feel across the application. |
| 52 | + |
| 53 | +By centralizing the management of common interface components in a parent component, you can: |
| 54 | + |
| 55 | +- Reduce Redundancy: Avoid duplicating code for common UI elements across multiple child components. |
| 56 | +- Ensure Consistency: Maintain a uniform appearance and behavior for UI elements, ensuring a consistent user experience. |
| 57 | +- Simplify Maintenance: Make it easier to update and manage UI components, as changes are made in a single place rather than across multiple components. |
| 58 | + |
| 59 | +## Input signals |
| 60 | + |
| 61 | +Components in Angular should receive input data through inputs, specifically as input signals. This practice becomes particularly advantageous when writing code in a reactive style using signals. |
| 62 | + |
| 63 | +### Advantages of Using Input Signals |
| 64 | + |
| 65 | +#### Reactive Style |
| 66 | + |
| 67 | +By using signals as inputs, all the component's input data become signals themselves. This allows for easily deriving computed signals from them and defining effects that react to input changes. In other words, it facilitates writing components in a reactive, signal-based style. |
| 68 | + |
| 69 | +#### Data Transformation |
| 70 | + |
| 71 | +The parent component does not need to transform or update the input data. This responsibility falls on the child component, which simplifies the logic in the parent component and makes it easier to reuse and test the child components. |
| 72 | + |
| 73 | +#### Change Detection |
| 74 | + |
| 75 | +Using signals as inputs improves the component's change detection. Angular can detect changes more efficiently because signals allow for more precise tracking of state modifications. |
| 76 | + |
| 77 | +#### State Derivation |
| 78 | + |
| 79 | +Signals enable more effective derivation of the input state. For example, you can create computed signals that depend on the inputs and automatically update the component's state when the inputs change. |
| 80 | + |
| 81 | +A more comprehensive article on signals in Angular can be found [here](https://blog.angular-university.io/angular-signal-inputs/). |
0 commit comments