Skip to content

Commit

Permalink
feat: add new theming mechanism (#16191)
Browse files Browse the repository at this point in the history
* chore: set react-dom to same version as react
  • Loading branch information
J-Son89 authored Jun 23, 2023
1 parent 1591a5e commit e5778ee
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 108 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"node-libs-react-native": "^1.2.1",
"qrcode": "^1.4.1",
"react": "18.0.0",
"react-dom": "^16.4.2",
"react-dom": "18.0.0",
"react-native": "0.69.10",
"react-native-background-timer": "^2.1.1",
"react-native-blob-util": "^0.13.18",
Expand Down
10 changes: 5 additions & 5 deletions src/quo2/components/buttons/button.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@
(when disabled
{:opacity 0.3})))

(defn button
(defn- button-internal
"with label
[button opts \"label\"]
opts
Expand All @@ -223,17 +223,15 @@
(let [pressed-in (reagent/atom false)]
(fn
[{:keys [on-press disabled type size before after above icon-secondary-no-color
width customization-color override-theme override-background-color pressed
width customization-color theme override-background-color pressed
on-long-press accessibility-label icon icon-no-color style inner-style test-ID]
:or {type :primary
size 40
customization-color :primary}}
children]
(let [{:keys [icon-color icon-secondary-color background-color label-color border-color]}
(get-in (themes customization-color)
[(or
override-theme
(theme/get-theme)) type])
[theme type])
state (cond disabled :disabled
(or @pressed-in pressed) :pressed
:else :default)
Expand Down Expand Up @@ -309,3 +307,5 @@
:no-color icon-secondary-no-color
:color icon-secondary-color
:size icon-size}]])]]]))))

(def button (theme/with-theme button-internal))
15 changes: 10 additions & 5 deletions src/quo2/components/dividers/divider_label.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns quo2.components.dividers.divider-label
(:require [quo2.components.icon :as icons]
(:require [quo2.theme :as theme]
[quo2.components.icon :as icons]
[quo2.components.markdown.text :as markdown.text]
[quo2.foundations.colors :as colors]
[react-native.core :as rn]))
Expand All @@ -8,15 +9,16 @@

(def chevron-icon-container-height 20)

(defn divider-label
(defn themed-view
"label -> string
chevron-position -> :left, :right
chevron-icon -> keyword
on-press -> function
padding-bottom -> number
counter-value -> number
increase-padding-top? -> boolean
blur? -> boolean"
blur? -> boolean
theme -> theme value passed from with-theme HOC"
[{:keys [label
chevron-position
chevron-icon
Expand All @@ -25,8 +27,9 @@
padding-bottom
blur?
container-style
on-press]}]
(let [dark? (colors/dark?)
on-press
theme]}]
(let [dark? (= :dark theme)
border-and-counter-bg-color (if dark?
(if blur? colors/white-opa-5 colors/neutral-70)
colors/neutral-10)
Expand Down Expand Up @@ -83,3 +86,5 @@
:weight :medium
:style {:color counter-text-color}}
counter-value]])]]))

(def divider-label (theme/with-theme themed-view))
9 changes: 4 additions & 5 deletions src/quo2/components/tabs/tab/style.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns quo2.components.tabs.tab.style
(:require [quo2.foundations.colors :as colors]
[quo2.theme :as theme]))
(:require [quo2.foundations.colors :as colors]))

(def tab-background-opacity 0.3)

Expand Down Expand Up @@ -99,10 +98,10 @@
:label {:style {:color colors/white}}}}})

(defn by-theme
[{:keys [override-theme disabled active blur?]}]
[{:keys [disabled active blur? theme]}]
(let [state (cond disabled :disabled
active :active
:else :default)
theme (or override-theme (theme/get-theme))]
:else :default)]

(get-in (if blur? themes-for-blur-background themes)
[theme state])))
18 changes: 10 additions & 8 deletions src/quo2/components/tabs/tab/view.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
(ns quo2.components.tabs.tab.view
(:require [quo2.components.icon :as icons]
[quo2.components.markdown.text :as text]
[quo2.components.tabs.tab.style :as style]
[quo2.components.notifications.notification-dot :as notification-dot]
[quo2.components.tabs.tab.style :as style]
[quo2.theme :as theme]
[react-native.core :as rn]
[react-native.svg :as svg]))

