diff --git a/experiments/example.nim b/experiments/example.nim index 229cce6..caa7285 100644 --- a/experiments/example.nim +++ b/experiments/example.nim @@ -1,5 +1,5 @@ -import vdom, components, karax, karaxdsl, jdict, jstrutils, dom +import vdom, vstyles, components, karax, karaxdsl, jdict, jstrutils, dom var images: seq[cstring] = @[cstring"a", "b", "c", "d"] @@ -24,10 +24,20 @@ proc carousel*(key: VKey): VNode {.component.} = clearTimeout(timer) timer = setTimeout(docount, 30) + let col = + case counter + of 0: cstring"#4d4d4d" + of 1: cstring"#ff00ff" + of 2: cstring"#00ffff" + of 3: cstring"#ffff00" + else: cstring"red" + result = buildHtml(tdiv(key=key)): text images[counter] button(onclick = next): text "Next" + tdiv(style = style(StyleAttr.color, col)): + text "This changes its color." if cntdown != 5: text &cntdown diff --git a/src/dom.nim b/src/dom.nim index f3d3f73..f2dabcf 100644 --- a/src/dom.nim +++ b/src/dom.nim @@ -188,7 +188,7 @@ type vspace*: int width*: int - Style = ref StyleObj + Style* = ref StyleObj StyleObj {.importc.} = object of RootObj background*: cstring backgroundAttachment*: cstring diff --git a/src/karax.nim b/src/karax.nim index ed956d8..2ac4e91 100644 --- a/src/karax.nim +++ b/src/karax.nim @@ -1,6 +1,6 @@ ## Karax -- Single page applications for Nim. -import dom, vdom, jstrutils, components, jdict +import dom, vdom, jstrutils, components, jdict, vstyles export dom.Event @@ -104,6 +104,7 @@ proc vnodeToDom(n: VNode): Node = wrapEvent(result, n, e, h) if n == toFocusV and toFocus.isNil: toFocus = result + if not n.style.isNil: applyStyle(result, n.style) proc same(n: VNode, e: Node): bool = if toTag[n.kind] == e.nodename: @@ -135,6 +136,7 @@ proc equalsShallow(a, b: VNode): bool = if not equalsShallow(a[i], b[i]): return false if not sameAttrs(a, b): return false if a.class != b.class: return false + if a.style != b.style: return false # XXX test event listeners here? return true diff --git a/src/karaxdsl.nim b/src/karaxdsl.nim index faa43c3..190545c 100644 --- a/src/karaxdsl.nim +++ b/src/karaxdsl.nim @@ -4,7 +4,7 @@ from strutils import startsWith, toLowerAscii const StmtContext = ["kout", "inc", "echo", "dec", "!"] - SpecialAttrs = ["id", "class", "value", "key"] + SpecialAttrs = ["id", "class", "value", "key", "style"] proc getName(n: NimNode): string = case n.kind diff --git a/src/vdom.nim b/src/vdom.nim index 1dc389e..9d2b90d 100644 --- a/src/vdom.nim +++ b/src/vdom.nim @@ -1,7 +1,7 @@ ## Virtual DOM implementation for Karax. from dom import Event, Node -import shash, macros +import shash, macros, vstyles from strutils import toUpperAscii type @@ -77,6 +77,7 @@ type onkeyuplater ## vdom extension: a key was pressed and some time ## passed (useful for on-the-fly text completions) + macro buildLookupTables(): untyped = var a = newTree(nnkBracket) for i in low(VNodeKind)..high(VNodeKind): @@ -99,6 +100,7 @@ buildLookupTables() type EventHandler* = proc (ev: Event; target: VNode) {.closure.} VKey* = int + VNode* = ref object kind*: VNodeKind key*: VKey @@ -109,6 +111,7 @@ type events*: seq[(EventKind, EventHandler)] hash*: Hash validHash*: bool + style*: VStyle ## the style that should be applied to the virtual node. dom*: Node ## the attached real DOM node. Can be 'nil' if the virtual node ## is not part of the virtual DOM anymore. @@ -143,6 +146,7 @@ proc eq*(a, b: VNode): bool = if a.attrs.len != b.attrs.len: return false for i in 0..