Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/js/libs/moveable.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as _ from '../utils/utils';

const clamp = v => Math.max(Math.min(v, 1), 0);
export default function Moveable(opt) {
const doc = opt.element.ownerDocument;

const that = {

Expand All @@ -18,7 +19,7 @@ export default function Moveable(opt) {

// Check to see if the Movable is focused and then move it based on arrow key inputs
// For improved accessibility
if (document.activeElement === options.wrapper) {
if (doc.activeElement === options.wrapper) {
const {lock} = that.options;
const up = key === 'ArrowUp';
const right = key === 'ArrowRight';
Expand Down Expand Up @@ -50,8 +51,8 @@ export default function Moveable(opt) {
},

_tapstart(evt) {
_.on(document, ['mouseup', 'touchend', 'touchcancel'], that._tapstop);
_.on(document, ['mousemove', 'touchmove'], that._tapmove);
_.on(doc, ['mouseup', 'touchend', 'touchcancel'], that._tapstop);
_.on(doc, ['mousemove', 'touchmove'], that._tapmove);

if (evt.cancelable) {
evt.preventDefault();
Expand Down Expand Up @@ -115,9 +116,13 @@ export default function Moveable(opt) {
},

_tapstop() {
that._unbindDrag();
that.options.onstop();
_.off(document, ['mouseup', 'touchend', 'touchcancel'], that._tapstop);
_.off(document, ['mousemove', 'touchmove'], that._tapmove);
},

_unbindDrag() {
_.off(doc, ['mouseup', 'touchend', 'touchcancel'], that._tapstop);
_.off(doc, ['mousemove', 'touchmove'], that._tapmove);
},

trigger() {
Expand All @@ -139,7 +144,8 @@ export default function Moveable(opt) {

destroy() {
const {options, _tapstart, _keyboard} = that;
_.off(document, ['keydown', 'keyup'], _keyboard);
that._unbindDrag();
_.off(doc, ['keydown', 'keyup'], _keyboard);
_.off([options.wrapper, options.element], 'mousedown', _tapstart);
_.off([options.wrapper, options.element], 'touchstart', _tapstart, {
passive: false
Expand All @@ -154,7 +160,7 @@ export default function Moveable(opt) {
passive: false
});

_.on(document, ['keydown', 'keyup'], _keyboard);
_.on(doc, ['keydown', 'keyup'], _keyboard);

return that;
}
51 changes: 32 additions & 19 deletions src/js/pickr.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ export default class Pickr {
// Will be cancelled in case of destruction.
_setupAnimationFrame = null;

_repositionAnimationFrame = null;
_repositionTimeout = null;

// UI Components
_components = null;

Expand Down Expand Up @@ -127,9 +130,9 @@ export default class Pickr {

// Initialize picker
this._preBuild();
this._finalBuild();
this._buildComponents();
this._bindEvents();
this._finalBuild();

// Append pre-defined swatch colors
if (swatches && swatches.length) {
Expand All @@ -138,7 +141,9 @@ export default class Pickr {

// Initialize positioning engine
const {button, app} = this._root;
this._nanopop = createPopper(button, app, {
this._nanopop = createPopper({
reference: button,
popper: app,
margin: padding
});

Expand Down Expand Up @@ -382,6 +387,8 @@ export default class Pickr {

_bindEvents() {
const {_root, options} = this;
const doc = _root.app.ownerDocument;
const view = doc.defaultView;

const eventBindings = [

Expand Down Expand Up @@ -445,10 +452,10 @@ export default class Pickr {
_.on(_root.button, 'click', () => this.isOpen() ? this.hide() : this.show()),

// Close with escape key
_.on(document, 'keyup', e => this.isOpen() && (e.key === ck || e.code === ck) && this.hide()),
_.on(doc, 'keyup', e => this.isOpen() && (e.key === ck || e.code === ck) && this.hide()),

// Cancel selecting if the user taps behind the color picker
_.on(document, ['touchstart', 'mousedown'], e => {
_.on(doc, ['touchstart', 'mousedown'], e => {
if (this.isOpen() && !_.eventPath(e).some(el => el === _root.app || el === _root.button)) {
this.hide();
}
Expand All @@ -465,7 +472,7 @@ export default class Pickr {
cmyk: [100, 100, 100, 100]
};

_.adjustableInputNumbers(_root.interaction.result, (o, step, index) => {
eventBindings.push(..._.adjustableInputNumbers(_root.interaction.result, (o, step, index) => {
const range = ranges[this.getColorRepresentation().toLowerCase()];

if (range) {
Expand All @@ -479,33 +486,34 @@ export default class Pickr {
}

return o;
});
}));
}

if ((options.autoReposition || options.closeOnScroll) && !options.inline) {
let timeout = null;
const that = this;

// Re-calc position on window resize, scroll and wheel
// Re-calc position on window resize and scroll
eventBindings.push(
_.on(window, ['scroll', 'resize'], () => {
_.on(view, ['scroll', 'resize'], () => {
if (that.isOpen()) {

if (options.closeOnScroll) {
that.hide();
}

if (timeout === null) {
timeout = setTimeout(() => timeout = null, 100);
if (that._repositionTimeout === null) {
that._repositionTimeout = setTimeout(() => that._repositionTimeout = null, 100);

// Update position on every frame
requestAnimationFrame(function rs() {
that._repositionAnimationFrame = requestAnimationFrame(function rs() {
that._rePositioningPicker();
(timeout !== null) && requestAnimationFrame(rs);
if (that._repositionTimeout !== null) {
that._repositionAnimationFrame = requestAnimationFrame(rs);
}
});
} else {
clearTimeout(timeout);
timeout = setTimeout(() => timeout = null, 100);
clearTimeout(that._repositionTimeout);
that._repositionTimeout = setTimeout(() => that._repositionTimeout = null, 100);
}
}
}, {capture: true})
Expand All @@ -521,16 +529,18 @@ export default class Pickr {

// No repositioning needed if inline
if (!options.inline) {
const doc = this._root.app.ownerDocument;
const view = doc.defaultView;
const success = this._nanopop.update({
container: document.body.getBoundingClientRect(),
container: doc.body.getBoundingClientRect(),
position: options.position
});

if (!success) {
const el = this._root.app;
const eb = el.getBoundingClientRect();
el.style.top = `${(window.innerHeight - eb.height) / 2}px`;
el.style.left = `${(window.innerWidth - eb.width) / 2}px`;
el.style.top = `${(view.innerHeight - eb.height) / 2}px`;
el.style.left = `${(view.innerWidth - eb.width) / 2}px`;
}
}
}
Expand Down Expand Up @@ -705,8 +715,11 @@ export default class Pickr {
*/
destroy() {

// Cancel setup-frame if set
// Cancel pending frames and positioning timeout
cancelAnimationFrame(this._setupAnimationFrame);
cancelAnimationFrame(this._repositionAnimationFrame);
clearTimeout(this._repositionTimeout);
this._repositionTimeout = null;

// Unbind events
this._eventBindings.forEach(args => _.off(...args));
Expand Down
16 changes: 11 additions & 5 deletions src/js/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ export function eventPath(evt) {
path.push(el);
}

path.push(document, window);
const doc = evt.target.ownerDocument;
path.push(doc, doc.defaultView);
return path;
}

Expand All @@ -130,7 +131,7 @@ export function eventPath(evt) {
* @returns {null|Document|Element}
*/
export function resolveElement(val) {
if (val instanceof Element) {
if (val instanceof Element || val instanceof (val?.ownerDocument?.defaultView?.Element || Element)) {
return val;
} else if (typeof val === 'string') {
return val.split(/>>/g).reduce((pv, cv, ci, a) => {
Expand All @@ -146,8 +147,10 @@ export function resolveElement(val) {
* Creates the ability to change numbers in an input field with the scroll-wheel.
* @param el
* @param mapper
* @returns {Array} Event bindings to remove on destruction.
*/
export function adjustableInputNumbers(el, mapper = v => v) {
const view = el.ownerDocument.defaultView;

function handleScroll(e) {
const inc = ([0.001, 0.01, 0.1])[Number(e.shiftKey || e.ctrlKey * 2)] * (e.deltaY < 0 ? 1 : -1);
Expand All @@ -171,10 +174,13 @@ export function adjustableInputNumbers(el, mapper = v => v) {

// Prevent default and trigger input event
e.preventDefault();
el.dispatchEvent(new Event('input'));
el.dispatchEvent(new view.Event('input'));
}

// Bind events
on(el, 'focus', () => on(window, 'wheel', handleScroll, {passive: false}));
on(el, 'blur', () => off(window, 'wheel', handleScroll));
return [
on(el, 'focus', () => on(view, 'wheel', handleScroll, {passive: false})),
on(el, 'blur', () => off(view, 'wheel', handleScroll)),
[view, 'wheel', handleScroll]
];
}
Loading