Skip to content
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

Prevent native context menu from being invoked on webviews #1736

Merged
merged 4 commits into from
May 19, 2022

Conversation

rogermparent
Copy link
Contributor

Demo in VSCode:

suppressed-contextmenu-vscode-demo.mp4

Demo in Storybook (proving it works in no-data states):

suppressed-contextmenu-storybook-demo.mp4

This PR suppresses native context menu in both Experiments Table and Plots webviews by adding a wrapper div with the prop onContextmenu={(e)=>{e.preventDefault()}} as the outermost component for both webviews.

In order to keep unnecessary wrappers to a minimum, I also refactored the Theme component's logic out into a hook which can be applied to normal divs so that we can use the same wrapper div for both purposes.

// eslint-disable-next-line sonarjs/cognitive-complexity
export const Plots = ({ state }: { state: PlotsWebviewState }) => {
const PlotsContent = ({ state }: PlotsProps) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PlotsContent has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.

@@ -12,7 +12,7 @@ type CSSVariables = DetailedHTMLProps<
HTMLDivElement
>

export const Theme: React.FC = ({ children }) => {
export const useThemeVariables = () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function useThemeVariables has 34 lines of code (exceeds 30 allowed). Consider refactoring.

Comment on lines +150 to +161
export const Plots = ({ state }: PlotsProps) => {
const variables = useThemeVariables()

return (
<div
style={variables}
onContextMenu={e => {
e.preventDefault()
}}
>
<PlotsContent state={state} />
</div>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This container refactor makes it easier to ensure all states are wrapped with click-prevention even with the early-return logic we had before. It could probably use a larger refactor, but that's not the focus of this PR.

Comment on lines +61 to +62
export const Theme: React.FC = ({ children }) => {
const variables = useThemeVariables()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept Theme for backward-compatibility, but it'd probably be best to use the hook everywhere in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think backward-compatibility is the right terminology here. If we are going to use the hook can you please follow up and remove this component. Thank you.

@rogermparent rogermparent requested a review from a team May 19, 2022 03:34
@rogermparent rogermparent self-assigned this May 19, 2022
@rogermparent rogermparent added the product PR that affects product label May 19, 2022
@mattseddon
Copy link
Contributor

Closes #1723

Copy link
Contributor

@mattseddon mattseddon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

(also tested manually)

Comment on lines +61 to +62
export const Theme: React.FC = ({ children }) => {
const variables = useThemeVariables()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think backward-compatibility is the right terminology here. If we are going to use the hook can you please follow up and remove this component. Thank you.

@rogermparent rogermparent enabled auto-merge (squash) May 19, 2022 17:07
// eslint-disable-next-line sonarjs/cognitive-complexity
export const Plots = ({ state }: { state: PlotsWebviewState }) => {
const PlotsContent = ({ state }: PlotsProps) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function PlotsContent has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.

@codeclimate
Copy link

codeclimate bot commented May 19, 2022

Code Climate has analyzed commit bf2312a and detected 2 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 2

The test coverage on the diff in this pull request is 100.0% (85% is the threshold).

This pull request will bring the total coverage in the repository to 96.7% (0.0% change).

View more on Code Climate.

@rogermparent rogermparent merged commit 253f043 into main May 19, 2022
@rogermparent rogermparent deleted the prevent-context-menu-on-webviews branch May 19, 2022 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product PR that affects product
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants