BaseCSSCompiler.js
2 years ago
BorderCSSCompiler.js
2 years ago
BorderRadiusCSSCompiler.js
2 years ago
BaseCSSCompiler.js
69 lines
| 1 | const { get } = window._; |
| 2 | |
| 3 | function BaseCSSCompiler() { |
| 4 | this.cssVar = ''; |
| 5 | /** |
| 6 | * @type {String[]} |
| 7 | */ |
| 8 | this.path = []; |
| 9 | } |
| 10 | |
| 11 | BaseCSSCompiler.prototype = { |
| 12 | /** |
| 13 | * @param path {String[]} |
| 14 | * @returns {boolean} |
| 15 | */ |
| 16 | isSupported( path ) { |
| 17 | return true; |
| 18 | }, |
| 19 | compileDeclarations( styleRoot, response, classNames ) { |
| 20 | const value = get( styleRoot, this.path ); |
| 21 | |
| 22 | if ( !value ) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | response[ this.cssVar ] = value; |
| 27 | }, |
| 28 | compileClassNames( classNames, styleRoot ) { |
| 29 | if ( |
| 30 | !this.hasHoverPath() || |
| 31 | !get( styleRoot, this.path ) |
| 32 | ) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | // Remove selector from path |
| 37 | const withoutFirst = this.path.slice( 1 ); |
| 38 | |
| 39 | classNames.push( 'has-hover-' + withoutFirst.join( '-' ) ); |
| 40 | }, |
| 41 | hasHoverPath() { |
| 42 | const parts = this.path[ 0 ].split( ':' ); |
| 43 | |
| 44 | return ( |
| 45 | parts?.[ 1 ] && 'hover' === parts[ 1 ] |
| 46 | ); |
| 47 | }, |
| 48 | /** |
| 49 | * @param cssVar {String} |
| 50 | * @returns {BaseCSSCompiler} |
| 51 | */ |
| 52 | setCssVar( cssVar ) { |
| 53 | this.cssVar = cssVar; |
| 54 | |
| 55 | return this; |
| 56 | }, |
| 57 | |
| 58 | /** |
| 59 | * @param path {String[]} |
| 60 | * @returns {BaseCSSCompiler} |
| 61 | */ |
| 62 | setPath( path ) { |
| 63 | this.path = path; |
| 64 | |
| 65 | return this; |
| 66 | }, |
| 67 | }; |
| 68 | |
| 69 | export default BaseCSSCompiler; |