Expand Down Expand Up @@ -46,7 +47,7 @@
(vector? children)
children)])

(defn view
(defn- themed-view
[{:keys [accessibility-label
active
before
Expand All @@ -56,7 +57,7 @@
disabled
id
on-press
override-theme
theme
segmented?
size
notification-dot?]
Expand All @@ -65,11 +66,10 @@
(let [show-notification-dot? (and notification-dot? (= size 32))
{:keys [icon-color
background-color
label]}
(style/by-theme {:override-theme override-theme
:blur? blur?
:disabled disabled
:active active})]
label]} (style/by-theme {:theme theme
:blur? blur?
:disabled disabled
:active active})]
[rn/touchable-without-feedback
(merge {:disabled disabled
:accessibility-label accessibility-label}
Expand Down Expand Up @@ -101,3 +101,5 @@
:height size
:disabled disabled
:background-color background-color}])]]))

(def view (theme/with-theme themed-view))
6 changes: 0 additions & 6 deletions src/quo2/components/tabs/tabs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
flat-list-ref
number-of-items
on-change
override-theme
scroll-on-press?
size
style]}
Expand All @@ -80,7 +79,6 @@
:notification-dot? notification-dot?
:accessibility-label accessibility-label
:size size
:override-theme override-theme
:blur? blur?
:active (= id @active-tab-id)
:on-press (fn [id]
Expand All @@ -101,7 +99,6 @@
- `blur?` Boolean passed down to `quo2.components.tabs.tab/tab`.
- `data` Vector of tab items.
- `on-change` Callback called after a tab is selected.
- `override-theme` Passed down to `quo2.components.tabs.tab/tab`.
- `size` 32/24
- `style` Style map passed to View wrapping tabs or to the FlatList when tabs
are scrollable.
Expand Down Expand Up @@ -132,7 +129,6 @@
style
size
blur?
override-theme
in-scroll-view?]
:or {fade-end-percentage fade-end-percentage
fade-end? false
Expand Down Expand Up @@ -183,7 +179,6 @@
:flat-list-ref flat-list-ref
:number-of-items (count data)
:on-change on-change
:override-theme override-theme
:scroll-on-press? scroll-on-press?
:size size
:style style})})]]]
Expand All @@ -195,7 +190,6 @@
:blur? blur?
:number-of-items (count data)
:on-change on-change
:override-theme override-theme
:size size
:style style}
item
Expand Down
42 changes: 37 additions & 5 deletions src/quo2/theme.cljs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
(ns quo2.theme
(:require [reagent.core :as reagent]))
(:require [react-native.core :as rn]
["react" :as react]
[reagent.core :as reagent]
utils.transforms))

(def theme (reagent/atom :light))
(defonce ^:private theme-context (react/createContext :light))
(defonce ^:private theme-state (reagent/atom :light))

(defn dark?
[]
(= :dark @theme))
(= :dark @theme-state))

(defn get-theme
[]
@theme)
@theme-state)

(defn set-theme
[value]
(reset! theme value))
(reset! theme-state value))

(defn theme-value
"Returns a value based on the current/override-theme theme."
Expand All @@ -22,3 +26,31 @@
([light-value dark-value override-theme]
(let [theme (or override-theme (get-theme))]
(if (= theme :light) light-value dark-value))))

(defn provider
"Wrap `children` in a React Provider using `quo2.theme/theme-context` as the
context.
`options`: Clojure map. Currently we only use the `:theme` key. In the future
we may support other settings.
"
[options & children]
(into [:> (.-Provider theme-context) {:value options}]
children))

(defn use-theme
"A hook that returns the current theme context."
[]
(utils.transforms/js->clj (rn/use-context theme-context)))

(defn ^:private f-with-theme
[component props & args]
(let [theme (-> (use-theme) :theme keyword)]
(into [component (assoc props :theme theme)] args)))

(defn with-theme
"Create a functional component that assoc `:theme` into the first arg of
`component`. The theme value is taken from the nearest `quo2.theme/provider`."
[component]
(fn [& args]
(into [:f> f-with-theme component] args)))
4 changes: 4 additions & 0 deletions src/react_native/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@
[ref]
(oops/oget ref "current"))

