1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.preventedEvents = exports.trimLeft = exports.innerDimensions = exports.getTextWidth = exports.canUseDOM = exports.toCamel = exports.getBreakpoint = exports.formatBreakpointMods = exports.setBreakpointCssVars = exports.pluralize = exports.getNextIndex = exports.findTabbableElements = exports.keyHandler = exports.fillTemplate = exports.sideElementIsOutOfView = exports.isElementInView = exports.debounce = exports.getUniqueId = exports.capitalize = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const ReactDOM = tslib_1.__importStar(require("react-dom"));
|
6 | const constants_1 = require("./constants");
|
7 |
|
8 |
|
9 |
|
10 | function capitalize(input) {
|
11 | return input[0].toUpperCase() + input.substring(1);
|
12 | }
|
13 | exports.capitalize = capitalize;
|
14 |
|
15 |
|
16 |
|
17 | function getUniqueId(prefix = 'pf') {
|
18 | const uid = new Date().getTime() +
|
19 | Math.random()
|
20 | .toString(36)
|
21 | .slice(2);
|
22 | return `${prefix}-${uid}`;
|
23 | }
|
24 | exports.getUniqueId = getUniqueId;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function debounce(func, wait) {
|
31 | let timeout;
|
32 | return (...args) => {
|
33 | clearTimeout(timeout);
|
34 | timeout = setTimeout(() => func.apply(this, args), wait);
|
35 | };
|
36 | }
|
37 | exports.debounce = debounce;
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function isElementInView(container, element, partial, strict = false) {
|
49 | if (!container || !element) {
|
50 | return false;
|
51 | }
|
52 | const containerBounds = container.getBoundingClientRect();
|
53 | const elementBounds = element.getBoundingClientRect();
|
54 | const containerBoundsLeft = Math.ceil(containerBounds.left);
|
55 | const containerBoundsRight = Math.floor(containerBounds.right);
|
56 | const elementBoundsLeft = Math.ceil(elementBounds.left);
|
57 | const elementBoundsRight = Math.floor(elementBounds.right);
|
58 |
|
59 | const isTotallyInView = elementBoundsLeft >= containerBoundsLeft && elementBoundsRight <= containerBoundsRight;
|
60 | const isPartiallyInView = (partial || (!strict && containerBounds.width < elementBounds.width)) &&
|
61 | ((elementBoundsLeft < containerBoundsLeft && elementBoundsRight > containerBoundsLeft) ||
|
62 | (elementBoundsRight > containerBoundsRight && elementBoundsLeft < containerBoundsRight));
|
63 |
|
64 | return isTotallyInView || isPartiallyInView;
|
65 | }
|
66 | exports.isElementInView = isElementInView;
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function sideElementIsOutOfView(container, element) {
|
75 | const containerBounds = container.getBoundingClientRect();
|
76 | const elementBounds = element.getBoundingClientRect();
|
77 | const containerBoundsLeft = Math.floor(containerBounds.left);
|
78 | const containerBoundsRight = Math.floor(containerBounds.right);
|
79 | const elementBoundsLeft = Math.floor(elementBounds.left);
|
80 | const elementBoundsRight = Math.floor(elementBounds.right);
|
81 |
|
82 | const isOffLeft = elementBoundsLeft < containerBoundsLeft;
|
83 | const isOffRight = elementBoundsRight > containerBoundsRight;
|
84 | let side = constants_1.SIDE.NONE;
|
85 | if (isOffRight && isOffLeft) {
|
86 | side = constants_1.SIDE.BOTH;
|
87 | }
|
88 | else if (isOffRight) {
|
89 | side = constants_1.SIDE.RIGHT;
|
90 | }
|
91 | else if (isOffLeft) {
|
92 | side = constants_1.SIDE.LEFT;
|
93 | }
|
94 |
|
95 | return side;
|
96 | }
|
97 | exports.sideElementIsOutOfView = sideElementIsOutOfView;
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | function fillTemplate(templateString, templateVars) {
|
115 | return templateString.replace(/\${(.*?)}/g, (_, match) => templateVars[match] || '');
|
116 | }
|
117 | exports.fillTemplate = fillTemplate;
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | function keyHandler(index, innerIndex, position, refsCollection, kids, custom = false) {
|
129 | if (!Array.isArray(kids)) {
|
130 | return;
|
131 | }
|
132 | const isMultiDimensional = refsCollection.filter(ref => ref)[0].constructor === Array;
|
133 | let nextIndex = index;
|
134 | let nextInnerIndex = innerIndex;
|
135 | if (position === 'up') {
|
136 | if (index === 0) {
|
137 |
|
138 | nextIndex = kids.length - 1;
|
139 | }
|
140 | else {
|
141 | nextIndex = index - 1;
|
142 | }
|
143 | }
|
144 | else if (position === 'down') {
|
145 | if (index === kids.length - 1) {
|
146 |
|
147 | nextIndex = 0;
|
148 | }
|
149 | else {
|
150 | nextIndex = index + 1;
|
151 | }
|
152 | }
|
153 | else if (position === 'left') {
|
154 | if (innerIndex === 0) {
|
155 | nextInnerIndex = refsCollection[index].length - 1;
|
156 | }
|
157 | else {
|
158 | nextInnerIndex = innerIndex - 1;
|
159 | }
|
160 | }
|
161 | else if (position === 'right') {
|
162 | if (innerIndex === refsCollection[index].length - 1) {
|
163 | nextInnerIndex = 0;
|
164 | }
|
165 | else {
|
166 | nextInnerIndex = innerIndex + 1;
|
167 | }
|
168 | }
|
169 | if (refsCollection[nextIndex] === null ||
|
170 | refsCollection[nextIndex] === undefined ||
|
171 | (isMultiDimensional &&
|
172 | (refsCollection[nextIndex][nextInnerIndex] === null || refsCollection[nextIndex][nextInnerIndex] === undefined))) {
|
173 | keyHandler(nextIndex, nextInnerIndex, position, refsCollection, kids, custom);
|
174 | }
|
175 | else if (custom) {
|
176 | if (refsCollection[nextIndex].focus) {
|
177 | refsCollection[nextIndex].focus();
|
178 | }
|
179 |
|
180 | const element = ReactDOM.findDOMNode(refsCollection[nextIndex]);
|
181 | element.focus();
|
182 | }
|
183 | else if (position !== 'tab') {
|
184 | if (isMultiDimensional) {
|
185 | refsCollection[nextIndex][nextInnerIndex].focus();
|
186 | }
|
187 | else {
|
188 | refsCollection[nextIndex].focus();
|
189 | }
|
190 | }
|
191 | }
|
192 | exports.keyHandler = keyHandler;
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function findTabbableElements(containerRef, tababbleSelectors) {
|
199 | const tabbable = containerRef.current.querySelectorAll(tababbleSelectors);
|
200 | const list = Array.prototype.filter.call(tabbable, function (item) {
|
201 | return item.tabIndex >= '0';
|
202 | });
|
203 | return list;
|
204 | }
|
205 | exports.findTabbableElements = findTabbableElements;
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 | function getNextIndex(index, position, collection) {
|
213 | let nextIndex;
|
214 | if (position === 'up') {
|
215 | if (index === 0) {
|
216 |
|
217 | nextIndex = collection.length - 1;
|
218 | }
|
219 | else {
|
220 | nextIndex = index - 1;
|
221 | }
|
222 | }
|
223 | else if (index === collection.length - 1) {
|
224 |
|
225 | nextIndex = 0;
|
226 | }
|
227 | else {
|
228 | nextIndex = index + 1;
|
229 | }
|
230 | if (collection[nextIndex] === undefined || collection[nextIndex][0] === null) {
|
231 | return getNextIndex(nextIndex, position, collection);
|
232 | }
|
233 | else {
|
234 | return nextIndex;
|
235 | }
|
236 | }
|
237 | exports.getNextIndex = getNextIndex;
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | function pluralize(i, singular, plural) {
|
246 | if (!plural) {
|
247 | plural = `${singular}s`;
|
248 | }
|
249 | return `${i || 0} ${i === 1 ? singular : plural}`;
|
250 | }
|
251 | exports.pluralize = pluralize;
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | const setBreakpointCssVars = (mods, cssVar) => Object.entries(mods || {}).reduce((acc, [breakpoint, value]) => breakpoint === 'default' ? Object.assign(Object.assign({}, acc), { [cssVar]: value }) : Object.assign(Object.assign({}, acc), { [`${cssVar}-on-${breakpoint}`]: value }), {});
|
259 | exports.setBreakpointCssVars = setBreakpointCssVars;
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 | const formatBreakpointMods = (mods, styles, stylePrefix = '', breakpoint) => {
|
267 | if (!mods) {
|
268 | return '';
|
269 | }
|
270 | if (breakpoint) {
|
271 | if (breakpoint in mods) {
|
272 | return styles.modifiers[exports.toCamel(`${stylePrefix}${mods[breakpoint]}`)];
|
273 | }
|
274 |
|
275 | const breakpointsOrder = ['2xl', 'xl', 'lg', 'md', 'sm', 'default'];
|
276 | const breakpointsIndex = breakpointsOrder.indexOf(breakpoint);
|
277 | for (let i = breakpointsIndex; i < breakpointsOrder.length; i++) {
|
278 | if (breakpointsOrder[i] in mods) {
|
279 | return styles.modifiers[exports.toCamel(`${stylePrefix}${mods[breakpointsOrder[i]]}`)];
|
280 | }
|
281 | }
|
282 | return '';
|
283 | }
|
284 | return Object.entries(mods || {})
|
285 | .map(([breakpoint, mod]) => `${stylePrefix}${mod}${breakpoint !== 'default' ? `-on-${breakpoint}` : ''}`)
|
286 | .map(exports.toCamel)
|
287 | .map(mod => mod.replace(/-?(\dxl)/gi, (_res, group) => `_${group}`))
|
288 | .map(modifierKey => styles.modifiers[modifierKey])
|
289 | .filter(Boolean)
|
290 | .join(' ');
|
291 | };
|
292 | exports.formatBreakpointMods = formatBreakpointMods;
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 | const getBreakpoint = (width) => {
|
300 | if (width === null) {
|
301 | return null;
|
302 | }
|
303 | if (width >= 1450) {
|
304 | return '2xl';
|
305 | }
|
306 | if (width >= 1200) {
|
307 | return 'xl';
|
308 | }
|
309 | if (width >= 992) {
|
310 | return 'lg';
|
311 | }
|
312 | if (width >= 768) {
|
313 | return 'md';
|
314 | }
|
315 | if (width >= 576) {
|
316 | return 'sm';
|
317 | }
|
318 | return 'default';
|
319 | };
|
320 | exports.getBreakpoint = getBreakpoint;
|
321 | const camelize = (s) => s
|
322 | .toUpperCase()
|
323 | .replace('-', '')
|
324 | .replace('_', '');
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | const toCamel = (s) => s.replace(/([-_][a-z])/gi, camelize);
|
330 | exports.toCamel = toCamel;
|
331 |
|
332 |
|
333 |
|
334 | exports.canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement);
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | const getTextWidth = (text, node) => {
|
344 | const computedStyle = getComputedStyle(node);
|
345 |
|
346 | const getFontFromComputedStyle = () => {
|
347 | let computedFont = '';
|
348 |
|
349 |
|
350 |
|
351 | const fontStretchLookupTable = {
|
352 | '50%': 'ultra-condensed',
|
353 | '62.5%': 'extra-condensed',
|
354 | '75%': 'condensed',
|
355 | '87.5%': 'semi-condensed',
|
356 | '100%': 'normal',
|
357 | '112.5%': 'semi-expanded',
|
358 | '125%': 'expanded',
|
359 | '150%': 'extra-expanded',
|
360 | '200%': 'ultra-expanded'
|
361 | };
|
362 |
|
363 |
|
364 | let fontStretch;
|
365 | if (computedStyle.fontStretch in fontStretchLookupTable) {
|
366 | fontStretch = fontStretchLookupTable[computedStyle.fontStretch];
|
367 | }
|
368 | else {
|
369 | fontStretch = 'normal';
|
370 | }
|
371 | computedFont =
|
372 | computedStyle.fontStyle +
|
373 | ' ' +
|
374 | computedStyle.fontVariant +
|
375 | ' ' +
|
376 | computedStyle.fontWeight +
|
377 | ' ' +
|
378 | fontStretch +
|
379 | ' ' +
|
380 | computedStyle.fontSize +
|
381 | '/' +
|
382 | computedStyle.lineHeight +
|
383 | ' ' +
|
384 | computedStyle.fontFamily;
|
385 | return computedFont;
|
386 | };
|
387 | const canvas = document.createElement('canvas');
|
388 | const context = canvas.getContext('2d');
|
389 | context.font = computedStyle.font || getFontFromComputedStyle();
|
390 | return context.measureText(text).width;
|
391 | };
|
392 | exports.getTextWidth = getTextWidth;
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | const innerDimensions = (node) => {
|
399 | const computedStyle = getComputedStyle(node);
|
400 | let width = node.clientWidth;
|
401 | let height = node.clientHeight;
|
402 | height -= parseFloat(computedStyle.paddingTop) + parseFloat(computedStyle.paddingBottom);
|
403 | width -= parseFloat(computedStyle.paddingLeft) + parseFloat(computedStyle.paddingRight);
|
404 | return { height, width };
|
405 | };
|
406 | exports.innerDimensions = innerDimensions;
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 | const trimLeft = (node, value) => {
|
414 | const availableWidth = exports.innerDimensions(node).width;
|
415 | let newValue = value;
|
416 | if (exports.getTextWidth(value, node) > availableWidth) {
|
417 |
|
418 | while (exports.getTextWidth(`...${newValue}`, node) > availableWidth) {
|
419 | newValue = newValue.substring(1);
|
420 | }
|
421 |
|
422 | if (node.value) {
|
423 | node.value = `...${newValue}`;
|
424 | }
|
425 | else {
|
426 | node.innerText = `...${newValue}`;
|
427 | }
|
428 | }
|
429 | else {
|
430 | if (node.value) {
|
431 | node.value = value;
|
432 | }
|
433 | else {
|
434 | node.innerText = value;
|
435 | }
|
436 | }
|
437 | };
|
438 | exports.trimLeft = trimLeft;
|
439 |
|
440 |
|
441 |
|
442 | const preventedEvents = (events) => events.reduce((handlers, eventToPrevent) => (Object.assign(Object.assign({}, handlers), { [eventToPrevent]: (event) => {
|
443 | event.preventDefault();
|
444 | } })), {});
|
445 | exports.preventedEvents = preventedEvents;
|
446 |
|
\ | No newline at end of file |