summaryrefslogtreecommitdiff
path: root/node_modules/postcss-easings/index.js
blob: add6ee897c0e80e33900e6d4237286de5407af95 (plain)
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
84
85
var postcss = require('postcss');
var parser = require('postcss-value-parser');

var easings = {
    easeInSine:     'cubic-bezier(0.47, 0, 0.745, 0.715)',
    easeOutSine:    'cubic-bezier(0.39, 0.575, 0.565, 1)',
    easeInOutSine:  'cubic-bezier(0.445, 0.05, 0.55, 0.95)',
    easeInQuad:     'cubic-bezier(0.55, 0.085, 0.68, 0.53)',
    easeOutQuad:    'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
    easeInOutQuad:  'cubic-bezier(0.455, 0.03, 0.515, 0.955)',
    easeInCubic:    'cubic-bezier(0.55, 0.055, 0.675, 0.19)',
    easeOutCubic:   'cubic-bezier(0.215, 0.61, 0.355, 1)',
    easeInOutCubic: 'cubic-bezier(0.645, 0.045, 0.355, 1)',
    easeInQuart:    'cubic-bezier(0.895, 0.03, 0.685, 0.22)',
    easeOutQuart:   'cubic-bezier(0.165, 0.84, 0.44, 1)',
    easeInOutQuart: 'cubic-bezier(0.77, 0, 0.175, 1)',
    easeInQuint:    'cubic-bezier(0.755, 0.05, 0.855, 0.06)',
    easeOutQuint:   'cubic-bezier(0.23, 1, 0.32, 1)',
    easeInOutQuint: 'cubic-bezier(0.86, 0, 0.07, 1)',
    easeInExpo:     'cubic-bezier(0.95, 0.05, 0.795, 0.035)',
    easeOutExpo:    'cubic-bezier(0.19, 1, 0.22, 1)',
    easeInOutExpo:  'cubic-bezier(1, 0, 0, 1)',
    easeInCirc:     'cubic-bezier(0.6, 0.04, 0.98, 0.335)',
    easeOutCirc:    'cubic-bezier(0.075, 0.82, 0.165, 1)',
    easeInOutCirc:  'cubic-bezier(0.785, 0.135, 0.15, 0.86)',
    easeInBack:     'cubic-bezier(0.6, -0.28, 0.735, 0.045)',
    easeOutBack:    'cubic-bezier(0.175, 0.885, 0.32, 1.275)',
    easeInOutBack:  'cubic-bezier(0.68, -0.55, 0.265, 1.55)'
};

var toSnake = function (str) {
    return str.replace(/[A-Z]/g, function (letter) {
        return '-' + letter.toLowerCase();
    });
};

var toCamel = function (str) {
    return str.replace(/-[a-z]/g, function (letter) {
        return letter[1].toUpperCase();
    });
};

var camels = Object.keys(easings);
for ( var i = 0; i < camels.length; i++ ) {
    var camel = camels[i];
    easings[toSnake(camel)] = easings[camel];
}

module.exports = postcss.plugin('postcss-easings', function (opts) {
    if ( typeof opts === 'undefined' ) opts = { };

    var locals = { };
    if ( opts.easings ) {
        for ( var name in opts.easings ) {
            if ( !/^ease([\w-]+)$/.test(name) ) {
                throw new Error('Custom easing ' + name + ' has bad name. ' +
                                'Name should start from `ease` and contain ' +
                                'only letters and dashes');
            }
            locals[name] = opts.easings[name];
            if ( name.indexOf('-') !== -1 ) {
                locals[toCamel(name)] = opts.easings[name];
            } else if ( /[A-Z]/.test(name) ) {
                locals[toSnake(name)] = opts.easings[name];
            }
        }
    }

    return function (css) {
        css.walkDecls(function (decl) {
            if (decl.value.indexOf('ease') === -1) return;
            var root = parser(decl.value);
            root.nodes = root.nodes.map(function (node) {
                const value = node.value;
                if (node.type === 'word' && /^ease([\w-]+)$/.test(value)) {
                    node.value = locals[value] || easings[value] || node.value;
                }
                return node;
            });
            decl.value = root.toString();
        });
    };
});

module.exports.easings = easings;