Skip to content

Commit

Permalink
Add CSS-classes indicating position
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenswieringa committed Dec 12, 2017
1 parent 7b720f6 commit 91b3e72
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -157,6 +157,17 @@ Pikaday has many useful options:
* `onDraw` callback function for when the picker draws a new month
* `keyboardInput` enable keyboard input support (default `true`)

### Styling

If the `reposition` configuration-option is enabled (default), Pikaday will apply CSS-classes to the datepicker according to how it is positioned:

* `top-aligned`
* `left-aligned`
* `right-aligned`
* `bottom-aligned`

Note that the DOM element at any time will typically have 2 CSS-classes (eg. `top-aligned right-aligned` etc).

## jQuery Plugin

The normal version of Pikaday does not require jQuery, however there is a jQuery plugin if that floats your boat (see `plugins/pikaday.jquery.js` in the repository). This version requires jQuery, naturally, and can be used like other plugins:
Expand Down
11 changes: 10 additions & 1 deletion pikaday.js
Expand Up @@ -1032,7 +1032,7 @@

adjustPosition: function()
{
var field, pEl, width, height, viewportWidth, viewportHeight, scrollTop, left, top, clientRect;
var field, pEl, width, height, viewportWidth, viewportHeight, scrollTop, left, top, clientRect, leftAligned, bottomAligned;

if (this._o.container) return;

Expand All @@ -1045,6 +1045,8 @@
viewportWidth = window.innerWidth || document.documentElement.clientWidth;
viewportHeight = window.innerHeight || document.documentElement.clientHeight;
scrollTop = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
leftAligned = true;
bottomAligned = true;

if (typeof field.getBoundingClientRect === 'function') {
clientRect = field.getBoundingClientRect();
Expand All @@ -1067,6 +1069,7 @@
)
) {
left = left - width + field.offsetWidth;
leftAligned = false;
}
if ((this._o.reposition && top + height > viewportHeight + scrollTop) ||
(
Expand All @@ -1075,10 +1078,16 @@
)
) {
top = top - height - field.offsetHeight;
bottomAligned = false;
}

this.el.style.left = left + 'px';
this.el.style.top = top + 'px';

addClass(this.el, leftAligned ? 'left-aligned' : 'right-aligned');
addClass(this.el, bottomAligned ? 'bottom-aligned' : 'top-aligned');
removeClass(this.el, !leftAligned ? 'left-aligned' : 'right-aligned');
removeClass(this.el, !bottomAligned ? 'bottom-aligned' : 'top-aligned');
},

/**
Expand Down

0 comments on commit 91b3e72

Please sign in to comment.