-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #477 from reagent-project/feature/functional-compo…
…nents Test creating functional components
- Loading branch information
Showing
26 changed files
with
2,243 additions
and
1,597 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
coverage: | ||
 status: | ||
 patch: | ||
 default: | ||
 enabled: no |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Reagent Compiler | ||
|
||
Reagent Compiler object is a new way to configure how Reagent | ||
turns the Hiccup-style markup into React components and elements. | ||
|
||
As a first step, this can be used to turn on option to create | ||
functional components when a function is referred in a Hiccup vector: | ||
`[component-fn parameters]`. | ||
|
||
[./ReactFeatures.md#hooks](React more about Hooks) | ||
|
||
```cljs | ||
(def functional-compiler (r/create-compiler {:functional-components? true})) | ||
|
||
;; Using the option | ||
(r/render [main] div functional-compiler) | ||
(r/as-element [main] functional-compiler) | ||
;; Setting compiler as the default | ||
(r/set-default-compiler! functional-compiler) | ||
``` | ||
|
||
## Reasoning | ||
|
||
Now that this mechanism to control how Reagent compiles Hiccup-style markup | ||
to React calls is in place, it will be probably used later to control | ||
some other things also: | ||
|
||
From [Clojurist Together announcenment](https://www.clojuriststogether.org/news/q1-2020-funding-announcement/): | ||
|
||
> As this [hooks] affects how Reagent turns Hiccup to React elements and components, I | ||
> have some ideas on allowing users configure the Reagent Hiccup compiler, | ||
> similar to what [Hicada](https://github.com/rauhs/hicada) does. This would also allow introducing optional | ||
> features which would break existing Reagent code, by making users opt-in to | ||
> these. One case would be to make React component interop simpler. | ||
Some ideas: | ||
|
||
- Providing options to control how component parameters are converted to JS | ||
objects (or even disable automatic conversion) | ||
- Implement support for custom tags (if you can provide your own function | ||
to create element from a keyword, this will be easy) | ||
|
||
Open questions: | ||
|
||
- Will this cause problems for libraries? Do the libraries have to start | ||
calling `as-element` with their own Compiler to ensure compatibility. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Reagent example app | ||
|
||
Run "`lein figwheel`" in a terminal to compile the app, and then open http://localhost:3449 | ||
|
||
Any changes to ClojureScript source files (in `src`) will be reflected in the running page immediately (while "`lein figwheel`" is running). | ||
|
||
Run "`lein clean; lein with-profile prod compile`" to compile an optimized version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
(defproject functional-components-and-hooks "0.6.0" | ||
:dependencies [[org.clojure/clojure "1.10.1"] | ||
[org.clojure/clojurescript "1.10.597"] | ||
[reagent "1.0.0-SNAPSHOT"] | ||
[figwheel "0.5.19"]] | ||
|
||
:plugins [[lein-cljsbuild "1.1.7"] | ||
[lein-figwheel "0.5.19"]] | ||
|
||
:resource-paths ["resources" "target"] | ||
:clean-targets ^{:protect false} [:target-path] | ||
|
||
:profiles {:dev {:cljsbuild | ||
{:builds {:client | ||
{:figwheel {:on-jsload "example.core/run"} | ||
:compiler {:main "example.core" | ||
:optimizations :none}}}}} | ||
|
||
:prod {:cljsbuild | ||
{:builds {:client | ||
{:compiler {:optimizations :advanced | ||
:elide-asserts true | ||
:pretty-print false}}}}}} | ||
|
||
:figwheel {:repl false | ||
:http-server-root "public"} | ||
|
||
:cljsbuild {:builds {:client | ||
{:source-paths ["src"] | ||
:compiler {:output-dir "target/public/client" | ||
:asset-path "client" | ||
:output-to "target/public/client.js"}}}}) |
22 changes: 22 additions & 0 deletions
22
examples/functional-components-and-hooks/resources/public/example.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
div, h1, input { | ||
font-family: HelveticaNeue, Helvetica; | ||
color: #777; | ||
} | ||
|
||
.example-clock { | ||
font-size: 128px; | ||
line-height: 1.2em; | ||
font-family: HelveticaNeue-UltraLight, Helvetica; | ||
} | ||
|
||
@media (max-width: 768px) { | ||
.example-clock { | ||
font-size: 64px; | ||
} | ||
} | ||
|
||
.color-input, .color-input input { | ||
font-size: 24px; | ||
line-height: 1.5em; | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/functional-components-and-hooks/resources/public/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Example</title> | ||
<link rel="stylesheet" href="example.css"> | ||
</head> | ||
<body> | ||
<div id="app"> | ||
<h1>Reagent example app – see README.md</h1> | ||
</div> | ||
<script src="client.js"></script> | ||
</body> | ||
</html> |
43 changes: 43 additions & 0 deletions
43
examples/functional-components-and-hooks/src/example/core.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
(ns example.core | ||
(:require [reagent.core :as r] | ||
[reagent.dom :as rdom] | ||
[clojure.string :as str] | ||
["react" :as react])) | ||
|
||
;; Same as simpleexample, but uses Hooks instead of Ratoms | ||
|
||
(defn greeting [message] | ||
[:h1 message]) | ||
|
||
(defn clock [time-color] | ||
(let [[timer update-time] (react/useState (js/Date.)) | ||
time-str (-> timer .toTimeString (str/split " ") first)] | ||
(react/useEffect | ||
(fn [] | ||
(let [i (js/setInterval #(update-time (js/Date.)) 1000)] | ||
(fn [] | ||
(js/clearInterval i))))) | ||
[:div.example-clock | ||
{:style {:color time-color}} | ||
time-str])) | ||
|
||
(defn color-input [time-color update-time-color] | ||
[:div.color-input | ||
"Time color: " | ||
[:input {:type "text" | ||
:value time-color | ||
:on-change #(update-time-color (-> % .-target .-value))}]]) | ||
|
||
(defn simple-example [] | ||
(let [[time-color update-time-color] (react/useState "#f34")] | ||
[:div | ||
[greeting "Hello world, it is now"] | ||
[clock time-color] | ||
[color-input time-color update-time-color]])) | ||
|
||
(def functional-compiler (r/create-compiler {:functional-components? true})) | ||
|
||
(defn run [] | ||
(rdom/render [simple-example] (js/document.getElementById "app") functional-compiler)) | ||
|
||
(run) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.