• This repository has been archived on 26/Sep/2023
  • Stars
    star
    431
  • Rank 100,866 (Top 2 %)
  • Language
    HTML
  • License
    Eclipse Public Li...
  • Created over 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Idiomatic ClojureScript interface to modern React.js

Idiomatic ClojureScript interface to modern React.js

Discuss at #uix on Clojurians Slack. Bug reports, feature requests and PRs are welcome.

UIxv2 is available at https://github.com/pitch-io/uix

Try it in online REPL

Docs and Guides

API Documentation

If you like what I do, consider supporting my work via donation

CircleCI

Clojars updates are pushed occasionally, depend via Git deps to get the most recent updates.

Clojars Project Clojars Project Clojars Project

{:deps {uix.core {:git/url "https://github.com/roman01la/uix.git"
                  :deps/root "core"
                  :sha "{{replace with commit hash}}"}
        uix.dom {:git/url "https://github.com/roman01la/uix.git"
                 :deps/root "dom"
                 :sha "{{replace with commit hash}}"}
        uix.rn {:git/url "https://github.com/roman01la/uix.git"
                :deps/root "rn"
                :sha "{{replace with commit hash}}"}}}
(require '[uix.core.alpha :as uix])
(require '[uix.dom.alpha :as uix.dom])

(defn button [{:keys [on-click]} text]
  [:button.btn {:on-click on-click}
    text])

(defn app []
  (let [state* (uix/state 0)]
    [:<>
      [button {:on-click #(swap! state* dec)} "-"]
      [:span @state*]
      [button {:on-click #(swap! state* inc)} "+"]]))

(uix.dom/render [app] js/root)

Recipes

Features

Hiccup syntax extension

  • [:div#id.class] or [:#id.class]
  • [:> js/Component attrs & children] - interop with JS components
  • [:<> attrs & children] - React.Fragment
  • [:# {:fallback element} & children] - React.Suspense

Hooks

React Hooks in idiomatic Clojure style

;; state hook
;; (mutable ref type, re-renders component when mutated)
(let [state (uix/state 0)]
  (swap! state inc)
  @state) ; 1

;; ref hook
;; (mutable ref type, doesn't cause re-renders)
(let [ref (uix/ref 0)]
  (swap! ref inc)
  @ref) ; 1

;; effect hook
(uix/effect!
  (fn []
    (prn "after update")
    #(prn "before unmount"))
  [deps])

;; convenience macro for uix.core/effect!
(uix/with-effect [deps]
  (prn "after update")
  #(prn "before unmount"))

;; more in uix.core.alpha ns

Attributes syntax extension

Injects provided function into attributes transformation stage. Could be used for various side effects, such as processing styles with CSS-in-JS libraries (see uix.recipes.dynamic-styles).

(uix.core.alpha/add-transform-fn
  (fn [attrs]
    (my-transform-attrs attrs)))

Hiccup pre-compilation (advanced)

NOTE: UIx interpreter is already super fast (3x faster than Reagent and only 2x slower than vanilla React). Use pre-compilation ONLY if you are hitting performance problems.

Compiles Hiccup into inlined React elements at compile-time and hoists constant elements so they can be shared across components in different namespaces (for reference see @babel/plugin-transform-react-inline-elements and @babel/plugin-transform-react-constant-elements). Hoisting is enabled with :optimize-constants compiler option, which is automatically enabled for :optimizations :advanced.

(uix/html
  [:h1 "Title"])

;; emits this
{
  $$typeof: Symbol.for("react.element"),
  key: null,
  ref: null,
  props: { children: "Title" },
  _owner: null
}

Lazy loading components

Loading React components on-demand as Closure modules. See code splitting guide and how lazy loading is used in React with Suspense: guide.

(uix.core.lazy-loader/require-lazy
  '[uix.components :refer [ui-list]])

[:# {:fallback "Loading..."}
  (when show?
    [ui-list])]

Server-side rendering

UIx can be used for SSR or usual templating in both JVM and JavaScript runtimes

Server-side rendering in JVM

See an example in uix.recipes.server-rendering

(uix.dom/render-to-string element) ;; see https://reactjs.org/docs/react-dom-server.html#rendertostring
(uix.dom/render-to-static-markup element) ;; see https://reactjs.org/docs/react-dom-server.html#rendertostaticmarkup

;; Streaming HTML
(uix.dom/render-to-stream element {:on-chunk f}) ;; see https://reactjs.org/docs/react-dom-server.html#rendertonodestream
(uix.dom/render-to-static-stream element {:on-chunk f}) ;; see https://reactjs.org/docs/react-dom-server.html#rendertostaticnodestream

Server-side rendering in JS

SSR works in JavaScript environment via React's serializer using same API.

  1. Add ReactDOMServer into your dependencies (as cljsjs/react-dom-server or any other way)
  2. Run (uix.dom/render-to-string element)

Benchmarks

  • Hiccup interpretation clojure -A:dev:benchmark:bench-front
  • SSR on JVM clojure -A:dev:benchmark:bench-ssr

Hiccup interpretation

react x 23866 ops/s, elapsed 419ms
uix-interpret x 11848 ops/s, elapsed 844ms
reagent-interpret x 4031 ops/s, elapsed 2481ms

SSR on JVM

lib test 1 test 2 test 3
rum 107.8 µs 3.6 ms 7.7 ms
uix 120.8 µs 3.8 ms 8.1 ms
uix streaming 115.7 µs 3.4 ms 7.6 ms
hiccup 205.7 µs 6.5 ms 16.6 ms

TodoMVC bundle size

lib size gzip
rum 254KB 70KB
reagent 269KB 74KB
uix 234KB 65KB

Figwheel

When developing with Figwheel it is recommended to mark root render function with ^:after-load meta, so Figwheel can update UI tree once the code was re-evaluated.

(ns ^:figwheel-hooks my.ns)

(defn ^:after-load render []
  (uix.dom/render [app] js/root))

React DevTools

When inspecting UI tree in React DevTools, filter out memo components to get cleaner view of components tree.

Testing

scripts/test

Note: to ensure you're using the right Node.js version, you can use nvm and run nvm use once in the directory. Otherwise the Node.js version you use is in the .nvmrc file. See nvm repo for more documentation.

Who’s using UIx

More Repositories

1

html-to-react-components

Converts HTML pages into React components
JavaScript
2,131
star
2

webpack-closure-compiler

[DEPRECATED] Google Closure Compiler plugin for Webpack
JavaScript
464
star
3

closure-compiler-handbook

How to use Google's Closure Compiler
441
star
4

threegn

Procedural 3D graphics editor for the web
JavaScript
309
star
5

react-native-babel

Configuration to build React Native apps with ES6 using webpack and Babel
Objective-C
234
star
6

clj-wasm

Clojure-flavored WASM's text format
Clojure
159
star
7

react-flux-workshop

Материалы к воркшопу «React.js и архитектура Flux»
JavaScript
128
star
8

react-horizon

React Horizon makes it easier to use your React application with horizon.io realtime backend
JavaScript
119
star
9

JSONFormData

HTML JSON form submission polyfill
JavaScript
104
star
10

javascript-to-clojurescript

JavaScript to ClojureScript translator
JavaScript
102
star
11

react-webrtc

WebRTC React mixins for real-time communication in React components
JavaScript
85
star
12

slack-traductor

Slack bot to translate chat messages of any language into specified language
JavaScript
85
star
13

babel-plugin-stateful-functional-react-components

Stateful functional React components without runtime overhead
JavaScript
83
star
14

proton-native-cljs

Clojure
79
star
15

prum

ClojureScript's Rum with Preact.js instead of React
HTML
64
star
16

anybar-webpack

Webpack build status plugin for menubar status indicator applications
JavaScript
62
star
17

js-memory-usage

Data Structures Memory Usage in JavaScript
JavaScript
59
star
18

minimax

Minimalist 3D game engine in Clojure
Clojure
59
star
19

scrum-ssr-example

HN clone app with server-side rendering built with Scrum
Clojure
57
star
20

solid-cljs

ClojureScript bindings to SolidJS
Clojure
57
star
21

cljs-rum-realworld-example-app

ClojureScript + Rum codebase containing real world examples
Clojure
53
star
22

github-issues

Sample React application built with Flux
JavaScript
48
star
23

threejs-cljs-playground

three.js playground in ClojureScript
CSS
44
star
24

figcup

Converts Figma designs into Reagent/Hiccup components to render in the browser
Clojure
40
star
25

cljs-async-await

Experimental ClojureScript's compiler extension that enables JavaScript's async/await
Clojure
36
star
26

clojurescript-workshop

Repository of related materials to the workshop
Clojure
36
star
27

recursion-exercises

Recursion exercises in JavaScript
JavaScript
34
star
28

cljs-react-devtools

React DevTools for ClojureScript wrappers
Clojure
30
star
29

react-horizon-example

Example React application built with React Horizon connector to horizon.io realtime backend
JavaScript
29
star
30

amsterdamjs-clojurescript-workshop

Educational materials for ClojureScript workshop @ AmsterdamJS '18
Clojure
28
star
31

hooks

React Hooks for ClojureScript
Clojure
22
star
32

grunt-load-perf

Grunt task for generating visual report to measure web applications loading and rendering performance.
JavaScript
21
star
33

impact-node

Node.js CLI for game development with Impact HTML5 game engine
JavaScript
21
star
34

react-dom-visualizer

Visualize components structure in your React application as a tree chart
JavaScript
20
star
35

babel-plugin-react-hyperscript

HyperScript syntax for React components without runtime overhead
JavaScript
20
star
36

cljs-source-mapped-stack-traces

Displays exceptions with stack frames mapped to ClojureScript source
JavaScript
19
star
37

path-following

JavaScript implementation of Craig Reynolds’s path-following algorithm
JavaScript
18
star
38

adapton

A Minimal Implementation of Incremental Computation in Clojure and ClojureScript
Clojure
17
star
39

imtbl

Immutable operations for JavaScript data structures
JavaScript
17
star
40

web-components-polyfills

A set of lightweight polyfills to enable Web Components support in major browsers, including IE9+
JavaScript
16
star
41

cljs-worklet

Run ClojureScript functions on a worklet thread in React Native
Clojure
16
star
42

voice-to-chat

A small CLI and menubar app that transcribes voice recording and rewrites it in a style of a written text
JavaScript
14
star
43

0ui

A set of utilities for building UIs with React
JavaScript
14
star
44

leap-motion-controlled-hand

Leap Motion controlled 3D hand made with Three.js
JavaScript
14
star
45

vscode-structed

Structured editing in JavaScript extension for VS Code
JavaScript
13
star
46

part-js

Persistent Adaptive Radix Tree (PART) for JavaScript
TypeScript
13
star
47

zfort-js-course

JavaScript course for Zfort company.
JavaScript
13
star
48

postcss-camel-case

PostCSS plugin to convert CSS selector names to camelCase
JavaScript
13
star
49

clojurescript-studio

Online coding sandbox tailored for web development
Clojure
13
star
50

react-a11y-video

Accessible HTML5 Video player React component
JavaScript
10
star
51

node-stm

Multiversion Concurrency Control (MVCC) based Software Transactional Memory (STM)
JavaScript
10
star
52

atom-cljs-doc

ClojureScript core library documentation viewer for Atom
JavaScript
9
star
53

kottans-intro-into-fp

Introduction into functional programming
JavaScript
8
star
54

virtual.list

Virtual list component for Prum
Clojure
8
star
55

grunt-mocha-casperjs

Grunt wrapper for mocha-casperjs
JavaScript
8
star
56

websockets-device-controller

WebGL demo powered by WebSockets and device accelerometer data to control things on the web.
JavaScript
7
star
57

generator-website

A website generator for Yeoman
JavaScript
7
star
58

advent-of-code-2018

Solutions to puzzles from Advent of Code 2018
Clojure
6
star
59

shapy

Web-based UI design tool
Clojure
6
star
60

roman01la-devtools

Custom UI theme for Chrome's DevTools
HTML
5
star
61

structed

Structured editing for JavaScript
JavaScript
5
star
62

jsbin-cljs

ClojureScript for JS Bin
Clojure
5
star
63

zprint-atom

Atom plugin for zprint-clj
JavaScript
5
star
64

rc-cljs-workshop

ClojureScript Workshop @ ReactiveConf '17
5
star
65

uix.css

CSS-in-CLJS
4
star
66

react-images-preloader

HOC to preload images before rendering
JavaScript
4
star
67

babel-plugin-inline

Array map/filter/reduce inliner plugin
JavaScript
4
star
68

webpack-sri

Subresource Integrity hash generator plugin for Webpack
JavaScript
4
star
69

clojure-hacking-day

Clojure
4
star
70

pwl-kyiv

Papers We Love @ Kyiv
HTML
3
star
71

re-frame-chat

Clojure
3
star
72

uix-cookbook

UIx Cookbook is a set of recipes that will assist you in creating web apps with UIx
2
star
73

rc18-clojure-workshop

Clojure
2
star
74

happy-paw

HappyPaw + Tinder
Clojure
2
star
75

sqlite-vector-search

A demo of vector search with HNSW and SQLite
JavaScript
2
star
76

create-uix-app

Scaffolding tool for UIx
JavaScript
1
star
77

rum-code-splitting

Clojure
1
star
78

roman01la

1
star
79

sub-deep

Transcribe and translate audio with AI
JavaScript
1
star
80

canvas-text-layout

JavaScript
1
star
81

slack-yue

Clojure
1
star
82

subform-css-layout-worklet

Subform’s Layout engine via CSS Layout API
JavaScript
1
star
83

libx.core

Enhanced Clojure's standard library
Clojure
1
star