This repository was archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaper-article.html
More file actions
171 lines (146 loc) Β· 4.88 KB
/
paper-article.html
File metadata and controls
171 lines (146 loc) Β· 4.88 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
167
168
169
170
171
<!--
Copyright (c) 2017 wasc GbR. All rights reserved.
This code may only be used under the BSD style license found at https://wasc-io.github.io/LICENSE.txt
-->
<link rel="import" href="../polymer/polymer-element.html">
<!--
Simple paper Article element. This element is useful for seperating your content visually with color.
The element is able to display every material style color as background.
This example creates a contrastet article with a blue-600 background:
<paper-article contrast color="blue-600">Basic Article</paper-article>
@demo demo/index.html
@polymer
@customElement
-->
<dom-module id="paper-article">
<template>
<style>
:host {
display: block;
}
:host([contrast]) {
background-color: var(--contrast-background-color, #90A4AE);
color: var(--contrast-color, #424242);
}
@media only screen and (min-width: 960px) {
.article {
padding: 10px 163px;
margin: 0;
}
:host([slim]) .article {
padding: 10px 273px;
margin: 0;
}
}
@media only screen and (min-width: 600px) and (max-width: 960px) {
.article {
padding: 10px 63px;
margin: 0;
}
:host([slim]) .article {
padding: 10px 103px;
margin: 0;
}
}
@media only screen and (max-width: 600px) {
.article {
padding: 10px 23px;
margin: 0;
}
}
</style>
<div class="article">
<section>
<slot></slot>
</section>
</div>
</template>
<script>
/** @polymerElement */
class PaperArticle extends Polymer.Element {
static get is() {
return 'paper-article';
}
static get properties() {
return {
/** Wether or not the Element should inherit the background or set a custom one. */
contrast: {
type: Boolean,
observer: '_colorChanged',
value: false,
},
/** The Background-Color of the Element. */
color: {
type: String,
observer: '_colorChanged',
value: 'grey-300',
},
/** The Text-Color of the Element's content. */
textColor: {
type: String,
observer: '_colorChanged',
value: 'grey-200',
},
};
}
_colorChanged(newValue, oldValue) {
// Dirty check change
if (newValue === oldValue) {
return;
}
// Check if value is hex value
if (this.color.includes('#')) {
// Set background color to value
const backgroundColor = this.color;
let color = '';
// strip hash from string
const colorBuffer = this.color.substring(1);
// convert rrggbb to decimal
const rgb = parseInt(colorBuffer, 16);
// extract RGB
const r = (rgb >> 16) & 0xff;
const g = (rgb >> 8) & 0xff;
const b = (rgb >> 0) & 0xff;
// calculate luma
const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b; // per ITU-R BT.709
// if the luma is to low switch to white text
if (luma < 141) {
color = '#fff';
} else {
color = '#424242';
}
// Set color and background color vars
this.style.setProperty('--contrast-background-color', backgroundColor);
this.style.setProperty('--contrast-color', color);
// If the color is a CSS custom property
} else if (this.color.includes('--')) {
// Compute CSS Vars
const backgroundColor = 'var(' + this.color + ')';
const color = 'var(--paper-' + this.textColor + ')';
// Set CSS Vars
this.style.setProperty('--contrast-background-color', backgroundColor);
this.style.setProperty('--contrast-color', color);
// Fallback to paper-color
} else {
// Prepare strings
const backgroundColorBuffer = this.color.split('-')[0];
const backgroundColorGradeBuffer = this.color.split('-')[1];
let colorBuffer;
// Switch text-color to white or black depending on background
if (backgroundColorBuffer == 'grey' || backgroundColorBuffer == 'yellow') {
colorBuffer = 'grey-800';
} else {
colorBuffer = 'grey-200';
}
// Compute CSS Vars
const backgroundColor = 'var(--paper-' + backgroundColorBuffer + '-' + backgroundColorGradeBuffer + ')';
const color = 'var(--paper-' + colorBuffer + ')';
// Set CSS Vars
this.style.setProperty('--contrast-background-color', backgroundColor);
this.style.setProperty('--contrast-color', color);
}
}
}
window.customElements.define(PaperArticle.is, PaperArticle);
</script>
</dom-module>