diff --git a/frontend/src/app/main/ui/components/truncated_text.cljs b/frontend/src/app/main/ui/components/truncated_text.cljs new file mode 100644 index 0000000000..215cd344ce --- /dev/null +++ b/frontend/src/app/main/ui/components/truncated_text.cljs @@ -0,0 +1,59 @@ +;; 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 SUBSIDIARY SL + +(ns app.main.ui.components.truncated-text + (:require-macros [app.main.style :as stl]) + (:require + [app.common.data.macros :as dm] + [app.main.ui.ds.tooltip.tooltip :refer [tooltip*]] + [rumext.v2 :as mf])) + +(defn truncated? + "True when the text of `node` is wider than the box that shows it, so + CSS cuts it (for example with an ellipsis)." + [node] + (> (.-scrollWidth node) (.-clientWidth node))) + +(mf/defc truncated-text* + "Renders `text` in an `as` element (a `span` by default) styled by + `class`, which is expected to cut long text. Only while the text is + cut, hovering or focusing it shows a tooltip with the full text." + [{:keys [text as class tooltip-class]}] + (let [as (or as "span") + text-ref (mf/use-ref nil) + truncated* (mf/use-state false) + truncated (deref truncated*) + + check-truncated + (mf/use-fn + (fn [] + (when-let [node (mf/ref-val text-ref)] + (reset! truncated* (truncated? node))))) + + text-props + (mf/props {:class class + :ref text-ref})] + + ;; Wrapping the text in the tooltip mounts a new node, so the + ;; observer is set up again whenever `truncated` changes. + (mf/with-effect [text truncated] + (check-truncated) + (when-let [node (mf/ref-val text-ref)] + (let [observer (js/ResizeObserver. check-truncated)] + (.observe observer node) + #(.disconnect observer)))) + + (if ^boolean truncated + ;; The tooltip trigger sets `line-height: 0`, which the text would + ;; inherit and collapse. An inline style restores the parent one: a + ;; class here would tie with the trigger class and depend on the + ;; order of the stylesheets. + [:> tooltip* {:content text + :trigger-ref text-ref + :style #js {:lineHeight "inherit"} + :class (dm/str (stl/css :tooltip) " " tooltip-class)} + [:> as text-props text]] + [:> as text-props text]))) diff --git a/frontend/src/app/main/ui/components/truncated_text.scss b/frontend/src/app/main/ui/components/truncated_text.scss new file mode 100644 index 0000000000..5ad292409c --- /dev/null +++ b/frontend/src/app/main/ui/components/truncated_text.scss @@ -0,0 +1,11 @@ +// 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 SUBSIDIARY SL + +// The tooltip wraps the text in its own element: let it shrink inside a +// flex or grid parent, so the text keeps being cut instead of overflowing. +.tooltip { + min-inline-size: 0; +} diff --git a/frontend/src/app/main/ui/workspace/libraries.cljs b/frontend/src/app/main/ui/workspace/libraries.cljs index b2065ebbdd..d22239fb78 100644 --- a/frontend/src/app/main/ui/workspace/libraries.cljs +++ b/frontend/src/app/main/ui/workspace/libraries.cljs @@ -33,6 +33,7 @@ [app.main.ui.components.link-button :as lb] [app.main.ui.components.search-bar :refer [search-bar*]] [app.main.ui.components.title-bar :refer [title-bar*]] + [app.main.ui.components.truncated-text :refer [truncated-text*]] [app.main.ui.context :as ctx] [app.main.ui.ds.buttons.button :refer [button*]] [app.main.ui.ds.buttons.icon-button :refer [icon-button*]] @@ -575,7 +576,7 @@ :key (dm/str id) :data-testid "library-item"} [:div {:class (stl/css :item-content)} - [:div {:class (stl/css :item-name)} name] + [:> truncated-text* {:as "div" :class (stl/css :item-name) :text name}] [:ul {:class (stl/css :item-contents-legacy)} (let [summary (get-library-summary data)] [:* @@ -620,7 +621,7 @@ :key (dm/str id) :data-testid "library-item"} [:div {:class (stl/css :item-content)} - [:div {:class (stl/css :item-name)} name] + [:> truncated-text* {:as "div" :class (stl/css :item-name) :text name}] [:ul {:class (stl/css :item-contents-legacy)} (let [summary (-> (:library-summary library) (adapt-backend-summary))] @@ -737,7 +738,7 @@ :key (dm/str id) :data-testid "library-item"} [:div {:class (stl/css :item-content)} - [:div {:class (stl/css :item-name)} name] + [:> truncated-text* {:as "div" :class (stl/css :item-name) :text name}] [:ul {:class (stl/css :item-contents)} (let [summary (-> (:library-summary library) (adapt-backend-summary))] @@ -955,7 +956,7 @@ :key (dm/str id) :data-testid "library-item"} [:div {:class (stl/css :item-content)} - [:div {:class (stl/css :item-name)} name] + [:> truncated-text* {:as "div" :class (stl/css :item-name) :text name}] [:ul {:class (stl/css :item-contents)} (let [summary (get-library-summary data)] [:> library-description* {:summary summary :hint-name parent-name}])]] @@ -1117,7 +1118,7 @@ [:div {:class (stl/css :section-list-item) :key (dm/str id)} [:div {:class (stl/css :item-content)} - [:div {:class (stl/css :item-name)} name] + [:> truncated-text* {:as "div" :class (stl/css :item-name) :text name}] [:ul {:class (stl/css :item-contents)} (describe-library (count components) diff --git a/frontend/src/app/main/ui/workspace/sidebar/assets/file_library.cljs b/frontend/src/app/main/ui/workspace/sidebar/assets/file_library.cljs index 8596dfd11a..8eb3084926 100644 --- a/frontend/src/app/main/ui/workspace/sidebar/assets/file_library.cljs +++ b/frontend/src/app/main/ui/workspace/sidebar/assets/file_library.cljs @@ -21,6 +21,7 @@ [app.main.router :as rt] [app.main.store :as st] [app.main.ui.components.title-bar :refer [title-bar*]] + [app.main.ui.components.truncated-text :refer [truncated-text*]] [app.main.ui.context :as ctx] [app.main.ui.ds.foundations.assets.icon :as i :refer [icon*]] [app.main.ui.ds.tooltip :refer [tooltip*]] @@ -117,8 +118,8 @@ tokens-source-icon]) ;; Do we need to add shared info here? (mf/html [:div {:class (stl/css :special-title)} - [:span {:class (stl/css :special-title-text)} - file-name] + [:> truncated-text* {:class (stl/css :special-title-text) + :text file-name}] tokens-source-icon]))} (when-not ^boolean is-local [:span {:title (tr "workspace.assets.open-library")} diff --git a/frontend/test/frontend_tests/runner.cljs b/frontend/test/frontend_tests/runner.cljs index 2349664354..3c0e5121ce 100644 --- a/frontend/test/frontend_tests/runner.cljs +++ b/frontend/test/frontend_tests/runner.cljs @@ -204,6 +204,7 @@ [frontend-tests.ui.subscription-test] [frontend-tests.ui.team-container-test] [frontend-tests.ui.text-attrs-multiple-test] + [frontend-tests.ui.truncated-text-test] [frontend-tests.ui.viewport-texts-html-test] [frontend-tests.ui.viewport-top-bar-test] [frontend-tests.ui.workspace-libraries-test] @@ -448,6 +449,7 @@ 'frontend-tests.ui.subscription-test 'frontend-tests.ui.team-container-test 'frontend-tests.ui.text-attrs-multiple-test + 'frontend-tests.ui.truncated-text-test 'frontend-tests.ui.viewport-texts-html-test 'frontend-tests.ui.viewport-top-bar-test 'frontend-tests.ui.workspace-libraries-test diff --git a/frontend/test/frontend_tests/ui/truncated_text_test.cljs b/frontend/test/frontend_tests/ui/truncated_text_test.cljs new file mode 100644 index 0000000000..80d8e7f963 --- /dev/null +++ b/frontend/test/frontend_tests/ui/truncated_text_test.cljs @@ -0,0 +1,130 @@ +;; 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 SUBSIDIARY SL + +(ns frontend-tests.ui.truncated-text-test + "`truncated-text*` shows a tooltip with the full text only while the + text is cut. jsdom does no layout, so the tests set the widths the + component reads and play the part of the browser's `ResizeObserver`." + (:require + ["jsdom" :refer [JSDOM]] + ["react" :as react] + ["react-dom/client" :as rdc] + [app.main.ui.components.truncated-text :as tt] + [app.main.ui.hooks :as hooks] + [cljs.test :as t :include-macros true] + [frontend-tests.helpers.mock :as mock] + [rumext.v2 :as mf])) + +(def ^:private long-name + "DS_Feedback,Content&Tables_Components_Forms,Inputs&Validation_Patterns_Library_v2") + +(t/deftest truncated-when-the-text-is-wider-than-its-box + (t/is (true? (tt/truncated? #js {:scrollWidth 320 :clientWidth 200}))) + (t/is (false? (tt/truncated? #js {:scrollWidth 200 :clientWidth 200}))) + (t/is (false? (tt/truncated? #js {:scrollWidth 120 :clientWidth 200})))) + +(defn- ^:async with-mounted-text + "Mounts `truncated-text*` with `long-name` in a jsdom document whose + elements report `widths` (an atom of `{:scroll n :client n}`), and + calls `test-fn` with the container and a function that fires the + resize observers, as the browser does when a box changes size." + [widths test-fn] + (let [dom (JSDOM. "
") + window (.-window dom) + document (.-document window) + container (.getElementById document "root") + proto (.. window -HTMLElement -prototype) + ;; Observed [node callback] pairs. Like the browser, a resize only + ;; reaches the nodes that are still in the document. + observed (atom #{}) + old-window (.-window js/globalThis) + old-document (.-document js/globalThis) + old-observer (.-ResizeObserver js/globalThis) + old-act (.-IS_REACT_ACT_ENVIRONMENT js/globalThis) + resize (fn [] + (doseq [[node callback] @observed] + (when (.-isConnected node) + (callback))))] + (js/Object.defineProperty proto "scrollWidth" #js {:get #(:scroll @widths)}) + (js/Object.defineProperty proto "clientWidth" #js {:get #(:client @widths)}) + (set! (.-window js/globalThis) window) + (set! (.-document js/globalThis) document) + (set! (.-IS_REACT_ACT_ENVIRONMENT js/globalThis) true) + (set! (.-ResizeObserver js/globalThis) + (fn [callback] + (this-as this + (let [nodes (atom #{})] + (set! (.-observe this) + (fn [node] + (swap! nodes conj node) + (swap! observed conj [node callback]))) + (set! (.-disconnect this) + (fn [] + (swap! observed #(reduce disj % (map (fn [n] [n callback]) @nodes)))))) + this))) + (try + (await + (mock/with-mocks* {hooks/use-portal-container (fn ([] (.-body document)) + ([_] (.-body document)))} + (let [root (rdc/createRoot container)] + (try + (await (react/act + #(.render root (mf/element tt/truncated-text* + #js {:text long-name :as "div"})))) + (await (test-fn container (fn [] (react/act resize)))) + (finally + (await (react/act #(.unmount root)))))))) + (finally + (.close window) + (set! (.-window js/globalThis) old-window) + (set! (.-document js/globalThis) old-document) + (set! (.-ResizeObserver js/globalThis) old-observer) + (set! (.-IS_REACT_ACT_ENVIRONMENT js/globalThis) old-act))))) + +(defn- tooltip-trigger + "The element the tooltip listens on: it carries the full text as its + accessible name." + [container] + (.querySelector container (str "[aria-label=\"" long-name "\"]"))) + +(t/deftest ^:async text-that-fits-has-no-tooltip + (await + (with-mounted-text (atom {:scroll 200 :client 200}) + (fn [container _] + (t/is (= long-name (.-textContent container))) + (t/is (nil? (tooltip-trigger container))))))) + +(t/deftest ^:async cut-text-has-a-tooltip-with-the-full-text + (await + (with-mounted-text (atom {:scroll 320 :client 200}) + (fn [container _] + (t/is (= long-name (.-textContent container))) + (t/is (some? (tooltip-trigger container))))))) + +(t/deftest ^:async cut-text-keeps-the-line-height-of-its-parent + ;; The DS tooltip trigger sets `line-height: 0`. A cut text inside it + ;; would inherit it and collapse, hiding the text. + (await + (with-mounted-text (atom {:scroll 320 :client 200}) + (fn [container _] + (t/is (= "inherit" (.. (tooltip-trigger container) -style -lineHeight))))))) + +(t/deftest ^:async tooltip-follows-the-size-of-the-box + ;; Wrapping the text in the tooltip mounts a new node: the observer + ;; must follow it, or the tooltip would stay after the box grows again. + (let [widths (atom {:scroll 200 :client 200})] + (await + (with-mounted-text widths + (^:async fn [container resize] + (t/is (nil? (tooltip-trigger container))) + + (reset! widths {:scroll 320 :client 200}) + (await (resize)) + (t/is (some? (tooltip-trigger container))) + + (reset! widths {:scroll 320 :client 400}) + (await (resize)) + (t/is (nil? (tooltip-trigger container))))))))