3 ## About the Application panel
5 The Application panel is a Firefox Developer Tools panel meant to allow the inspection and debugging of features usually present in Progressive Web Apps, such as service workers or the Web App Manifest.
9 The Application panel is a React + Redux web app.
11 ### Source directory structure
13 The application panel lives in the `devtools/client/application` directory. Inside this root directory, the most relevant subdirectories and files are:
17 ├── application.css -> root CSS stylesheet
18 ├── initializer.js -> panel initialization
20 │ ├── actions -> Redux actions
21 │ ├── components -> React components
22 │ ├── modules -> Business logic and helpers
23 │ └── reducers -> Redux reducers and states
25 ├── browser -> mochitests (e2e/integration)
26 ├── node -> Jest unit tests
27 └── xpcshell -> xpcshell unit tests
30 ### Panel registration
32 The panel is defined in `devtools/client/application/panel.js` – which in turn calls the `.bootstrap()` method defined in `devtools/client/application/initializer.js`.
34 The panel is registered along the rest of the Developer Tools panels, in `devtools/client/definitions.js`.
38 The panel uses the [fluent-react](https://github.com/projectfluent/fluent.js/wiki/React-Bindings) library. The localization file is located at `devtools/client/locales/en-US/application.ftl` and it follows [Fluent syntax](https://projectfluent.org/fluent/guide/).
40 You should read the [Fluent for Firefox developers](https://firefox-source-docs.mozilla.org/l10n/fluent/tutorial.html) and [Guidelines for Fluent Reviewers](https://firefox-source-docs.mozilla.org/l10n/fluent/review.html) guides.
42 #### Adding a new string
44 If you need to localize a text, add a new message ID to the localization file (`devtoos/client/en-US/application.ftl`). Then you can use this ID as a prop for Fluent's `<Localized>` component or `getString()` function.
46 When using ID's from the React code, always write the full ID instead of building it with concatenation, interpolation, etc. (this makes localing ID's easier afterwards). For instance, let's say you have two ID's, `error-message` and `warning-message`, and you need to use one or the other depending on a condition.
51 const localizationIds = {
52 error: "error-message",
53 warning: "warning-message",
56 const id = localizationIds[messageLevel];
62 const id = `${messageLevel}-message`;
65 #### Updating an existing string
67 If you need to modify an existing string, you need to create a new ID and don't remove the previous one. For instance, if we had this string:
70 some-localized-string = This will be modified later
73 And we need to update the content, we would create a new ID and remove the previous one intact:
76 - some-localized-string = This will be modified later
77 + some-localized-string2 = This is the updated string
80 Within the React component code, you would use the newly created ID.
83 const localizedText = l10n.getString("some-localized-string2");
88 Components are located in the `devtools/client/application/src/components` directory.
90 Each component has a single `.js` file, plus an optional `.css` file, so each component is responsible of handling their own styles.
92 - The `App` component is the root component of the Application panel.
93 - In `components/service-workers` are the components that render all of the UI related to inspect and debug service workers.
94 - In `components/manifest` are the components that render everything related to inspecting a Web App Manifest.
95 - In the `components/routing` directory there are components related to switching from one section of the panel to the other, like a sidebar or a page switcher container.
96 - Inside the `components/ui` directory there are generic UI components that can be reused from other parts of the panel.
100 The Application panel uses Redux to handle app state and communication between components.
104 The Redux state is set up in `devtools/client/application/src/create-store.js`. The state contains the following substates:
106 - `workers`: contains data related to inspecting and debugging service workers.
107 - `manifest`: contains state related to inspecting the Web App Manifest.
108 - `page`: contains general data related to the web page that is being inspected.
109 - `ui`: contains data related to the UI that don't really fit any other state.
113 Synchronous actions are regular Redux actions. Their type is defined in the general constants file (`devtools/client/application/src/constants.js`).
115 **Asynchronous** actions are achieved thanks to the **thunk middleware**. They follow a three-action pattern `*_START`, `*_SUCCESS`, `*_FAILURE`. For instance:
118 function fooAction() {
119 return async ({ dispatch, getState }) => {
120 dispatch({ type: FOO_START });
123 const result = await foo();
124 dispatch({ type: FOO_SUCCESS });
128 dispatch({ type: FOO_FAILURE });
136 We are using the following middlewares with Redux:
138 - [redux-thunk](https://github.com/reduxjs/redux-thunk): This is a shared middleware used by other DevTools modules that allows to dispatch asynchronous actions.
142 We are sharing some constants / enum-like constants across the source. They are located in `devtools/client/application/src/constants.js`. For values that are not shared, they should stay within their relevant scope/file.
146 You should read DevTools' general [guidelines for automated tests](https://firefox-source-docs.mozilla.org/devtools/tests/README.html).
148 ### Mochitests (e2e / integration)
150 End to end and integration tests are made with **Mochitests**. These tests open a browser and will load an HTML and open the toolbox with the Application panel.
152 These tests are located in `devtools/client/application/test/browser`.
154 Besides the usual DevTools test helpers, the Application panel adds some other helper functions in `devtools/client/application/test/browser/head.js`
156 ### Unit tests with xpcshell
158 We are using xpcshell unit tests for the **Redux reducers** in `devtools/client/application/test/xpcshell`.
160 Other unit tests that don't need Enzyme or other npm modules should be added here too.
162 ### Unit tests with Jest
164 We are using Jest with Enzyme to test **React components** and **Redux actions** in `devtools/client/application/test/node`. Some considerations:
166 - There are some **stubs/mocks** for some libraries and modules. These are placed in `devtools/client/application/test/node/fixtures`.
167 - The **localization system is mocked**. For `<Localized>` components, the Enzyme snapshots will reflect the localization ID. Ex: `<Localized id="some-localization">`. The actual localized string will not be rendered with `.shallow()`. Calls to `getstring()` will return the string ID that we pass as an argument.
168 - The **redux store is mocked** as well. To create a fake store to test a component that uses `connect()` to fetch data from the store, use the helper `setupStore()` included in `devtools/client/application/test/node/helpers.js`.
172 Most Jest tests will involve a snapshot matching expectation.
174 **Snapshots should be produced with [`shallow()`](https://airbnb.io/enzyme/docs/api/shallow.html)** and _not_ `mount()`, to ensure that we are only testing the current component.
176 **Components that are wrapped** with `connect()` or other wrapper components, need their snapshot to be rendered with [`dive()`](https://airbnb.io/enzyme/docs/api/ShallowWrapper/dive.html) (one call to `dive()` per wrapper component).
180 We are using a BEM-like style for the following reasons:
182 - To avoid collision between the styles of components.
183 - To have a consistent methodology on how to use CSS.
184 - To ensure we have rules with low specificity and thus styles are easier to override and maintain.
186 ### Stylesheet organization
190 **Each component has its own CSS stylesheet**, in the same directory and with the same name as the JavaScript file, only with a `.css` extension. So `Foo.js` would have a `Foo.css` stylesheet.
192 These stylesheets are imported from `devtools/client/application/application.css` in alphabetical order –BEM-like naming and low specificity, when done right, should ensure that styles work regardless of the order the stylesheets are included.
196 Another important stylesheet is `devtools/client/application/base.css`, which is the one that is imported first from `application.css`, regardless the alphabetical order. In this stylesheet we do:
198 - Variables definitions
200 - Some styles that are applied to the whole panel, like some resets or default values.
202 #### External stylesheets imported in the panel
204 Besides the usual user-agent styles, all DevTools panels automatically import the stylesheets located at `devtools/client/themes/`. The most relevant for the Application panel are:
206 - `devtools/client/themes/variables.css`: variables are defined here
207 - `devtools/client/themes/common.css`: rules that are applied to every panel
209 In the Application panel, we are not using some of the classes and rules defined in `common.css`, since they don't follow the BEM convention and some of them have high specificity. However, there are rules defined at the element level and this will get applied to the markup we use.
211 From `variables.css`, at the moment we mainly use color names. See the section below for details. Other variables available there that contain values for common elements (like heights, widths, etc.) should also be used when we implement those components.
213 ### Colors and themes
215 All DevTools panels should support both **light and dark themes**. The active theme can be changed by the user at DevTools Settings.
217 The way this is handled is that a `.theme-light` or `.theme-dark` class is automatically added to the root `<html>` element of each panel. Then, in `variables.css`, there are some CSS variables with different values depending on which of those classes the root element has.
219 - We should use the colors defined in `variables.css`.
220 - If the other panels are using a particular color for an element we also have, we should re-use the same color.
221 - If needed, we can create a CSS variable in `base.css` that takes the value of another color variable defined in `variables.css`
222 - If the desired color is not available in `variables.css`, we can create a new one, but we have to define values for both light and dark themes.
224 #### Writing CSS selectors
226 As mentioned earlier, we are using a system to create rules similar to [BEM](http://getbem.com/naming/), although not strictly identical.
228 #### Low specificity rules
230 Rules should have **the lowest specifity** that is possible. In practice, this means that we should try to use a **single class selector** for writing the rule. Ex:
238 Note that **pseudoclasses are pseudoselectors are OK** to use.
242 When an element is semantically the child of another element, we should also use a single class selector. In BEM, this is achieved by separating potential class names with a double underscore `__`, so we have a single classname.
247 .manifest-item { /* ... */ }
248 .manifest-item__value { /* ... */ }
254 .manifest-item { /* ... */ }
255 .manifest-item .value { /* ... */ }
258 We should not go deeper than one level of `__`. We don't have to replicate the whole DOM structure:
260 - If the style rule should be independent of the parent, it does not need to use an `__`.
261 - If the style rule does not make sense on its own, and it will be scoped to a particular section of the DOM tree, don't replicate the whole DOM structure and use the top-level ancestor class name instead.
263 For instance, if we have the following HTML markup and we have to choose a class name for the `<dt>` element:
267 <dl class="worker__data">
268 <dt class="..."></dt>
275 .worker__meta-name { /* ... */ }
281 .worker__data__meta-name { /* ... */ }
284 #### Style variations
286 Sometimes we have a component that should change its style depending on its state, or we have some rules that allow us to customize further how something looks. For instance, we could have a generic rule for buttons, and then some extra rules for primary buttons, or for tiny ones.
288 In BEM, style variants are created with a double hyphen `--`. For example:
291 .ui-button { /* styles for buttons */ }
292 .ui-button--micro { /* specific styles for tiny buttons */ }
295 Then, in the component, _both_ classes are to be used.
298 <button class="ui-button ui-button--micro">
303 Photon has a base unit of `4px` for sizes, paddings, margins… This value is stored in the CSS variable `--base-unit`.
305 Our sizes and other dimensions should be defined based on this variable. Since we are using raw CSS and not a pre/post processor, we need to use `calc()` to achieve this.
311 height: calc(var(--base-unit) * 6);
329 If you have questions about the code, features, planning, etc. the current active team is:
331 - Product management: Harald Kischner (`:digitarald`)
332 - Engineering management: Patrick Brosset (`:pbro`)
333 - Engineering: Belén Albeza (`:ladybenko`)
334 - Engineering: Ola Gasidlo (`:ola`)