Ultra Tiny, Opinionated Positioning Engine
NanoPop is an ultra-tiny positioning engine. But wait isn't there PopperJS? Yeah - and PopperJS is great! But there are tons of features you might not need in most cases. This library is brotlied only ~ 700 Bytes (PopperJS is around 3kB).
What are the use-cases compared to PopperJS?
- Situations where you want full controll over positioning, including handling events such as scrolling / resize manually.
- Performance-critical cases with lots of elements [...] nanopop will only makes changes if you say so.
- Poppers with minimal footprint such as drop-downs and tooltips which don't require much configurability.
- You might have some special needs about how your popper behaves. NanoPop could be used as super-class and you can, based on what's required, extend NanoPop as you will :)
This library was originally part of pickr - now ported to TS with tests and a few updates / bug-fixes.
Getting Started
Install via npm:
$ npm install nanopop
Install via yarn:
$ yarn add nanopop
Include directly via jsdelivr:
<script src="https://cdn.jsdelivr.net/npm/nanopop/lib/nanopop.min.js"></script>
Using JavaScript Modules:
import {NanoPop} from 'https://cdn.jsdelivr.net/npm/nanopop/lib/nanopop.min.mjs'
Usage
const reference = document.querySelector('.btn');
const popper = document.querySelector('.dropdown');
const nanopop = new NanoPop(reference, popper);
nanopop.update();
The popper-element must have set position
to fixed
.
All options
const nanopop = new NanoPop(reference, popper, {
container: document.documentElement.getBoundingClientRect(),
margin: 8,
position: 'bottom-start',
forceApplyOnFailure: false,
variantFlipOrder: {
start: 'sme',
middle: 'mse',
end: 'ems'
},
positionFlipOrder: {
top: 'tbrl',
right: 'rltb',
bottom: 'btrl',
left: 'lrbt'
}
});
Functions
nanopop.update(newOptions?: Partial<Options>)
- Update the position and optionally update the options of this NanoPop instance.
It'll return a position-pair (For example te
for Top-End) or null
based on if it was possible to find a position for the popper without clipping it.
Tip: The returned position-pair is perfect for tool-tips to give them a little arrow!
Properties
nanopop.version
- Current version.
These are static default-values used in case you're not specifying something else:
NanoPop.defaultVariantFlipOrder
- Default variantFlipOrder
values.NanoPop.defaultPositionFlipOrder
- Default positionFlipOrder
.
Caveats
- The popper-element must have
position
set to fixed
. window
is (currently) the only bounding-element supported.- The library does not perform any automatic updates if the window gets resized, or the user scrolls, you have to take care of that
yourself and call
update()
in the case. - You might have to fiddle around with
z-index
to make it work inside of complex, nested, scrollable containers.