Skip to content
This repository was archived by the owner on Jan 22, 2021. It is now read-only.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ The `dropdown` helper takes additional arguments for positioning and custom clas
- `classes` - Additional class names for the dropdown. None as default.
- `direction` - One of `n`, `s`, `e` or `w`. Where to position the dropdown around the element. Defaults to `s`.
- `persistent` - Defaults to `false`. Set to `true` if you want the dropdown *not* to hide when clicking outside it (on `document`).
- `on` - Defaults to `click`. Set to `hover` for respond to the pointing of the mouse. Set `none` for disable trigger logic. You can use `usePosition` for write your own reactions logic.

```html
{{#dropdownTrigger name="testDropdown3"}}
Expand Down Expand Up @@ -275,6 +276,9 @@ Dropdowns.removeAll()
# Manually set a position of a dropdown. Both x and y are optional.
Dropdowns.setPosition('name', {x: Number, y: Number})

# Automatically calculate position based on trigger template for current dropdown.
Dropdowns.usePosition('name', dropdownTriggerTemplate)

# Hide all dropdowns except for `name` (can also be an array of names).
Dropdowns.hideAllBut('name')

Expand Down
22 changes: 21 additions & 1 deletion lib/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ const factory = () => {

const removeAll = () =>
allKeys().forEach(remove);

const usePosition = (name, tmpl) =>
Tracker.afterFlush(positionDropdown(name, tmpl.find(DROPDOWN_TRIGGER)));

return {
all,
Expand All @@ -190,6 +193,7 @@ const factory = () => {
remove,
removeAll,
setPosition,
usePosition,
create: add,
animations: {
default: DEFAULT_ANIMATION
Expand Down Expand Up @@ -374,13 +378,29 @@ const positionDropdown = (key, reference) => {

Template.dropdownTrigger.events({
click(evt, tmpl) {
if (tmpl.data.on && tmpl.data.on != 'click') return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use !== if possible.

evt.preventDefault();
const name = tmpl.data.name;

Dropdowns.hideAllBut(name);
Dropdowns.toggle(name);
Dropdowns.usePosition(name, tmpl);
},
mouseover(evt, tmpl) {
if (tmpl.data.on != 'hover') return;
evt.preventDefault();
const name = tmpl.data.name;

Tracker.afterFlush(positionDropdown(name, tmpl.find(DROPDOWN_TRIGGER)));
Dropdowns.hideAllBut(name);
Dropdowns.show(name);
Dropdowns.usePosition(name, tmpl);
},
mouseout(evt, tmpl) {
if (tmpl.data.on != 'hover') return;
evt.preventDefault();
const name = tmpl.data.name;

Dropdowns.hide(name);
}
});

Expand Down