forked from lyonghwan/shibui-dropdown-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshibui-dropdown.js
More file actions
166 lines (139 loc) · 4.01 KB
/
shibui-dropdown.js
File metadata and controls
166 lines (139 loc) · 4.01 KB
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
/**
`shibui-dropdown` is a simple and modern dropdown inspired by the dropdown on [Heroku](https://heroku.com).
Example:
<shibui-dropdown>
<a>Account Settings</a>
<a>Notifications</a>
<a>Sign out</a>
</shibui-dropdown>
### Styling
The following custom properties and mixins are also available for styling:
Custom property | Description | Default
----------------|-------------|----------
`--shibui-dropdown-color` | A property that controls the color of the dropdown items | `black`
`--shibui-dropdown-content` | A mixin that is applied to the dropdown | `{}`
@demo demo/shibui-dropdown.html
*/
/*
FIXME(polymer-modulizer): the above comments were extracted
from HTML and may be out of place here. Review them and
then delete this comment!
*/
import {Polymer} from '@polymer/polymer/polymer-legacy.js';
import {afterNextRender} from '@polymer/polymer/lib/utils/render-status.js';
import '@webcomponents/webcomponentsjs/webcomponents-loader.js';
import '@polymer/iron-flex-layout/iron-flex-layout.js';
const $_documentContainer = document.createElement('template');
$_documentContainer.setAttribute('style', 'display: none;');
$_documentContainer.innerHTML = `<dom-module id="shibui-dropdown">
<template strip-whitespace="">
<style>
:host {
background: var(--shibui-dropdown-background, white);
z-index: var(--shibui-dropdown-zindex, 9999);
display: inline-block;
font-size: 14px;
position: fixed;
width: auto;
border-radius: 3px;
box-shadow: 0 3px 20px rgba(89, 105, 129, 0.3), 0 1px 2px rgba(0, 0, 0, 0.05), 0 0 0 1px rgba(89, 105, 129, .1);
white-space: nowrap;
overflow: hidden;
opacity: 0;
pointer-events: none;
transition: ease 0.1s;
transition-property: transform, opacity;
transform: translateY(-10px);
@apply --layout-vertical;
@apply --layout-center-center;
@apply --shibui-dropdown-content;
}
:host([opened]) {
opacity: 1;
transform: translateY(0);
pointer-events: all;
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
:host([alignment="left"]) {
right: 0;
}
:host ::slotted(*) {
color: var(--shibui-dropdown-color, black);
position: relative;
box-sizing: border-box;
width: 100%;
padding: 10px;
text-decoration: none;
cursor: pointer;
border-bottom: 1px solid #EEF1F6;
}
:host ::slotted(*:hover) {
background-color: #F7F8FB;
}
</style>
<slot id="content"></slot>
</template>
</dom-module>`;
document.head.appendChild($_documentContainer.content);
Polymer({
is: 'shibui-dropdown',
properties: {
/**
* Determines whether the dropdown is opened or closed
*/
opened: {
type: Boolean,
value: false,
notify: true,
reflectToAttribute: true,
observer: '_openedChanged'
},
/**
* Whether to align the dropdown to the 'right' or 'left' of its relative parent
*/
alignment: {
type: String,
value: 'right',
reflectToAttribute: true
}
},
attached: function() {
this.toggle = this.toggle.bind(this);
this.open = this.open.bind(this);
this.close = this.close.bind(this);
console.log("test")
},
detached: function() {
document.removeEventListener('click', this.close);
},
/**
* Toggles the opened state of the dropdown
*/
toggle: function() {
this.set('opened', !this.opened);
},
/**
* Opens the dropdown
*/
open: function() {
this.set('opened', true);
},
/**
* Closes the dropdown
*/
close: function() {
this.set('opened', false);
},
_openedChanged: function(opened) {
if (opened) {
afterNextRender(this, function() {
document.addEventListener('click', this.close);
});
} else {
document.removeEventListener('click', this.close);
}
}
});