Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Moodle App Remote Themes: Difference between revisions

From MoodleDocs
m (Noeldemartin moved page Moodle Mobile Themes to Moodle App Remote Themes: Consistency)
(Update after 3.9.5 release)
Line 1: Line 1:
{{Moodle App (Ionic 5)}}
{{Moodle App (Ionic 5)}}


{{Work in progress}}
== How do Remote Themes work? ==


== How to create your own theme ==
When you enter a site, it downloads any file configured on the <code>mobilecssurl</code> administration field and injects the styles in the app. Every time you change between sites, each style will be enabled or disabled appropriately.


First of all, Mobile Themes are only available for sites that purchased a '''Premium subscription''' to the Moodle app. You can check the Moodle app different plans [here https://moodle.com/app/]
The styles will remain enabled in the login page, but any other page that is not related with a specific site will use the default styles. For example, pages to add or remove sites cannot be customized with Remote Themes.


Due to a framework update (Ionic 1 to Ionic 3), Moodle app version 3.5 onwards is not compatible with the existing custom themes / styles. You can keep your current style sheet so they will work with the Moodle Classic app or old Moodle Mobile versions.
== How can you create your own theme? ==


In order to do that, you just need to add this line at the end of your existing theme:
First of all, Remote Themes are only available for sites that purchased a Premium subscription for the Moodle App. You can check the different plans in [https://apps.moodle.com the Apps Portal]. If you want, you can follow the instructions in this document without purchasing a subscription and it will work in your development environment.
<syntaxhighlight lang="css">
/* 3.5 styles */
</syntaxhighlight>


And add below this line your 3.5 onwards styles.
In order to create your own theme, we recommend that you use the app from [https://master.apps.moodledemo.net master.apps.moodledemo.net] and check the styles using the browser inspector. You can use any Chromium-based browser, but you should launch it with some special arguments. You can read more about that in the [[Using the Moodle App in a browser]] page. If you need to test a different version of the app, you can also use [https://integration.apps.moodledemo.net integration.apps.moodledemo.net] for the latest development version or [[Moodle App Docker Images|the Docker images]] for anything more specific.


See the following file for an example of a theme with styles for the old and new versions of the app:
Once you have everything ready, you can configure your theme by going to "Site administration > Mobile app > Mobile appearance" in your site and setting the <code>mobilecssurl</code> field to a url pointing to a CSS file. This file can be placed inside your Moodle installation in your custom theme, inside a local plugin, or hosted elsewhere.


https://gist.githubusercontent.com/crazyserver/ed49913ac40e606c8ce29696b1b3669f/raw/9ad3549140a77f01c86267c42b85e4fb2505577e/moodlemobileapp.css
You can get started with the following example, and you should see the background of the top bar change to red once you log into the app:


This example above is not complete, there are many more colors and styles you can change. It has some clarifications and you can use it as an start for your personalization.
<syntaxhighlight lang="css">
body {
    --core-header-toolbar-background: red;
}
</syntaxhighlight>


In order to create your own theme we recommend to use the proper webapp. Those will require [https://docs.moodle.org/dev/Moodle_Mobile_development_using_Chrome_or_Chromium Chrome or Chromium browser] and use the inspection tool to check the classes you need to change. To test your theme loads correctly you should exit and enter the site again so the new file will be reloaded.
=== Applying theme changes during development ===


=== Moodle app 3.5.0 to 3.9.4 (Ionic 3, legacy) ===
For performance reasons, the app caches the styles after you log in for the first time. So if you make any changes, you won't see them unless you log out and log in again. However, there is a faster way to update them. You can also open the Preferences page in the app and click on the "Synchronise now" button. This will download the files again, and you can use this method to iterate on your styles while you make the theme.
{{Moodle App version|3.5 - 3.9.4}}


You can check Ionic version 3 documentation for more info: https://ionicframework.com/docs/v3/
The file can also be cached by the browser, so when you do this make sure to [https://developer.chrome.com/docs/devtools/network/reference/#disable-cache disable network cache] as well.


=== Moodle 3.9.5 onwards (Ionic 5) ===
=== Knowing what to style ===
{{Moodle App version|3.9.5+}}


Webapp to test 3.9.5 onwards: https://integration.apps.moodledemo.net/ (Only works in Google Chrome)
Depending on how much you want to customize the UI, you'll need to do different things.


You can check Ionic version 5 documentation for more info: https://ionicframework.com/docs/
The application defines some [https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties CSS properties] that can help you customize basic styles, like the one we used in our example above to change the background color of the header toolbar. You can find these variables in the source code, the main ones are defined within [https://github.com/moodlehq/moodleapp/blob/master/src/theme/theme.light.scss theme.light.scss] and [https://github.com/moodlehq/moodleapp/blob/master/src/theme/theme.dark.scss theme.dark.scss], and you can find others within component styles.


The comment '''3.5 styles''' is not needed anymore, you can check: [[Adapt your Mobile plugins to Ionic 5]] for more information.
If you need anything more specific, the application is built using [https://ionicframework.com/docs/theming/basics the Ionic Framework], so reading their documentation can help you understand how the UI works and which components are available. We have some custom components that you won't find listed on their documentation, but most of them built on top of Ionic's.


=== How to inspect and identify the elements for styling ===
Finally, if you need to style something even more specific, you can always [https://github.com/moodlehq/moodleapp browse the source code] to see how a specific page is built. You can also use the [Elements Panel](https://developer.chrome.com/docs/devtools/dom/) of your browser to inspect styles and debug anything that isn't working as you'd expect. Depending what you are trying to do, remember that this is only a development environment and it may not work correctly in a native device. If you are doing anything complicated, make sure to double check using a real device to see that everything looks good.


The best approach to identify elements to be styled is to download and use the Moodle Desktop, you can download it from here: https://download.moodle.org/desktop/
=== Showing course summary image on course page ===


Once you are logged in into your site with the Desktop app, you can enable the Developer tools:
By default, course summary images are hidden to reduce scrolling when entering a course. If you want to change this behaviour, you can include the following CSS in your Remote Theme:
* In MacOS: Cmd + Option + I
* In Windows or Linux: Ctrl + Shift + I


And from there inspect the HTML and dynamically try/change the styles until you find the correct CSS to apply.
<syntaxhighlight lang="css">
ion-app core-course-format .core-format-progress-list .core-course-thumb {
    display: block !important;
}
</syntaxhighlight>


=== Styles based on platform ===
Notice how we needed to use <code>!important</code> in this case. That's something you'll see often if you override component styles, because the default styles are usually scoped to the Angular component and you won't be able to provide more [https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity specificity] in your selector.
{{Moodle App version|3.9.5+}}
The html tag will receive classes with all the platform information based on the following rules:


* '''plt-ipad''': on an iPad device.
=== Using styles based on app and site versions ===
* '''plt-iphone''': on an iPhone device.
* '''plt-ios''': on a device running iOS.
* '''plt-android''': on a device running Android.
* '''plt-phablet''': on a phablet device.
* '''plt-tablet''': on a tablet device.
* '''plt-mobile''': in a browser on a mobile device.
* '''plt-cordova''': on a device running Cordova.
* '''plt-mobileweb''': on a mobile device.
* '''plt-desktop''': on a desktop device, or browser.


Also '''ios''' and '''md''' classes will be added to know if iOS design or Material design (Android) guidelines is being used.
The body element contains classes that indicate the version of the app and the Moodle site, so you can restrict CSS rules to a specific version by prepending one of these classes to the selector.


You can use css rules html.plt-* to have different styles based on platform.
For example, when accessing a 3.11.2 site using the 3.9.5 app the following classes will be present in the body:


=== Styles based on Moodle version ===
* <code>version-3</code>
{{Moodle App version|3.6}}
* <code>version-3-11</code>
* <code>version-3-11-2</code>
* <code>moodleapp-3</code>
* <code>moodleapp-3-9</code>
* <code>moodleapp-3-9-5</code>


Starting from version 3.6.0 of the app you can style the app depending on the version of the Moodle site. The body tag contains classes that indicate the version of the site, you can restrict CSS rules to a specific version by prepending one of these classes to the selector. For example, when accessing a 3.6.1 site these classes are present in the body tag: "moodleapp-3", "moodleapp-3-6", "moodleapp-3-6-1".
=== Supporting older versions of the app ===
 
=== Showing course summary image on course page ===
{{Moodle App version|3.6}}
By default course summary images has been hidden to reduce scrolling when entering a course. If you want to recover the previous behaviour, you can use the following css on your remote theme:
 
<syntaxhighlight lang="css">
ion-app.app-root core-course-format .core-format-progress-list .core-course-thumb {
    display: block;
}</syntaxhighlight>
 
== How remote themes work ==
# When you enter a site in the app it checks if there's any file configured on mobilecssurl administration field on the site.
# The app downloads and load the file.
## If the separator /* 3.5 styles */ is found it will load the css before or after depending on the version of the app you are running.
## If the separator is not found, it will load the whole css.
# CSS selectors that contains the platform name (ios, md or wp) will only be used depending on the platform. If you want to apply it to every platform, add the three selectors.
## ios is for all apple platforms: including iPhone, iPad and macOS desktop app.
## md is for all material design platform: including Android phones and tablets, Chrome OS and Linux desktop app.
## wp is for all windows platforms: including Windows phone and Windows Store app.
## Example:
<syntaxhighlight lang="css">
.toolbar-background-ios,
.toolbar-background-md,
.toolbar-background-wp {
  background: #5069A1;
}
</syntaxhighlight>


== Updating your theme in the app ==
If you need to support different versions of the app, or you're upgrading your theme from an older version, you should read the [[Ionic5 style migration guide]].


You might see that, after editing your CSS file, the changes aren't reflected in the Mobile or Desktop app. The app only receives the URL of the CSS file, it doesn't receive its timestamp, so the only way it can tell if the CSS file has changed is if the URL changes. This means that the app will only update the file if the URL has changed.
== Updating your theme after release ==


To force a download of the file you can add a hash to the URL to indicate the version of the file. So if your CSS url is like:
Once you have configured your theme, some users may already have downloaded previous styles and they will be cached.


https://mysite.com/mobile/mobiletheme.css
If you are updating the styles and you want users to get the latest version, you can change the url of the theme file. This doesn't mean that you need to change the name of the file itself, you can just add some query parameters that will be irrelevant when the file is downloaded:


Then you can add the hash to the end of it with the version number:
<syntaxhighlight lang="txt">https://mysite.com/mobile/mobiletheme.css?version=1</syntaxhighlight>


https://mysite.com/mobile/mobiletheme.css#1
Every time you make some changes in your theme and you want the file to be re-downloaded in the app, just increase this number.


Every time you make some changes in the CSS and you want the file to be re-downloaded in the app, just increase this number.
== Difference between Remote Themes and Branded Apps ==


== Difference between remote themes and Branded apps ==
Remote Theme styles can be tricky to modify. There are lots of CSS rules and some of them can change between versions. Using your own Branded App, you will have better integrations because you can also use Sass variables to change colors and styles. Additionally, you will get your custom application icon and the theming will cover the entire application, not just pages using your site.
Remote theme styles can be tricky to modify. There are lots of css rules and some of them can change between versions. Using your own branded mobile app will have better style integrations because they use SaSS variables to change all the colors or styles. You can find more info on the [https://moodle.com/branded-app Branded App] page. Additionally, you will get your icon placed on all the apps stores and a fixed URL site.


You can play with that use the app variables, use sass to compile new css and use it as you personailzation.
You can find more info on the [https://moodle.com/branded-app Branded Apps] page.

Revision as of 10:53, 7 September 2021


How do Remote Themes work?

When you enter a site, it downloads any file configured on the mobilecssurl administration field and injects the styles in the app. Every time you change between sites, each style will be enabled or disabled appropriately.

The styles will remain enabled in the login page, but any other page that is not related with a specific site will use the default styles. For example, pages to add or remove sites cannot be customized with Remote Themes.

How can you create your own theme?

First of all, Remote Themes are only available for sites that purchased a Premium subscription for the Moodle App. You can check the different plans in the Apps Portal. If you want, you can follow the instructions in this document without purchasing a subscription and it will work in your development environment.

In order to create your own theme, we recommend that you use the app from master.apps.moodledemo.net and check the styles using the browser inspector. You can use any Chromium-based browser, but you should launch it with some special arguments. You can read more about that in the Using the Moodle App in a browser page. If you need to test a different version of the app, you can also use integration.apps.moodledemo.net for the latest development version or the Docker images for anything more specific.

Once you have everything ready, you can configure your theme by going to "Site administration > Mobile app > Mobile appearance" in your site and setting the mobilecssurl field to a url pointing to a CSS file. This file can be placed inside your Moodle installation in your custom theme, inside a local plugin, or hosted elsewhere.

You can get started with the following example, and you should see the background of the top bar change to red once you log into the app:

body {
    --core-header-toolbar-background: red;
}

Applying theme changes during development

For performance reasons, the app caches the styles after you log in for the first time. So if you make any changes, you won't see them unless you log out and log in again. However, there is a faster way to update them. You can also open the Preferences page in the app and click on the "Synchronise now" button. This will download the files again, and you can use this method to iterate on your styles while you make the theme.

The file can also be cached by the browser, so when you do this make sure to disable network cache as well.

Knowing what to style

Depending on how much you want to customize the UI, you'll need to do different things.

The application defines some CSS properties that can help you customize basic styles, like the one we used in our example above to change the background color of the header toolbar. You can find these variables in the source code, the main ones are defined within theme.light.scss and theme.dark.scss, and you can find others within component styles.

If you need anything more specific, the application is built using the Ionic Framework, so reading their documentation can help you understand how the UI works and which components are available. We have some custom components that you won't find listed on their documentation, but most of them built on top of Ionic's.

Finally, if you need to style something even more specific, you can always browse the source code to see how a specific page is built. You can also use the [Elements Panel](https://developer.chrome.com/docs/devtools/dom/) of your browser to inspect styles and debug anything that isn't working as you'd expect. Depending what you are trying to do, remember that this is only a development environment and it may not work correctly in a native device. If you are doing anything complicated, make sure to double check using a real device to see that everything looks good.

Showing course summary image on course page

By default, course summary images are hidden to reduce scrolling when entering a course. If you want to change this behaviour, you can include the following CSS in your Remote Theme:

ion-app core-course-format .core-format-progress-list .core-course-thumb {
    display: block !important;
}

Notice how we needed to use !important in this case. That's something you'll see often if you override component styles, because the default styles are usually scoped to the Angular component and you won't be able to provide more specificity in your selector.

Using styles based on app and site versions

The body element contains classes that indicate the version of the app and the Moodle site, so you can restrict CSS rules to a specific version by prepending one of these classes to the selector.

For example, when accessing a 3.11.2 site using the 3.9.5 app the following classes will be present in the body:

  • version-3
  • version-3-11
  • version-3-11-2
  • moodleapp-3
  • moodleapp-3-9
  • moodleapp-3-9-5

Supporting older versions of the app

If you need to support different versions of the app, or you're upgrading your theme from an older version, you should read the Ionic5 style migration guide.

Updating your theme after release

Once you have configured your theme, some users may already have downloaded previous styles and they will be cached.

If you are updating the styles and you want users to get the latest version, you can change the url of the theme file. This doesn't mean that you need to change the name of the file itself, you can just add some query parameters that will be irrelevant when the file is downloaded:

https://mysite.com/mobile/mobiletheme.css?version=1

Every time you make some changes in your theme and you want the file to be re-downloaded in the app, just increase this number.

Difference between Remote Themes and Branded Apps

Remote Theme styles can be tricky to modify. There are lots of CSS rules and some of them can change between versions. Using your own Branded App, you will have better integrations because you can also use Sass variables to change colors and styles. Additionally, you will get your custom application icon and the theming will cover the entire application, not just pages using your site.

You can find more info on the Branded Apps page.