Open ajhsu opened 7 years ago
Introduction of Sass functions from the official document http://sass-lang.com/documentation/Sass/Script/Functions.html
Docs of api function of libSass https://github.com/sass/node-sass/blob/master/src/libsass/docs/api-function.md
Draft of the PR
Since the Custom functions [was introduced](https://github.com/sass/node-sass/pull/644) with the first version of its document, it has been 2+ years past.
I think the section of document about the Custom functions should be explained in more detailed.
Original issue about custom functions:
https://github.com/sass/node-sass/issues/332
Original PR about custom functions:
https://github.com/sass/node-sass/pull/644
functions (>= v3.0.0) - experimental
This is an experimental LibSass feature. Use with caution.
// Mention about internal functions Like these useful functions Sass provided out of box, you can implement your own functions as well.
functions
is anObject
that holds a collection of custom functions that may be invoked by the sass files being compiled.Function Signature (String)
Function signature is a plain JavaScript string that should be written in following function declaration syxtax:
"function-name($param1, $param2, $param3)"
For example:
"custom-url($path)": function (path) { }
So that you can call custom function within your Sass files:
background-image: custom-url('/images/example.jpg');
Since function signatures are also part of SassScript, few things should be remind:
Callback (Function)
Callback may take zero or more input parameters and must return a value either synchronously (
return ...;
) or asynchronously (done();
). Those parameters will be instances of one of the constructors contained in therequire('node-sass').types
hash. The return value must be of one of these types as well. See the list of available types below:types.Number(value [, unit = ""])
getValue()
/setValue(value)
: gets / sets the numerical portion of the numbergetUnit()
/setUnit(unit)
: gets / sets the unit portion of the numbertypes.String(value)
getValue()
/setValue(value)
: gets / sets the enclosed stringtypes.Color(r, g, b [, a = 1.0]) or types.Color(argb)
getR()
/setR(value)
: red component (integer from0
to255
)getG()
/setG(value)
: green component (integer from0
to255
)getB()
/setB(value)
: blue component (integer from0
to255
)getA()
/setA(value)
: alpha component (number from0
to1.0
)Example:
types.Boolean(value)
getValue()
: gets the enclosed booleantypes.Boolean.TRUE
: Singleton instance oftypes.Boolean
that holds "true"types.Boolean.FALSE
: Singleton instance oftypes.Boolean
that holds "false"types.List(length [, commaSeparator = true])
getValue(index)
/setValue(index, value)
:value
must itself be an instance of one of the constructors insass.types
.getSeparator()
/setSeparator(isComma)
: whether to use commas as a separatorgetLength()
types.Map(length)
getKey(index)
/setKey(index, value)
getValue(index)
/setValue(index, value)
getLength()
types.Null()
types.Null.NULL
: Singleton instance oftypes.Null
.Example