Skip to content

Commit 9d46fea

Browse files
committed
Add option to force position without regard to viewport
1 parent f27d21d commit 9d46fea

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ Pikaday has many useful options:
7878
* `trigger` use a different element to trigger opening the datepicker, see [trigger example][] (default to `field`)
7979
* `bound` automatically show/hide the datepicker on `field` focus (default `true` if `field` is set)
8080
* `position` preferred position of the datepicker relative to the form field, e.g.: `top right`, `bottom right` **Note:** automatic adjustment may occur to avoid datepicker from being displayed outside the viewport, see [positions example][] (default to 'bottom left')
81+
* `reposition` can be set to false to not reposition datepicker within the viewport, forcing it to take the configured `position` (default: true)
8182
* `container` DOM node to render calendar into, see [container example][] (default: undefined)
8283
* `format` the default output format for `.toString()` and `field` value (requires [Moment.js][moment] for custom formatting)
8384
* `defaultDate` the initial date to view when first opened

examples/positions.html

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
6-
<title>Pikaday alignement example</title>
6+
<title>Pikaday alignment example</title>
77
<meta name="author" content="Maxime Thirouin">
88
<link rel="stylesheet" href="../css/pikaday.css">
99
<link rel="stylesheet" href="../css/site.css">
@@ -30,9 +30,11 @@ <h1>Pikaday alignement example</h1>
3030
<br />
3131
<input type="text" id="datepicker-bottomright" value="Bottom right (waat?)">
3232
<br />
33+
<input type="text" id="datepicker-bottomright-forced" value="forced to position outside viewport">
34+
<br />
3335
Here is the bottom right position with content before (no automatic adjustment used, , same code as above) <input type="text" id="datepicker-bottomright-forreal" value="Bottom right">
3436

35-
<p><i>Also, take a look to the bottom right of this page to see an example with position automaticaly adjusted.</i></p>
37+
<p><i>Also, take a look to the bottom right of this page to see an example with position automatically adjusted.</i></p>
3638
<input type="text" id="datepicker-auto" style="position: absolute; bottom: 0; right: 0;" value="Automatic position">
3739

3840
<h2>What is this?</h2>
@@ -81,6 +83,13 @@ <h2>What is this?</h2>
8183
position: 'bottom right'
8284
});
8385

86+
new Pikaday(
87+
{
88+
field: document.getElementById('datepicker-bottomright-forced'),
89+
position: 'bottom right',
90+
reposition: false
91+
});
92+
8493
new Pikaday(
8594
{
8695
field: document.getElementById('datepicker-bottomright-forreal'),

pikaday.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@
181181
// ('bottom' & 'left' keywords are not used, 'top' & 'right' are modifier on the bottom/left position)
182182
position: 'bottom left',
183183

184+
// automatically fit in the viewport even if it means repositioning from the position option
185+
reposition: true,
186+
184187
// the default output format for `.toString()` and `field` value
185188
format: 'YYYY-MM-DD',
186189

@@ -869,22 +872,23 @@
869872
}
870873

871874
// default position is bottom & left
872-
if (left + width > viewportWidth ||
875+
if ((this._o.reposition && left + width > viewportWidth) ||
873876
(
874877
this._o.position.indexOf('right') > -1 &&
875878
left - width + field.offsetWidth > 0
876879
)
877880
) {
878881
left = left - width + field.offsetWidth;
879882
}
880-
if (top + height > viewportHeight + scrollTop ||
883+
if ((this._o.reposition && top + height > viewportHeight + scrollTop) ||
881884
(
882885
this._o.position.indexOf('top') > -1 &&
883886
top - height - field.offsetHeight > 0
884887
)
885888
) {
886889
top = top - height - field.offsetHeight;
887890
}
891+
888892
this.el.style.cssText = [
889893
'position: absolute',
890894
'left: ' + left + 'px',

0 commit comments

Comments
 (0)