git.iko.soy/typeable/elm-ui
on master help sign in
cssom experiment
commit8f0ce1358e6df2b9f3f760e6879b9e8b79367e68 authorMatthew Griffith <mdg.griffith@gmail.com> dateSun 7 Jul 2019, 10:49 (7 years ago) parent103299f
5 files changed+177−8
experiments/virtual-css/Main.elm+46 experiments/virtual-css/Makefile+1 experiments/virtual-css/cssom.js+87−8 experiments/virtual-css/elm.json+25 experiments/virtual-css/test.html+18
experiments/virtual-css/Main.elmnew file+46
@@ -0,0 +1,46 @@
1+module Main exposing (main)
2+
3+import Browser
4+import Element exposing (..)
5+import Element.Background as Background
6+import Element.Events as Events
7+import Element.Font as Font
8+import Html
9+import Internal.Model as Internal
10+
11+
12+main =
13+ Browser.sandbox
14+ { init = 0
15+ , update = update
16+ , view = view
17+ }
18+
19+
20+update msg model =
21+ case msg of
22+ Click ->
23+ model + 1
24+
25+
26+type Msg
27+ = Click
28+
29+
30+view model =
31+ Element.layoutWith
32+ { options =
33+ [ Internal.RenderModeOption
34+ Internal.WithVirtualCss
35+ ]
36+ }
37+ [ Events.onClick Click ]
38+ (el
39+ [ padding 10
40+ , Background.color (rgb 0 0 0.8)
41+ , Font.color (rgb 1 1 1)
42+ , centerX
43+ , centerY
44+ ]
45+ (text ("hello!" ++ String.fromInt model))
46+ )
experiments/virtual-css/Makefilenew file+1
@@ -0,0 +1 @@
1+elm-live Main.elm --open --start-page=test.html -- --output=elm.js
experiments/virtual-css/cssom.js+87−8
@@ -1,16 +1,95 @@
1−class CssRulesNode extends HTMLParagraphElement {
1+(function () {
2+ var ruleCache = new Set()
3+
4+ function createStyleNode() {
5+
6+ const node = document.createElement('style')
7+ // Without it, IE will have a broken source order specificity if we
8+ // insert rules after we insert the style tag.
9+ // It seems to kick-off the source order specificity algorithm.
10+ node.textContent = '\n'
11+ document.head.appendChild(node);
12+ return node
13+ }
14+
15+
16+ var staticSheet = createStyleNode();
17+ var sheet = createStyleNode().sheet;
18+
19+
20+
21+ function sync_static(newStyleString) {
22+ staticSheet.textContent = newStyleString
23+ }
24+
25+ function sync(newStyles) {
26+ Object.keys(newStyles).forEach((key) => {
27+ if (!ruleCache.has(key)) {
28+ const newRule = newStyles[key]
29+ sheet.insertRule(newRule)
30+ ruleCache.add(key)
31+ }
32+ });
33+ }
34+
35+ class CssRulesNode extends HTMLElement {
236 constructor() {
337 var self = super();
4−
5− // Our css rule cache
6− self.rules = {}
7− return self
838 }
39+ connectedCallback() {
40+ sync(this.rulesPayload)
41+ }
42+ set rules(rules) {
43+ console.log("set rules")
44+ console.log(rules)
45+ if (self.rulesPayload) {
46+ console.log(self.rulesPayload)
47+ }
48+ this._rules = rules;
49+ console.log(this._rules)
50+ }
51+ attributeChangedCallback(attrName, oldVal, newVal) {
52+ console.log(attrName, oldVal, newVal)
53+ }
54+ }
55+ customElements.define('elm-ui-rules', CssRulesNode);
56+
957
58+
59+ class CssStaticRulesNode extends HTMLElement {
60+ constructor() {
61+ var self = super();
62+ this._rules = null;
63+ }
64+ connectedCallback() {
65+ // console.log("static connected")
66+ // console.log(this)
67+ // if (this._rules) {
68+ // sync_static(this._rules)
69+ // }
70+ }
1071 set rules(rules) {
11− this._rules = rules;
72+ this._rules = rules
73+ sync_static(this._rules)
74+ // console.log("set static rules")
75+ // console.log(rules)
76+ }
77+
78+ attributeChangedCallback(attrName, oldVal, newVal) {
79+ console.log("attribute cng")
80+ console.log(attrName)
81+ if (attrName == "rules") {
82+ sync_static(newVal)
83+ }
1284 }
13−}
85+ }
86+ customElements.define('elm-ui-static-rules', CssStaticRulesNode);
87+
88+
89+
90+
91+
92+
1493
1594
16−customElements.define('elm-ui-css', CssRulesNode, { extends: 'div' });
95+}())
experiments/virtual-css/elm.jsonnew file+25
@@ -0,0 +1,25 @@
1+{
2+ "type": "application",
3+ "source-directories": [
4+ ".",
5+ "../../src/"
6+ ],
7+ "elm-version": "0.19.0",
8+ "dependencies": {
9+ "direct": {
10+ "elm/browser": "1.0.0",
11+ "elm/core": "1.0.0",
12+ "elm/html": "1.0.0",
13+ "elm/json": "1.0.0",
14+ "elm/virtual-dom": "1.0.0"
15+ },
16+ "indirect": {
17+ "elm/time": "1.0.0",
18+ "elm/url": "1.0.0"
19+ }
20+ },
21+ "test-dependencies": {
22+ "direct": {},
23+ "indirect": {}
24+ }
25+}
experiments/virtual-css/test.htmlnew file+18
@@ -0,0 +1,18 @@
1+<!DOCTYPE HTML>
2+<html>
3+<head>
4+ <meta charset="UTF-8">
5+ <title>Main</title>
6+ <script src="cssom.js"></script>
7+ <script src="elm.js"></script>
8+</head>
9+
10+<body>
11+ <div id="elm"></div>
12+ <script>
13+ var app = Elm.Main.init({
14+ node: document.getElementById('elm')
15+ });
16+ </script>
17+</body>
18+</html>