cloudinary-community / photocrate

Photo library and interactive editor built with Next.js & Cloudinary
https://www.photobox.dev/
MIT License
123 stars 14 forks source link

Deselect items when hitting ESC #14

Open colbyfayock opened 3 months ago

nickytonline commented 2 months ago

Is this just for the Media Gallery component? I've got the project running locally, and adding the following does the job.

const MediaGallery = ({ resources: initialResources, tag }: MediaGalleryProps) => {
  const { assetsTag, libraryTag, creationTag, favoritesTag, gallery } = getConfig();
  const { resources, addResources } = useResources({ initialResources, tag });

  const [selected, setSelected] = useState<Array<string>>([]);
  const [creation, setCreation] = useState<Creation>();
+
+  useEffect(() => {
+    const keydownHandler = (event: KeyboardEvent) => {
+      if ( event.key === 'Escape' ) {
+        setSelected([]);
+      }
+    }
+
+    document.body.addEventListener('keydown', keydownHandler);
+
+    return () => {
+      document.body.removeEventListener('keydown', keydownHandler);
+    }
+  }, []);

   ...
}

If the functionality is required elsewhere, we could wrap the logic into a custom hook that receives the keydownHandler. You could also leverage a custom hook, like https://usehooks.com/usekeypress, but I'd opt for on keydown.

colbyfayock commented 2 months ago

yeah that looks great!!

the only other place i was kinda thinking about it was the individual media view. two ideas there, 1 if youre just viewing and hit ESC, it goes back to the main view, almost like it's a modal (but is this unexpected? feels intuitive to me) and in the same media view, if you hit Edit or really any of the menus, if you hit ESC, it would close it

the only consideration to be careful with in the last part is do we need some kind of confirmation that they'll lose changes?

wdyt? also dont feel to wrap this all in the scope of this one issue :D

nickytonline commented 2 months ago

I can create a separate issue for the individual media view as it is out of scope for this particular issue.

Pressing escape makes sense to me to go back to the main view, and I definitely agree about a confirmation before returning to the main gallery view if there are unsaved changes.

nickytonline commented 2 months ago

Can you assign this to me, @colbyfayock? Thanks! PR incoming.