Skip to content
This repository was archived by the owner on Jan 22, 2021. It is now read-only.

Added on argument with hover and click value. #12

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ The `dropdown` helper takes additional arguments for positioning and custom clas
- `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.
- `timeout` - Defaults is `0`. Set a number to have time to move the mouse from the `dropdownTrigger` on the `dropdown`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is an option for timeout really necessary to include in this package?

Copy link
Author

Choose a reason for hiding this comment

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

What have you decided with options?

Copy link
Author

Choose a reason for hiding this comment

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

Move to the dropdown, or remove from its default object and leave only in dropdownTrigger?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can't timeout be a hard coded constant somewhere, instead of an option?


```html
{{#dropdownTrigger name="testDropdown3"}}
Expand Down
46 changes: 37 additions & 9 deletions lib/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ const createDropdown = (key, opts = {}) => {
x: 0,
y: 0,
top: 0,
left: 0
left: 0,
on: 'click',
timeout: 0
};

const toIntOr = (val, org) => {
Expand Down Expand Up @@ -179,8 +181,9 @@ const factory = () => {
const removeAll = () =>
allKeys().forEach(remove);

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

Choose a reason for hiding this comment

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

Watch the whitespace here.

}

return {
all,
Expand Down Expand Up @@ -233,7 +236,9 @@ Template.dropdown.onCreated(function() {
'left',
'direction',
'persistent',
'animation'
'animation',
'on',
'timeout'
);

return Dropdowns.create(this.data.name, opts);
Expand Down Expand Up @@ -278,6 +283,8 @@ Template.dropdown.helpers({
attrs['data-dropdown-left'] = dropdown.left;
attrs['data-dropdown-align'] = dropdown.align;
attrs['data-dropdown-direction'] = dropdown.direction;
attrs['data-dropdown-on'] = dropdown.on;
attrs['data-dropdown-timeout'] = dropdown.timeout;
return attrs;
}

Expand Down Expand Up @@ -315,8 +322,13 @@ const positionDropdown = (key, reference) => {
}

const $dropdown = dropdown.element();
const $el = $(reference);


if (typeof(reference) === 'object' && reference instanceof Blaze.TemplateInstance)
var $el = $(reference.find('>'));
else
var $el = $(reference);


if ($dropdown.length === 0) {
console.error(`Dropdowns: Couldn't find a dropdown: ${key}`);
return;
Expand Down Expand Up @@ -376,12 +388,15 @@ const positionDropdown = (key, reference) => {
};
};

Dropdowns._timeout;
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually no need to define this here.

Copy link
Author

Choose a reason for hiding this comment

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

Rewrite it to?

var timeout;

Copy link
Contributor

Choose a reason for hiding this comment

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

No need to declare it – you'll attach it further down, at this line anyway, and JS/Meteor doesn't care if the parameter passed to Meteor.clearTimeout is undefined.

Copy link
Author

Choose a reason for hiding this comment

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

I try. It does not work.


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

Meteor.clearTimeout(Dropdowns._timeout);
Dropdowns.hideAllBut(name);
Dropdowns.toggle(name);
Dropdowns.usePosition(name, tmpl);
Expand All @@ -391,6 +406,7 @@ Template.dropdownTrigger.events({
evt.preventDefault();
const name = tmpl.data.name;

Meteor.clearTimeout(Dropdowns._timeout);
Dropdowns.hideAllBut(name);
Dropdowns.show(name);
Dropdowns.usePosition(name, tmpl);
Expand All @@ -399,8 +415,20 @@ Template.dropdownTrigger.events({
if (tmpl.data.on != 'hover') return;
evt.preventDefault();
const name = tmpl.data.name;

Dropdowns.hide(name);

Meteor.clearTimeout(Dropdowns._timeout);
Dropdowns._timeout = Meteor.setTimeout(() => {
Dropdowns.hide(name);
}, tmpl.data.timeout);

Dropdowns.get(name).element().hover(function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Mind the whitespace here.

Copy link
Author

Choose a reason for hiding this comment

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

How? Please example.

Copy link
Contributor

Choose a reason for hiding this comment

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

What's the reasoning for this whole hover() block?

Copy link
Contributor

Choose a reason for hiding this comment

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

The block that starts with Dropdowns.get(name) is indented a few tabs for me.

Meteor.clearTimeout(Dropdowns._timeout);
}, function() {
Meteor.clearTimeout(Dropdowns._timeout);
Dropdowns._timeout = Meteor.setTimeout(function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please stick to ES2015 arrow functions when possible, thanks :)

Dropdowns.hide(name);
}, tmpl.data.timeout);
})
}
});

Expand Down
20 changes: 20 additions & 0 deletions test-app/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,26 @@ <h2 id="arguments">Additional arguments</h2>
{{/dropdown}}
</div>

<div class="test-area dropdown-container">
{{#dropdownTrigger name="testDropdownHover" on="hover"}}
<button>Hover</button>
{{/dropdownTrigger}}

{{#dropdown name="testDropdownHover" direction="n" top="-10"}}
<p>Trigger responds to hover event <code>on="hover"</code>.</p>
{{/dropdown}}
</div>

<div class="test-area dropdown-container">
{{#dropdownTrigger name="testDropdownHoverTimeout" on="hover" timeout=500}}
<button>Hover with timeout</button>
{{/dropdownTrigger}}

{{#dropdown name="testDropdownHoverTimeout" direction="n" top="-10"}}
<p>You can move your mouse to the dropdown with <code>on="hover" timeout=500</code>.</p>
{{/dropdown}}
</div>

<pre><code>&#123;&#123;#dropdownTrigger name="testDropdown3"&#125;&#125;
&lt;button&gt;Custom dropdown&lt;/button&gt;
&#123;&#123;/dropdownTrigger&#125;&#125;
Expand Down