-
Notifications
You must be signed in to change notification settings - Fork 440
/
utils.js
54 lines (43 loc) · 1.38 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// @flow
// ==============================
// NO OP
// ==============================
export const noop = () => {}
// ==============================
// Class Name Prefixer
// ==============================
type State = { [key: string]: boolean }
type List = Array<string>
export const CLASS_PREFIX = 'react-images'
/**
String representation of component state for styling with class names.
Expects an array of strings OR a string/object pair:
- className(['comp', 'comp-arg', 'comp-arg-2'])
@returns 'react-images__comp react-images__comp-arg react-images__comp-arg-2'
- className('comp', { some: true, state: false })
@returns 'react-images__comp react-images__comp--some'
*/
export function className(name: string | List, state?: State): string {
const arr: List = Array.isArray(name) ? name : [name]
// loop through state object, remove falsey values and combine with name
if (state && typeof name === 'string') {
for (let key in state) {
if (state.hasOwnProperty(key) && state[key]) {
arr.push(`${name}--${key}`)
}
}
}
// prefix everything and return a string
return arr.map(cn => `${CLASS_PREFIX}__${cn}`).join(' ')
}
// ==============================
// Touch Capability Detector
// ==============================
export function isTouch() {
try {
document.createEvent('TouchEvent')
return true
} catch (e) {
return false
}
}