Note:

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

SCSS: Difference between revisions

From MoodleDocs
m (Text replacement - "</code>" to "</syntaxhighlight>")
 
(2 intermediate revisions by the same user not shown)
Line 9: Line 9:


Moodle includes a built in SCSS compiler so to use SCSS in your theme, you only need to set the theme scss variable, in your theme's config.php file, to point to the main scss file for your theme:
Moodle includes a built in SCSS compiler so to use SCSS in your theme, you only need to set the theme scss variable, in your theme's config.php file, to point to the main scss file for your theme:
<code php>
<syntaxhighlight lang="php">
// Points to the file theme/themename/scss/main.scss.
// Points to the file theme/themename/scss/main.scss.
$THEME->scss = 'main';
$THEME->scss = 'main';
</code>
</syntaxhighlight>


Moodle will look for a file named 'main.scss' in the 'theme/themename/scss' folder and compile it. If you don't see the affect of your changes right away, try purging the caches or enabling theme designer mode.
Moodle will look for a file named 'main.scss' in the 'theme/themename/scss' folder and compile it. If you don't see the affect of your changes right away, try purging the caches or enabling theme designer mode.
Line 20: Line 20:
Even if your theme is inheriting from a parent, the SCSS file itself will not inherit from anything, this is something you should do manually. For instance, if you want your SCSS file to include all of the SCSS code provided by ''theme_boost'', usually to change the variables, you need to manually import the file like this:
Even if your theme is inheriting from a parent, the SCSS file itself will not inherit from anything, this is something you should do manually. For instance, if you want your SCSS file to include all of the SCSS code provided by ''theme_boost'', usually to change the variables, you need to manually import the file like this:


<code>
<syntaxhighlight lang="php">
// In your theme's SCSS file
// In your theme's SCSS file
@import "../../boost/scss/moodle";
@import "../../boost/scss/moodle";
</code>
</syntaxhighlight>


The path needs to be relative and not absolute. You would use it like this:
The path needs to be relative and not absolute. You would use it like this:


<code>
<syntaxhighlight lang="php">
$brand-primary:            #00acdf !default;
$brand-primary:            #00acdf !default;
$my-color:                  #ff9900 !default;
$my-color:                  #ff9900 !default;
Line 36: Line 36:
     background-color: $my-color;
     background-color: $my-color;
}
}
</code>
</syntaxhighlight>


  NOTICE: At the moment, Moodle is using a PHP sass compiler which does not support '@import url' at-rule MDL-57875, which is a challenge when you wish to use bootswatch presets or Google fonts directly. so it is advise to use the OS level sass binary compiler (by installing libsass via 'yum install libsass' || 'apt install sassc', or other means) and then setting the admin $CFG->pathtosassc to use the 'sassc' binary.
  NOTICE: At the moment, Moodle is using a PHP sass compiler which does not support '@import url' at-rule MDL-57875, which is a challenge when you wish to use bootswatch presets or Google fonts directly. so it is advise to use the OS level sass binary compiler (by installing libsass via 'yum install libsass' || 'apt install sassc', or other means) and then setting the admin $CFG->pathtosassc to use the 'sassc' binary.
Line 46: Line 46:
The following settings allow themers to point to a function which will either prepend, or append, SCSS code to content gotten from ''$THEME->scss''.
The following settings allow themers to point to a function which will either prepend, or append, SCSS code to content gotten from ''$THEME->scss''.


<code php>
<syntaxhighlight lang="php">
// The content returned by this function is added before the main SCSS.
// The content returned by this function is added before the main SCSS.
$THEME->prescsscallback = 'theme_themename_scss_to_prepend';
$THEME->prescsscallback = 'theme_themename_scss_to_prepend';
Line 52: Line 52:
// The content returned by this function is added after the main SCSS.
// The content returned by this function is added after the main SCSS.
$THEME->extrascsscallback = 'theme_themename_scss_to_append';
$THEME->extrascsscallback = 'theme_themename_scss_to_append';
</code>
</syntaxhighlight>


The functions will receive the ''theme config'' as first argument.
The functions will receive the ''theme config'' as first argument.
Line 60: Line 60:
Rather than giving the name of your SCSS file in ''$THEME->scss'', you can make it a function which returns the SCSS content.
Rather than giving the name of your SCSS file in ''$THEME->scss'', you can make it a function which returns the SCSS content.


<code php>
<syntaxhighlight lang="php">
$THEME->scss = function($themeconfig) {
$THEME->scss = function($themeconfig) {
     $scsscontent = '$my-var: red;';
     $scsscontent = '$my-var: red;';
Line 66: Line 66:
     return $scsscontent;
     return $scsscontent;
}
}
</code>
</syntaxhighlight>


== Example ==
== Example ==

Latest revision as of 20:21, 14 July 2021

Moodle 3.2

Overview

SCSS or SASS Sass (Syntactically Awesome Stylesheets) is a style sheet language initially designed by Hampton Catlin and developed by Natalie Weizenbaum. It's an advanced way of writing CSS that we use in some themes within Moodle.

Browsers don't interpret it themselves, however, so SCSS files need to be converted into normal CSS before use.

Using in a theme

Moodle includes a built in SCSS compiler so to use SCSS in your theme, you only need to set the theme scss variable, in your theme's config.php file, to point to the main scss file for your theme:

// Points to the file theme/themename/scss/main.scss.
$THEME->scss = 'main';

Moodle will look for a file named 'main.scss' in the 'theme/themename/scss' folder and compile it. If you don't see the affect of your changes right away, try purging the caches or enabling theme designer mode.

Inheriting from a parent

Even if your theme is inheriting from a parent, the SCSS file itself will not inherit from anything, this is something you should do manually. For instance, if you want your SCSS file to include all of the SCSS code provided by theme_boost, usually to change the variables, you need to manually import the file like this:

// In your theme's SCSS file
@import "../../boost/scss/moodle";

The path needs to be relative and not absolute. You would use it like this:

$brand-primary:             #00acdf !default;
$my-color:                  #ff9900 !default;

@import "../../boost/scss/moodle";

body {
    background-color: $my-color;
}
NOTICE: At the moment, Moodle is using a PHP sass compiler which does not support '@import url' at-rule MDL-57875, which is a challenge when you wish to use bootswatch presets or Google fonts directly. so it is advise to use the OS level sass binary compiler (by installing libsass via 'yum install libsass' || 'apt install sassc', or other means) and then setting the admin $CFG->pathtosassc to use the 'sassc' binary.

Advance usage

On-the-fly injections

The following settings allow themers to point to a function which will either prepend, or append, SCSS code to content gotten from $THEME->scss.

// The content returned by this function is added before the main SCSS.
$THEME->prescsscallback = 'theme_themename_scss_to_prepend';

// The content returned by this function is added after the main SCSS.
$THEME->extrascsscallback = 'theme_themename_scss_to_append';

The functions will receive the theme config as first argument.

Dynamic $THEME->scss

Rather than giving the name of your SCSS file in $THEME->scss, you can make it a function which returns the SCSS content.

$THEME->scss = function($themeconfig) {
    $scsscontent = '$my-var: red;';
    $scsscontent .= body { color: $my-var; }';
    return $scsscontent;
}

Example

Moodle's Boost theme has rules written using SCSS.

See also