Merge pull request #5557 from penpot/niwinz-enhancements-4

 Add the ability to export helpers for storybook
This commit is contained in:
Andrey Antukh 2025-01-14 15:38:26 +01:00 committed by GitHub
commit 80d6968156
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 65 additions and 40 deletions

View file

@ -126,8 +126,10 @@
:modules :modules
{:base {:base
{:entries []} {:entries []}
:components :components
{:exports {default app.main.ui.ds/default} {:exports {default app.main.ui.ds/default
helpers app.main.ui.ds.helpers/default}
:depends-on #{:base}}} :depends-on #{:base}}}
:compiler-options :compiler-options

View file

@ -24,14 +24,16 @@
[app.main.ui.ds.product.loader :refer [loader*]] [app.main.ui.ds.product.loader :refer [loader*]]
[app.main.ui.ds.storybook :as sb] [app.main.ui.ds.storybook :as sb]
[app.main.ui.ds.utilities.swatch :refer [swatch*]] [app.main.ui.ds.utilities.swatch :refer [swatch*]]
[app.util.i18n :as i18n])) [app.util.i18n :as i18n]
[rumext.v2 :as mf]))
(i18n/init! cf/translations) (i18n/init! cf/translations)
(def default (def default
"A export used for storybook" "A export used for storybook"
#js {:Button button* (mf/object
{:Button button*
:Heading heading* :Heading heading*
:Icon icon* :Icon icon*
:IconButton icon-button* :IconButton icon-button*
@ -47,11 +49,13 @@
:TokenStatusIcon token-status-icon* :TokenStatusIcon token-status-icon*
:Swatch swatch* :Swatch swatch*
;; meta / misc ;; meta / misc
:meta #js {:icons (clj->js (sort icon-list)) :meta
{:icons (clj->js (sort icon-list))
:tokenStatus (clj->js (sort token-status-list)) :tokenStatus (clj->js (sort token-status-list))
:svgs (clj->js (sort raw-svg-list)) :svgs (clj->js (sort raw-svg-list))
:typography (clj->js typography-list)} :typography (clj->js typography-list)}
:storybook #js {:StoryGrid sb/story-grid* :storybook
{:StoryGrid sb/story-grid*
:StoryGridCell sb/story-grid-cell* :StoryGridCell sb/story-grid-cell*
:StoryGridRow sb/story-grid-row* :StoryGridRow sb/story-grid-row*
:StoryHeader sb/story-header*}}) :StoryHeader sb/story-header*}}))

View file

@ -0,0 +1,14 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns app.main.ui.ds.helpers
"A collection of helpers for exporting them to be used on storybook code."
(:require
[rumext.v2 :as mf]))
(def default
(mf/object
{:uuid parse-uuid}))

View file

@ -7,11 +7,8 @@
(ns app.main.ui.ds.utilities.swatch (ns app.main.ui.ds.utilities.swatch
(:require-macros (:require-macros
[app.main.style :as stl]) [app.main.style :as stl])
(:require (:require
[app.common.data.macros :as dm] [app.common.data.macros :as dm]
[app.common.json :as json] [app.common.json :as json]
[app.common.schema :as sm] [app.common.schema :as sm]
@ -22,14 +19,6 @@
[cuerdas.core :as str] [cuerdas.core :as str]
[rumext.v2 :as mf])) [rumext.v2 :as mf]))
(def ^:private schema:swatch
[:map {:title "SchemaSwatch"}
[:background {:optional true} ct/schema:color]
[:class {:optional true} :string]
[:size {:optional true} [:enum "small" "medium" "large"]]
[:active {:optional true} :boolean]
[:on-click {:optional true} fn?]])
(defn- color-title (defn- color-title
[color-item] [color-item]
(let [name (:name color-item) (let [name (:name color-item)
@ -63,12 +52,28 @@
(some? image) (some? image)
(tr "media.image"))))) (tr "media.image")))))
(def ^:private schema:swatch
[:map {:title "SchemaSwatch"}
[:background {:optional true} ct/schema:color]
[:class {:optional true} :string]
[:size {:optional true} [:enum "small" "medium" "large"]]
[:active {:optional true} ::sm/boolean]
[:on-click {:optional true} ::sm/fn]])
(mf/defc swatch* (mf/defc swatch*
{::mf/props :obj {::mf/schema (sm/schema schema:swatch)}
::mf/schema (sm/schema schema:swatch)}
[{:keys [background on-click size active class] [{:keys [background on-click size active class]
:rest props}] :rest props}]
(let [background (if (object? background) (json/->clj background) background) (let [;; NOTE: this code is only relevant for storybook, because
;; storybook is unable to pass in a comfortable way a complex
;; object; the "interactive" way of storybook only allows
;; plain object. So for this case we accept them and
;; automatically convert them to clojure map (which is exactly
;; what this component expects). On normal usage of this
;; component this code should be always fallback to else case.
background (if (object? background)
(json/->clj background)
background)
read-only? (nil? on-click) read-only? (nil? on-click)
id? (some? (:id background)) id? (some? (:id background))
element-type (if read-only? "div" "button") element-type (if read-only? "div" "button")