SASS | @forward rule
Last Updated :
21 Jun, 2020
The @forward rule processes the Sass stylesheets and makes their functions, mixins, and variables available in the cases when the stylesheet is loaded using @use rule. Organizing Sass libraries across many files are made possible, along with providing users to process a single entry point file.
Syntax:
The above code loads the module available at the given URL. It makes the public members of the loaded module available to users of the module as though they were defined directly in the module. Those members aren’t available in the module if needed, @use rule is also likely to be used. It only processes the module once. If both @forward and @use are written for the same module in the same file, always write @forward first. Through this, if the users want to configure the forwarded module, that configuration will be applied to @forward before @use processes it without any configuration.
Example:
// gfg/_list.css
@mixin geeks
font-family : times new roman
font-size : 4px
padding : 2px
// geeksforgeeks.scss
@forward "gfg/list"
// style.scss
@use "geeksforgeeks"
gfg 1
@include geeksforgeeks.geeks
|
This would result in the following CSS output:
gfg1 {
font-family: times new roman
font-size: 4px
padding: 2px
}
Adding Prefix:
Sometimes the names may not be correct outside of the module that they’re defined in, for this reason, @forward has the option of adding an extra prefix to all the members that it points to.
Syntax:
@forward "<url>" as <prefix>-*
|
This adds the given prefix to the beginning of each mixin, function, and variable name pointed by the module.
Example:
// gfg/_list.css
@mixin forgeeks
font-family : times new roman
font-size : 4px
padding : 2px
// geeks.scss
@forward "gfg/list" as geeks*
// style.scss
@use "geeksforgeeks"
gfg 1
@include geeks.geeksforgeeks
|
This would result in the following CSS output:
gfg1 {
font-family: times new roman
font-size: 4px
padding: 2px
}
Configuring Modules:
Processing a module with configuration is also possible using @forward rule. @forward rule’s configuration can also use !default flag in its configuration. This allows the module to change the defaults of the upstream stylesheet while also allowing downstream stylesheets to override them.
Example:
// _gfg.sass
$green: #000 ! default
$border-radius: 2px ! default
$box-shadow: 0 2px 1px rgba($ green , 1 ) ! default
geeks
border-radius: $border-radius
box-shadow: $box-shadow
// _geeksforgeeks.sass
@forward 'gfg' with ($green: #222 ! default ,
$border-radius: 4px ! default )
// style.sass
@use 'geeksforgeeks' with ($green: #333 )
|
This would result in the following CSS output:
geeks {
border-radius: 4px;
box-shadow: 0 2px 1px rgba(51, 51, 51, 1);
}
Share your thoughts in the comments
Please Login to comment...