-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (71 loc) · 3.37 KB
/
Copy pathindex.js
File metadata and controls
83 lines (71 loc) · 3.37 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const postcss = require('postcss');
module.exports = postcss.plugin('postcss-touch-hover', () => {
return root => {
// Look for @touch-hover at rule
root.walkAtRules('touch-hover', atRule => {
const hoverRules = [];
const normalRules = [];
// Start by identifying all :hover rules
atRule.walkRules(/:hover/i, rule => {
hoverRules.push(rule);
rule.remove();
});
atRule.walkRules(rule => {
normalRules.push(rule);
rule.remove();
});
const parent = atRule.parent;
// If there are any :hover rules in the input, then create media queries
// to automatically translate it into :active on touch-based devices
if (hoverRules.length > 0) {
// Create a media query targetting ie10 + 11, as these browsers
// wont support @media (hover: hover) - but we know that this
// browser never runs on mobile devices, so we'll just push the
// hover rules there
const ieQuery = parent.append({
name: 'media',
params: 'all and ' + '(-ms-high-contrast: none), ' + '(-ms-high-contrast: active)',
}).last;
// Create a media query targetting firefox, as this browser doesn't
// support @media (hover: hover)... Technically this browser could
// run on both desktop and mobile devices, but we're going to be
// applying :hover and hope for the best
const firefoxQuery = parent.append({
name: 'media',
params: 'all and (min--moz-device-pixel-ratio:0)',
}).last;
// Create a media query targetting devices that actually support
// hover
const hoverQuery = parent.append({
name: 'media',
params: '(hover: hover)',
}).last;
// Create a media query targetting devices that don't support hover
// (ie. devices where we should fall back to :active instead)
const activeQuery = parent.append({
name: 'media',
params: '(hover: none)',
}).last;
// Loop through the hover rules and apply them to each of the media
// queries
for (const hoverRule of hoverRules) {
// Push a clone of the :hover rule 'as is' to queries where we
// expect the user's device to support hover
ieQuery.append(hoverRule.clone());
firefoxQuery.append(hoverRule.clone());
hoverQuery.append(hoverRule.clone());
// Push a clone of the :hover rule, where we transform the
// selector to :active to the query targetting devices that
// don't support hover
activeQuery.append(
hoverRule.clone({
selector: hoverRule.selector.replace(/:hover/gi, ':active'),
}),
);
}
}
parent.append(normalRules);
atRule.remove();
});
};
});