You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
chore: update package-lock.json
chore: update package-lock.json take 2
chore: remove console.log statements
fix: ignore system preference change when theme variant set in localstorage
chore: add tests for updates to AppProvider
chore: update react-intl to pass peer dependencies after pinning all deps
chore: split hooks.js up into separate files and begin some related tests
test: add testing to useParagonTheme hooks (openedx#514)
* test: add testing to useParagonThemeCore
* test: add test to useThemeVariants hook
* fix: Paragon definition and remove onload mock
* test: change test message to be clear
Copy file name to clipboardExpand all lines: docs/how_tos/theming.md
+77-13Lines changed: 77 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,47 @@
1
-
# Theming support with Paragon
2
-
3
-
This document serves as a guide to using `@edx/frontend-platform` to support MFE theming with Paragon using theme CSS loaded externally (e.g., from a CDN). By serving CSS loaded externally, consuming applications of Paragon no longer need to be responsible for compiling the theme SCSS to CSS themselves and instead use a pre-compiled CSS file. In doing so, this allows making changes to the Paragon theme without needing to necessarily re-build and re-deploy all consuming applications. We would also get a meaningful gain in performance as loading the compiled theme CSS from an external CDN means micro-frontends (MFEs) can include cached styles instead of needing to load essentially duplicate theme styles as users navigate across different MFEs.
1
+
# Theming support with `@edx/paragon` and `@edx/brand`
4
2
5
3
## Overview
6
4
5
+
This document serves as a guide to using `@edx/frontend-platform` to support MFE theming with Paragon using theme CSS loaded externally (e.g., from a CDN).
6
+
7
+
To do this, configured URLs pointing to relevant CSS files from `@edx/paragon` and (optionally) `@edx/brand` are loaded and injected to the HTML document at runtime. This differs than the consuming application importing the styles from `@edx/paragon` and `@edx/brand` directly, which includes these styles in the application's production assets.
8
+
9
+
By serving CSS loaded externally, consuming applications of Paragon no longer need to be responsible for compiling the theme SCSS to CSS themselves and instead use a pre-compiled CSS file. In doing so, this allows making changes to the Paragon theme without needing to necessarily re-build and re-deploy all consuming applications.
10
+
11
+
### Dark mode and theme variant preferences
12
+
13
+
`@edx/frontend-platform` supports both `light` (required) and `dark` (optional) theme variants. The choice of which theme variant should be applied on page load is based on the following preference cascade:
14
+
15
+
1.**Get theme preference from localStorage.** Supports persisting and loading the user's preference for their selected theme variant, until cleared.
16
+
1.**Detect user system settings.** Rely on the `prefers-color-scheme` media query to detect if the user's system indicates a preference for dark mode. If so, use the default dark theme variant, if one is configured.
17
+
1.**Use default theme variant as configured (see below).** Otherwise, load the default theme variant as configured by the `defaults` option described below.
18
+
19
+
Whenever the current theme variant changes, an attrivbute `data-paragon-theme-variant="*"` is updated on the `<html>` element. This attribute enables applications' both JS and CSS to have knowledge of the currently applied theme variant.
20
+
21
+
### Supporting custom theme variants beyond `light` and `dark`
22
+
23
+
If your use case necessitates additional variants beyond the default supported `light` and `dark` theme variants, you may pass any number of custom theme variants. Custom theme variants will work the user's persisted localStorage setting (i.e., if a user switches to a custom theme variant, the MFE will continue to load the custom theme variant by default). By supporting custom theme variants, it also supports having multiple or alternative `light` and/or `dark` theme variants.
24
+
25
+
### Performance implications
26
+
27
+
There is also a meaningful improvement in performance as loading the compiled theme CSS from an external CDN means micro-frontends (MFEs) can include cached styles instead of needing to load essentially duplicate theme styles included in each individual MFE as users navigate across the platform.
28
+
29
+
However, as the styles from `@edx/paragon` and `@edx/brand` get loaded at runtime by `@edx/frontend-platform`, the associated CSS files do not get processed through the consuming application's Webpack build process (e.g., if the MFE used PurgeCSS or any custom PostCSS plugins specifically for Paragon).
30
+
31
+
### Falling back to styles installed in consuming application
32
+
33
+
If any of the configured external `PARAGON_THEME_URLS` fail to load for whatever reason (e.g., CDN is down, URL is incorrectly configured), `@edx/paragon` will attempt to fallback to the relevant files installed in `node_modules` from the consuming application.
34
+
35
+
## Technical architecture
36
+
7
37

8
38
9
-
## Basic theme URL configuration
39
+
## Development
40
+
41
+
### Basic theme URL configuration
10
42
11
43
Paragon supports 2 mechanisms for configuring the Paragon theme urls:
12
-
* JavaScript-based configuration via `env.config.js`.
44
+
* JavaScript-based configuration via `env.config.js`
13
45
* MFE runtime configuration API via `edx-platform`
14
46
15
47
Using either configuration mechanism, a `PARAGON_THEME_URLS` configuration setting must be created to point to the externally hosted Paragon theme CSS files, e.g.:
@@ -19,16 +51,45 @@ Using either configuration mechanism, a `PARAGON_THEME_URLS` configuration setti
The `PARAGON_THEME_URLS` configuration object supports using only the default styles from `@edx/paragon` or, optionally, extended/overridden styles via `@edx/brand`. To utilize `@edx/brand` overrides, see the `core.urls` and `variants.*.urls` options below.
68
+
69
+
The `dark` theme variant options are optional.
70
+
71
+
| Property | Data Type | Description |
72
+
| -------- | ----------- | ----------- |
73
+
|`core`| Object | Metadata about the core styles from `@edx/paragon` and `@edx/brand`. |
74
+
|`core.url`| String | URL for the `core.css` file from `@edx/paragon`. |
75
+
|`core.urls`| Object | URL(s) for the `core.css` files from `@edx/paragon` CSS and (optionally) `@edx/brand`. |
76
+
|`core.urls.default`| String | URL for the `core.css` file from `@edx/paragon`. |
77
+
|`core.urls.brandOverride`| Object | URL for the `core.css` file from `@edx/brand`. |
78
+
|`defaults`| Object | Mapping of theme variants to Paragon's default supported light and dark theme variants. |
79
+
|`defaults.light`| String | Default `light` theme variant from the theme variants in the `variants` object. |
80
+
|`defaults.dark`| String | Default `dark` theme variant from the theme variants in the `variants` object. |
81
+
|`variants`| Object | Metadata about each supported theme variant. |
82
+
|`variants.light`| Object | Metadata about the light theme variant styles from `@edx/paragon` and (optionally)`@edx/brand`. |
83
+
|`variants.light.url`| String | URL for the `light.css` file from `@edx/paragon`. |
84
+
|`variants.light.urls`| Object | URL(s) for the `light.css` files from `@edx/paragon` CSS and (optionally) `@edx/brand`. |
85
+
|`variants.light.urls.default`| String | URL for the `light.css` file from `@edx/paragon`. |
86
+
|`variants.light.urls.brandOverride`| String | URL for the `light.css` file from `@edx/brand`. |
87
+
|`variants.dark`| Object | Metadata about the dark theme variant styles from `@edx/paragon` and (optionally)`@edx/brand`. |
88
+
|`variants.dark.url`| String | URL for the `dark.css` file from `@edx/paragon`. |
89
+
|`variants.dark.urls`| Object | URL(s) for the `dark.css` files from `@edx/paragon` CSS and (optionally) `@edx/brand`. |
90
+
|`variants.dark.urls.default`| String | URL for the `dark.css` file from `@edx/paragon`. |
91
+
|`variants.dark.urls.brandOverride`| String | URL for the `dark.css` file from `@edx/brand`. |
92
+
32
93
### JavaScript-based configuration
33
94
34
95
One approach to configuring the `PARAGON_THEME_URLS` is to create a `env.config.js` file in the root of the repository. The configuration is defined as a JavaScript file, which affords consumers to use more complex data types, amongst other benefits.
0 commit comments