1 |
|
2 | 'use strict';
|
3 |
|
4 | var React = require('react');
|
5 |
|
6 | function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
7 |
|
8 | var React__default = _interopDefaultLegacy(React);
|
9 |
|
10 | function _classCallCheck(instance, Constructor) {
|
11 | if (!(instance instanceof Constructor)) {
|
12 | throw new TypeError("Cannot call a class as a function");
|
13 | }
|
14 | }
|
15 |
|
16 | function _defineProperties(target, props) {
|
17 | for (var i = 0; i < props.length; i++) {
|
18 | var descriptor = props[i];
|
19 | descriptor.enumerable = descriptor.enumerable || false;
|
20 | descriptor.configurable = true;
|
21 | if ("value" in descriptor) descriptor.writable = true;
|
22 | Object.defineProperty(target, descriptor.key, descriptor);
|
23 | }
|
24 | }
|
25 |
|
26 | function _createClass(Constructor, protoProps, staticProps) {
|
27 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
28 | if (staticProps) _defineProperties(Constructor, staticProps);
|
29 | return Constructor;
|
30 | }
|
31 |
|
32 | function _inherits(subClass, superClass) {
|
33 | if (typeof superClass !== "function" && superClass !== null) {
|
34 | throw new TypeError("Super expression must either be null or a function");
|
35 | }
|
36 |
|
37 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
38 | constructor: {
|
39 | value: subClass,
|
40 | writable: true,
|
41 | configurable: true
|
42 | }
|
43 | });
|
44 | if (superClass) _setPrototypeOf(subClass, superClass);
|
45 | }
|
46 |
|
47 | function _getPrototypeOf(o) {
|
48 | _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
49 | return o.__proto__ || Object.getPrototypeOf(o);
|
50 | };
|
51 | return _getPrototypeOf(o);
|
52 | }
|
53 |
|
54 | function _setPrototypeOf(o, p) {
|
55 | _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
56 | o.__proto__ = p;
|
57 | return o;
|
58 | };
|
59 |
|
60 | return _setPrototypeOf(o, p);
|
61 | }
|
62 |
|
63 | function _isNativeReflectConstruct() {
|
64 | if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
65 | if (Reflect.construct.sham) return false;
|
66 | if (typeof Proxy === "function") return true;
|
67 |
|
68 | try {
|
69 | Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
|
70 | return true;
|
71 | } catch (e) {
|
72 | return false;
|
73 | }
|
74 | }
|
75 |
|
76 | function _construct(Parent, args, Class) {
|
77 | if (_isNativeReflectConstruct()) {
|
78 | _construct = Reflect.construct;
|
79 | } else {
|
80 | _construct = function _construct(Parent, args, Class) {
|
81 | var a = [null];
|
82 | a.push.apply(a, args);
|
83 | var Constructor = Function.bind.apply(Parent, a);
|
84 | var instance = new Constructor();
|
85 | if (Class) _setPrototypeOf(instance, Class.prototype);
|
86 | return instance;
|
87 | };
|
88 | }
|
89 |
|
90 | return _construct.apply(null, arguments);
|
91 | }
|
92 |
|
93 | function _isNativeFunction(fn) {
|
94 | return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
95 | }
|
96 |
|
97 | function _wrapNativeSuper(Class) {
|
98 | var _cache = typeof Map === "function" ? new Map() : undefined;
|
99 |
|
100 | _wrapNativeSuper = function _wrapNativeSuper(Class) {
|
101 | if (Class === null || !_isNativeFunction(Class)) return Class;
|
102 |
|
103 | if (typeof Class !== "function") {
|
104 | throw new TypeError("Super expression must either be null or a function");
|
105 | }
|
106 |
|
107 | if (typeof _cache !== "undefined") {
|
108 | if (_cache.has(Class)) return _cache.get(Class);
|
109 |
|
110 | _cache.set(Class, Wrapper);
|
111 | }
|
112 |
|
113 | function Wrapper() {
|
114 | return _construct(Class, arguments, _getPrototypeOf(this).constructor);
|
115 | }
|
116 |
|
117 | Wrapper.prototype = Object.create(Class.prototype, {
|
118 | constructor: {
|
119 | value: Wrapper,
|
120 | enumerable: false,
|
121 | writable: true,
|
122 | configurable: true
|
123 | }
|
124 | });
|
125 | return _setPrototypeOf(Wrapper, Class);
|
126 | };
|
127 |
|
128 | return _wrapNativeSuper(Class);
|
129 | }
|
130 |
|
131 | function _assertThisInitialized(self) {
|
132 | if (self === void 0) {
|
133 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
134 | }
|
135 |
|
136 | return self;
|
137 | }
|
138 |
|
139 | function _possibleConstructorReturn(self, call) {
|
140 | if (call && (typeof call === "object" || typeof call === "function")) {
|
141 | return call;
|
142 | }
|
143 |
|
144 | return _assertThisInitialized(self);
|
145 | }
|
146 |
|
147 | function _createSuper(Derived) {
|
148 | var hasNativeReflectConstruct = _isNativeReflectConstruct();
|
149 |
|
150 | return function _createSuperInternal() {
|
151 | var Super = _getPrototypeOf(Derived),
|
152 | result;
|
153 |
|
154 | if (hasNativeReflectConstruct) {
|
155 | var NewTarget = _getPrototypeOf(this).constructor;
|
156 |
|
157 | result = Reflect.construct(Super, arguments, NewTarget);
|
158 | } else {
|
159 | result = Super.apply(this, arguments);
|
160 | }
|
161 |
|
162 | return _possibleConstructorReturn(this, result);
|
163 | };
|
164 | }
|
165 |
|
166 | var IS_BROWSER = typeof window !== 'undefined';
|
167 | var IS_ANDROID = IS_BROWSER && /(android)/i.test(navigator.userAgent);
|
168 |
|
169 | var IS_IOS = IS_BROWSER && /iPad|iPhone|iPod/.test(String(navigator.platform));
|
170 |
|
171 |
|
172 | if (!IS_BROWSER && !global.HTMLElement) {
|
173 | global.HTMLElement = function () {
|
174 | function _class() {
|
175 | _classCallCheck(this, _class);
|
176 | }
|
177 |
|
178 | return _class;
|
179 | }();
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 | var closest$1 = function () {
|
189 | var proto = typeof window === 'undefined' ? {} : window.Element.prototype;
|
190 | var match = proto.matches || proto.msMatchesSelector || proto.webkitMatchesSelector;
|
191 | return proto.closest ? function (el, css) {
|
192 | return el.closest(css);
|
193 | } : function (el, css) {
|
194 |
|
195 |
|
196 | if (el.correspondingUseElement) el = el.correspondingUseElement;
|
197 |
|
198 | for (; el; el = el.parentElement) {
|
199 | if (match.call(el, css)) return el;
|
200 | }
|
201 |
|
202 | return null;
|
203 | };
|
204 | }();
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | function dispatchEvent(element, name) {
|
214 | var detail = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
215 | var ignore = "prevent_recursive_dispatch_maximum_callstack".concat(name);
|
216 | var event;
|
217 | if (element[ignore]) return true;
|
218 | else element[ignore] = true;
|
219 |
|
220 | if (typeof window.CustomEvent === 'function') {
|
221 | event = new window.CustomEvent(name, {
|
222 | bubbles: true,
|
223 | cancelable: true,
|
224 | detail: detail
|
225 | });
|
226 | } else {
|
227 | event = document.createEvent('CustomEvent');
|
228 | event.initCustomEvent(name, true, true, detail);
|
229 | }
|
230 |
|
231 |
|
232 |
|
233 | var result = element.dispatchEvent(event);
|
234 | element[ignore] = null;
|
235 |
|
236 | return result;
|
237 | }
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | function getUUID() {
|
244 | return Date.now().toString(36) + Math.random().toString(36).slice(2, 5);
|
245 | }
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | function toggleAttribute(el, name) {
|
255 | var force = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : !this.hasAttribute(name);
|
256 | if (!force === el.hasAttribute(name)) el[force ? 'setAttribute' : 'removeAttribute'](name, '');
|
257 | return force;
|
258 | }
|
259 |
|
260 | var SCROLLER = IS_BROWSER && document.createElement('div');
|
261 |
|
262 | var CoreToggle = function (_HTMLElement) {
|
263 | _inherits(CoreToggle, _HTMLElement);
|
264 |
|
265 | var _super = _createSuper(CoreToggle);
|
266 |
|
267 | function CoreToggle() {
|
268 | _classCallCheck(this, CoreToggle);
|
269 |
|
270 | return _super.apply(this, arguments);
|
271 | }
|
272 |
|
273 | _createClass(CoreToggle, [{
|
274 | key: "connectedCallback",
|
275 | value: function connectedCallback() {
|
276 | if (IS_IOS) document.documentElement.style.cursor = 'pointer';
|
277 |
|
278 | if (!IS_ANDROID) this.setAttribute('aria-labelledby', this.button.id = this.button.id || getUUID());
|
279 |
|
280 | this.value = this.button.textContent;
|
281 |
|
282 | this.setAttribute('role', 'group');
|
283 |
|
284 | this.button.setAttribute('aria-expanded', this._open = !this.hidden);
|
285 | this.button.setAttribute('aria-controls', this.id = this.id || getUUID());
|
286 | document.addEventListener('keydown', this, true);
|
287 |
|
288 | document.addEventListener('click', this);
|
289 | }
|
290 | }, {
|
291 | key: "disconnectedCallback",
|
292 | value: function disconnectedCallback() {
|
293 | this._button = null;
|
294 | document.removeEventListener('keydown', this, true);
|
295 | document.removeEventListener('click', this);
|
296 | handleAutoposition(this, true);
|
297 | }
|
298 | }, {
|
299 | key: "attributeChangedCallback",
|
300 | value: function attributeChangedCallback() {
|
301 | if (this._open === this.hidden) {
|
302 |
|
303 | this.button.setAttribute('aria-expanded', this._open = !this.hidden);
|
304 |
|
305 | try {
|
306 | this.querySelector('[autofocus]').focus();
|
307 | } catch (err) {}
|
308 |
|
309 | handleAutoposition(this, this.hidden);
|
310 | dispatchEvent(this, 'toggle');
|
311 | }
|
312 | }
|
313 | }, {
|
314 | key: "handleEvent",
|
315 | value: function handleEvent(event) {
|
316 | if (event.defaultPrevented) return;
|
317 | if (event.type === 'resize' || event.type === 'scroll') return this.updatePosition();
|
318 |
|
319 | if (event.type === 'keydown' && event.keyCode === 27) {
|
320 | var isButton = event.target.getAttribute && event.target.getAttribute('aria-expanded') === 'true';
|
321 | var isHiding = isButton ? event.target === this.button : closest$1(event.target, this.nodeName) === this;
|
322 |
|
323 | if (isHiding) {
|
324 | this.hidden = true;
|
325 | this.button.focus();
|
326 |
|
327 | return event.preventDefault();
|
328 | }
|
329 | }
|
330 |
|
331 | if (event.type === 'click') {
|
332 | var btn = closest$1(event.target, 'a,button');
|
333 | if (btn && !btn.hasAttribute('aria-expanded') && closest$1(event.target, this.nodeName) === this) dispatchEvent(this, 'toggle.select', btn);else if (btn && btn.getAttribute('aria-controls') === this.id) this.hidden = !this.hidden;else if (this.popup && !this.contains(event.target)) this.hidden = true;
|
334 | }
|
335 | }
|
336 | |
337 |
|
338 |
|
339 |
|
340 |
|
341 | }, {
|
342 | key: "updatePosition",
|
343 | value: function updatePosition() {
|
344 | var _this = this;
|
345 |
|
346 | if (this._skipPosition || !this.button) return;
|
347 |
|
348 | this._skipPosition = true;
|
349 | this.style.position = 'fixed';
|
350 |
|
351 | var triggerRect = this.button.getBoundingClientRect();
|
352 | var contentRect = this.getBoundingClientRect();
|
353 | var hasSpaceRight = triggerRect.left + contentRect.width < window.innerWidth;
|
354 | var hasSpaceUnder = triggerRect.bottom + contentRect.height < window.innerHeight;
|
355 | var hasSpaceOver = triggerRect.top - contentRect.height > 0;
|
356 |
|
357 | var placeUnder = hasSpaceUnder || !hasSpaceOver;
|
358 | var scroll = placeUnder ? window.pageYOffset + triggerRect.bottom + contentRect.height + 30 : 0;
|
359 | this.style.left = "".concat(Math.round(hasSpaceRight ? triggerRect.left : triggerRect.right - contentRect.width), "px");
|
360 | this.style.top = "".concat(Math.round(placeUnder ? triggerRect.bottom : triggerRect.top - contentRect.height), "px");
|
361 | SCROLLER.style.cssText = "position:absolute;padding:1px;top:".concat(Math.round(scroll), "px");
|
362 | setTimeout(function () {
|
363 | return _this._skipPosition = null;
|
364 | });
|
365 | }
|
366 | }, {
|
367 | key: "button",
|
368 | get: function get() {
|
369 | if (this._button && (this._button.getAttribute('data-for') || this._button.getAttribute('for')) === this.id) return this._button;
|
370 |
|
371 | return (this._button = this.id && document.querySelector("[for=\"".concat(this.id, "\"],[data-for=\"").concat(this.id, "\"]"))) || this.previousElementSibling;
|
372 | }
|
373 |
|
374 | }, {
|
375 | key: "popup",
|
376 | get: function get() {
|
377 | return this.getAttribute('popup') === 'true' || this.getAttribute('popup') || this.hasAttribute('popup');
|
378 | },
|
379 | set: function set(val) {
|
380 | this[val === false ? 'removeAttribute' : 'setAttribute']('popup', val);
|
381 | }
|
382 | }, {
|
383 | key: "autoposition",
|
384 | get: function get() {
|
385 | return this.hasAttribute('autoposition');
|
386 | },
|
387 | set: function set(val) {
|
388 | toggleAttribute(this, 'autoposition', val);
|
389 | }
|
390 |
|
391 | }, {
|
392 | key: "hidden",
|
393 | get: function get() {
|
394 | return this.hasAttribute('hidden');
|
395 | },
|
396 | set: function set(val) {
|
397 | toggleAttribute(this, 'hidden', val);
|
398 | }
|
399 |
|
400 |
|
401 |
|
402 | }, {
|
403 | key: "value",
|
404 | get: function get() {
|
405 | return this.button.value || this.button.textContent;
|
406 | },
|
407 | set: function set(data) {
|
408 | if (data === void 0) {
|
409 | data = false;
|
410 | }
|
411 |
|
412 | if (!this.button || !this.popup.length) return;
|
413 | var button = this.button;
|
414 | var popup = (button.getAttribute('aria-label') || ",".concat(this.popup)).split(',')[1];
|
415 | var label = data.textContent || data || '';
|
416 |
|
417 | if (popup === this.popup) {
|
418 | var target = button.querySelector('span') || button;
|
419 |
|
420 | button.value = data.value || label;
|
421 | target[data.innerHTML ? 'innerHTML' : 'textContent'] = data.innerHTML || label;
|
422 | button.setAttribute('aria-label', "".concat(button.textContent, ",").concat(this.popup));
|
423 | }
|
424 | }
|
425 | }], [{
|
426 | key: "observedAttributes",
|
427 | get: function get() {
|
428 | return ['hidden', 'autoposition'];
|
429 | }
|
430 | }]);
|
431 |
|
432 | return CoreToggle;
|
433 | }( _wrapNativeSuper(HTMLElement));
|
434 |
|
435 | function handleAutoposition(self, teardown) {
|
436 | if (teardown) {
|
437 | if (self._positionObserver) self._positionObserver.disconnect();
|
438 | if (SCROLLER.parentNode) SCROLLER.parentNode.removeChild(SCROLLER);
|
439 | self.style.position = self._positionObserver = null;
|
440 | window.removeEventListener('scroll', self, true);
|
441 |
|
442 | window.removeEventListener('resize', self);
|
443 | } else if (self.autoposition) {
|
444 |
|
445 | if (!self._positionObserver) self._positionObserver = window.MutationObserver && new window.MutationObserver(self.updatePosition.bind(self));
|
446 | if (self._positionObserver) self._positionObserver.observe(self, {
|
447 | childList: true,
|
448 | subtree: true,
|
449 | attributes: true
|
450 | });
|
451 | document.body.appendChild(SCROLLER);
|
452 | window.addEventListener('scroll', self, true);
|
453 |
|
454 | window.addEventListener('resize', self);
|
455 | self.updatePosition();
|
456 | }
|
457 | }
|
458 |
|
459 | var version = "3.1.0";
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 | var closest = function () {
|
469 | var proto = typeof window === 'undefined' ? {} : window.Element.prototype;
|
470 | var match = proto.matches || proto.msMatchesSelector || proto.webkitMatchesSelector;
|
471 | return proto.closest ? function (el, css) {
|
472 | return el.closest(css);
|
473 | } : function (el, css) {
|
474 | for (; el; el = el.parentElement) {
|
475 | if (match.call(el, css)) {
|
476 | return el;
|
477 | }
|
478 | }
|
479 |
|
480 | return null;
|
481 | };
|
482 | }();
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 | function customElementToReact(elementClass, options) {
|
492 | if (options === void 0) options = {};
|
493 | var name = elementClass.name || String(elementClass).match(/function ([^(]+)/)[1];
|
494 |
|
495 | var dashCase = name.replace(/.[A-Z]/g, function (ref) {
|
496 | var a = ref[0];
|
497 | var b = ref[1];
|
498 | return a + "-" + b;
|
499 | });
|
500 |
|
501 | var customProps = options.props || [];
|
502 | var customEvents = options.customEvents || [];
|
503 | var eventMap = customEvents.reduce(function (map, eventName) {
|
504 | map[eventName] = "on" + eventName.replace(/(^|\.)./g, function (m) {
|
505 | return m.slice(-1).toUpperCase();
|
506 | });
|
507 |
|
508 | return map;
|
509 | }, {});
|
510 | var skipProps = customProps.concat('forwardRef', Object.keys(eventMap).map(function (onEventName) {
|
511 | return eventMap[onEventName];
|
512 | }));
|
513 | var tagName = (dashCase + "-" + (options.suffix || 'react')).replace(/\W+/g, '-').toLowerCase();
|
514 | return function (superclass) {
|
515 | function anonymous(props) {
|
516 | var this$1$1 = this;
|
517 | superclass.call(this, props);
|
518 |
|
519 | this.ref = function (el) {
|
520 | if (typeof this$1$1.props.forwardRef === 'function') {
|
521 | this$1$1.props.forwardRef(el);
|
522 | } else if (this$1$1.props.forwardRef) {
|
523 | this$1$1.props.forwardRef.current = el;
|
524 | }
|
525 |
|
526 | return this$1$1.el = el;
|
527 | };
|
528 |
|
529 |
|
530 | Object.keys(eventMap).forEach(function (eventName) {
|
531 | var onEventName = eventMap[eventName];
|
532 |
|
533 | this$1$1[eventName] = function (event) {
|
534 | if (this$1$1.props[onEventName] && closest(event.target, this$1$1.el.nodeName) === this$1$1.el) {
|
535 | this$1$1.props[onEventName](event);
|
536 | }
|
537 | };
|
538 | });
|
539 | }
|
540 |
|
541 | if (superclass) anonymous.__proto__ = superclass;
|
542 | anonymous.prototype = Object.create(superclass && superclass.prototype);
|
543 | anonymous.prototype.constructor = anonymous;
|
544 |
|
545 | anonymous.prototype.componentDidMount = function componentDidMount() {
|
546 | var this$1$1 = this;
|
547 |
|
548 | if (!window.customElements.get(tagName)) {
|
549 | window.customElements.define(tagName, elementClass);
|
550 | }
|
551 |
|
552 |
|
553 | customProps.forEach(function (propName) {
|
554 | if (propName in this$1$1.props) {
|
555 | this$1$1.el[propName] = this$1$1.props[propName];
|
556 | }
|
557 | });
|
558 |
|
559 | customEvents.forEach(function (eventName) {
|
560 | this$1$1.el.addEventListener(eventName, this$1$1[eventName]);
|
561 | });
|
562 | };
|
563 |
|
564 | anonymous.prototype.componentDidUpdate = function componentDidUpdate(prev) {
|
565 | var this$1$1 = this;
|
566 |
|
567 | customProps.forEach(function (propName) {
|
568 | if (prev[propName] !== this$1$1.props[propName]) {
|
569 | this$1$1.el[propName] = this$1$1.props[propName];
|
570 | }
|
571 | });
|
572 | };
|
573 |
|
574 | anonymous.prototype.componentWillUnmount = function componentWillUnmount() {
|
575 | var this$1$1 = this;
|
576 |
|
577 | customEvents.forEach(function (eventName) {
|
578 | this$1$1.el.removeEventListener(eventName, this$1$1[eventName]);
|
579 | });
|
580 | };
|
581 |
|
582 | anonymous.prototype.render = function render() {
|
583 | var this$1$1 = this;
|
584 |
|
585 | return React__default['default'].createElement(tagName, Object.keys(this.props).reduce(function (thisProps, propName) {
|
586 | if (skipProps.indexOf(propName) === -1) {
|
587 |
|
588 | if (propName === 'className') {
|
589 | thisProps["class"] = this$1$1.props[propName];
|
590 | }
|
591 | else if (this$1$1.props[propName] === true) {
|
592 | thisProps[propName] = '';
|
593 | }
|
594 | else if (this$1$1.props[propName] !== false) {
|
595 | thisProps[propName] = this$1$1.props[propName];
|
596 | }
|
597 |
|
598 | }
|
599 |
|
600 | return thisProps;
|
601 | }, {
|
602 | ref: this.ref
|
603 | }));
|
604 | };
|
605 |
|
606 | return anonymous;
|
607 | }(React__default['default'].Component);
|
608 | }
|
609 |
|
610 | var coreToggle = customElementToReact(CoreToggle, {
|
611 | props: ['popup'],
|
612 | customEvents: ['toggle', 'toggle.select'],
|
613 | suffix: version
|
614 | });
|
615 |
|
616 | module.exports = coreToggle;
|