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