shospodarets / sass-to-js Goto Github PK
View Code? Open in Web Editor NEWLibrary to pass Sass variables via CSS to JavaScript
Home Page: https://hospodarets.com/passing_data_from_sass_to_js
Library to pass Sass variables via CSS to JavaScript
Home Page: https://hospodarets.com/passing_data_from_sass_to_js
To reproduce:
git checkout v1.2.0
npm install
npm run test
Output:
$ npm run test
> [email protected] test /Users/jess.telford/dev/sass-to-js
> grunt test
Running "sass:test" (sass) task
Running "jasmine:src" (jasmine) task
Testing jasmine specs via PhantomJS
Getting JSON using AngularJS
X shouldn convert generated CSS to JSON
Expected Object({ }) to equal Object({ colorHexShort: '#f00', colorHex: '#ff0000', colorRgba: 'rgba(255,0,0,0.5)' }). (1)
✓ shouldn convert generated CSS to JSON in the same way as usual global function call
Debug mode test
✓ should throw an error when element is not provided and return an empty object
✓ should throw an error when jQuery element is not provided and return an empty object
✓ should throw an error when AngularJS element is not provided and return an empty object
✓ should throw an error when there isn't CSS value for property and return an empty object
✓ should throw an error when CSS value cannot be parsed as JSON and return an empty object
Test getting JSON from <body/> CSS
X shouldn be not empty JSON object in <body> CSS
Expected 0 not to be 0. (1)
X shouldn get expected complex JSON object from global element
Expected '{}' to be '{"stringsMap":{"stringWithQuotes":"string","stringWithDoubleQuotes":"string","stringWithoutQuotes":"string","stringTrue":"true","stringFalse":"false","pixels":"480px","percents":"4%"},"colorMap":{"colorHexShort":"#f00","colorHex":"#ff0000","colorRgba":"rgba(255,0,0,0.5)"},"numberMap":{"int":10,"positiveNum":4.7,"negativeNum":-43.67},"othersMap":{"boolean":true,"nulls":null},"mapOfMaps":{"map":{"prop1":1,"prop2":2,"prop3":3},"map-nested":{"prop1":{"prop2":[1,2,{"prop3":"test"}]}},"map-full":{"prop1":[1,2,{"prop12":1}],"prop2":["#ff0",false,{"prop21":1,"prop22":"string2"}],"prop3":[2,3,4,"string3"]}},"listMap":{"numbers":[1,2,3],"strings":["string1","string2"],"booleans":[true,false],"cssValuesList":["1.5em","1em",0,"2em"],"listCommaSeparated":["item-1","item-2","item-3"],"withPodLists":[["1px","2px"],["5px","6px"]],"nestedList":["a","b",["c","d","e",["f","g"],"h"],"i"]}}'. (1)
Global JS scope test
✓ shouldn't be a global function when jQuery or Angular is loaded
✓ shouldn't be a global jQuery function
✓ shouldn't be a global AngularJS function
Getting JSON using jQuery
X shouldn convert generated CSS to JSON
Expected Object({ }) to equal Object({ colorHexShort: '#f00', colorHex: '#ff0000', colorRgba: 'rgba(255,0,0,0.5)' }). (1)
✓ shouldn convert generated CSS to JSON in the same way as usual global function call
Getting JSON using pure JavaScript
X shouldn convert generated CSS to JSON
Expected Object({ }) to equal Object({ colorHexShort: '#f00', colorHex: '#ff0000', colorRgba: 'rgba(255,0,0,0.5)' }). (1)
X shouldn convert CSS to JSON from provided CSS property in CSS generated element
Expected Object({ }) to equal Object({ pseudoEl: 'pseudoEl' }). (1)
- shouldn convert CSS to JSON from provided CSS property in CSS generated elementSass string value for which was provided wit X shouldn convert CSS to JSON from provided CSS property in CSS generated elementSass string value for which was provided with property name
Expected undefined to equal 'colorMap'. (1)
- shouldn convert CSS to JSON from provided CSS property in CSS generated elementSass complex value for which was provided wi X shouldn convert CSS to JSON from provided CSS property in CSS generated elementSass complex value for which was provided with property name
Expected undefined to equal Object({ colorHexShort: '#f00', colorHex: '#ff0000', colorRgba: 'rgba(255,0,0,0.5)' }). (1)
18 specs in 0.011s.
>> 8 failures
Warning: Task "jasmine:src" failed. Use --force to continue.
Aborted due to warnings.
I am saving an Angular Material2 theme to an element to parse out with Angular on the client side. IE11 and IE Edge show the value in the DOM Explorer with a strikeout, and when SassToJs tries to pull it out on the client side it gets no values. This solution works in Chrome and Firefox.
Is this a problem with how values are encoded into the font-family, or is there a complexity limit to the encoded JSON object?
sass (within an angular component):
$app-theme: mat-light-theme($app-primary, $app-accent, $app-warn);
$dark-theme: mat-dark-theme($app-primary, $app-accent, $app-warn);
:host::before {
font-family: sassToJs($app-theme); //$app-theme or $dark-theme
}
:host::after {
font-family: sassToJs($dark-theme); //$app-theme or $dark-theme
}
In the Angular component in ngAfterViewInit we pull the values out:
const lightPalettes = sassToJs(this.el.nativeElement, {pseudoEl: '::before'});
console.log("getThemeFromScss nativeEl:", this.el.nativeElement);
console.log("getThemeFromScss lightPalettes:", lightPalettes);
const darkPalettes = sassToJs(this.el.nativeElement, {pseudoEl: '::after'});
The console log from this event
Edge:
Here is the value copied from the Chrome style window:
{
"primary": {
"50": "#DAF9E8",
"100": "#C0E1D0",
"200": "#A6CAB9",
"300": "#8CB2A2",
"400": "#729B8B",
"500": "#598474",
"600": "#4E7869",
"700": "#436D5E",
"800": "#386253",
"900": "#2D5749",
"contrast": {
"50": "rgba(0, 0, 0, 0.82)",
"100": "rgba(0, 0, 0, 0.9)",
"200": "rgba(0, 0, 0, 0.9)",
"300": "rgba(0, 0, 0, 0.9)",
"400": "rgba(0, 0, 0, 0.9)",
"500": "rgba(255, 255, 255, 0.87)",
"600": "rgba(255, 255, 255, 0.87)",
"700": "rgba(255, 255, 255, 0.87)",
"800": "rgba(255, 255, 255, 0.87)",
"900": "rgba(255, 255, 255, 0.87)"
},
"default": "#598474",
"lighter": "#C0E1D0",
"darker": "#436D5E",
"default-contrast": "rgba(255, 255, 255, 0.87)",
"lighter-contrast": "rgba(0, 0, 0, 0.9)",
"darker-contrast": "rgba(255, 255, 255, 0.87)",
"50-contrast": "rgba(0, 0, 0, 0.82)",
"100-contrast": "rgba(0, 0, 0, 0.9)",
"200-contrast": "rgba(0, 0, 0, 0.9)",
"300-contrast": "rgba(0, 0, 0, 0.9)",
"400-contrast": "rgba(0, 0, 0, 0.9)",
"500-contrast": "rgba(255, 255, 255, 0.87)",
"600-contrast": "rgba(255, 255, 255, 0.87)",
"700-contrast": "rgba(255, 255, 255, 0.87)",
"800-contrast": "rgba(255, 255, 255, 0.87)",
"900-contrast": "rgba(255, 255, 255, 0.87)",
"contrast-contrast": null
},
"accent": {
"50": "#FBE5DA",
"100": "#E0C0B4",
"200": "#C59C8E",
"300": "#AA7868",
"400": "#8F5442",
"500": "#75301C",
"600": "#692415",
"700": "#5D190E",
"800": "#510E07",
"900": "#460300",
"contrast": {
"50": "rgba(0, 0, 0, 0.82)",
"100": "rgba(0, 0, 0, 0.82)",
"200": "rgba(0, 0, 0, 0.82)",
"300": "rgba(0, 0, 0, 0.9)",
"400": "rgba(255, 255, 255, 0.87)",
"500": "rgba(255, 255, 255, 0.87)",
"600": "rgba(255, 255, 255, 0.87)",
"700": "rgba(255, 255, 255, 0.87)",
"800": "rgba(255, 255, 255, 0.77)",
"900": "rgba(255, 255, 255, 0.77)"
},
"default": "#75301C",
"lighter": "#E0C0B4",
"darker": "#5D190E",
"default-contrast": "rgba(255, 255, 255, 0.87)",
"lighter-contrast": "rgba(0, 0, 0, 0.82)",
"darker-contrast": "rgba(255, 255, 255, 0.87)",
"50-contrast": "rgba(0, 0, 0, 0.82)",
"100-contrast": "rgba(0, 0, 0, 0.82)",
"200-contrast": "rgba(0, 0, 0, 0.82)",
"300-contrast": "rgba(0, 0, 0, 0.9)",
"400-contrast": "rgba(255, 255, 255, 0.87)",
"500-contrast": "rgba(255, 255, 255, 0.87)",
"600-contrast": "rgba(255, 255, 255, 0.87)",
"700-contrast": "rgba(255, 255, 255, 0.87)",
"800-contrast": "rgba(255, 255, 255, 0.77)",
"900-contrast": "rgba(255, 255, 255, 0.77)",
"contrast-contrast": null
},
"warn": {
"50": "#ffebee",
"100": "#ffcdd2",
"200": "#ef9a9a",
"300": "#e57373",
"400": "#ef5350",
"500": "#f44336",
"600": "#e53935",
"700": "#d32f2f",
"800": "#c62828",
"900": "#b71c1c",
"A100": "#ff8a80",
"A200": "#ff5252",
"A400": "#ff1744",
"A700": "#d50000",
"contrast": {
"50": "rgba(0, 0, 0, 0.87)",
"100": "rgba(0, 0, 0, 0.87)",
"200": "rgba(0, 0, 0, 0.87)",
"300": "rgba(0, 0, 0, 0.87)",
"400": "rgba(0, 0, 0, 0.87)",
"500": "white",
"600": "white",
"700": "white",
"800": "rgba(255, 255, 255, 0.87)",
"900": "rgba(255, 255, 255, 0.87)",
"A100": "rgba(0, 0, 0, 0.87)",
"A200": "white",
"A400": "white",
"A700": "white"
},
"default": "#f44336",
"lighter": "#ffcdd2",
"darker": "#d32f2f",
"default-contrast": "white",
"lighter-contrast": "rgba(0, 0, 0, 0.87)",
"darker-contrast": "white",
"50-contrast": "rgba(0, 0, 0, 0.87)",
"100-contrast": "rgba(0, 0, 0, 0.87)",
"200-contrast": "rgba(0, 0, 0, 0.87)",
"300-contrast": "rgba(0, 0, 0, 0.87)",
"400-contrast": "rgba(0, 0, 0, 0.87)",
"500-contrast": "white",
"600-contrast": "white",
"700-contrast": "white",
"800-contrast": "rgba(255, 255, 255, 0.87)",
"900-contrast": "rgba(255, 255, 255, 0.87)",
"A100-contrast": "rgba(0, 0, 0, 0.87)",
"A200-contrast": "white",
"A400-contrast": "white",
"A700-contrast": "white",
"contrast-contrast": null
},
"is-dark": false,
"foreground": {
"base": "black",
"divider": "rgba(0, 0, 0, 0.12)",
"dividers": "rgba(0, 0, 0, 0.12)",
"disabled": "rgba(0, 0, 0, 0.38)",
"disabled-button": "rgba(0, 0, 0, 0.38)",
"disabled-text": "rgba(0, 0, 0, 0.38)",
"hint-text": "rgba(0, 0, 0, 0.38)",
"secondary-text": "rgba(0, 0, 0, 0.54)",
"icon": "rgba(0, 0, 0, 0.54)",
"icons": "rgba(0, 0, 0, 0.54)",
"text": "rgba(0, 0, 0, 0.87)"
},
"background": {
"status-bar": "#e0e0e0",
"app-bar": "#f5f5f5",
"background": "#fafafa",
"hover": "rgba(0, 0, 0, 0.04)",
"card": "white",
"dialog": "white",
"disabled-button": "rgba(0, 0, 0, 0.12)",
"raised-button": "white",
"focused-button": "rgba(0, 0, 0, 0.06)",
"selected-button": "#e0e0e0",
"selected-disabled-button": "#bdbdbd",
"disabled-button-toggle": "#eeeeee"
}
}
I've created a Codepen to demonstrate the faulty behaviour.
The problem is that:
window.getComputedStyle(
document.querySelector('body'), '::before')
.getPropertyValue('content');
...returns none
for pseudo elements that don't exist.
Ideally, that never happens. But I think this is an enough usual use case to be handled by the library.
If you want I can try and prepare a PR to solve this issue.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.