(def create-context react/createContext)

(def use-context react/useContext)

(defn use-effect
([effect-fn]
(use-effect effect-fn []))
Expand Down
11 changes: 7 additions & 4 deletions src/status_im/bottom_sheet/sheets.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns status-im.bottom-sheet.sheets
(:require [utils.re-frame :as rf]
[quo2.theme :as theme]
[status-im.ui.screens.about-app.views :as about-app]
[status-im.ui.screens.keycard.views :as keycard]
[status-im.ui.screens.mobile-network-settings.view :as mobile-network-settings]
Expand Down Expand Up @@ -28,14 +29,16 @@
(merge keycard/more-sheet)

(= view :learn-more)
(merge about-app/learn-more))]
(merge about-app/learn-more))
page-theme (:theme options)]

[:f>
(fn []
(rn/use-effect (fn []
(rn/hw-back-add-listener dismiss-bottom-sheet-callback)
(fn []
(rn/hw-back-remove-listener dismiss-bottom-sheet-callback))))
[bottom-sheet/bottom-sheet opts
(when content
[content (when options options)])])]))
[theme/provider {:theme (or page-theme (theme/get-theme))}
[bottom-sheet/bottom-sheet opts
(when content
[content (when options options)])]])]))
1 change: 0 additions & 1 deletion src/status_im2/contexts/activity_center/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
{:size 32
:scrollable? true
:blur? true
:override-theme :dark
:style style/tabs
:fade-end-percentage 0.79
:scroll-on-press? true
Expand Down
3 changes: 1 addition & 2 deletions src/status_im2/contexts/onboarding/profiles/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@
:size 32
:icon true
:on-press show-new-account-options
:accessibility-label :show-new-account-options
:override-theme :dark}
:accessibility-label :show-new-account-options}
:main-icons/add]]
[rn/flat-list
{:data (sort-by :timestamp > profiles-data)
Expand Down
30 changes: 16 additions & 14 deletions src/status_im2/contexts/shell/components/shell_screen/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [utils.i18n :as i18n]
[quo2.core :as quo]
[quo2.foundations.colors :as colors]
[quo2.theme :as theme]
[utils.re-frame :as rf]
[react-native.core :as rn]
[react-native.blur :as blur]
Expand Down Expand Up @@ -98,17 +99,18 @@
width (rf/sub [:dimensions/window-width])
top (safe-area/get-top)
shell-margin (/ (- width (* 2 shell.constants/switcher-card-size)) 3)]
[rn/view
{:style {:top 0
:left 0
:right 0
:bottom -1
:position :absolute
:background-color colors/neutral-100}}
[jump-to-list switcher-cards shell-margin]
[top-nav-blur-overlay top]
[common.home/top-nav
{:type :shell
:avatar {:customization-color customization-color}
:style {:margin-top top
:z-index 2}}]]))
[theme/provider {:theme :dark}
[rn/view
{:style {:top 0
:left 0
:right 0
:bottom -1
:position :absolute
:background-color colors/neutral-100}}
[jump-to-list switcher-cards shell-margin]
[top-nav-blur-overlay top]
[common.home/top-nav
{:type :shell
:avatar {:customization-color customization-color}
:style {:margin-top top
:z-index 2}}]]]))
21 changes: 10 additions & 11 deletions src/status_im2/contexts/syncing/setup_syncing/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,16 @@
:default-shown? true
:editable false}]
[quo/button
{:on-press (fn []
(clipboard/set-string @code)
(rf/dispatch [:toasts/upsert
{:icon :correct
:icon-color colors/success-50
:text (i18n/label
:t/sharing-copied-to-clipboard)}]))
:override-theme :dark
:type :grey
:style {:margin-top 12}
:before :i/copy}
{:on-press (fn []
(clipboard/set-string @code)
(rf/dispatch [:toasts/upsert
{:icon :correct
:icon-color colors/success-50
:text (i18n/label
:t/sharing-copied-to-clipboard)}]))
:type :grey
:style {:margin-top 12}
:before :i/copy}
(i18n/label :t/copy-qr)]])]]
[rn/view {:style style/sync-code}
[quo/divider-label
Expand Down
Loading

0 comments on commit e5778ee

Please sign in to comment.