-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhirst-dialog-base.html
233 lines (206 loc) · 6.79 KB
/
hirst-dialog-base.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../paper-dialog/paper-dialog.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../paper-icon-button/paper-icon-button.html">
<!--
`hirst-dialog-base` renders a dialog base instance, on which other dialogs will be based.
It contains 'X' button out of the box.
### Example
```html
<hirst-dialog-base
title="Congratulations">
It works!
</hirst-dialog-base>
```
### Styling
The following custom properties and mixins are available for styling:
Custom property | Description | Default
----------------|-------------|----------
`--hirst-dialog` | Mixin applied to the dialog | `{}`
`--hirst-dialog-color` | Color of the dialog | `Based on the button's color`
`--hirst-dialog-max-width` | Max width of the dialog | `800px`
`--hirst-dialog-max-height` | Max height of the dialog | `600px`
`--hirst-dialog-title` | Mixin applied to the dialog title | `{}`
`--hirst-dialog-title-color` | Color of the dialog title | ``
`--hirst-dialog-close` | Mixin applied to the dialog close button | `{}`
`--hirst-dialog-close-color` | Color of the dialog close button | ``
`--hirst-dialog-close-size` | Size of the dialog close button | `32px`
`--hirst-dialog-buttons` | Mixin applied to the dialog buttons container | `{}`
`--hirst-dialog-button` | Mixin applied to the dialog button | `{}`
`--hirst-dialog-button-color` | Color of the dialog button | `--hirst-dialog-color`
`--hirst-dialog-button-width` | Width of the dialog button | `45%`
@demo demo/index.html
-->
<dom-module id="hirst-dialog-base">
<template>
<style>
paper-dialog {
position: fixed;
top: 50%;
left: 50%;
margin: 0px;
transform: translate(-50%, -50%);
--paper-dialog: {
width: 100vw;
height: 100vh;
overflow: hidden;
padding: 0px;
@apply --hirst-dialog;
color: var(--hirst-dialog-color);
max-width: var(--hirst-dialog-max-width, 800px);
max-height: var(--hirst-dialog-max-height, 600px);
};
--paper-dialog-title: {
font-weight: bold;
@apply --hirst-dialog-title;
color: var(--hirst-dialog-title-color);
};
}
paper-icon-button[close] {
position: absolute;
right: 16px;
top: 16px;
margin: 0;
padding: 0;
@apply --hirst-dialog-close;
color: var(--hirst-dialog-close-color);
width: var(--hirst-dialog-close-size, 32px);
height: var(--hirst-dialog-close-size, 32px);
}
</style>
<paper-dialog id="dialog" opened="{{opened}}" with-backdrop>
<h2 hidden$="[[!title]]">[[title]]</h2>
<p>
<paper-icon-button close icon="icons:close" on-click="close"></paper-icon-button>
<slot></slot>
</p>
</paper-dialog>
</template>
<script>
/**
* `hirst-dialog-base`
* hirst-dialog-base
*
* @customElement
* @polymer
* @demo demo/index.html
*/
class HirstDialogBase extends Polymer.Element {
static get is() {
return 'hirst-dialog-base';
}
static get properties() {
return {
/**
* Title of the dialog
*/
title: {
type: String,
value: null
},
/**
* True if dialog is opened
*/
opened: {
type: Boolean,
notify: true
},
/**
* Parent element, where the dialog is attached to, body by default
*/
attach: {
type: String,
value: null,
reflectToAttribute: true
}
};
}
/**
* Open the dialog
*
* @handle {Object} optional dialog handle, typically given as the parent element
* of this element in order to attach the proper instance to the DOM tree
*/
open(handle) {
// Determine where the dialog is attached to
var attachTo = null;
if (this.attach) {
attachTo = document.querySelector(this.attach);
}
if (!attachTo) {
attachTo = document.body;
}
// Attach dialog, to ensure it's on top of all existing overlays
Polymer.dom(attachTo).appendChild(handle ? handle : this);
this._applyPatch();
// Wait until dialog is added to the DOM (required for Safari)
setTimeout(function() {
this.$.dialog.open();
}.bind(this), 1);
}
/**
* Close the dialog
*/
close() {
this._restorePatch();
this.$.dialog.close();
}
/**
* Apply patch before dialog opened
*/
_applyPatch() {
if (this._patchDisabled() || this._patchApplied) {
return;
}
this._patchApplied = true;
if (this._isIOS11()) {
this._bodyPosition = document.body.style.position;
this._bodyLeft = document.body.style.left;
this._bodyTop = document.body.style.top;
this._bodyWidth = document.body.style.width;
this._bodyHeight = document.body.style.height;
this._bodyOverflow = document.body.style.overflow;
this._docScrollX = window.scrollX;
this._docScrollY = window.scrollY;
document.body.style.position = 'fixed';
document.body.style.left = -this._docScrollX + 'px';
document.body.style.top = -this._docScrollY + 'px';
document.body.style.width = document.body.style.width ? document.body.style.width : '100%';
document.body.style.height = '100%';
document.body.style.overflow = 'hidden';
}
}
/**
* Restore patch after dialog closed
*/
_restorePatch() {
if (this._patchDisabled() || !this._patchApplied) {
return;
}
this._patchApplied = false;
if (this._isIOS11()) {
document.body.style.position = this._bodyPosition;
document.body.style.left = this._bodyLeft;
document.body.style.top = this._bodyTop;
document.body.style.width = this._bodyWidth;
document.body.style.height = this._bodyHeight;
document.body.style.overflow = this._bodyOverflow;
window.scrollTo(this._docScrollX, this._docScrollY);
}
}
/**
* Check if dialog patch is disabled
*/
_patchDisabled() {
return document.body.getAttribute('hirst-dialog-patch') === 'disabled';
}
/**
* Patch target - Safari on iOS 11
*/
_isIOS11() {
return /iPad|iPhone|iPod/.test(navigator.userAgent) && /OS 11_/.test(navigator.userAgent);
}
}
window.customElements.define(HirstDialogBase.is, HirstDialogBase);
</script>
</dom-module>