Skip to content

Instantly share code, notes, and snippets.

@msuchodolski
Last active July 19, 2023 13:08
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save msuchodolski/41c9338f3a732379dc570142826ed76e to your computer and use it in GitHub Desktop.
Save msuchodolski/41c9338f3a732379dc570142826ed76e to your computer and use it in GitHub Desktop.
Disable dragging and selecting html elements (like links, images etc...) in Electron
/**
* MAKE ELECTRON APP FEEL MORE NATIVE
*
* * Prevent dragging all HTML elements, specially:
* - images
* - links (anchors)
*
* * Prevent text selection
*/
*, *::after, *::before {
-webkit-user-select: none;
-webkit-user-drag: none;
-webkit-app-region: no-drag;
cursor: default;
}
@brunolaranjeira
Copy link

That was very useful. Thank you for publishing it.

@giorgioprovenzale
Copy link

I am working on an Electron app with inside ReactJS, I tried to add these rules but I can still drag images from my OS to the app. If I do it the app becomes all black or shows the image.
Do you have some suggestions to deny the drag and drop on Electron at all?

@pjebs
Copy link

pjebs commented Apr 25, 2019

@yanlee26
Copy link

great!

@yanlee26
Copy link

@javedbaloch4
Copy link

Helpful, Thank you.

@julioflima
Copy link

That cursor: default; was not so useful. All rest is aewsome. Thank you!

@cheack
Copy link

cheack commented May 21, 2020

Thanks!

@foxt
Copy link

foxt commented Jun 20, 2020

https://github.com/ecrmnn/electron-disable-file-drop

or if you don't want to include yet another npm module, paste this into your html

<script>document.addEventListener('dragover', event => event.preventDefault());document.addEventListener('drop', event => event.preventDefault());</script>

@brandoncc
Copy link

Thank you @msuchodolski and @theLMGN!

@cdbst
Copy link

cdbst commented Feb 26, 2022

great solution. thx

@NeurAlch
Copy link

NeurAlch commented Sep 6, 2022

Since I do want some text-selection and different cursors, I used only:

*, *::after, *::before {
	-webkit-user-drag: none;
	-webkit-app-region: no-drag;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment