Experiments >

Code animation: Placement engine

Experiment #19626th May, 2021by Joshua Nussbaum

For the context menus, we can’t assume there will always be space below the mouse cursor. That would make the menu go off-screen when the mouse is close to the bottom of the screen.

So we need some logic to determine placement.

A simple algorithm is to check where the most space is for each dimension (vertical & horizontal), and then place it there.

For example, if there is more space to the right than the left, and more space above than below, then the menu should be placed North-East of the mouse cursor.

Code

https://svelte.dev/repl/79b8bfbfd6e54176ac0b1ed7b3647998?version=3.38.2

Demo

view all experiments

Stay tuned in

Learn how to add more experimentation to your workflow