Inspired by: https://www.smashingmagazine.com/2023/08/better-context-menus-safe-triangles/
Install the package using one of the following options:
npm install @svondervoort/safe-triangles
yarn add @svondervoort/safe-triangles
import generateSafeTriangles from "@svondervoort/safe-triangles";
// CSS
import '@svondervoort/safe-triangles/dist/css/style.css';
// SCSS
import '@svondervoort/safe-triangles/src/scss/style.scss';
data-safe-triangle-dropdown="..."
Add this to the menu-item anchor element and the dropdown it is associated with.
Initialize the logic using generateSafeTriangles();
.
The function requires either a selector <string>
or a DOM node list <NodeList>
.
Name | Type | Default | Description |
---|---|---|---|
delay | number |
100 |
The delay of which the path of the safe-triangle gets updated. A minimum of 100ms is recommended since the path follows the cursor of the user. |
debug | boolean |
false |
If set to true it will show the overlays so you can see the Safe Triangles doing their work |
// Import Javascript
import generateSafeTriangles from "@svondervoort/safe-triangles";
// Import SCSS
import '@svondervoort/safe-triangles/src/scss/style.scss';
// Create NodeList
let elements = document.querySelectorAll('.header-main-nav-l1__item-link--has-children-js, .header-main-nav-l2__item-link--has-children-js');
// Initialize with delay and debug
generateSafeTriangles(elements, { delay: 100, debug: true });
You can find a demo here with 3 variants:
- Add
left
andtop
options for dropdown direction/position.