1- // tooling
2- const postcss = require ( 'postcss' ) ;
1+ const creator = ( ) => {
2+ // side properties
3+ const properties = [ 'top' , 'right' , 'bottom' , 'left' ] ;
34
4- // side properties
5- const properties = [ 'top' , 'right' , 'bottom' , 'left' ] ;
5+ return {
6+ postcssPlugin : 'postcss-inset' ,
7+ Declaration : {
8+ 'inset' : ( decl , { list } ) => {
9+ // space-separated side values (top, right, bottom, left)
10+ const values = list . space ( decl . value ) ;
611
7- // inset plugin
8- module . exports = postcss . plugin ( 'postcss-inset' , ( ) => ( root ) => {
9- root . walkDecls ( 'inset' , ( decl ) => {
10- // space-separated side values (top, right, bottom, left)
11- const values = postcss . list . space ( decl . value ) ;
12+ // conditionally add a right value
13+ if ( values . length === 1 ) {
14+ values . push ( values [ 0 ] ) ;
15+ }
1216
13- // conditionally add a right value
14- if ( values . length === 1 ) {
15- values . push ( values [ 0 ] ) ;
16- }
17+ // conditionally add a bottom value
18+ if ( values . length === 2 ) {
19+ values . push ( values [ 0 ] ) ;
20+ }
1721
18- // conditionally add a bottom value
19- if ( values . length === 2 ) {
20- values . push ( values [ 0 ] ) ;
21- }
22+ // conditionally add a left value
23+ if ( values . length === 3 ) {
24+ values . push ( values [ 1 ] ) ;
25+ }
2226
23- // conditionally add a left value
24- if ( values . length === 3 ) {
25- values . push ( values [ 1 ] ) ;
26- }
27+ // only transform up to 4 side values
28+ if ( values . length === 4 ) {
29+ // for each side property
30+ properties . forEach ( ( property , index ) => {
31+ // create a new declaration for the side
32+ decl . cloneBefore ( {
33+ prop : property ,
34+ value : values [ index ]
35+ } ) ;
36+ } ) ;
37+ }
2738
28- // only transform up to 4 side values
29- if ( values . length === 4 ) {
30- // for each side property
31- properties . forEach ( ( property , index ) => {
32- // create a new declaration for the side
33- decl . cloneBefore ( {
34- prop : property ,
35- value : values [ index ]
36- } ) ;
37- } ) ;
39+ decl . remove ( ) ;
40+ } ,
3841 }
42+ }
43+ }
44+
45+ creator . postcss = true
3946
40- decl . remove ( ) ;
41- } ) ;
42- } ) ;
47+ export default creator
0 commit comments