Skip to content

Commit e16b088

Browse files
committed
Initial commit
0 parents  commit e16b088

18 files changed

+24249
-0
lines changed

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where 3rd-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
node_modules

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# ElixirScriptReact
2+
3+
A library for using React with ElixirScript
4+
5+
## Example
6+
7+
```elixir
8+
use ReactUI
9+
10+
def my_section()
11+
section id: "todoapp" do
12+
header id: "header" do
13+
h1 do
14+
"todos"
15+
end
16+
input [
17+
id: "new-todo",
18+
placeholder: "What needs to be done?",
19+
autoFocus: true,
20+
onKeyPress: fn(event, _) -> process_event(event) end
21+
]
22+
end
23+
end
24+
end
25+
26+
ReactDOM.render(my_section(), "my_dom_container_query")
27+
```
28+
29+
`my_dom_container_query` is a string that is passed to `document.querySelector` in the browser
30+
31+
## Installation
32+
33+
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
34+
by adding `elixir_script_react` to your list of dependencies in `mix.exs`:
35+
36+
```elixir
37+
def deps do
38+
[
39+
{:elixir_script_react, "~> 0.1.0"}
40+
]
41+
end
42+
```
43+
44+
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
45+
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
46+
be found at [https://hexdocs.pm/elixir_script_react](https://hexdocs.pm/elixir_script_react).
47+

config/config.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file is responsible for configuring your application
2+
# and its dependencies with the aid of the Mix.Config module.
3+
use Mix.Config
4+
5+
# This configuration is loaded before any dependency and is restricted
6+
# to this project. If another project depends on this project, this
7+
# file won't be loaded nor affect the parent project. For this reason,
8+
# if you want to provide default values for your application for
9+
# 3rd-party users, it should be done in your "mix.exs" file.
10+
11+
# You can configure your application as:
12+
#
13+
# config :elixir_script_react, key: :value
14+
#
15+
# and access this configuration in your application as:
16+
#
17+
# Application.get_env(:elixir_script_react, :key)
18+
#
19+
# You can also configure a 3rd-party app:
20+
#
21+
# config :logger, level: :info
22+
#
23+
24+
# It is also possible to import configuration files, relative to this
25+
# directory. For example, you can emulate configuration per environment
26+
# by uncommenting the line below and defining dev.exs, test.exs and such.
27+
# Configuration from the imported file will override the ones defined
28+
# here (which is why it is important to import them last).
29+
#
30+
# import_config "#{Mix.env}.exs"

lib/react.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule React do
2+
use ElixirScript.FFI
3+
4+
foreign createElement(type, props, children)
5+
end

lib/react_dom.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule ReactDOM do
2+
use ElixirScript.FFI
3+
4+
foreign render(element, container)
5+
end

lib/reactui.ex

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
defmodule ReactUI do
2+
3+
defmacro __using__(_) do
4+
quote do
5+
import ReactUI
6+
end
7+
end
8+
9+
@external_resource tags_path = Path.join([__DIR__, "tags.txt"])
10+
11+
@tags (for line <- File.stream!(tags_path, [], :line) do
12+
line |> String.strip |> String.to_atom
13+
end)
14+
15+
for tag <- @tags do
16+
@doc """
17+
Defines a macro for the html element, #{tag}
18+
"""
19+
defmacro unquote(tag)(attrs, do: inner) do
20+
tag = Atom.to_string(unquote(tag))
21+
{ inner, attributes } = do_tag(inner, attrs)
22+
23+
quote do
24+
React.createElement(unquote(tag), unquote(attributes), unquote(inner))
25+
end
26+
end
27+
28+
defmacro unquote(tag)(attrs \\ []) do
29+
tag = Atom.to_string(unquote(tag))
30+
31+
{ inner, attributes } = Dict.pop(attrs, :do)
32+
{ inner, attributes } = do_tag(inner, attributes)
33+
34+
quote do
35+
React.createElement(unquote(tag), unquote(attributes), unquote(inner))
36+
end
37+
end
38+
end
39+
40+
defp do_tag(inner, attributes) do
41+
inner = case inner do
42+
{:__block__, _, params} ->
43+
params
44+
nil ->
45+
[]
46+
x ->
47+
[x]
48+
end
49+
50+
attributes = config_to_map(attributes)
51+
52+
{inner, attributes}
53+
end
54+
55+
defp config_to_map(config) do
56+
config = Enum.map(config, fn({key, value}) ->
57+
if is_atom(key) do
58+
{Atom.to_string(key), value}
59+
else
60+
{key, value}
61+
end
62+
end)
63+
64+
{:%{}, [], config}
65+
end
66+
67+
end

lib/tags.txt

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
a
2+
abbr
3+
address
4+
area
5+
article
6+
aside
7+
audio
8+
b
9+
base
10+
bdi
11+
bdo
12+
blockquote
13+
body
14+
br
15+
button
16+
canvas
17+
caption
18+
cite
19+
code
20+
col
21+
colgroup
22+
command
23+
datalist
24+
dd
25+
del
26+
details
27+
dfn
28+
dialog
29+
div
30+
dl
31+
dt
32+
em
33+
embed
34+
fieldset
35+
figcaption
36+
figure
37+
footer
38+
form
39+
h1
40+
h2
41+
h3
42+
h4
43+
head
44+
header
45+
hgroup
46+
hr
47+
html
48+
i
49+
iframe
50+
img
51+
input
52+
ins
53+
kbd
54+
keygen
55+
label
56+
legend
57+
li
58+
link
59+
main
60+
map
61+
mark
62+
menu
63+
menuitem
64+
meta
65+
meter
66+
nav
67+
noscript
68+
object
69+
ol
70+
optgroup
71+
option
72+
output
73+
p
74+
param
75+
pre
76+
progress
77+
q
78+
rp
79+
rt
80+
ruby
81+
s
82+
samp
83+
script
84+
section
85+
select
86+
small
87+
source
88+
span
89+
strong
90+
style
91+
sub
92+
summary
93+
sup
94+
table
95+
tbody
96+
td
97+
textarea
98+
tfoot
99+
th
100+
thead
101+
time
102+
title
103+
tr
104+
track
105+
u
106+
ul
107+
var
108+
video
109+
wbr

mix.exs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule ElixirScriptReact.Mixfile do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :elixir_script_react,
7+
version: "1.0.0-react.15.6.1",
8+
elixir: "~> 1.5",
9+
start_permanent: Mix.env == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
def application do
15+
[
16+
extra_applications: [:logger]
17+
]
18+
end
19+
20+
defp deps do
21+
[
22+
{:elixir_script, git: "[email protected]:elixirscript/elixirscript.git", ref: "72b8adea911569667901ed9d38a0a52ddaa8fc0f"},
23+
]
24+
end
25+
end

mix.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%{"elixir_script": {:git, "[email protected]:elixirscript/elixirscript.git", "72b8adea911569667901ed9d38a0a52ddaa8fc0f", [ref: "72b8adea911569667901ed9d38a0a52ddaa8fc0f"]},
2+
"estree": {:hex, :estree, "2.6.0", "86a301b0c355fa55c19e7ef9dceb1b1e983c6df526a2b7846818a38c258fc3fb", [], [], "hexpm"},
3+
"fs": {:hex, :fs, "3.4.0", "6d18575c250b415b3cad559e6f97a4c822516c7bc2c10bfbb2493a8f230f5132", [], [], "hexpm"}}

0 commit comments

Comments
 (0)