11---
2- layout : none
32---
43
54<h3 style =' text-align :center ' >JavaScript</h3 >
65
6+ Because 4bitCSS sets CSS root variables, you can _ get_ a value by using [ getPropertyValue] ( https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/getPropertyValue )
7+
78~~~ javascript
89var body = document .querySelector (" body" );
9- var fourBitCssLink = document .getElementById (" 4bitcss" );
1010var foregroundValue = getComputedStyle (body).getPropertyValue (' --foreground' );
11- var colorHexPreview = document .getElementById (' ColorHexPreview' );
12- colorHexPreview .value = fourBitCssLink .style .getPropertyValue (' --foreground' );
13- var colorHexPreviewText = document .getElementById (' ColorHexPreviewText' )
14- colorHexPreviewText .value = foregroundValue;
11+ ~~~
12+
13+ To set a value, we will want to be a little more clever.
14+
15+ We can set use [ setProperty] ( https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty ) to change the value.
16+
17+ In order to ensure that it can still be modified when the stylesheet changes, we'll want to find the stylesheet first.
18+
19+ ~~~ javascript
20+ // Get our stylesheet
21+ var FourBitCssLink = document .getElementById (" 4bitcss" );
22+ // change the property in the first CSS rule.
23+ FourBitCssLink .sheet .cssRules [0 ].style .setProperty (" --foreground" ," #ffffff" )
24+ ~~~
25+
26+ If we want to know when the property changes, we'll need to use a [ MutationObserver] ( https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver )
27+
28+ ~~~ javascript
29+ var observer = new MutationObserver (function (mutations ) {
30+ mutations .forEach (function (mutationRecord ) {
31+ console .log (' style changed!' );
32+ ForegroundPreview .value = getComputedStyle (FourBitCssLink).getPropertyValue (' --foreground' );
33+ ForegroundPreviewText .innerText = " Foreground " + ForegroundPreview .value ;
34+ });
35+ });
1536~~~
1637
1738<div style =' text-align :center ' >
@@ -20,27 +41,56 @@ colorHexPreviewText.value = foregroundValue;
2041var observer = new MutationObserver (function (mutations ) {
2142 mutations .forEach (function (mutationRecord ) {
2243 console .log (' style changed!' );
44+ ForegroundPreview .value = getComputedStyle (FourBitCssLink).getPropertyValue (' --foreground' );
45+ ForegroundPreviewText .innerText = " Foreground " + ForegroundPreview .value ;
46+ BackgroundPreview .value = getComputedStyle (FourBitCssLink).getPropertyValue (' --background' );
47+ BackgroundPreviewText .innerText = " Background " + BackgroundPreview .value ;
2348 });
2449});
2550
26- var colorHexPreview = document .getElementById (' ColorHexPreview' );
27- var fourBitCssLink = document .getElementById (" 4bitcss" );
28- observer .observe (fourBitCssLink, { attributes : true , attributeFilter : [' href' ] });
51+ var FourBitCssLink = document .getElementById (" 4bitcss" );
52+ var ForegroundPreview = document .getElementById (' ForegroundPreview' );
53+ var ForegroundPreviewText = document .getElementById (' ForegroundPreviewText' )
54+ var BackgroundPreview = document .getElementById (' BackgroundPreview' );
55+ var BackgroundPreviewText = document .getElementById (' BackgroundPreviewText' );
2956
30- fourBitCssLink . addEventListener ( ' change ' , ( event ) => {
31- colorHexPreview . value = getComputedStyle (fourBitCssLink). getPropertyValue ( ' --foreground ' );
32- colorHexPreviewText . value = colorHexPreview . value ;
33- });
34- colorHexPreview . value = getComputedStyle (fourBitCssLink). getPropertyValue ( ' --foreground ' ) ;
35- colorHexPreview .addEventListener (' change' , (event )=> {
36- var styleSheet = fourBitCssLink .sheet ;
57+ observer . observe (FourBitCssLink, { attributes : true , attributeFilter : [ ' href ' ] });
58+
59+ var foregroundValue = getComputedStyle (FourBitCssLink). getPropertyValue ( ' --foreground ' ) ;
60+ ForegroundPreview . value = foregroundValue
61+ ForegroundPreviewText . innerText = " Foreground " + foregroundValue ;
62+ ForegroundPreview .addEventListener (' change' , (event )=> {
63+ var styleSheet = FourBitCssLink .sheet ;
3764 styleSheet .cssRules [0 ].style .setProperty (" --foreground" ,event .target .value );
65+ ForegroundPreviewText .innerText = " Foreground" + ForegroundPreview .value ;
66+ });
67+
68+ var backgroundValue = getComputedStyle (FourBitCssLink).getPropertyValue (' --background' )
69+ BackgroundPreview .value = backgroundValue;
70+ BackgroundPreviewText .innerText = " Background" + backgroundValue;
71+ BackgroundPreview .addEventListener (' change' , (event )=> {
72+ var styleSheet = FourBitCssLink .sheet ;
73+ styleSheet .cssRules [0 ].style .setProperty (" --background" ,event .target .value );
74+ BackgroundPreviewText .innerText = " Background" + BackgroundPreview .value ;
3875});
39- var colorHexPreviewText = document .getElementById (' ColorHexPreviewText' )
40- colorHexPreviewText .value = colorHexPreview .value ;
4176
4277</script >
43- <input type =' color ' id =' ColorHexPreview ' />
44- <input type =' text ' id =' ColorHexPreviewText ' />
45- </div >
78+ <table >
79+ <tr >
80+ <td >
81+ <input type =' color ' id =' ForegroundPreview ' />
82+ </td >
83+ <td >
84+ <label for =' ForegroundPreview ' id =' ForegroundPreviewText ' >Foreground</label >
85+ </td >
86+ </tr >
87+ <tr >
88+ <td >
89+ <input type =' color ' id =' BackgroundPreview ' />
90+ </td >
91+ <td >
92+ <label for =' BackgroundPreview ' id =' BackgroundPreviewText ' >Background</label >
93+ </td >
94+ </tr >
95+ </table >
4696<script >hljs .highlightAll ();</script >
0 commit comments