|
1 | 1 | # Sodium |
2 | 2 |
|
3 | | -A wrapper around [soda-ash](https://github.com/gadfly361/soda-ash) |
4 | | -and [semantic-ui-react](https://github.com/Semantic-Org/Semantic-UI-React) that is |
5 | | -friendlier to re-frame. And the name? Simple, Sodium is highly reactive, and starts |
6 | | -with an "S", like Soda-ash and Semantic-UI. |
| 3 | +## Introduction |
7 | 4 |
|
8 | | -Sodium is still an early work in progress. I am using it to help me with other projects. |
9 | | -It will only grow as it helps me (or other contributers) accomplish what they need. If |
10 | | -you need features now, PRs are welcome. |
| 5 | +Sodium is a wrapper around [soda-ash](https://github.com/gadfly361/soda-ash) which, in |
| 6 | +turn is a ClojureScript wrapper |
| 7 | +around [semantic-ui-react](https://github.com/Semantic-Org/Semantic-UI-React). Sodium |
| 8 | +adds two sets of features into the mix, both aimed at making it easier to include |
| 9 | +Semantic UI in re-frame projects: |
11 | 10 |
|
12 | | -Sodium offers the following: |
| 11 | +1. Utility functions to get values in and out of components. |
| 12 | +2. Validity-checking aids to help avoid typos. |
13 | 13 |
|
14 | | -## Cleaner inter-op between Semantic-UI and [re-frame](https://github.com/Day8/re-frame) |
| 14 | +### Why "Sodium"? |
15 | 15 |
|
16 | | -`sodium.core/>event` and `sodium.core/>atom` - Create `:on-*` handlers to pass a value |
17 | | -to a re-frame event or react atom. |
| 16 | +The element sodium is highly reactive. It also starts with an "S", like Soda-ash and |
| 17 | +Semantic-UI. In addition, the chemical symbol for sodium is "NA", nicely similar to the |
| 18 | +"SA" abbreviation used for the Soda-ash namespace. And, hey, I like puns. |
18 | 19 |
|
19 | | -`sodium.core/<atom` - Get a value from a react atom. |
| 20 | +### State of this project |
20 | 21 |
|
21 | | -`sodium.core/defcontrol` and `sodium.core/def-simple-control` Define a control; |
22 | | -typically a thin wrapper around the Soda-ash control, to give parameter checking, |
23 | | -etc. (see below). Soon, Sodium will contain definitions wrapping all the Soda-ash |
24 | | -(Semantic-UI) controls, but we are not there yet. |
| 22 | +Sodium is still an early work in progress. I am using it to help me with other projects |
| 23 | +and have only addded functionality that I need and can test. Sodium will only grow as |
| 24 | +it helps me (or other contributers) accomplish what they need. If you need features now, |
| 25 | +PRs are welcome. |
| 26 | + |
| 27 | +### Background |
| 28 | + |
| 29 | +I have used [re-com](https://github.com/Day8/re-com) for a while in my |
| 30 | +[re-frame](https://github.com/Day8/re-frame) projects. Re-com is a wonderful, reliable, |
| 31 | +easy to use, and opinionated library of UI components. In many ways, it was |
| 32 | +perfect. But, as the authors warn, it is designed for desktop applications. When I tried |
| 33 | +using it for mobile and portable pages, I kept running into little annoyances. |
| 34 | + |
| 35 | +I recently discovered Semantic UI and its Soda-ash wrapper. I switched to it, and my |
| 36 | +pages became much more usable on mobile devices. But, I miss the slick features and |
| 37 | +re-frame integration of re-com. Sodium is an attempt to bridge this gap. |
| 38 | + |
| 39 | + |
| 40 | +## Setup |
25 | 41 |
|
| 42 | +[](https://clojars.org/com.degel/sodium) |
| 43 | + |
| 44 | +Sodium is a wrapper around Soda-ash, Semantic UI React, and Semantic UI. Setup requires |
| 45 | +a couple of steps: |
26 | 46 |
|
27 | | -## Clojure-friendly naming conventions |
| 47 | +- Your project.clj should include a dependency on Sodium: `[com.degel/sodium "0.1.0"]` |
| 48 | +- It will probably (see below) also need a dependency on Soda-ash (currently `[soda-ash |
| 49 | + "0.3.0"]`, but see the [Soda-ash README](https://github.com/gadfly361/soda-ash) for |
| 50 | + any recent changes). |
| 51 | +- You will also need to include the Semantic UI stylesheet in your _index.html_: |
| 52 | + `<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/semantic.min.css">` |
| 53 | +- Finally, you will need to require Sodium, and maybe also Soda-ash, in your namespace: |
| 54 | + - `[sodium.core :as na]` - Most of Sodium's functionality |
| 55 | + - `[sodium.utils :refer [<sub >evt]]` - Useful utilities |
| 56 | + - `[sodium.chars :as chars]` - Minor Unicocde support |
| 57 | + - `[soda-ash.core :as sa]` - Soda-ash functions |
28 | 58 |
|
29 | | -Controls and parameters in kebab-case (`:on-change`) rather than JavaScript-friendly |
30 | | -camel-case (`onChange`). |
31 | 59 |
|
32 | | -## Parameter type-checking |
| 60 | +If you want to contribute to this project, you will want to test your changes. You can |
| 61 | +extend the (very minimal) tests inside Sodium, but you will probably need to really test |
| 62 | +by using Sodium in a ClojureScript project. The easiest way to do this, while you are |
| 63 | +changing Sodium, is by using Leiningen's |
| 64 | +[checkout directory](https://github.com/technomancy/leiningen/blob/master/doc/TUTORIAL.md#checkout-dependencies) |
| 65 | +feature by sym-linking directly to your copy of Sodium. |
33 | 66 |
|
34 | | -For now, just starting to check if parameters are valid. This is a bit of a mixed |
35 | | -blessing, because I've not yet found a definitive list of all the valid parameters. So, |
36 | | -this may be overly restrictive. |
| 67 | +## Using Sodium |
37 | 68 |
|
38 | | -I will, soon, add an option to partially disable checking. But, NYI. For now, you can |
39 | | -call the Soda-ash function directly. `>event` and friends will work just fine. |
| 69 | +Sodium is a library, with a grab-bag of functions to enhance the usability of Semantic |
| 70 | +UI from your re-frame projects. You can use it in several ways: |
40 | 71 |
|
41 | | -## A few helpful utility functions |
| 72 | +### Data-in and -out |
| 73 | + |
| 74 | +Sodium offers the following: |
42 | 75 |
|
43 | | -### In `sodium.utils` |
| 76 | +- `na/>event` and `na/>atom` - Create `:on-*` handlers to pass a value |
| 77 | +to a re-frame event or react atom. |
| 78 | +- `sodium.core/<atom` - Get a value from a react atom. |
| 79 | + |
| 80 | +#### Typical usage |
| 81 | + |
| 82 | +```` |
| 83 | +(defn login-panel [] |
| 84 | + (let [email (reagent/atom "") |
| 85 | + password (reagent/atom "")] |
| 86 | + (fn [] |
| 87 | + [na/form {} |
| 88 | + [na/form-input {:label "Email" |
| 89 | + :type "email" |
| 90 | + :on-change (na/>atom email)}] |
| 91 | + [na/form-input {:label "Password" |
| 92 | + :type "password" |
| 93 | + :on-change (na/>atom password)}] |
| 94 | + [na/form-button {:content "Login" |
| 95 | + :on-click (na/>event [:login @email @password])}]]))) |
| 96 | +```` |
| 97 | + |
| 98 | +These functions work equally with with Sodium components and bare Soda-ash ones. I |
| 99 | +recommend using Sodium components where available. But, see below, you will still often |
| 100 | +need to use Soda-ash components. |
| 101 | + |
| 102 | +### Helper functions |
| 103 | + |
| 104 | +These are some some of the functions that I've needed often in re-frame projects. I |
| 105 | +expect this will grow rapidly with time. PRs are welcome here, though I'm likely to be |
| 106 | +opinionated about what I accept. |
| 107 | + |
| 108 | +#### In `sodium.utils` |
44 | 109 | - `validate` - Wrapper for Clojure specs checking in pre-conditions. |
45 | 110 | - "Camelize" functions - Convert Clojure-style names to JavaScript style |
46 | 111 |
|
47 | | -### In `sodium.re-utils` |
| 112 | +#### In `sodium.re-utils` |
48 | 113 | - `<sub` and `>evt` - Re-frame wrappers, taken from <https://lambdaisland.com/blog/11-02-2017-re-frame-form-1-subscriptions> |
49 | 114 |
|
50 | | -### In `sodium.chars` |
| 115 | +#### In `sodium.chars` |
51 | 116 |
|
52 | 117 | Definitions for a few common Unicode characters. |
53 | 118 |
|
54 | 119 |
|
55 | | -## Useful links (for now) |
| 120 | +### Semantic enhancements |
56 | 121 |
|
57 | | -- https://react.semantic-ui.com/introduction |
58 | | -- https://github.com/Semantic-Org/Semantic-UI-React |
| 122 | +HTML controls offer a lot of freedom, and sometimes too many choices. Our front-end code |
| 123 | +can be much more readable if we define controls with relatively constrained and |
| 124 | +consistent behavior. But, there is an art to doing this right; constrain a control too |
| 125 | +much, and it is perfect for one project, but too limited to be useful in other |
| 126 | +projects. Semantic UI helps a lot, but still offers too many choices for a consistent, |
| 127 | +simple, web page. |
59 | 128 |
|
| 129 | +I am trying, slowly, to create a set of components that meet my needs yet are general |
| 130 | +purpose. Most of them are still in my other projects, mostly not public, that use |
| 131 | +Sodium. But, I intend to migrate them into Sodium as I gain comfort. |
60 | 132 |
|
| 133 | +One component is already here: |
61 | 134 |
|
62 | | -## Usage |
| 135 | +- `form-button` - HTML forms are, by default, very tied to the old-school HTML notions |
| 136 | + of page submission. This does not play well with the re-frame philosophy that a form |
| 137 | + handler should simply trigger a re-frame event. When I naively use the Soda-ash |
| 138 | + sa/FormButton component at first, I was hit with unexpected extra connections to my |
| 139 | + server, trying to submit the form. The fix for this, once I realized the problem, was |
| 140 | + simple: the component needs to explicitly speicify a type of "button." The |
| 141 | + `na/form-button` component handles this automatically. |
63 | 142 |
|
64 | | -[](https://clojars.org/com.degel/sodium) |
| 143 | +### Type-checked wrappers |
65 | 144 |
|
66 | | -You will need to include a dependency `[com.degel/sodium "0.1.0-SNAPSHOT"]` and `require` |
67 | | -- `[sodium.core :as na]` - Most of Sodium's functionality |
68 | | -- `[sodium.chars :as chars]` - Minor Unicocde support |
69 | | -- `[sodium.utils :refer [<sub >evt]]` - Useful utilities |
| 145 | +This is the most experimental part of Sodium and, sadly, still mostly incomplete. |
70 | 146 |
|
| 147 | +So far, it includes the following components, each a very thin wrapper around the |
| 148 | +corresponding Soda-ash / Semantic UI one: |
71 | 149 |
|
72 | | -### Running |
| 150 | +- `na/checkbox`: `sa/Checkbox` |
| 151 | +- `na/container`: `sa/Container` |
| 152 | +- `na/dropdown`: `sa/Dropdown` |
| 153 | +- `na/form`: `sa/Form` |
| 154 | +- `na/form-input`: `sa/FormInput` |
| 155 | +- `na/form-group`: `sa/FormGroup` |
| 156 | +- `na/grid`: `sa/Grid` |
| 157 | +- `na/header`: `sa/Header` |
| 158 | +- `na/input`: `sa/Input` |
| 159 | +- `na/menu`: `sa/Menu` |
| 160 | +- `na/menu-item`: `sa/MenuItem` |
| 161 | +- `na/rail`: `sa/Rail` |
| 162 | +- `na/text-area`: `sa/TextArea` |
73 | 163 |
|
74 | | -At this stage, I expect that most users of Sodium will be myself or contributers. If |
75 | | -you will be changing Sodium, it is easier to run this library from within the project |
76 | | -using it. |
77 | 164 |
|
78 | | -Sym-link to Sodium from the checkouts directory of your project. (See the |
79 | | -[Leiningen notes](https://github.com/technomancy/leiningen/blob/master/doc/TUTORIAL.md#checkout-dependencies) |
80 | | -for a discussion of this feature). If you are me, a working example is at |
81 | | -~/Documents/git/projects/receipts/client/checkouts. |
| 165 | +These wrappers improve the components in two ways (both admittedly arguable): |
| 166 | +- The names are in Clojure-friendly kebab-case, rather than camelCase. |
| 167 | +- The valid parameters are listed in the arglist. Parameters you supply are |
| 168 | + checked for validity, catching typos and undefind parameters. |
82 | 169 |
|
| 170 | +These improvements have some drawbacks, which make Sodium still less usable that |
| 171 | +Soda-ash in many circumstances. For that reason, I deliberately designed the Sodium |
| 172 | +functions to work with both Sodium and Soda-ash components. |
83 | 173 |
|
| 174 | +Some current limitations: |
| 175 | + |
| 176 | +- I've only defined some components so far; only about a dozen of the 140+ exported by Soda-ash. |
| 177 | +- I've tried to list all the valid parameters for each component. I built these lists |
| 178 | + mostly from the Semantic UI React [documentation](https://react.semantic-ui.com/), but |
| 179 | + discovered them to be incomplete. I've added missing parameters as I discovered them, |
| 180 | + but have surely missed some. [PRs *very* eagerly welcome here]. |
| 181 | +- The params map is mandatory in Sodium components. It is not valid, e.g., to say |
| 182 | + `[na/form [na/...]]`. Instead, you have to explicitly specify even an empty map: |
| 183 | + `[na/form {} [na/...]]` |
| 184 | + |
| 185 | + |
| 186 | +### Internals |
| 187 | + |
| 188 | +`sodium.core/defcomponent` and `sodium.core/def-simple-component` Define a component; |
| 189 | +typically a thin wrapper around the Soda-ash component, to give parameter checking, |
| 190 | +etc. (see below). Soon, Sodium will contain definitions wrapping all the Soda-ash |
| 191 | +(Semantic-UI) components, but we are not there yet. |
84 | 192 |
|
85 | | -You will also need to follow the instruction in |
86 | | -the [soda-ash README](https://github.com/gadfly361/soda-ash), particularly to include |
87 | | -the stylesheet and dependency. |
88 | 193 |
|
89 | 194 | ### Testing |
90 | 195 |
|
91 | | -`lein doo phantom test auto` will run the few unit tests I've written so far. |
| 196 | +`lein doo phantom test auto` will run the few unit tests I've written so far. More are |
| 197 | +needed. PRs especially welcome here. |
92 | 198 |
|
93 | | -## License |
| 199 | +## Useful references |
94 | 200 |
|
95 | | -Licensed under the Eclipse Public License. |
| 201 | +- https://semantic-ui.com/ |
| 202 | +- https://react.semantic-ui.com/introduction |
| 203 | +- https://github.com/Semantic-Org/Semantic-UI-React |
| 204 | +- https://github.com/gadfly361/soda-ashsoda |
| 205 | + |
| 206 | + |
| 207 | +## Questions |
| 208 | + |
| 209 | +I can usually be found on the [Clojurians Slack](https://clojurians.net) #reagent or |
| 210 | +#re-frame slack channels. My handle is @deg. Email is also fine. |
| 211 | + |
| 212 | +## License |
96 | 213 |
|
97 | 214 | Copyright © 2017, David Goldfarb <deg@degel.com> |
| 215 | + |
| 216 | +Licensed under the Eclipse Public License. |
0 commit comments