Skip to content

Commit 9600612

Browse files
committed
Update dist files and bump version
1 parent d71032b commit 9600612

File tree

6 files changed

+49
-10
lines changed

6 files changed

+49
-10
lines changed

dist/coloris.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,12 @@ input.clr-color:focus {
307307
margin: 0;
308308
padding: 0;
309309
border: 0;
310+
border-radius: 50%;
311+
outline-offset: -2px;
310312
background-color: transparent;
311313
text-indent: -9999px;
312314
cursor: pointer;
315+
overflow: hidden;
313316
}
314317

315318
.clr-marker,

dist/coloris.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
(function (window, document, Math) {
88
var ctx = document.createElement('canvas').getContext('2d');
99
var currentColor = { r: 0, g: 0, b: 0, h: 0, s: 0, v: 0, a: 1 };
10-
var container, picker, colorArea, colorAreaDims, colorMarker, colorPreview, colorValue, clearButton,
11-
closeButton, hueSlider, hueMarker, alphaSlider, alphaMarker, currentEl, currentFormat, oldColor;
10+
var container, picker, colorArea, colorAreaDims, colorMarker, colorPreview, colorValue, clearButton, closeButton,
11+
hueSlider, hueMarker, alphaSlider, alphaMarker, currentEl, currentFormat, oldColor, keyboardNav;
1212

1313
// Default settings
1414
var settings = {
@@ -322,6 +322,11 @@
322322
colorValue.select();
323323
}
324324

325+
// Always focus the first element when using keyboard navigation
326+
if (keyboardNav || settings.swatchesOnly) {
327+
getFocusableElements().shift().focus();
328+
}
329+
325330
// Trigger an "open" event
326331
currentEl.dispatchEvent(new Event('open', { bubbles: true }));
327332
});
@@ -1032,20 +1037,40 @@
10321037
});
10331038

10341039
addListener(document, 'mousedown', function (event) {
1040+
keyboardNav = false;
10351041
picker.classList.remove('clr-keyboard-nav');
10361042
closePicker();
10371043
});
10381044

10391045
addListener(document, 'keydown', function (event) {
1046+
var key = event.key;
1047+
var target = event.target;
1048+
var shiftKey = event.shiftKey;
10401049
var navKeys = ['Tab', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'];
10411050

1042-
if (event.key === 'Escape') {
1051+
if (key === 'Escape') {
10431052
closePicker(true);
10441053

10451054
// Display focus rings when using the keyboard
1046-
} else if (navKeys.includes(event.key)) {
1055+
} else if (navKeys.includes(key)) {
1056+
keyboardNav = true;
10471057
picker.classList.add('clr-keyboard-nav');
10481058
}
1059+
1060+
// Trap the focus within the color picker while it's open
1061+
if (key === 'Tab' && target.matches('.clr-picker *')) {
1062+
var focusables = getFocusableElements();
1063+
var firstFocusable = focusables.shift();
1064+
var lastFocusable = focusables.pop();
1065+
1066+
if (shiftKey && target === firstFocusable) {
1067+
lastFocusable.focus();
1068+
event.preventDefault();
1069+
} else if (!shiftKey && target === lastFocusable) {
1070+
firstFocusable.focus();
1071+
event.preventDefault();
1072+
}
1073+
}
10491074
});
10501075

10511076
addListener(document, 'click', '.clr-field button', function (event) {
@@ -1077,6 +1102,17 @@
10771102
addListener(alphaSlider, 'input', setAlpha);
10781103
}
10791104

1105+
/**
1106+
* Return a list of focusable elements within the color picker.
1107+
* @return {array} The list of focusable DOM elemnts.
1108+
*/
1109+
function getFocusableElements() {
1110+
var controls = Array.from(picker.querySelectorAll('input, button'));
1111+
var focusables = controls.filter(function (node) {return !!node.offsetWidth;});
1112+
1113+
return focusables;
1114+
}
1115+
10801116
/**
10811117
* Shortcut for getElementById to optimize the minified JS.
10821118
* @param {string} id The element id.
@@ -1105,7 +1141,7 @@
11051141
});
11061142

11071143
// If the selector is not a string then it's a function
1108-
// in which case we need regular event listener
1144+
// in which case we need a regular event listener
11091145
} else {
11101146
fn = selector;
11111147
context.addEventListener(type, fn);

0 commit comments

Comments
 (0)