-
Notifications
You must be signed in to change notification settings - Fork 0
/
screens.ts
87 lines (74 loc) · 2.64 KB
/
screens.ts
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
86
87
import defaultTheme from 'tailwindcss/defaultTheme';
// @ts-ignore
import tailwindPlugin from 'tailwindcss/plugin';
export interface Options {
enabled: boolean,
ignore: string[],
selector: string,
label: string,
position: ['bottom' | 'top', 'left' | 'right'],
}
export interface Styles {
content: string,
[key: string]: any,
}
export const defaultOptions: Options = {
enabled: !process?.env?.NODE_ENV?.startsWith('prod'),
ignore: ['dark'],
selector: 'body',
label: '{key} ({value})',
position: ['bottom', 'left'],
};
export const defaultStyles: Styles = {
content: `'-'`,
padding: defaultTheme?.spacing?.[2],
color: defaultTheme?.colors?.['white'],
fontSize: defaultTheme?.fontSize?.xs[0],
lineHeight: defaultTheme?.lineHeight?.none,
fontFamily: `"Source code Pro", ${defaultTheme?.fontFamily?.mono}`,
boxShadow: defaultTheme?.boxShadow?.md,
backgroundColor: defaultTheme?.colors?.gray[900],
borderTopRightRadius: defaultTheme?.borderRadius?.lg,
position: 'fixed',
zIndex: 2147483647,
};
/**
* Heavily inspired by : https://github.com/jorenvanhee/tailwindcss-debug-screens
*/
export function factory(options: Options = defaultOptions) {
options = { ...defaultOptions, ...options };
if (!options?.enabled) {
return () => {};
}
return function plugin({ addComponents, theme }: { addComponents: Function, theme: Function }) {
const screens: { [key: string]: string } = theme('screens', {});
const definedStyles = theme('debug.screens', {});
const styles = {
...defaultStyles,
[options.position[0]]: 0,
[options.position[1]]: 0,
...definedStyles,
};
const components = Object
.entries(screens)
.sort(([, valueA], [, valueB]) => +valueA.replace('px', '') - +valueB.replace('px', ''))
.reduce((acc: any, [key, value]) => {
if (options.ignore.includes(key)) {
return;
}
acc[`@screen ${key}`] = {
[`${options.selector}::before`]: {
content: `"${options.label.replace('{key}', key).replace('{value}', value)}"`,
},
};
return acc;
}, { [`${options.selector}::before`]: styles });
addComponents(components);
};
};
const plugin = tailwindPlugin.withOptions(
(options: Options = defaultOptions) => factory(options),
// (options: Options = defaultOptions) => ({ theme: { debug: { screens: defaultStyles } } }),
);
module.exports.default = plugin;
export default plugin;