Files
rose-ash/sx/sxc/init-client.sx.txt
giles 623f947b52
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 21s
Fix duplicate sx-cssx-live style tags
Cache the style element reference in _cssx-style-el so flush-cssx-to-dom
never creates more than one. Previous code called dom-query on every
flush, which could miss the element during rapid successive calls,
creating duplicates.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-14 20:08:36 +00:00

44 lines
2.0 KiB
Plaintext

;; ---------------------------------------------------------------------------
;; SX app boot — styles, behaviors, and post-render hooks
;;
;; Replaces inline_css and init_sx from Python app config.
;; Called as a data-init script on every page.
;; ---------------------------------------------------------------------------
;; Framework styles — request indicators + link jiggle
(collect! "cssx" ".sx-indicator{display:none}")
(collect! "cssx" ".sx-request .sx-indicator{display:inline-flex}")
(collect! "cssx" "@keyframes sxJiggle{0%,100%{transform:translateX(0)}25%{transform:translateX(-.5px)}75%{transform:translateX(.5px)}}")
(collect! "cssx" "a.sx-request{animation:sxJiggle .3s ease-in-out infinite}")
;; CSSX flush hook — inject collected CSS rules into a <style> tag.
;; The spec calls (run-post-render-hooks) after hydration/swap/mount.
;; This is the application's CSS injection strategy.
(console-log "init-client: registering cssx flush hook, type:" (type-of (fn () nil)))
(register-post-render-hook
(fn ()
(console-log "cssx flush: running, rules:" (len (collected "cssx")))
(let ((rules (collected "cssx")))
(when (not (empty? rules))
(let ((style (or (dom-query "[data-cssx]")
(let ((s (dom-create-element "style" nil)))
(dom-set-attr s "data-cssx" "")
(dom-append-to-head s)
s))))
(dom-set-prop style "textContent"
(str (or (dom-get-prop style "textContent") "")
(join "" rules))))
(clear-collected! "cssx")))))
;; Nav link aria-selected update on client-side routing
(dom-listen (dom-body) "sx:clientRoute"
(fn (e)
(let ((p (get (event-detail e) "pathname")))
(when p
(for-each
(fn (a) (dom-set-attr a "aria-selected" "false"))
(dom-query-all "nav a[aria-selected]"))
(for-each
(fn (a) (dom-set-attr a "aria-selected" "true"))
(dom-query-all (str "nav a[href=\"" p "\"]")))))))