Browse Source

Disable some menu buttons when no item selected (#393)

* feat(menu): disable buttons when items not selected

* cut and copy options not shown when item isn't selected
* added cut option on ContextMenu

* Show buttons but disabled

Co-authored-by: Steve Ruiz <steveruizok@gmail.com>
main
Siddhartha Varma 3 years ago
parent
commit
8c06a2866f
No account linked to committer's email address

+ 7
- 0
packages/tldraw/src/components/ContextMenu/ContextMenu.tsx View File

@@ -93,6 +93,10 @@ export const ContextMenu = ({ onBlur, children }: ContextMenuProps): JSX.Element
93 93
     app.copyJson()
94 94
   }, [app])
95 95
 
96
+  const handleCut = React.useCallback(() => {
97
+    app.cut()
98
+  }, [app])
99
+
96 100
   const handleCopy = React.useCallback(() => {
97 101
     app.copy()
98 102
   }, [app])
@@ -177,6 +181,9 @@ export const ContextMenu = ({ onBlur, children }: ContextMenuProps): JSX.Element
177 181
                 />
178 182
               )}
179 183
               <Divider />
184
+              <CMRowButton onClick={handleCut} kbd="#X">
185
+                Cut
186
+              </CMRowButton>
180 187
               <CMRowButton onClick={handleCopy} kbd="#C">
181 188
                 Copy
182 189
               </CMRowButton>

+ 22
- 4
packages/tldraw/src/components/TopPanel/Menu/Menu.tsx View File

@@ -15,14 +15,20 @@ import { useFileSystemHandlers } from '~hooks'
15 15
 import { HeartIcon } from '~components/Primitives/icons/HeartIcon'
16 16
 import { preventEvent } from '~components/preventEvent'
17 17
 import { DiscordIcon } from '~components/Primitives/icons'
18
+import type { TDSnapshot } from '~types'
18 19
 
19 20
 interface MenuProps {
20 21
   showSponsorLink: boolean
21 22
   readOnly: boolean
22 23
 }
23 24
 
25
+const numberOfSelectedIdsSelector = (s: TDSnapshot) => {
26
+  return s.document.pageStates[s.appState.currentPageId].selectedIds.length
27
+}
28
+
24 29
 export const Menu = React.memo(function Menu({ showSponsorLink, readOnly }: MenuProps) {
25 30
   const app = useTldrawApp()
31
+  const numberOfSelectedIds = app.useStore(numberOfSelectedIdsSelector)
26 32
 
27 33
   const { onNewProject, onOpenProject, onSaveProject, onSaveProjectAs } = useFileSystemHandlers()
28 34
 
@@ -70,6 +76,8 @@ export const Menu = React.memo(function Menu({ showSponsorLink, readOnly }: Menu
70 76
 
71 77
   const showSignInOutMenu = app.callbacks.onSignIn || app.callbacks.onSignOut || showSponsorLink
72 78
 
79
+  const hasSelection = numberOfSelectedIds > 0
80
+
73 81
   return (
74 82
     <DropdownMenu.Root dir="ltr">
75 83
       <DMTriggerIcon isSponsor={showSponsorLink}>
@@ -110,20 +118,30 @@ export const Menu = React.memo(function Menu({ showSponsorLink, readOnly }: Menu
110 118
                 Redo
111 119
               </DMItem>
112 120
               <DMDivider dir="ltr" />
113
-              <DMItem onSelect={preventEvent} onClick={handleCut} kbd="#X">
121
+              <DMItem onSelect={preventEvent} disabled={!hasSelection} onClick={handleCut} kbd="#X">
114 122
                 Cut
115 123
               </DMItem>
116
-              <DMItem onSelect={preventEvent} onClick={handleCopy} kbd="#C">
124
+              <DMItem
125
+                onSelect={preventEvent}
126
+                disabled={!hasSelection}
127
+                onClick={handleCopy}
128
+                kbd="#C"
129
+              >
117 130
                 Copy
118 131
               </DMItem>
119 132
               <DMItem onSelect={preventEvent} onClick={handlePaste} kbd="#V">
120 133
                 Paste
121 134
               </DMItem>
122 135
               <DMDivider dir="ltr" />
123
-              <DMItem onSelect={preventEvent} onClick={handleCopySvg} kbd="#⇧C">
136
+              <DMItem
137
+                onSelect={preventEvent}
138
+                disabled={!hasSelection}
139
+                onClick={handleCopySvg}
140
+                kbd="#⇧C"
141
+              >
124 142
                 Copy as SVG
125 143
               </DMItem>
126
-              <DMItem onSelect={preventEvent} onClick={handleCopyJson}>
144
+              <DMItem onSelect={preventEvent} disabled={!hasSelection} onClick={handleCopyJson}>
127 145
                 Copy as JSON
128 146
               </DMItem>
129 147
               <DMDivider dir="ltr" />

Loading…
Cancel
Save