mirror of
https://github.com/penpot/penpot.git
synced 2026-11-26 21:00:32 +00:00
🐛 Keep context menu submenus inside the window (#13302)
* 🐛 Keep context menu submenus inside the window A submenu opens at the height of its entry, so one opened from an entry near the bottom of the window ran past it and its last entries could not be reached. It now moves up to end 8px above the window's bottom. Signed-off-by: Florian Schroedl <flo.schroedl@gmail.com> Signed-off-by: Thomas Ramé <thomas@rame.fr> * 🐛 Keep nested context submenus inside the window * ♻️ Compute the context submenu position in a testable function --------- Signed-off-by: Florian Schroedl <flo.schroedl@gmail.com> Signed-off-by: Thomas Ramé <thomas@rame.fr> Co-authored-by: Florian Schroedl <flo.schroedl@gmail.com>
This commit is contained in:
parent
a0bbafaf12
commit
e6f3d21dce
@ -55,6 +55,31 @@
|
||||
(dom/prevent-default event)
|
||||
(dom/stop-propagation event))
|
||||
|
||||
;; Distance in pixels kept between a submenu and the bottom of the window
|
||||
(def ^:private submenu-margin 8)
|
||||
|
||||
;; Distance in pixels kept between a submenu and the menu it opens from
|
||||
(def ^:private submenu-gap 10)
|
||||
|
||||
(defn submenu-position
|
||||
"The `{:left :top}` position, in window pixels, of a submenu of size
|
||||
`submenu` (`:width` and `:height`) opened from the entry at `entry`
|
||||
of the menu at `menu` (bounding rects), in a window of size `window`.
|
||||
|
||||
It goes on the right of the menu, or on its left when it would
|
||||
overflow the window, without going past its left edge. It is aligned
|
||||
with the top of the entry, moved up to stay `submenu-margin` above the
|
||||
bottom of the window, and never above `submenu-margin` from its top."
|
||||
[{:keys [menu entry submenu window]}]
|
||||
(let [right (+ (:right menu) submenu-gap)
|
||||
left (if (> (+ right (:width submenu)) (:width window))
|
||||
(max 0 (- (:left menu) submenu-gap (:width submenu)))
|
||||
right)
|
||||
top (max submenu-margin
|
||||
(min (:top entry)
|
||||
(- (:height window) (:height submenu) submenu-margin)))]
|
||||
{:left left :top top}))
|
||||
|
||||
(mf/defc menu-entry*
|
||||
{::mf/private true}
|
||||
[{:keys [title shortcut shortcut-key on-click on-pointer-enter on-pointer-leave
|
||||
@ -74,13 +99,36 @@
|
||||
(st/emit! dw/hide-context-menu)
|
||||
(when on-click (on-click event))))
|
||||
|
||||
entry-ref (mf/use-ref nil)
|
||||
|
||||
on-pointer-enter
|
||||
(mf/use-fn
|
||||
(fn []
|
||||
(mf/set-ref-val! hovering? true)
|
||||
(let [submenu-node (mf/ref-val submenu-ref)]
|
||||
(when (some? submenu-node)
|
||||
(dom/set-css-property! submenu-node "display" "block")))
|
||||
(let [submenu-node (mf/ref-val submenu-ref)
|
||||
entry-node (mf/ref-val entry-ref)]
|
||||
;; Close at once the submenus of the sibling entries, which
|
||||
;; would otherwise stay open for the delay of their leave
|
||||
(when (some? entry-node)
|
||||
(doseq [node (array-seq (.from js/Array (dom/query-all (dom/get-parent entry-node) ":scope > li > ul")))]
|
||||
(when-not (identical? node submenu-node)
|
||||
(dom/set-css-property! node "display" "none"))))
|
||||
(when (and (some? submenu-node) (some? entry-node))
|
||||
;; Fixed to the window from the entry's position: an
|
||||
;; absolute submenu would be clipped by its parent
|
||||
;; submenu, which scrolls, when menus nest more than
|
||||
;; twice
|
||||
(dom/set-css-property! submenu-node "display" "block")
|
||||
(let [{:keys [left top]}
|
||||
(submenu-position
|
||||
{:entry (dom/get-bounding-rect entry-node)
|
||||
;; Beside the menu of the entry, not its entry, which
|
||||
;; sits inside the menu's padding and border
|
||||
:menu (dom/get-bounding-rect (dom/get-parent entry-node))
|
||||
:submenu (dom/get-bounding-rect submenu-node)
|
||||
:window (dom/get-window-size)})]
|
||||
(dom/set-css-property! submenu-node "left" (str left "px"))
|
||||
(dom/set-css-property! submenu-node "top" (str top "px")))))
|
||||
(when on-pointer-enter (on-pointer-enter))))
|
||||
|
||||
on-pointer-leave
|
||||
@ -98,9 +146,7 @@
|
||||
set-dom-node
|
||||
(mf/use-fn
|
||||
(fn [dom]
|
||||
(let [submenu-node (mf/ref-val submenu-ref)]
|
||||
(when (and (some? dom) (some? submenu-node))
|
||||
(dom/set-css-property! submenu-node "top" (str (.-offsetTop dom) "px"))))))]
|
||||
(mf/set-ref-val! entry-ref dom)))]
|
||||
|
||||
(mf/use-effect
|
||||
(mf/deps on-unmount)
|
||||
@ -146,7 +192,7 @@
|
||||
(when (> (count children) 1)
|
||||
[:ul {:class (stl/css :submenu)
|
||||
:ref submenu-ref
|
||||
:style {:display "none" :left 250}
|
||||
:style {:display "none"}
|
||||
:on-context-menu prevent-default}
|
||||
children])])))
|
||||
|
||||
|
||||
@ -32,7 +32,11 @@
|
||||
}
|
||||
|
||||
.submenu {
|
||||
position: absolute;
|
||||
// Placed from its entry when it opens, not clipped by a parent submenu
|
||||
position: fixed;
|
||||
|
||||
// Room for the `submenu-margin` (8px) kept above and below it
|
||||
max-block-size: calc(100vh - px2rem(16));
|
||||
}
|
||||
|
||||
.menu-item {
|
||||
|
||||
@ -175,6 +175,7 @@
|
||||
[frontend-tests.ui.comment-input-ime-test]
|
||||
[frontend-tests.ui.comments-clustering-test]
|
||||
[frontend-tests.ui.comments-position-modifier-test]
|
||||
[frontend-tests.ui.context-menu-submenu-test]
|
||||
[frontend-tests.ui.dashboard-import-linking-test]
|
||||
[frontend-tests.ui.drawarea-test]
|
||||
[frontend-tests.ui.ds-controls-numeric-input-test]
|
||||
@ -419,6 +420,7 @@
|
||||
'frontend-tests.ui.comment-input-ime-test
|
||||
'frontend-tests.ui.comments-clustering-test
|
||||
'frontend-tests.ui.comments-position-modifier-test
|
||||
'frontend-tests.ui.context-menu-submenu-test
|
||||
'frontend-tests.ui.dashboard-import-linking-test
|
||||
'frontend-tests.ui.drawarea-test
|
||||
'frontend-tests.ui.ds-controls-numeric-input-test
|
||||
|
||||
@ -0,0 +1,50 @@
|
||||
;; 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.context-menu-submenu-test
|
||||
(:require
|
||||
[app.main.ui.workspace.context-menu :as ctx-menu]
|
||||
[cljs.test :as t :include-macros true]))
|
||||
|
||||
;; A 240px wide menu, its submenus 240 x 300, in a 1200 x 800 window.
|
||||
;; Submenus keep 10px from their menu and 8px from the window edges.
|
||||
|
||||
(defn- position
|
||||
[& {:keys [menu-left entry-top submenu-height window-width window-height]
|
||||
:or {menu-left 100 entry-top 200 submenu-height 300
|
||||
window-width 1200 window-height 800}}]
|
||||
(ctx-menu/submenu-position
|
||||
{:menu {:left menu-left :right (+ menu-left 240)}
|
||||
:entry {:top entry-top}
|
||||
:submenu {:width 240 :height submenu-height}
|
||||
:window {:width window-width :height window-height}}))
|
||||
|
||||
(t/deftest submenu-on-the-right-of-its-menu
|
||||
(t/is (= {:left 350 :top 200} (position))
|
||||
"beside the menu, at the height of its entry"))
|
||||
|
||||
(t/deftest submenu-touching-the-right-edge-stays-on-the-right
|
||||
;; 700 + 240 + 10 + 240 = 1190 <= 1200
|
||||
(t/is (= {:left 950 :top 200} (position :menu-left 700))))
|
||||
|
||||
(t/deftest submenu-on-the-left-when-it-would-overflow-the-window
|
||||
(t/is (= {:left 650 :top 200} (position :menu-left 900))
|
||||
"beside the left side of the menu")
|
||||
(t/is (= {:left 1150 :top 200} (position :menu-left 900 :window-width 1700))
|
||||
"back on the right in a wider window"))
|
||||
|
||||
(t/deftest submenu-never-past-the-left-edge-of-the-window
|
||||
(t/is (= 0 (:left (position :menu-left 100 :window-width 500)))
|
||||
"no room on either side"))
|
||||
|
||||
(t/deftest submenu-moved-up-to-stay-inside-the-window
|
||||
(t/is (= 492 (:top (position :entry-top 700)))
|
||||
"8px above the bottom: 800 - 300 - 8")
|
||||
(t/is (= 700 (:top (position :entry-top 700 :window-height 1200)))
|
||||
"at the entry in a taller window"))
|
||||
|
||||
(t/deftest submenu-taller-than-the-window-starts-at-its-top
|
||||
(t/is (= 8 (:top (position :entry-top 700 :submenu-height 900)))))
|
||||
Loading…
x
Reference in New Issue
Block a user