-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.html
231 lines (192 loc) · 7.75 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Color Picker ✨ A Javascript Component</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🎨</text></svg>">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="keywords" content="color, colors, picker, javascript, component, lightweight, web">
<meta name="description" content="Lightweight javascript color picker component for the web, small & easy to use">
<meta name="author" content="Yair Even Or">
<link rel="stylesheet" href="./dist/styles.css" media="print" onload="this.media='all'">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@300&display=swap" rel="stylesheet">
<style>
body{
display: flex;
flex-flow: column;
gap: 5vh;
justify-content: center;
align-items: stretch;
text-align: center;
height: 100vh;
max-height: 100vh;
margin: 0;
background: repeating-conic-gradient(#F1F1F1 0% 25%, transparent 0% 50%) 0 / 25px 25px;
}
h1{
font: calc(2.5em + 1vw)/1 'Quicksand', sans-serif;
margin: 0;
}
p{
font-family: 'Quicksand', sans-serif;
font-size: calc(1.5em + .5vw);
margin: -3vh 0 0;
}
a.button{
align-self: center;
font-family: 'Roboto', sans-serif;
font-size: 14px;
background: #333;
color: white;
border-radius: 20px;
padding: .5em 1.2em;
text-decoration: none;
}
.colors{
display: flex;
justify-content: center;
gap: 6%;
}
@supports (display: grid) {
.myColor{
background: var(--color, #000);
border: none;
border-radius: 50%;
width: 50px;
height: 50px;
outline: none;
font-size: 0;
cursor: pointer;
box-shadow: 0 0 0 0 var(--color);
transition: .25s cubic-bezier(.35,0,0,1.6);
}
.myColor:hover{
box-shadow: 0 0 0 5px var(--color);
}
}
.color-picker[positioned]{
--x: calc(var(--pos-left) + var(--window-scroll-x));
--y: calc(var(--pos-top) + var(--window-scroll-y));
position: absolute;
z-index: 999999;
border-radius: 10px;
padding: .5em;
box-shadow: 0 5px 20px #00000044;
backdrop-filter: blur(3px);
background: white;
top: 0;
left: 0;
animation: .15s reveal-picker ease-out forwards;
transform: translate(calc(var(--x) * 1px),
calc(var(--y) * 1px));
@media only screen and (max-device-width : 640px) {
max-width: 70%;
}
}
.color-picker[data-placement~=left]{ --left: 15px; }
.color-picker[data-placement~=right]{ --left: -15px; }
.color-picker[data-placement~=below]{ --top: -15px; }
.color-picker[data-placement~=above]{ --top: 15px; }
@keyframes reveal-picker {
from { opacity: 0; top: var(--top, 0); left: var(--left, 0); }
}
</style>
</head>
<body>
<a class='button' href='https://github.com/yairEO/color-picker'>GITHUB</a>
<h1>Color Picker</h1>
<p>
Minimal, <span title='Compressed'>5KB</span> Color-Picker in Vanila-js
</p>
<div class='colors'>
<input class='myColor' inputmode='none' value='gold' style='--color:gold' data-swatches='false' />
<input class='myColor' inputmode='none' value='HSLA(244, 64%, 62%, 100%)' style='--color:HSLA(244, 64%, 62%, 100%)' data-swatches='["LightCoral", "MediumSeaGreen", "#3680FA99"]' data-placement='center below'/>
<input class='myColor' inputmode='none' value='RGBA(255,0,170,.30' style='--color:rgba(255,0,170,.3)' data-swatches='["rgba(50,250,80,.5)"]' data-placement='right center' />
</div>
<script src='https://cdn.jsdelivr.net/npm/@yaireo/position'></script>
<!-- <script src='./dist/color-picker.iife.js'></script> -->
<script type='module'>
import ColorPicker, {any_to_hex, changeColorFormat} from './dist/color-picker.es.js'
// because "@yaireo/position" is used (in this demo) as a script file and not an node module (ES export)
position = position.default;
// sample input "value" changes to reflect any change back to the color-picker component
const VALUE_CHANGE_SAMPLING_DURATION = 1000;
// get the ColorPicker & some helper functions for color format transformations
// const { default:ColorPicker, any_to_hex, changeColorFormat } = window.colorPicker
// let the delayed (non-blocking) CSS a chance to load first
setTimeout(init, 200)
function init(){
//
const colorInputs = document.querySelectorAll('.myColor')
// iterate all color inputs and instantiate new ColorPicker instances (for each one)
colorInputs.forEach(colorInput => {
const observerCallback = (entries) => {
!cPicker.DOM.scope.classList.contains('hidden') &&
position({ target:cPicker.DOM.scope, ref:colorInput, placement:colorInput.dataset.placement || 'center above', offset:[20] });
}
const resizeObserver = new ResizeObserver(observerCallback)
const intersectionObserver = new IntersectionObserver(observerCallback, {root:document, threshold:1});
// instanciate the color-picker
const cPicker = new ColorPicker({
color: colorInput.value, // accepts formats: HEX(A), RGB(A), HSL(A)
defaultFormat: 'hsla',
swatches: colorInput.dataset.swatches == 'false'
? false
: JSON.parse(colorInput.dataset.swatches),
// enable saving colors locally to also be shared across other color-picker intances
swatchesLocalStorage: true,
// when clicking anywhere that is not within the color picker.
// use special logic if clicked on the color-input which is
// assosiacated with this specific picker
onClickOutside(e){
let showPicker = false,
isTargetColorInput = e.target == colorInput;
const pickerElem = cPicker.DOM.scope;
if( isTargetColorInput ) showPicker = true
if( e.key == 'Escape' ) showPicker = false
// remove the color-picker from the DOM
if( showPicker )
showColorPicker(pickerElem)
else
hideColorPicker(pickerElem);
isTargetColorInput && observerCallback()
},
onInput(c){
colorInput.value = c;
colorInput.style.setProperty('--color', c)
},
// onChange: console.log
})
cPicker.DOM.scope.setAttribute('positioned', true)
// document.body.appendChild(cPicker.DOM.scope)
resizeObserver.observe(document.body)
intersectionObserver.observe(cPicker.DOM.scope)
observerCallback()
// assign a custom property to color-input element
// which points to the corresponding color-picker instance
colorInput._colorPicker = cPicker
// sample input "value" changes
setInterval(() => {
const currentValue = any_to_hex(colorInput.value),
pickerValue = any_to_hex(cPicker.CSSColor)
if(pickerValue != currentValue) {
cPicker.setColor(currentValue)
}
}, VALUE_CHANGE_SAMPLING_DURATION);
})
function showColorPicker(pickerElem) {
// if picker isn't already in the DOM:
if( !document.body.contains(pickerElem) )
document.body.appendChild(pickerElem); // inject to DOM
}
function hideColorPicker(pickerElem) {
pickerElem.remove();
}
// setTimeout(() => {
// document.querySelector('.myColor')._colorPicker.setColor( 'red' )
// }, 2000)
}
</script>
</body>
</html>