-
Notifications
You must be signed in to change notification settings - Fork 226
feat(compass-context-menu): add a headless context menu package COMPASS-9386 #6937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gagik
wants to merge
10
commits into
main
Choose a base branch
from
gagik/headless-context-menu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aacdf1d
feat(compass-context-menu): add a headless context menu package
gagik cb99706
wip
gagik da22b51
fix: add tests
gagik 78ff94c
fix: add tests and fix types
gagik a24e89c
refactor: minor stylistic changes
gagik f3869ea
fix: export types and rename MenuItem
gagik c2d9ac1
fix: use React.RefCallback
gagik 1c6ef03
refactor: use item groups instead of React elements, use wrapper, kee…
gagik 3d14d6d
fix: delete redundant context menu
gagik ee658e1
fix: remove unused dep
gagik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
ignores: | ||
- '@mongodb-js/prettier-config-compass' | ||
- '@mongodb-js/tsconfig-compass' | ||
- '@types/chai' | ||
- '@types/sinon-chai' | ||
- 'sinon' | ||
ignore-patterns: | ||
- 'dist' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.nyc-output | ||
dist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
module.exports = { | ||
root: true, | ||
extends: ['@mongodb-js/eslint-config-compass'], | ||
parserOptions: { | ||
tsconfigRootDir: __dirname, | ||
project: ['./tsconfig-lint.json'], | ||
}, | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
'use strict'; | ||
module.exports = require('@mongodb-js/mocha-config-compass/react'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{ | ||
"name": "@mongodb-js/compass-context-menu", | ||
"author": { | ||
"name": "MongoDB Inc", | ||
"email": "[email protected]" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"bugs": { | ||
"url": "https://jira.mongodb.org/projects/COMPASS/issues", | ||
"email": "[email protected]" | ||
}, | ||
"homepage": "https://github.com/mongodb-js/compass", | ||
"version": "0.0.1", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mongodb-js/compass.git" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"license": "SSPL", | ||
"main": "dist/index.js", | ||
"compass:main": "src/index.ts", | ||
"exports": { | ||
"import": "./dist/.esm-wrapper.mjs", | ||
"require": "./dist/index.js" | ||
}, | ||
"compass:exports": { | ||
".": "./src/index.ts" | ||
}, | ||
"types": "./dist/index.d.ts", | ||
"scripts": { | ||
"bootstrap": "npm run compile", | ||
"prepublishOnly": "npm run compile && compass-scripts check-exports-exist", | ||
"compile": "tsc -p tsconfig.json && gen-esm-wrapper . ./dist/.esm-wrapper.mjs", | ||
"typecheck": "tsc -p tsconfig-lint.json --noEmit", | ||
"eslint": "eslint-compass", | ||
"prettier": "prettier-compass", | ||
"lint": "npm run eslint . && npm run prettier -- --check .", | ||
"depcheck": "compass-scripts check-peer-deps && depcheck", | ||
"check": "npm run typecheck && npm run lint && npm run depcheck", | ||
"check-ci": "npm run check", | ||
"test": "mocha", | ||
"test-cov": "nyc --compact=false --produce-source-map=false -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test", | ||
"test-watch": "npm run test -- --watch", | ||
"test-ci": "npm run test-cov", | ||
"reformat": "npm run eslint . -- --fix && npm run prettier -- --write ." | ||
}, | ||
"dependencies": { | ||
"react": "^17.0.2" | ||
}, | ||
"devDependencies": { | ||
"@mongodb-js/eslint-config-compass": "^1.3.8", | ||
"@mongodb-js/mocha-config-compass": "^1.6.8", | ||
"@mongodb-js/prettier-config-compass": "^1.2.8", | ||
"@mongodb-js/testing-library-compass": "^1.3.1", | ||
"@mongodb-js/tsconfig-compass": "^1.2.8", | ||
"@types/chai": "^4.2.21", | ||
"@types/mocha": "^9.0.0", | ||
"@types/react": "^17.0.5", | ||
"@types/sinon-chai": "^3.2.5", | ||
"chai": "^4.3.6", | ||
"depcheck": "^1.4.1", | ||
"gen-esm-wrapper": "^1.1.0", | ||
"mocha": "^10.2.0", | ||
"nyc": "^15.1.0", | ||
"sinon": "^9.2.3", | ||
"typescript": "^5.0.4" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { ContextMenuItemGroup } from './types'; | ||
|
||
const CONTEXT_MENUS_SYMBOL = Symbol('context_menus'); | ||
|
||
export type EnhancedMouseEvent = MouseEvent & { | ||
[CONTEXT_MENUS_SYMBOL]?: ContextMenuItemGroup[]; | ||
}; | ||
|
||
export function getContextMenuContent( | ||
event: EnhancedMouseEvent | ||
): ContextMenuItemGroup[] { | ||
return event[CONTEXT_MENUS_SYMBOL] ?? []; | ||
} | ||
|
||
export function appendContextMenuContent( | ||
event: EnhancedMouseEvent, | ||
content: ContextMenuItemGroup | ||
) { | ||
// Initialize if not already patched | ||
if (!event[CONTEXT_MENUS_SYMBOL]) { | ||
event[CONTEXT_MENUS_SYMBOL] = []; | ||
} | ||
event[CONTEXT_MENUS_SYMBOL].push(content); | ||
} |
84 changes: 84 additions & 0 deletions
84
packages/compass-context-menu/src/context-menu-provider.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import React, { | ||
useCallback, | ||
useEffect, | ||
useState, | ||
useMemo, | ||
createContext, | ||
} from 'react'; | ||
import type { ContextMenuContext, ContextMenuState } from './types'; | ||
import type { EnhancedMouseEvent } from './context-menu-content'; | ||
import { getContextMenuContent } from './context-menu-content'; | ||
|
||
export const Context = createContext<ContextMenuContext | null>(null); | ||
|
||
export function ContextMenuProvider({ | ||
children, | ||
wrapper, | ||
}: { | ||
children: React.ReactNode; | ||
wrapper: React.ComponentType<{ | ||
menu: ContextMenuState & { close: () => void }; | ||
}>; | ||
}) { | ||
const [menu, setMenu] = useState<ContextMenuState>({ | ||
isOpen: false, | ||
itemGroups: [], | ||
position: { x: 0, y: 0 }, | ||
}); | ||
const close = useCallback(() => setMenu({ ...menu, isOpen: false }), [menu]); | ||
|
||
const handleClosingEvent = useCallback( | ||
(event: Event) => { | ||
if (!event.defaultPrevented) { | ||
setMenu({ ...menu, isOpen: false }); | ||
} | ||
}, | ||
[menu] | ||
); | ||
|
||
useEffect(() => { | ||
function handleContextMenu(event: MouseEvent) { | ||
event.preventDefault(); | ||
|
||
const itemGroups = getContextMenuContent(event as EnhancedMouseEvent); | ||
|
||
if (itemGroups.length === 0) { | ||
return; | ||
} | ||
|
||
setMenu({ | ||
isOpen: true, | ||
itemGroups, | ||
position: { | ||
// TODO: Fix handling offset while scrolling | ||
x: event.clientX, | ||
y: event.clientY, | ||
}, | ||
}); | ||
} | ||
|
||
document.addEventListener('contextmenu', handleContextMenu); | ||
window.addEventListener('resize', handleClosingEvent); | ||
|
||
return () => { | ||
document.removeEventListener('contextmenu', handleContextMenu); | ||
window.removeEventListener('resize', handleClosingEvent); | ||
}; | ||
}, [handleClosingEvent]); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
close, | ||
}), | ||
[close] | ||
); | ||
|
||
const Wrapper = wrapper ?? React.Fragment; | ||
|
||
return ( | ||
<Context.Provider value={value}> | ||
{children} | ||
<Wrapper menu={{ ...menu, close }} /> | ||
</Context.Provider> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { useContextMenu } from './use-context-menu'; | ||
export { ContextMenuProvider } from './context-menu-provider'; | ||
export type { | ||
ContextMenuItem, | ||
ContextMenuItemGroup, | ||
ContextMenuWrapperProps, | ||
} from './types'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export interface ContextMenuItemGroup { | ||
items: ContextMenuItem[]; | ||
originListener: (event: MouseEvent) => void; | ||
} | ||
|
||
export type ContextMenuState = { | ||
isOpen: boolean; | ||
itemGroups: ContextMenuItemGroup[]; | ||
position: { | ||
x: number; | ||
y: number; | ||
}; | ||
}; | ||
|
||
export type ContextMenuWrapperProps = { | ||
menu: ContextMenuState & { close: () => void }; | ||
}; | ||
|
||
export type ContextMenuContext = { | ||
close(): void; | ||
}; | ||
|
||
export type ContextMenuItem = { | ||
label: string; | ||
onAction: (event: React.KeyboardEvent | React.MouseEvent) => void; | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not too sure what this was referring to, just came from the prototype
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a reminder to check how this functions if the menu is displayed inside a view which is scrolled 🤔