-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: adds support for loading external theme CSS for MFEs #689
base: master
Are you sure you want to change the base?
Conversation
Thanks for the pull request, @dcoa! What's next?Please work through the following steps to get your changes ready for engineering review: 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. 🔘 Let us know that your PR is ready for review:Who will review my changes?This repository is currently maintained by Where can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:
When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #689 +/- ##
=========================================
Coverage ? 83.65%
=========================================
Files ? 48
Lines ? 1389
Branches ? 291
=========================================
Hits ? 1162
Misses ? 211
Partials ? 16 ☔ View full report in Codecov by Sentry. |
aee582b
to
e81b549
Compare
docs/how_tos/theming.md
Outdated
@@ -0,0 +1,204 @@ | |||
# Theming support with `@edx/paragon` and `@edx/brand` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to refactor @edx/
to @openedx/
in docs, as since December npm packages for @openedx/paragon, @openedx/frontend-build, @openedx/frontend-plugin-framework, @openedx/brand-openedx
are published in the scope of @openedx/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brian-smith-tcril I couldn't find frontend-platform among npm published packages, shouldn't it be published with @openedx scope as well?
https://www.npmjs.com/search?q=%40openedx
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that one is still only published to the @edx
scope. I agree it would be good to make the switch but it hasn't made it to the top of the priority list yet.
package.json
Outdated
"@openedx/frontend-build": "14.0.3", | ||
"@openedx/paragon": "22.3.2", | ||
"@openedx/frontend-build": "github:edunext/frontend-build#dcoa/design-tokens-support", | ||
"@openedx/paragon": "npm:@openedx/paragon@^22.0.0-alpha.25", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might worth to wait for the Paragon sync PR to be merged, so we won't downgrade paragon requirements, and still will be able to use the latest version of paragon.
5b4c843
to
ed293e3
Compare
@dcoa I'm getting an error during build related to the
I tried to solve it by changing the export to Have you encountered such a problem? |
@PKulkoRaccoonGang this is related to frontend-build changes, I'm checking right now This line is making that the app is not rendering in frontend-platform https://github.com/openedx/frontend-build/pull/546/files#diff-22e13ddf245ea4fa81ca4d686dddf46fa9cbf70fbbff99991cccbf0c8ff82316R175 If you remove it, you will be able to see the app. Update I made an update to webpack dev config for example app and is working now. |
9ebae07
to
94e4a62
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dcoa This looks great! I have already started working on Update openedx/frontend-app-discussions
. I left a few questions about the operation of some features and the code in general.
`@openedx/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: | ||
|
||
1. **Get theme preference from localStorage.** Supports persisting and loading the user's preference for their selected theme variant, until cleared. | ||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[question]: Is there now support for adding a dark/light theme (or other custom themes) when using locally installed Paragon CSS?
For example:
@use "@openedx/paragon/dist/core.min.css" as paragonCore;
@use "@openedx/paragon/dist/light.min.css" as paragonLight;
@use "@openedx/paragon/dist/dark.min.css" as paragonDark;
This works great using JS and MFE runtime configurations, but I have not been able to test it for local themes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hook will fallback in the installed themes files if the URL is not provided or is not working.
If you define only the CSS inside SCSS file you can use the prefers-color-scheme
to support dark/light modes. But you can not create a custom switch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! It would be great to explicitly indicate exactly how it is possible to use different imports for a light/dark theme in the documentation. I plan to do this in the migration documentation and for Paragon.
For example:
@use "@openedx/paragon/dist/core.min.css" as paragonCore;
@import url('@openedx/paragon/dist/light.min.css') (prefers-color-scheme: light);
@import url('@my-brand/dark.min.css') (prefers-color-scheme: dark);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
100% agree that the information in your example should be part of the Paragon and migration documents.
This is a different approach, the explanation of how to use variants is below, in the title Basic theme URL configuration.
I will improve one of the examples to use dark mode, to make it more explicit.
|
||
### Falling back to styles installed in consuming application | ||
|
||
If any of the configured external `PARAGON_THEME_URLS` fail to load for whatever reason (e.g., CDN is down, URL is incorrectly configured), `@openedx/paragon` will attempt to fallback to the relevant files installed in `node_modules` from the consuming application. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PKulkoRaccoonGang thanks for the feedback, the fallback url was missing the publicPath
I solved it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don’t receive undefined
, but I couldn’t load styles from the locally installed Paragon package in node_modules
. Could you check if getParagonThemeCss
works correctly in a PR related to a frontend-build?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem here is related to tutor-mfe, they define BASE_URL
as the MFE_HOST
(you can see here) thing that is not 100% true in dev mode because the URL needs the PORT
that is specific for each MFE (you can see here )
You can add in the settings:
MFE_CONFIG_OVERRIDES = {
"discussions": {
"BASE_URL": "http://apps.local.edly.io:2002"
}
}
docs/how_tos/theming.md
Outdated
### Basic theme URL configuration | ||
|
||
Paragon supports 2 mechanisms for configuring the Paragon theme urls: | ||
* JavaScript-based configuration via `env.config.js` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[question]: This approach works great in MFE, but I encountered a problem in frontend-build/example
, unfortunately, the styles are not loading for me. Is everything working correctly?
For example: I tried adding a Button component with Paragon and noticed that the styles with Paragon were not being applied to this component
|
||
```shell | ||
https://cdn.jsdelivr.net/npm/@openedx/paragon@$paragonVersion/dist/core.min.css | ||
https://cdn.jsdelivr.net/npm/@openedx/paragon@$paragonVersion/dist/light.min.css |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[question]: Is it worth adding an example of importing in SCSS format here? This also applies to the local import example @openedx/brand-openedx
For example:
@use "@openedx/paragon/dist/core.min.css" as paragonCore;
@use "@openedx/paragon/dist/light.min.css" as paragonLight;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, because the main intention here is to identify the version and load the external resource compatible with that specific version.
For example, my app has paragon 23.0.0-alpha.2
, then the hook will replace $paragonVersion
.
This is the definition
https://cdn.jsdelivr.net/npm/@openedx/paragon@$paragonVersion/dist/core.min.css
after processing
https://cdn.jsdelivr.net/npm/@openedx/paragon@$23.0.0-alpha.2/dist/core.min.css
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation 💯
global.PARAGON_THEME = { | ||
paragon: { | ||
version: '1.0.0', | ||
themeUrls: { | ||
core: { | ||
fileName: 'core.min.css', | ||
}, | ||
defaults: { | ||
light: 'light', | ||
}, | ||
variants: { | ||
light: { | ||
fileName: 'light.min.css', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit]: We also use this configuration for the test in the file src/react/hooks/paragon/useParagonThemeVariants.test.js
I suggest putting this code into a separate file and reusing it to test both hooks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can get rid of this understanding that is being exported by frontend-build (including for testing)
Note: Once the frontend-build PR has been merged we need to update the devDependencies
and peerDependencies
}); | ||
|
||
it('returns expected object when configuration is valid (both Paragon + brand)', () => { | ||
const config = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[optional]: I suggest reusing the config constant, which refers to an object with the necessary configuration for tests in this file. This will allow us to get rid of code duplication. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer leaving this section as it is. The important part here is PARAGON_THEME_URLS
, which changes in each test. It's easier to understand this way, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good, I agree with you, thanks
src/react/AppProvider.test.jsx
Outdated
}); | ||
|
||
it('calls useParagonTheme', () => { | ||
const Component = ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[question]: Can we declare a global Сomponent constant for a block with paragon theme and brand
tests and reuse it in tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can declare the global component but this test doesn't cover the useParagonTheme behavior so it is not necessary to declare paragon theme and brand
. This is focused on the setThemeVariant
function and the theme state.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, thanks
a6a9211
to
229d79a
Compare
06fc4e6
to
837858e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall this is looking great! I left a suggestion to clarify the documentation is referring to design tokens, and a few comments noting PRs that need to land before this can.
Thank you so much for this!
@@ -0,0 +1,250 @@ | |||
# Theming support with `@openedx/paragon` and `@openedx/brand-openedx` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Theming support with `@openedx/paragon` and `@openedx/brand-openedx` | |
# Theming support with `@openedx/paragon` and `@openedx/brand-openedx` | |
> [!IMPORTANT] | |
> This document describes theming with design tokens. | |
> Information on theming MFEs that do not yet have design tokens support: | |
> * https://github.com/openedx/brand-openedx | |
> Information on the design tokens project: | |
> * https://github.com/openedx/paragon/blob/master/docs/decisions/0019-scaling-styles-with-design-tokens.rst | |
> * https://github.com/openedx/paragon/tree/alpha?tab=readme-ov-file#design-tokens |
package.json
Outdated
@@ -76,7 +77,7 @@ | |||
}, | |||
"peerDependencies": { | |||
"@openedx/frontend-build": ">= 14.0.0", | |||
"@openedx/paragon": ">= 21.5.7 < 23.0.0", | |||
"@openedx/paragon": " 23.0.0-alpha.1 || >= 21.5.7 < 23.0.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
Waiting on Paragon alpha to be merged to master
2565f02
to
ae5b53f
Compare
Sure is. It looks like it was temporary, but just to explicitly note it: the |
feat: adds support for loading external theme CSS for MFEs Introduces `useAppTheme` in `AppProvider` to load/inject `<link>` elements for the core theme CSS and any theme variant CSS into the HTML document. Exposes the app theme state and a way to mutate it to consumers via `AppContext`. fix: handle missing theme config fix: add env vars to env files fix: remove unused code chore: wip commit fix: prefer runtime config for paragon theme fix: rebase on master and resolve package conflicts fix: revert to original webpack.dev.config config chore: clean up unnecessary comment line fix: grammar Co-authored-by: Adolfo R. Brandes <[email protected]>
chore: refresh package-lock.json to lockfileVersion 3 fix: clean up link nodes in document head when no longer needed fix: fallback to locally installed core and light theme css urls docs: update to docs chore: update docs about fix: expose setThemeVariant
fix: rebase with master and update based on PARAGON changes chore: remove support for env vars config for paragon dynamic theming
fix: updates fix: one more update fix: refresh package-lock.json fix: refresh package-lock.json pt2 fix: updates fix: update package-lock.json fix: make it theme variant agnostic docs: update howto theming guide fix: ensure app loads without styles if the PARAGON_THEME_URLS and fallback urls all fail to load fix: ensure fallback theme links are removed if they also error docs: add link to mfe runtime config api adr fix: don't attempt to load paragon css urls if PARAGON_THEME_URLS is absent
docs: fix code example docs: add missing comma docs: update how to
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
3576ee7
to
818ae1c
Compare
Description:
This PR updates the original one #440 closer to the master branch and adds some extra tests.
Please read the original PR for additional context.
Warning
Merge checklist:
frontend-platform
. This can be done by runningnpm start
and opening http://localhost:8080.module.config.js
file infrontend-build
.fix
,feat
) and is appropriate for your code change. Consider whether your code is a breaking change, and modify your commit accordingly.Post merge: