-
Notifications
You must be signed in to change notification settings - Fork 985
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
(ns quo.components.token | ||
(:require [clojure.string :as string] | ||
[quo.foundations.resources :as resources] | ||
[react-native.core :as rn] | ||
[utils.number])) | ||
|
||
(defn- size->number | ||
"Remove `size-` prefix in size keywords and returns a number useful for styling." | ||
[k] | ||
(utils.number/parse-int (subs (name k) 5))) | ||
|
||
(defn- token-style | ||
[style size] | ||
(let [size-number (if (keyword? size) | ||
(size->number size) | ||
size)] | ||
(assoc style | ||
:width size-number | ||
:height size-number))) | ||
|
||
(defn- get-token-image* | ||
[token] | ||
(let [token-name (condp #(%1 %2) token | ||
string? (keyword (string/lower-case token)) | ||
keyword? (keyword (string/lower-case (name token))) | ||
nil)] | ||
(resources/get-token token-name))) | ||
|
||
(def ^:private get-token-image (memoize get-token-image*)) | ||
(def ^:private b64-png-image-prefix "data:image/png;base64,") | ||
|
||
(defn view | ||
"Render a token image. | ||
Props: | ||
- style: extra styles to apply to the `rn/image` component. | ||
- size: `:size-nn` or just `nn`, being `nn` any number. Defaults to 32. | ||
- token: string or keyword, it can contain upper case letters or not. | ||
E.g. all of these are valid and resolve to the same: | ||
:token/snt | :snt | :SNT | \"SNT\" | \"snt\". | ||
- image-source: Ignores `token` and uses this as parameter to `rn/image`'s source. | ||
- add-b64-prefix?: If true, adds `data:image/png;base64,` as prefix to the string | ||
passed as `image-source`. | ||
" | ||
[{:keys [token size style image-source add-b64-prefix?] | ||
:or {size 32 | ||
token :snt}}] | ||
(let [b64-string (if (and image-source add-b64-prefix?) | ||
(str b64-png-image-prefix image-source) | ||
image-source) | ||
source (or b64-string (get-token-image token))] | ||
[rn/image | ||
{:style (token-style style size) | ||
:source source}])) |
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