0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-04-06 20:11:29 -05:00

Add minor optimizations to tab-container react component

This commit is contained in:
Andrey Antukh 2024-01-08 17:51:33 +01:00
parent e0a1cd6e77
commit eeaee5ad42
7 changed files with 48 additions and 43 deletions

View file

@ -10,6 +10,7 @@
[app.common.data :as d]
[app.common.data.macros :as dm]
[app.main.ui.icons :as i]
[app.util.array :as array]
[app.util.dom :as dom]
[app.util.i18n :refer [tr]]
[cuerdas.core :as str]
@ -17,41 +18,32 @@
(mf/defc tab-element
{::mf/wrap-props false}
[props]
(let [children (unchecked-get props "children")]
[:div {:class (stl/css :tab-element)}
children]))
[{:keys [children]}]
[:div {:class (stl/css :tab-element)} children])
(mf/defc tab-container
{::mf/wrap-props false}
[props]
(let [children (->>
(unchecked-get props "children")
(filter some?))
selected (unchecked-get props "selected")
on-change (unchecked-get props "on-change-tab")
collapsable? (unchecked-get props "collapsable?")
handle-collapse (unchecked-get props "handle-collapse")
class (unchecked-get props "class")
content-class (unchecked-get props "content-class")
[{:keys [children selected on-change-tab collapsable handle-collapse header-class content-class]}]
(let [children (-> (array/normalize-to-array children)
(array/without-nils))
state (mf/use-state #(or selected (-> children first .-props .-id)))
selected (or selected @state)
selected* (mf/use-state #(or selected (-> children first .-props .-id)))
selected (or selected @selected*)
select-fn
(mf/use-fn
(mf/deps on-change)
(fn [event]
(let [id (-> event
(dom/get-current-target)
(dom/get-data "id")
(keyword))]
(reset! state id)
(when (fn? on-change) (on-change id)))))]
on-click (mf/use-fn
(mf/deps on-change-tab)
(fn [event]
(let [id (-> event
(dom/get-current-target)
(dom/get-data "id")
(keyword))]
(reset! selected* id)
(when (fn? on-change-tab)
(on-change-tab id)))))]
[:div {:class (stl/css :tab-container)}
[:div {:class (dm/str class " " (stl/css :tab-container-tabs))}
(when collapsable?
[:div {:class (dm/str header-class " " (stl/css :tab-container-tabs))}
(when ^boolean collapsable
[:button
{:on-click handle-collapse
:class (stl/css :collapse-sidebar)
@ -61,13 +53,16 @@
(for [tab children]
(let [props (.-props tab)
id (.-id props)
title (.-title props)]
[:div
{:key (str/concat "tab-" (d/name id))
:data-id (d/name id)
:on-click select-fn
:class (stl/css-case :tab-container-tab-title true
:current (= selected id))}
title (.-title props)
sid (d/name id)]
[:div {:key (str/concat "tab-" sid)
:data-id sid
:on-click on-click
:class (stl/css-case
:tab-container-tab-title true
:current (= selected id))}
title]))]]
[:div {:class (dm/str content-class " " (stl/css :tab-container-content))}
(d/seek #(= selected (-> % .-props .-id)) children)]]))
(d/seek #(= selected (-> % .-props .-id))
children)]]))

View file

@ -311,7 +311,7 @@
[:& tab-container
{:on-change-tab set-tab!
:selected @active-color-tab
:collapsable? false}
:collapsable false}
[:& tab-element {:id :ramp :title i/rgba-refactor}
(if picking-color?

View file

@ -502,7 +502,7 @@
[:& tab-container
{:on-change-tab on-tab-change
:selected selected-tab
:collapsable? false}
:collapsable false}
[:& tab-element {:id :libraries :title (tr "workspace.libraries.libraries")}
[:div {:class (stl/css :libraries-content)}
[:& libraries-tab {:file-id file-id

View file

@ -83,10 +83,9 @@
[:& tab-container
{:on-change-tab on-tab-change
:selected section
:shortcuts? shortcuts?
:collapsable? true
:collapsable true
:handle-collapse handle-collapse
:class (stl/css :tab-spacing)}
:header-class (stl/css :tab-spacing)}
[:& tab-element {:id :layers :title (tr "workspace.sidebar.layers")}
[:div {:class (stl/css :layers-tab)
:style #js {"--height" (str size-pages "px")}}

View file

@ -103,9 +103,9 @@
[:& tab-container
{:on-change-tab on-change-tab
:selected section
:collapsable? false
:collapsable false
:content-class (stl/css :content-class)
:class (stl/css :tab-spacing)}
:header-class (stl/css :tab-spacing)}
[:& tab-element {:id :design
:title (tr "workspace.options.design")}
[:div {:class (stl/css :element-options)}

View file

@ -299,6 +299,7 @@
(not (cfh/is-direct-child-of-root? shape))
(empty? (get shape :fills)))))
hover-shape
(->> ids
(remove remove-id?)

View file

@ -34,3 +34,13 @@
(.push ^js a v1 v2 v3 v4 v5 v6)
a))
(defn normalize-to-array
"If `o` is an array, returns it as-is, if not, wrap into an array."
[o]
(if (array? o)
o
#js [o]))
(defn without-nils
[^js/Array o]
(.filter o (fn [v] (some? v))))