forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorate-selector.ts
More file actions
46 lines (39 loc) · 1.45 KB
/
decorate-selector.ts
File metadata and controls
46 lines (39 loc) · 1.45 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
import memoizeOne from 'memoize-one';
import Color from 'color';
const colorMethods = [
'negate', // rgb(0, 100, 255) -> rgb(255, 155, 0)
'lighten', // hsl(100, 50%, 50%) -> hsl(100, 50%, 75%)
'darken', // hsl(100, 50%, 50%) -> hsl(100, 50%, 25%)
'saturate', // hsl(100, 50%, 50%) -> hsl(100, 75%, 50%)
'desaturate', // hsl(100, 50%, 50%) -> hsl(100, 25%, 50%)
'greyscale', // #5CBF54 -> #969696
'whiten', // hwb(100, 50%, 50%) -> hwb(100, 75%, 50%)
'blacken', // hwb(100, 50%, 50%) -> hwb(100, 50%, 75%)
'clearer', // rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 0.4)
'opaquer', // rgba(10, 10, 10, 0.8) -> rgba(10, 10, 10, 1.0)
'rotate', // hsl(60, 20%, 20%) -> hsl(330, 20%, 20%)
];
/**
* Takes a selector that returns a color string and returns new decorated selector that calls the
* original function to get the color and then modifies that color, ultimately returning another
* color string.
*
* vy60q8l043
*/
const addModifier = (fn, method, ...modifierArgs) => (...args) =>
Color(fn(...args))
[method](...modifierArgs)
.rgbString();
/**
* Add useful methods directly to selector function, as well as put an rgbString() call at the end
* @param selector
*/
export const decorateSelector = (selector: any) => {
// add member functions to our selector
colorMethods.forEach(method => {
selector[method] = memoizeOne((...args) =>
decorateSelector(addModifier(selector, method, ...args))
);
});
return selector;
};